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 = [] |
| 33 | |
| 34 | for line in infile: |
| 35 | line = line.strip() |
| 36 | |
| 37 | # Ignore comments and empty lines |
| 38 | if len(line) == 0 or line[0] == '#': |
| 39 | continue |
| 40 | |
| 41 | if state == 0: |
| 42 | name = line |
| 43 | state = 1 |
| 44 | elif state == 1: |
| 45 | strings.append({"name": name, "text": line}) |
| 46 | state = 2 |
| 47 | elif state == 2 and line[0] == ';': |
| 48 | state = 0 |
| 49 | |
| 50 | # Write output |
| 51 | |
| 52 | outfile = open(args.output, "w", encoding="iso-8859-15") |
| 53 | |
| 54 | outfile.write(""" |
| 55 | #ifndef Locale_Strings |
| 56 | #define Locale_Strings 1 |
| 57 | |
| 58 | /* Locale Catalog Source File |
| 59 | * |
| 60 | * Automatically generated by generate_header.py |
| 61 | * Do NOT edit by hand! |
| 62 | * |
| 63 | */ |
| 64 | |
| 65 | |
| 66 | |
| 67 | /****************************************************************************/ |
| 68 | |
| 69 | |
| 70 | #ifndef EXEC_TYPES_H |
| 71 | #include <exec/types.h> |
| 72 | #endif |
| 73 | |
| 74 | #ifdef CATCOMP_ARRAY |
| 75 | #undef CATCOMP_NUMBERS |
| 76 | #undef CATCOMP_STRINGS |
| 77 | #define CATCOMP_NUMBERS |
| 78 | #define CATCOMP_STRINGS |
| 79 | #endif |
| 80 | |
| 81 | #ifdef CATCOMP_BLOCK |
| 82 | #undef CATCOMP_STRINGS |
| 83 | #define CATCOMP_STRINGS |
| 84 | #endif |
| 85 | |
| 86 | |
| 87 | |
| 88 | /****************************************************************************/ |
| 89 | |
| 90 | |
| 91 | #ifdef CATCOMP_NUMBERS |
| 92 | |
| 93 | """) |
| 94 | |
| 95 | i = 0 |
| 96 | for string in strings: |
| 97 | outfile.write(f"#define {string['name']} {i}\n") |
| 98 | i = i + 1 |
| 99 | |
| 100 | outfile.write(f"\n#define CATCOMP_LASTID {i - 1}") |
| 101 | |
| 102 | outfile.write(""" |
| 103 | |
| 104 | #endif /* CATCOMP_NUMBERS */ |
| 105 | |
| 106 | |
| 107 | /****************************************************************************/ |
| 108 | |
| 109 | |
| 110 | #ifdef CATCOMP_STRINGS |
| 111 | |
| 112 | """) |
| 113 | |
| 114 | for string in strings: |
| 115 | outfile.write(f"#define {string['name']}_STR \"{string['text']}\"\n") |
| 116 | |
| 117 | outfile.write(""" |
| 118 | #endif /* CATCOMP_STRINGS */ |
| 119 | |
| 120 | |
| 121 | /****************************************************************************/ |
| 122 | |
| 123 | |
| 124 | #ifdef CATCOMP_ARRAY |
| 125 | |
| 126 | struct CatCompArrayType |
| 127 | { |
| 128 | LONG cca_ID; |
| 129 | STRPTR cca_Str; |
| 130 | }; |
| 131 | |
| 132 | static const struct CatCompArrayType CatCompArray[] = |
| 133 | { |
| 134 | """) |
| 135 | |
| 136 | for string in strings: |
| 137 | outfile.write(f" {{{string['name']},(STRPTR){string['name']}_STR}},\n") |
| 138 | |
| 139 | outfile.write("""}; |
| 140 | |
| 141 | #endif /* CATCOMP_ARRAY */ |
| 142 | |
| 143 | |
| 144 | /****************************************************************************/ |
| 145 | |
| 146 | |
| 147 | #ifdef CATCOMP_BLOCK |
| 148 | |
| 149 | static const char CatCompBlock[] = |
| 150 | { |
| 151 | """) |
| 152 | |
| 153 | i = 0 |
| 154 | for string in strings: |
| 155 | i0 = i >> 24 |
| 156 | i1 = i >> 16 |
| 157 | i2 = i >> 8 |
| 158 | i3 = i >> 0 |
| 159 | l = len(string['text']) + 1 |
| 160 | odd = False |
| 161 | if (l & 1) != 0: |
| 162 | odd = True |
| 163 | l = l + 1 |
| 164 | l0 = l >> 8 |
| 165 | l1 = l |
| 166 | outfile.write(f" \"\\x{i0:02X}\\x{i1:02X}\\x{i2:02X}\\x{i3:02X}\\x{l0:02X}\\x{l1:02X}\"\n") |
| 167 | if odd: |
| 168 | outfile.write(f" {string['name']}_STR \"\\x00\\x00\"\n") |
| 169 | else: |
| 170 | outfile.write(f" {string['name']}_STR \"\\x00\"\n") |
| 171 | i = i + 1 |
| 172 | |
| 173 | outfile.write("""}; |
| 174 | |
| 175 | #endif /* CATCOMP_BLOCK */ |
| 176 | |
| 177 | |
| 178 | /****************************************************************************/ |
| 179 | |
| 180 | |
| 181 | |
| 182 | #endif /* Locale_Strings */ |
| 183 | |
| 184 | """) |