PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 1 | #!python3 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # vim:fenc=utf-8 |
| 4 | # |
| 5 | # Copyright © 2023 Adrien Destugues <pulkomandy@pulkomandy.tk> |
| 6 | # |
| 7 | # Distributed under terms of the MIT license. |
| 8 | |
| 9 | """ |
| 10 | generate locale_strings.h from .cs file |
| 11 | |
| 12 | This is normally done on Amiga systems using SimpleCat. However, that is not open source, and |
| 13 | cannot be run anywhere else. |
| 14 | """ |
| 15 | |
| 16 | import argparse |
| 17 | |
| 18 | parser = argparse.ArgumentParser(prog = "generate_locale_strings_header", |
| 19 | description = "Generate locale_strings.h from SimpleCat .cs file") |
| 20 | |
| 21 | parser.add_argument("input") |
| 22 | parser.add_argument("-o", "--output", default="locale_strings.h") |
| 23 | |
| 24 | args = parser.parse_args() |
| 25 | |
| 26 | # Parse input |
| 27 | # Generate "strings" array with dictionaries of 'name' -> 'text' strings |
| 28 | |
| 29 | infile = open(args.input, "r", encoding="iso-8859-15") |
| 30 | |
| 31 | state = 0 |
| 32 | strings = [] |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 33 | currentstring = "" |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 34 | |
| 35 | for line in infile: |
| 36 | line = line.strip() |
| 37 | |
| 38 | # Ignore comments and empty lines |
| 39 | if len(line) == 0 or line[0] == '#': |
| 40 | continue |
| 41 | |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 42 | # State 0: reading the name of the entry |
| 43 | # State 1: reading the lines of text for the english translation |
| 44 | # State 2: reading the lines of text for other languages until we get a ; |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 45 | if state == 0: |
| 46 | name = line |
| 47 | state = 1 |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 48 | currentstring = "" |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 49 | elif state == 1: |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 50 | if line[-1] == '\\': |
| 51 | currentstring += line[:-1] + '\n' |
| 52 | else: |
| 53 | currentstring += line |
| 54 | strings.append({"name": name, "text": currentstring.replace('"', '\\"')}) |
| 55 | currentstring = "" |
| 56 | state = 2 |
| 57 | elif state == 2: |
| 58 | if line[0] == ';': |
| 59 | state = 0 |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 60 | |
| 61 | # Write output |
| 62 | |
| 63 | outfile = open(args.output, "w", encoding="iso-8859-15") |
| 64 | |
| 65 | outfile.write(""" |
| 66 | #ifndef Locale_Strings |
| 67 | #define Locale_Strings 1 |
| 68 | |
| 69 | /* Locale Catalog Source File |
| 70 | * |
| 71 | * Automatically generated by generate_header.py |
| 72 | * Do NOT edit by hand! |
| 73 | * |
| 74 | */ |
| 75 | |
| 76 | |
| 77 | |
| 78 | /****************************************************************************/ |
| 79 | |
| 80 | |
| 81 | #ifndef EXEC_TYPES_H |
| 82 | #include <exec/types.h> |
| 83 | #endif |
| 84 | |
| 85 | #ifdef CATCOMP_ARRAY |
| 86 | #undef CATCOMP_NUMBERS |
| 87 | #undef CATCOMP_STRINGS |
| 88 | #define CATCOMP_NUMBERS |
| 89 | #define CATCOMP_STRINGS |
| 90 | #endif |
| 91 | |
| 92 | #ifdef CATCOMP_BLOCK |
| 93 | #undef CATCOMP_STRINGS |
| 94 | #define CATCOMP_STRINGS |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | |
| 99 | /****************************************************************************/ |
| 100 | |
| 101 | |
| 102 | #ifdef CATCOMP_NUMBERS |
| 103 | |
| 104 | """) |
| 105 | |
| 106 | i = 0 |
| 107 | for string in strings: |
| 108 | outfile.write(f"#define {string['name']} {i}\n") |
| 109 | i = i + 1 |
| 110 | |
| 111 | outfile.write(f"\n#define CATCOMP_LASTID {i - 1}") |
| 112 | |
| 113 | outfile.write(""" |
| 114 | |
| 115 | #endif /* CATCOMP_NUMBERS */ |
| 116 | |
| 117 | |
| 118 | /****************************************************************************/ |
| 119 | |
| 120 | |
| 121 | #ifdef CATCOMP_STRINGS |
| 122 | |
| 123 | """) |
| 124 | |
| 125 | for string in strings: |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 126 | escaped = string['text'].replace('"', '\"').replace('\n', '\\n\\\n') |
| 127 | outfile.write(f"#define {string['name']}_STR \"{escaped}\"\n") |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 128 | |
| 129 | outfile.write(""" |
| 130 | #endif /* CATCOMP_STRINGS */ |
| 131 | |
| 132 | |
| 133 | /****************************************************************************/ |
| 134 | |
| 135 | |
| 136 | #ifdef CATCOMP_ARRAY |
| 137 | |
| 138 | struct CatCompArrayType |
| 139 | { |
| 140 | LONG cca_ID; |
| 141 | STRPTR cca_Str; |
| 142 | }; |
| 143 | |
| 144 | static const struct CatCompArrayType CatCompArray[] = |
| 145 | { |
| 146 | """) |
| 147 | |
| 148 | for string in strings: |
| 149 | outfile.write(f" {{{string['name']},(STRPTR){string['name']}_STR}},\n") |
| 150 | |
| 151 | outfile.write("""}; |
| 152 | |
| 153 | #endif /* CATCOMP_ARRAY */ |
| 154 | |
| 155 | |
| 156 | /****************************************************************************/ |
| 157 | |
| 158 | |
| 159 | #ifdef CATCOMP_BLOCK |
| 160 | |
| 161 | static const char CatCompBlock[] = |
| 162 | { |
| 163 | """) |
| 164 | |
| 165 | i = 0 |
| 166 | for string in strings: |
| 167 | i0 = i >> 24 |
| 168 | i1 = i >> 16 |
| 169 | i2 = i >> 8 |
| 170 | i3 = i >> 0 |
PulkoMandy | 112be65 | 2023-10-15 23:06:53 +0200 | [diff] [blame] | 171 | l = len(string['text'].replace('\e', '\033').encode('latin1', 'backslashreplace').decode('unicode-escape')) + 1 |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 172 | odd = False |
| 173 | if (l & 1) != 0: |
| 174 | odd = True |
| 175 | l = l + 1 |
| 176 | l0 = l >> 8 |
| 177 | l1 = l |
PulkoMandy | a35ca26 | 2023-03-18 12:06:48 +0100 | [diff] [blame] | 178 | outfile.write(f" \"\\x{i0:02X}\\x{i1:02X}\\x{i2:02X}\\x{i3:02X}\" \"\\x{l0:02X}\\x{l1:02X}\"\n") |
PulkoMandy | 3e08853 | 2023-02-12 13:21:06 +0100 | [diff] [blame] | 179 | if odd: |
| 180 | outfile.write(f" {string['name']}_STR \"\\x00\\x00\"\n") |
| 181 | else: |
| 182 | outfile.write(f" {string['name']}_STR \"\\x00\"\n") |
| 183 | i = i + 1 |
| 184 | |
| 185 | outfile.write("""}; |
| 186 | |
| 187 | #endif /* CATCOMP_BLOCK */ |
| 188 | |
| 189 | |
| 190 | /****************************************************************************/ |
| 191 | |
| 192 | |
| 193 | |
| 194 | #endif /* Locale_Strings */ |
| 195 | |
| 196 | """) |