blob: c818a563f35f52f0fcf19be2aadc926dba004583 [file] [log] [blame]
PulkoMandy3e088532023-02-12 13:21:06 +01001#!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"""
10generate locale_strings.h from .cs file
11
12This is normally done on Amiga systems using SimpleCat. However, that is not open source, and
13cannot be run anywhere else.
14"""
15
16import argparse
17
18parser = argparse.ArgumentParser(prog = "generate_locale_strings_header",
19 description = "Generate locale_strings.h from SimpleCat .cs file")
20
21parser.add_argument("input")
22parser.add_argument("-o", "--output", default="locale_strings.h")
23
24args = parser.parse_args()
25
26# Parse input
27# Generate "strings" array with dictionaries of 'name' -> 'text' strings
28
29infile = open(args.input, "r", encoding="iso-8859-15")
30
31state = 0
32strings = []
PulkoMandy112be652023-10-15 23:06:53 +020033currentstring = ""
PulkoMandy3e088532023-02-12 13:21:06 +010034
35for 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
PulkoMandy112be652023-10-15 23:06:53 +020042 # 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 ;
PulkoMandy3e088532023-02-12 13:21:06 +010045 if state == 0:
46 name = line
47 state = 1
PulkoMandy112be652023-10-15 23:06:53 +020048 currentstring = ""
PulkoMandy3e088532023-02-12 13:21:06 +010049 elif state == 1:
PulkoMandy112be652023-10-15 23:06:53 +020050 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
PulkoMandy3e088532023-02-12 13:21:06 +010060
61# Write output
62
63outfile = open(args.output, "w", encoding="iso-8859-15")
64
65outfile.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
106i = 0
107for string in strings:
108 outfile.write(f"#define {string['name']} {i}\n")
109 i = i + 1
110
111outfile.write(f"\n#define CATCOMP_LASTID {i - 1}")
112
113outfile.write("""
114
115#endif /* CATCOMP_NUMBERS */
116
117
118/****************************************************************************/
119
120
121#ifdef CATCOMP_STRINGS
122
123""")
124
125for string in strings:
PulkoMandy112be652023-10-15 23:06:53 +0200126 escaped = string['text'].replace('"', '\"').replace('\n', '\\n\\\n')
127 outfile.write(f"#define {string['name']}_STR \"{escaped}\"\n")
PulkoMandy3e088532023-02-12 13:21:06 +0100128
129outfile.write("""
130#endif /* CATCOMP_STRINGS */
131
132
133/****************************************************************************/
134
135
136#ifdef CATCOMP_ARRAY
137
138struct CatCompArrayType
139{
140 LONG cca_ID;
141 STRPTR cca_Str;
142};
143
144static const struct CatCompArrayType CatCompArray[] =
145{
146""")
147
148for string in strings:
149 outfile.write(f" {{{string['name']},(STRPTR){string['name']}_STR}},\n")
150
151outfile.write("""};
152
153#endif /* CATCOMP_ARRAY */
154
155
156/****************************************************************************/
157
158
159#ifdef CATCOMP_BLOCK
160
161static const char CatCompBlock[] =
162{
163""")
164
165i = 0
166for string in strings:
167 i0 = i >> 24
168 i1 = i >> 16
169 i2 = i >> 8
170 i3 = i >> 0
PulkoMandy112be652023-10-15 23:06:53 +0200171 l = len(string['text'].replace('\e', '\033').encode('latin1', 'backslashreplace').decode('unicode-escape')) + 1
PulkoMandy3e088532023-02-12 13:21:06 +0100172 odd = False
173 if (l & 1) != 0:
174 odd = True
175 l = l + 1
176 l0 = l >> 8
177 l1 = l
PulkoMandya35ca262023-03-18 12:06:48 +0100178 outfile.write(f" \"\\x{i0:02X}\\x{i1:02X}\\x{i2:02X}\\x{i3:02X}\" \"\\x{l0:02X}\\x{l1:02X}\"\n")
PulkoMandy3e088532023-02-12 13:21:06 +0100179 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
185outfile.write("""};
186
187#endif /* CATCOMP_BLOCK */
188
189
190/****************************************************************************/
191
192
193
194#endif /* Locale_Strings */
195
196""")