Sunday, September 3, 2006

v2 ( Python )


#!/usr/bin/env python
import os, sys

input_file = sys.stdin
output_file = sys.stdout
output_file = open( sys.argv[ 2 ], 'w' )
input_file = open( sys.argv[ 1 ] )

original = input_file.read()

str1="""#ifdef DEVELP /* Borland C v3.1 version */
unsigned char VoltTable[] = { \n"""

str2="""0 };\n#endif
#ifdef EMBED /* Dunfield assembler version */
void VoltTable(void) asm { \n"""

str3="""}
#endif\n"""

def test( x ):
count = 0
str = original
s = ""
if x == 2:
s += " DB "
else:
s += "\t"

for line in str:
for word in line:
if word == '\r':
count += 1
if count % 16 != 0:
s += ','
else:
if x == 1:
s += ',\n\t'
elif x == 2:
s += '\n DB '
elif word == '\n':
s += ''
else:
s += word
return s

result = ""
result = str1 + test( 1 ) + str2 + test( 2 ) + str3

output_file.write(result)

input_file.close()
output_file.close()


### output ####

#ifdef DEVELP /* Borland C v3.1 version */
unsigned char VoltTable[] = {
3,6,9,12,15,18,21,23,26,29,32,34,37,40,42,45,
47,50,52,55,57,59,62,64,66,68,71,73,75,77,79,81,
83,86,88,90,91,93,95,97,99,101,103,105,106,108,110,112,
113,115,117,118,120,121,123,125,126,128,129,131,132,133,135,136,
138,139,140,142,143,144,146,147,148,149,151,152,153,154,155,157,
158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,
174,175,176,177,178,179,180,181,181,182,183,184,185,185,186,187,
188,189,189,190,191,192,192,193,194,194,195,196,196,197,198,198,
199,200,200,201,201,202,203,203,204,204,205,205,206,206,207,208,
208,209,209,210,210,211,211,212,212,212,213,213,214,214,215,215,
216,216,216,217,217,218,218,218,219,219,220,220,220,221,221,221,
222,222,222,223,223,223,224,224,224,225,225,225,226,226,226,226,
227,227,227,228,228,228,228,229,229,229,229,230,230,230,230,231,
231,231,231,232,232,232,232,233,233,233,233,233,234,234,234,234,
234,235,235,235,235,235,235,236,236,236,236,236,236,237,237,237,
237,237,237,238,238,238,238,238,238,238,239,239,239,239,239,0 };
#endif
#ifdef EMBED /* Dunfield assembler version */
void VoltTable(void) asm {
DB 3,6,9,12,15,18,21,23,26,29,32,34,37,40,42,45
DB 47,50,52,55,57,59,62,64,66,68,71,73,75,77,79,81
DB 83,86,88,90,91,93,95,97,99,101,103,105,106,108,110,112
DB 113,115,117,118,120,121,123,125,126,128,129,131,132,133,135,136
DB 138,139,140,142,143,144,146,147,148,149,151,152,153,154,155,157
DB 158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173
DB 174,175,176,177,178,179,180,181,181,182,183,184,185,185,186,187
DB 188,189,189,190,191,192,192,193,194,194,195,196,196,197,198,198
DB 199,200,200,201,201,202,203,203,204,204,205,205,206,206,207,208
DB 208,209,209,210,210,211,211,212,212,212,213,213,214,214,215,215
DB 216,216,216,217,217,218,218,218,219,219,220,220,220,221,221,221
DB 222,222,222,223,223,223,224,224,224,225,225,225,226,226,226,226
DB 227,227,227,228,228,228,228,229,229,229,229,230,230,230,230,231
DB 231,231,231,232,232,232,232,233,233,233,233,233,234,234,234,234
DB 234,235,235,235,235,235,235,236,236,236,236,236,236,237,237,237
DB 237,237,237,238,238,238,238,238,238,238,239,239,239,239,239,}
#endif

2 comments:

Anonymous said...

Check this out, daily trivia questions and contests, come join in on the fun!

Anonymous said...

うーん、インプットのファイルの形式が分からないので、なんとも言えませんが、以下のコードはおかしいですよ。

for line in str:
for word in line:
if word == '\r':

一行目のstrは、ファイル全体のデータがstringで入ってますよね。それをfor line in strってやると、lineには1文字づつ返されます。inという演算子は、sequence(stringもsequenceの1タイプです)のエレメントを1っこづつ返します。for word in lineは一文字のstringから一文字とってます。