diff options
Diffstat (limited to 'tools/po2c')
-rwxr-xr-x | tools/po2c | 128 |
1 files changed, 57 insertions, 71 deletions
diff --git a/tools/po2c b/tools/po2c index f6aff97404..a2b006cd6b 100755 --- a/tools/po2c +++ b/tools/po2c @@ -22,7 +22,7 @@ # http://www.triptico.com # -$VERSION = "1.0.2"; +$VERSION = "1.0.2-scummvm"; if(scalar(@ARGV) == 0) { @@ -106,9 +106,9 @@ for(my $n = 0;$n < scalar(@msgids);$n++) $msgids{$msgids[$n]} = $n; } -# stage 3: dump as C code +# stage 3: dump as C++ code -print "/* generated by po2c $VERSION - Do not modify */\n\n"; +print "// generated by po2c $VERSION - Do not modify\n\n"; print "#include <stdio.h>\n"; print "#include <string.h>\n\n"; @@ -123,16 +123,16 @@ for(my $n = 0;$n < scalar(@msgids);$n++) print "\tNULL\n};\n\n"; # dump the lang structure -print "struct _po2c_msg {\n"; +print "struct PoMessageEntry {\n"; print "\tint msgid;\n"; -print "\tconst char * msgstr;\n"; +print "\tconst char *msgstr;\n"; print "};\n\n"; # dump now each language foreach my $l (keys(%msgs)) { - print "static const struct _po2c_msg _po2c_lang_${l}\[\] = {\n"; + print "static const PoMessageEntry _po2c_lang_${l}\[\] = {\n"; # get the translation table for the language $l my ($m) = $msgs{$l}; @@ -160,11 +160,12 @@ foreach my $l (keys(%msgs)) # finally, dump the languages -print "static const struct {\n"; -print "\tconst char * lang;\n"; -print "\tconst char * charset;\n"; -print "\tconst struct _po2c_msg * msgs;\n"; -print "} _po2c_langs[] = {\n"; +print "struct PoLangEntry {\n"; +print "\tconst char *lang;\n"; +print "\tconst char *charset;\n"; +print "\tconst PoMessageEntry *msgs;\n"; +print "};\n\n"; +print "const PoLangEntry _po2c_langs[] = {\n"; foreach my $l (keys(%msgs)) { @@ -176,103 +177,88 @@ foreach my $l (keys(%msgs)) print "\t{ NULL, NULL, NULL }\n};\n\n"; -print "/* code */\n"; +print "// code\n"; print << 'EOF'; -static const struct _po2c_msg * _po2c_lang=NULL; -static int _po2c_lang_size=0; -static const char * _po2c_charset=NULL; - -void po2c_setlang(const char * lang) -{ - int n; +static const struct PoMessageEntry *_po2c_lang = NULL; +static int _po2c_lang_size = 0; +static const char * _po2c_charset = NULL; - _po2c_lang=NULL; - _po2c_lang_size=0; - _po2c_charset=NULL; +void po2c_setlang(const char *lang) { + _po2c_lang = NULL; + _po2c_lang_size = 0; + _po2c_charset = NULL; - /* if lang is NULL or "", deactivate it */ - if(lang == NULL || *lang == '\0') + // if lang is NULL or "", deactivate it + if (lang == NULL || *lang == '\0') return; - /* searches for a valid language array */ - for(n=0;_po2c_lang == NULL && _po2c_langs[n].lang != NULL;n++) - { - if(strcmp(lang, _po2c_langs[n].lang) == 0) { - _po2c_lang=_po2c_langs[n].msgs; - _po2c_charset=_po2c_langs[n].charset; + // searches for a valid language array + for (int i = 0; _po2c_lang == NULL && _po2c_langs[i].lang != NULL; ++i) { + if (strcmp(lang, _po2c_langs[i].lang) == 0) { + _po2c_lang = _po2c_langs[i].msgs; + _po2c_charset = _po2c_langs[i].charset; } } - /* try partial searches */ - for(n=0;_po2c_lang == NULL && _po2c_langs[n].lang != NULL;n++) - { - if(strncmp(lang, _po2c_langs[n].lang, 2) == 0) { - _po2c_lang=_po2c_langs[n].msgs; - _po2c_charset=_po2c_langs[n].charset; + // try partial searches + for (int i = 0; _po2c_lang == NULL && _po2c_langs[i].lang != NULL; ++i) { + if (strncmp(lang, _po2c_langs[i].lang, 2) == 0) { + _po2c_lang = _po2c_langs[i].msgs; + _po2c_charset = _po2c_langs[i].charset; } } - /* if found, count entries */ - if(_po2c_lang != NULL) - { - const struct _po2c_msg * m; - - for(m=_po2c_lang;m->msgid != -1;m++) - _po2c_lang_size++; + // if found, count entries + if (_po2c_lang != NULL) { + for (const PoMessageEntry *m = _po2c_lang; m->msgid != -1; ++m) + ++_po2c_lang_size; } } -const char * po2c_gettext(const char * msgid) -{ - const struct _po2c_msg * m; - int b, t, n, c; +const char *po2c_gettext(const char *msgid) { + // if no language is set or msgid is empty, return msgid as is + if (_po2c_lang == NULL || *msgid == '\0') + return msgid; - /* if no language is set or msgid is empty, return msgid as is */ - if(_po2c_lang == NULL || *msgid == '\0') - return(msgid); + // binary-search for the msgid + int leftIndex = 0; + int rightIndex = _po2c_lang_size - 1; - /* binary-search for the msgid */ - b=0; t=_po2c_lang_size - 1; + while (rightIndex >= leftIndex) { + const int midIndex = (leftIndex + rightIndex) / 2; + const struct PoMessageEntry * const m = &_po2c_lang[midIndex]; - while(t >= b) - { - n=(b + t) / 2; - m=&_po2c_lang[n]; - - c=strcmp(msgid, _po2c_msgids[m->msgid]); + const int compareResult = strcmp(msgid, _po2c_msgids[m->msgid]); - if(c == 0) - return(m->msgstr); + if (compareResult == 0) + return m->msgstr; + else if (compareResult < 0) + rightIndex = midIndex - 1; else - if(c < 0) - t=n - 1; - else - b=n + 1; + leftIndex = midIndex + 1; } - return(msgid); + return msgid; } -const char * po2c_getcharset(void) -{ +const char *po2c_getcharset(void) { if (_po2c_charset) return _po2c_charset; else return "ASCII"; } -int po2c_getnumlangs(void) -{ +int po2c_getnumlangs(void) { int n = 0; + while (_po2c_langs[n].lang) n++; return n; } -const char * po2c_getlang(int num) -{ +const char *po2c_getlang(int num) { return _po2c_langs[num].lang; } EOF |