1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import sys
- filenames = sys.argv
- filenames.pop(0)
- if not filenames:
- print("I need filename(s) to convert.")
- sys.exit(0)
- def my_repr(line):
- """ Given a latin1 line, output valid C++ string.
- if the character is < 0x20 or > 0x7e, output in hex format.
- if the character is ', " or ?, output \', \", or \?.
- """
- r = ""
- for c in line:
- o = ord(c)
- if ((o< 0x20) or (o > 0x7e)):
- r += "\\x{0:02x}".format(o)
- else:
- if c == "'":
- r += "\\'"
- elif c == '"':
- r += '\\"'
-
-
- elif c == '\\':
- r += '\\\\'
- else:
- r += c
- return r
- def readfile(filename):
- """ Reads the given ANSI file and outputs valid C++ code. """
-
- basename = filename
- pos = basename.find('.')
- if pos != -1:
- basename = basename[:pos]
- pos = basename.rfind('/')
- if pos != -1:
- basename = basename[pos+1:]
-
-
- lines = []
- with open(filename, "r", encoding="latin1") as fp:
- for line in fp:
- line = line.rstrip("\r\n")
- lines.append(line)
- print("{0} := [...]string {{".format(basename.upper()))
-
-
- for line in lines:
-
-
-
-
- print(" \"{0}\",".format(my_repr(line)))
- print(" }\n")
- for filename in filenames:
- readfile(filename)
|