File: bomstrip.py

package info (click to toggle)
bomstrip 9-12
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 228 kB
  • sloc: pascal: 41; cpp: 34; ansic: 34; java: 33; python: 31; sh: 24; makefile: 22; perl: 7; haskell: 6; php: 6; ruby: 6; awk: 2; sed: 1
file content (45 lines) | stat: -rwxr-xr-x 678 bytes parent folder | download | duplicates (9)
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
#!/usr/bin/env python2.3

"""
Strip leading byte-order-mark from utf-8 files.
"""

import sys

def usage(prog):
	print >>sys.stderr, 'usage: %s' % prog
	sys.exit(1)

def main(prog, *args):
	bufsize = 65536
	utf8bom = '\xef\xbb\xbf'

	from getopt import getopt, GetoptError
	try:
		opts, args = getopt(args, '')
	except GetoptError:
		usage(prog)

	if args:
		usage(prog)

	inf = sys.stdin
	outf = sys.stdout

	buf = inf.read(len(utf8bom))
	if buf != utf8bom:
		outf.write(buf)
	if buf == '':
		return

	while True:
		buf = inf.read(bufsize)
		if buf == '':
			break
		outf.write(buf)
	
if __name__ == '__main__':
	try:
		main(*sys.argv)
	except KeyboardInterrupt:
		sys.exit(1)