src_to7bits.py: use HEX format
also fixes line endings, insert "" when necessary fix detection of // comments
This commit is contained in:
parent
0fffe05848
commit
b8ac772922
@ -12,11 +12,12 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
conv_format = '\\%03o' # octal
|
#conv_format = '\\%03o' # octal
|
||||||
#conv_format = '\\x%02x' # hex
|
conv_format = '\\x%02x' # hex
|
||||||
|
|
||||||
# for C++ style comments //
|
# for C++ style comments //
|
||||||
commentre = re.compile('(.*)(\/\/.*)', re.MULTILINE | re.DOTALL)
|
#commentre = re.compile('(.*)(\/\/.*)', re.MULTILINE | re.DOTALL)
|
||||||
|
commentre = re.compile('^(.*?)(\/\/.*)$')
|
||||||
|
|
||||||
def escape_8bit_chars(char):
|
def escape_8bit_chars(char):
|
||||||
if ord(char) >= 0x7f:
|
if ord(char) >= 0x7f:
|
||||||
@ -28,9 +29,13 @@ def escape_8bit_chars(char):
|
|||||||
# and a filename+'.out' file was written
|
# and a filename+'.out' file was written
|
||||||
def to_7bit(filename):
|
def to_7bit(filename):
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
|
has_changed = False
|
||||||
output = ''
|
output = ''
|
||||||
in_comment = False # C style comments /* */
|
in_comment = False # C style comments /* */
|
||||||
|
lineindex = 0
|
||||||
for line in f.readlines():
|
for line in f.readlines():
|
||||||
|
line = line.rstrip()
|
||||||
|
lineindex = lineindex + 1
|
||||||
comment_start = 0
|
comment_start = 0
|
||||||
comment_end = -1
|
comment_end = -1
|
||||||
if not in_comment:
|
if not in_comment:
|
||||||
@ -53,14 +58,22 @@ def to_7bit(filename):
|
|||||||
(line, comment) = m.group(1,2)
|
(line, comment) = m.group(1,2)
|
||||||
else:
|
else:
|
||||||
comment = ''
|
comment = ''
|
||||||
conv = ''.join(map(escape_8bit_chars, list(line)))
|
convlist = map(escape_8bit_chars, list(line))
|
||||||
output = output + conv + comment
|
# check for problematic hex strings
|
||||||
if f.tell() < len(output):
|
for i in range(len(convlist)-1):
|
||||||
|
if convlist[i][0:2] == '\\x':
|
||||||
|
c = convlist[i+1]
|
||||||
|
if (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f') or (c >= 'A' and c <= 'F'):
|
||||||
|
print "WARNING %15s:%-4d %s followed by %s : Inserting \"\"" % (filename, lineindex, convlist[i], c)
|
||||||
|
convlist[i] = convlist[i] + '""'
|
||||||
|
conv = ''.join(convlist)
|
||||||
|
if conv != line:
|
||||||
|
has_changed = True
|
||||||
|
output = output + conv + comment + '\n'
|
||||||
|
if has_changed:
|
||||||
with open(filename + '.out', "w") as fout:
|
with open(filename + '.out', "w") as fout:
|
||||||
fout.write(output)
|
fout.write(output)
|
||||||
return True
|
return has_changed
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# program entry point
|
# program entry point
|
||||||
if len(sys.argv) <= 1:
|
if len(sys.argv) <= 1:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user