#!/usr/bin/env python
#coding: utf-8
# 
# retu レコードのフィールド数を求める （Open usp Tukubai版）
#
# designed by Nobuaki Tounaka
# written by Ryuichi Ueda
#
# The MIT License
#
# Copyright (C) 2011 Universal Shell Programming Laboratory
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

_usage = "retu [-f] <file>"
_version = "Fri Oct 21 11:26:06 JST 2011"
_code = "Open usp Tukubai (LINUX+FREEBSD/PYTHON2.4/UTF-8)"

import os
import sys

def printUsage():
	print >> sys.stderr , "Usage    :", _usage
	print >> sys.stderr , "Version  :", _version
	print >> sys.stderr , "          ", _code
	sys.exit(1)

def die(message):
	print >> sys.stderr, "Error[retu] :", message
	sys.exit(1)

def openFile(filename):
	file = None

	if filename == "-":
		file = sys.stdin
	elif os.path.isfile(filename):
		try:
			file = open(filename,'r')
		except IOError:
			die(filename + ":ファイルが開けません。")
	elif os.path.isdir(filename):
		pass
	else:
		die(filename + ":ファイルがありません。")

	return file

def countNormal(filename):
	file = openFile(filename)
	if file == None: return 0

	prev_num = -1
	for line in file:
		tokens = [ t for t in line.rstrip().split(" ") if t != "" ]
		num = len(tokens)
		if num != prev_num:
			print num
			prev_num = num
		
	file.close()

def countFile(filename):
	file = openFile(filename)
	if file == None: return 0

	prev_num = -1
	for line in file:
		tokens = [ t for t in line.rstrip().split(" ") if t != "" ]
		num = len(tokens)
		if num != prev_num:
			print filename,num
			prev_num = num
		
	file.close()

###########################################
#メイン関数
###########################################
if __name__ == '__main__':

	###########################################
	#オプションの処理
	###########################################
	mode = "normal"
	files = []
	argc = len(sys.argv)

	for i in range(1, argc):
		arg = sys.argv[i]
		if arg == "-f":		mode = "file"
		elif arg == "-":	files.append(arg)
		elif arg[0] == "-":	printUsage()
		else:			files.append(arg)

	if len(files) == 0:
		files.append("-")

	###########################################
	#出力
	###########################################
	for f in files:
		if mode == "normal":	countNormal(f)
		else:			countFile(f)
