-
Notifications
You must be signed in to change notification settings - Fork 7
/
py_lineterm.py
executable file
·254 lines (211 loc) · 5.76 KB
/
py_lineterm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env python
#
# lineterm.py
#
# <This one may actually be cross-platform!!!>
#
# Usage: python lineterm.py [options] files
#
# Options:
# -h, --help : Show this message
# -m, --mac : Convert file(s) to Mac format (carriage returns)
# -p, --pc : Convert file(s) to PC format (carriage return/linefeeds)
# -u, --unix : Convert file(s) to Unix format (linefeeds)
# -v, --verbose : Show the number of CRs, LFs, and CR/LFs for the file(s)
#
# Arguments:
# files : any file or group of files (wildcards allowed)
#
# Note:
# Does not currently recurse directories or distinguish between
# binary and ASCII file types. Creates a ~temp file in the current
# directory when converting files to a different format.
#
import os, getopt, sys, curses
def main():
convertToMac = 0
convertToPC = 0
convertToUNIX = 0
isVerbose = 0
try:
opts, args = getopt.getopt( sys.argv[1:], "hmpuv:", ["help", "mac", "pc", "unix", "verbose"] )
except getopt.GetoptError:
# print help information and exit:
usage()
sys.exit( 2 )
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-m", "--mac"):
# print "output reached"
# output = a
convertToMac = 1
if o in ("-p", "--pc"):
convertToPC = 1
if o in ("-u", "--unix"):
convertToUNIX = 1
if o in ("-v", "--verbose"):
isVerbose = 1
if len(args) == 0:
usage()
sys.exit()
for FILE in args:
fileType = readFile( FILE, isVerbose )
if fileType != "Undefined":
if convertToMac == 1:
toMac( FILE, fileType )
if convertToPC == 1:
toPC( FILE, fileType )
if convertToUNIX == 1:
toUNIX( FILE, fileType )
else:
print "file "
print FILE
print " is undefined, therefore conversion was aborted"
def toMac( fileName, fileType ):
FILE = open( fileName, "r" )
os.system( "touch ~temp" )
OUTFILE = open( "~temp", "w" )
char = FILE.read( 1 )
last = 0
while char:
if fileType == "UNIX":
if char == chr(0x0a): # LINE FEED
OUTFILE.write( chr(0x0d) ) # replace LF with CR
else:
OUTFILE.write( char )
elif fileType == "MAC":
OUTFILE.write( char ) # file already in mac format
elif fileType == "PC":
if last == chr(0x0a): # if last character was CR
if char == chr(0x0d): # ...and this one is LF
OUTFILE.write( chr(0x0d) ) # then replace CR/LF with CR
last = 0
else:
OUTFILE.write( char )
elif char == chr(0x0a):
last = chr(0x0a)
else:
OUTFILE.write( char )
char = FILE.read( 1 )
FILE.close()
OUTFILE.close()
os.system( "mv ~temp " + fileName + ".MAC" )
print "MAC"
def toPC( fileName, fileType ):
FILE = open( fileName, "r" )
os.system( "touch ~temp" )
OUTFILE = open( "~temp", "w" )
char = FILE.read( 1 )
while char:
if fileType == "UNIX":
if char == chr(0x0a): # LF
OUTFILE.write( chr(0x0d) ) # replace LF with CR/LF
OUTFILE.write( chr(0x0a) )
else:
OUTFILE.write( char )
elif fileType == "MAC":
if char == chr(0x0d):
OUTFILE.write( chr(0x0d) ) # replace CR with CR/LF
OUTFILE.write( chr(0x0a) )
else:
OUTFILE.write( char )
elif fileType == "PC":
OUTFILE.write( char ) # file already in PC format
char = FILE.read( 1 )
FILE.close()
OUTFILE.close()
os.system( "mv ~temp " + fileName + ".PC" )
print "PC"
def toUNIX( fileName, fileType ):
FILE = open( fileName, "r" )
os.system( "touch ~temp" )
OUTFILE = open( "~temp", "w" )
char = FILE.read( 1 )
last = 0
while char:
if fileType == "UNIX":
OUTFILE.write( char ) # file already in UNIX format
elif fileType == "MAC":
if char == chr(0x0d):
OUTFILE.write( chr(0x0a) )
else:
OUTFILE.write( char )
elif fileType == "PC":
if last == chr(0x0d): # if last character was CR
if char == chr(0x0a): # ...and this one is LF
OUTFILE.write( chr(0x0a) ) # then replace CR/LF with LF
last = 0
else:
OUTFILE.write( char )
elif char == chr(0x0d):
last = chr(0x0d)
else:
OUTFILE.write( char )
char = FILE.read( 1 )
FILE.close()
OUTFILE.close()
os.system( "mv ~temp " + fileName + ".UNIX" )
print "UNIX"
def readFile( fileName, isVerbose ):
numCR = 0
numLF = 0
numCRLF = 0
FILE = open( fileName, "r" )
char = FILE.read( 1 )
last = 0
while char:
if char == chr(0x0a): # LINE FEED
numLF = numLF + 1
if last == chr(0x0d): # CR/LF
numCRLF = numCRLF + 1
elif char == chr(0x0d): # CARRIAGE RETURN
numCR = numCR + 1
else:
pass
last = char
char = FILE.read( 1 )
FILE.close()
fileType = "Undefined"
if numCRLF > 0:
print fileName, ": PC Format (CR/LF)"
fileType = "PC"
elif numCR > 0:
if numLF == 0:
print fileName, ": MAC Format (CR only)"
fileType = "MAC"
elif numCR > numLF:
print fileName, ": mix of CR and LF... probably MAC Format"
elif numCR == numLF:
print fileName, ": mix of CR and LF... undetermined format"
else:
print fileName, ": mix of CR and LF... probably UNIX Format"
elif numLF > 0:
print fileName, ": UNIX Format (LF only)"
fileType = "UNIX"
else:
print fileName, ": This file had no terminators"
if isVerbose:
print "CR: ", numCR
print "LF: ", numLF
print "CRLF: ", numCRLF
return fileType
def usage():
print '''\
Usage: python lineterm.py [options] files
Options:
-h, --help : Show this message
-m, --mac : Convert file(s) to Mac format (carriage returns)
-p, --pc : Convert file(s) to PC format (carriage return/linefeeds)
-u, --unix : Convert file(s) to Unix format (linefeeds)
-v, --verbose : Show the number of CRs, LFs, and CR/LFs for the file(s)
Arguments:
files : any file or group of files (wildcards allowed)
Note:
Does not currently recurse directories or distinguish between
binary and ASCII file types. Creates a ~temp file in current
directory when converting files.
'''
if __name__ == "__main__":
main()