blob: c818a563f35f52f0fcf19be2aadc926dba004583 [file] [log] [blame]
#!python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2023 Adrien Destugues <pulkomandy@pulkomandy.tk>
#
# Distributed under terms of the MIT license.
"""
generate locale_strings.h from .cs file
This is normally done on Amiga systems using SimpleCat. However, that is not open source, and
cannot be run anywhere else.
"""
import argparse
parser = argparse.ArgumentParser(prog = "generate_locale_strings_header",
description = "Generate locale_strings.h from SimpleCat .cs file")
parser.add_argument("input")
parser.add_argument("-o", "--output", default="locale_strings.h")
args = parser.parse_args()
# Parse input
# Generate "strings" array with dictionaries of 'name' -> 'text' strings
infile = open(args.input, "r", encoding="iso-8859-15")
state = 0
strings = []
currentstring = ""
for line in infile:
line = line.strip()
# Ignore comments and empty lines
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:
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
outfile = open(args.output, "w", encoding="iso-8859-15")
outfile.write("""
#ifndef Locale_Strings
#define Locale_Strings 1
/* Locale Catalog Source File
*
* Automatically generated by generate_header.py
* Do NOT edit by hand!
*
*/
/****************************************************************************/
#ifndef EXEC_TYPES_H
#include <exec/types.h>
#endif
#ifdef CATCOMP_ARRAY
#undef CATCOMP_NUMBERS
#undef CATCOMP_STRINGS
#define CATCOMP_NUMBERS
#define CATCOMP_STRINGS
#endif
#ifdef CATCOMP_BLOCK
#undef CATCOMP_STRINGS
#define CATCOMP_STRINGS
#endif
/****************************************************************************/
#ifdef CATCOMP_NUMBERS
""")
i = 0
for string in strings:
outfile.write(f"#define {string['name']} {i}\n")
i = i + 1
outfile.write(f"\n#define CATCOMP_LASTID {i - 1}")
outfile.write("""
#endif /* CATCOMP_NUMBERS */
/****************************************************************************/
#ifdef CATCOMP_STRINGS
""")
for string in strings:
escaped = string['text'].replace('"', '\"').replace('\n', '\\n\\\n')
outfile.write(f"#define {string['name']}_STR \"{escaped}\"\n")
outfile.write("""
#endif /* CATCOMP_STRINGS */
/****************************************************************************/
#ifdef CATCOMP_ARRAY
struct CatCompArrayType
{
LONG cca_ID;
STRPTR cca_Str;
};
static const struct CatCompArrayType CatCompArray[] =
{
""")
for string in strings:
outfile.write(f" {{{string['name']},(STRPTR){string['name']}_STR}},\n")
outfile.write("""};
#endif /* CATCOMP_ARRAY */
/****************************************************************************/
#ifdef CATCOMP_BLOCK
static const char CatCompBlock[] =
{
""")
i = 0
for string in strings:
i0 = i >> 24
i1 = i >> 16
i2 = i >> 8
i3 = i >> 0
l = len(string['text'].replace('\e', '\033').encode('latin1', 'backslashreplace').decode('unicode-escape')) + 1
odd = False
if (l & 1) != 0:
odd = True
l = l + 1
l0 = l >> 8
l1 = l
outfile.write(f" \"\\x{i0:02X}\\x{i1:02X}\\x{i2:02X}\\x{i3:02X}\" \"\\x{l0:02X}\\x{l1:02X}\"\n")
if odd:
outfile.write(f" {string['name']}_STR \"\\x00\\x00\"\n")
else:
outfile.write(f" {string['name']}_STR \"\\x00\"\n")
i = i + 1
outfile.write("""};
#endif /* CATCOMP_BLOCK */
/****************************************************************************/
#endif /* Locale_Strings */
""")