Fix multiline strings handling and warnings in escape characters handling
diff --git a/generate_header.py b/generate_header.py
index d8083a0..c818a56 100755
--- a/generate_header.py
+++ b/generate_header.py
@@ -30,6 +30,7 @@
state = 0
strings = []
+currentstring = ""
for line in infile:
line = line.strip()
@@ -38,14 +39,24 @@
if len(line) == 0 or line[0] == '#':
continue
+ # State 0: reading the name of the entry
+ # State 1: reading the lines of text for the english translation
+ # State 2: reading the lines of text for other languages until we get a ;
if state == 0:
name = line
state = 1
+ currentstring = ""
elif state == 1:
- strings.append({"name": name, "text": line})
- state = 2
- elif state == 2 and line[0] == ';':
- state = 0
+ if line[-1] == '\\':
+ currentstring += line[:-1] + '\n'
+ else:
+ currentstring += line
+ strings.append({"name": name, "text": currentstring.replace('"', '\\"')})
+ currentstring = ""
+ state = 2
+ elif state == 2:
+ if line[0] == ';':
+ state = 0
# Write output
@@ -112,7 +123,8 @@
""")
for string in strings:
- outfile.write(f"#define {string['name']}_STR \"{string['text']}\"\n")
+ escaped = string['text'].replace('"', '\"').replace('\n', '\\n\\\n')
+ outfile.write(f"#define {string['name']}_STR \"{escaped}\"\n")
outfile.write("""
#endif /* CATCOMP_STRINGS */
@@ -156,7 +168,7 @@
i1 = i >> 16
i2 = i >> 8
i3 = i >> 0
- l = len(string['text'].encode('latin1', 'backslashreplace').decode('unicode-escape')) + 1
+ l = len(string['text'].replace('\e', '\033').encode('latin1', 'backslashreplace').decode('unicode-escape')) + 1
odd = False
if (l & 1) != 0:
odd = True