forked from snowballstem/snowball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iconv.py
50 lines (46 loc) · 1.27 KB
/
iconv.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
#!env python
# Simple (but slow) iconv replacement in Python.
import sys
in_cs = out_cs = in_file = out_file = pending = None
for arg in sys.argv[1:]:
if pending != None:
arg = pending + arg
pending = None
if arg.startswith('-'):
if arg[1] in ('f', 't', 'o'):
if len(arg) == 2:
pending = arg
continue
if arg[1] == 'f':
in_cs = arg[2:]
continue
if arg[1] == 't':
out_cs = arg[2:]
continue
if arg[1] == 'o':
out_file = open(arg[2:], 'wb')
continue
print("Unknown option: '%s'" % arg)
sys.exit(1)
if in_file == None:
in_file = open(arg, 'rb')
continue
print("Too many arguments")
sys.exit(1)
if in_cs == None:
print("Need to specify input cs with -f")
sys.exit(1)
if out_cs == None:
print("Need to specify output cs with -t")
sys.exit(1)
if in_file == None:
if hasattr(sys.stdin, 'buffer'):
in_file = sys.stdin.buffer
else:
in_file = sys.stdin
if out_file == None:
if hasattr(sys.stdout, 'buffer'):
out_file = sys.stdout.buffer
else:
out_file = sys.stdout
out_file.write(in_file.read().decode(in_cs).encode(out_cs))