mirror of
https://gitlab.com/rnger/amath
synced 2025-10-05 18:39:41 +00:00
Localization on Standard C platforms
This commit is contained in:
@ -25,13 +25,25 @@
|
||||
*/
|
||||
|
||||
#include "clib.h"
|
||||
#include "localize/lex.h"
|
||||
#include "localize/help.h"
|
||||
#include "localize/kword.h"
|
||||
#include "localize/ident.h"
|
||||
#include "localize/text.h"
|
||||
#include "localize/tags.h"
|
||||
#include "localize/ialias.h"
|
||||
#include "system/language.h"
|
||||
#include "system/program.h"
|
||||
|
||||
Language::Language()
|
||||
{
|
||||
lastText = NOMEM;
|
||||
keywordsloc = NOMEM;
|
||||
keywordcount = sizeof(keywords) / sizeof(keyworddef);
|
||||
textcount = sizeof(textdefs) / sizeof(textdef);
|
||||
identcount = sizeof(identtexts) / sizeof(identhelpdef);
|
||||
helpcount = sizeof(helptexts) / sizeof(helptextdef);
|
||||
aliascount = sizeof(identaliases) / sizeof(identalias);
|
||||
}
|
||||
|
||||
Language::~Language()
|
||||
@ -39,20 +51,93 @@ Language::~Language()
|
||||
if (lastText != NOMEM) {
|
||||
delete lastText;
|
||||
}
|
||||
|
||||
if (keywordsloc != NOMEM) {
|
||||
delete [] keywordsloc;
|
||||
}
|
||||
}
|
||||
|
||||
char* Language::FindAlias(const char* ident)
|
||||
{
|
||||
static const unsigned int count = sizeof(identaliases) / sizeof(identalias);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
for (unsigned int i = 0; i < aliascount; i++) {
|
||||
if (StrIsEqual(identaliases[i].ident, ident)) {
|
||||
return (char*)identaliases[i].alias;
|
||||
}
|
||||
}
|
||||
|
||||
return (char*)ident;
|
||||
}
|
||||
|
||||
Symbol Language::FindKeyword(const char* ident)
|
||||
{
|
||||
for (unsigned int i = 0; i < keywordcount; i++) {
|
||||
if (
|
||||
Program->Language->StrIsEqualLoc(keywords[i].name, ident) ||
|
||||
(keywordsloc != NULL &&
|
||||
Program->Language->StrIsEqualLoc(keywordsloc[i].name, ident))) {
|
||||
return keywords[i].symbol;
|
||||
}
|
||||
}
|
||||
return (Symbol)0;
|
||||
}
|
||||
|
||||
char* Language::GetText(int id)
|
||||
{
|
||||
textdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < textcount; i++) {
|
||||
if (textdefs[i].id == id) {
|
||||
def = (textdef*)&textdefs[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
char *text = Translate(def);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
}
|
||||
|
||||
char* Language::GetHelpText(char* ident)
|
||||
{
|
||||
char *s = FindAlias(ident);
|
||||
identhelpdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < identcount; i++) {
|
||||
if (StrIsEqual(identtexts[i].ident, s)) {
|
||||
def = (identhelpdef*)&identtexts[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
char *text = Translate(def);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
}
|
||||
|
||||
char* Language::GetHelpText(Symbol symbol)
|
||||
{
|
||||
helptextdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < helpcount; i++) {
|
||||
if (helptexts[i].symbol == symbol) {
|
||||
def = (helptextdef*)&helptexts[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
char *text = Translate(def);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
}
|
||||
|
||||
char* Language::UntagText(const char* text)
|
||||
{
|
||||
if (lastText != NOMEM) {
|
||||
|
@ -30,14 +30,17 @@
|
||||
#include "localize/lex.h"
|
||||
#include "localize/help.h"
|
||||
#include "localize/text.h"
|
||||
#include "localize/ident.h"
|
||||
#include "localize/kword.h"
|
||||
|
||||
class Language {
|
||||
public:
|
||||
Language();
|
||||
virtual ~Language();
|
||||
virtual char* GetText(int id) = 0;
|
||||
virtual char* GetHelpText(char *ident) = 0;
|
||||
virtual char* GetHelpText(Symbol symbol) = 0;
|
||||
char* GetText(int id);
|
||||
char* GetHelpText(char *ident);
|
||||
char* GetHelpText(Symbol symbol);
|
||||
Symbol FindKeyword(const char *ident);
|
||||
virtual char GetFractionPoint() = 0;
|
||||
virtual bool CharIsAlNum(unsigned long character) = 0;
|
||||
virtual bool CharIsAlpha(unsigned long character) = 0;
|
||||
@ -46,9 +49,20 @@ public:
|
||||
virtual bool CharIsSpace(unsigned long character) = 0;
|
||||
virtual bool CharIsCntrl(unsigned long character) = 0;
|
||||
virtual bool StrIsEqualLoc(const char *s1, const char *s2) = 0;
|
||||
virtual Symbol FindKeyword(const char *ident) = 0;
|
||||
|
||||
protected:
|
||||
virtual char* Translate(textdef *def) = 0;
|
||||
virtual char* Translate(helptextdef *def) = 0;
|
||||
virtual char* Translate(identhelpdef *def) = 0;
|
||||
|
||||
keyworddef *keywordsloc;
|
||||
unsigned int keywordcount;
|
||||
unsigned int textcount;
|
||||
unsigned int identcount;
|
||||
unsigned int helpcount;
|
||||
unsigned int aliascount;
|
||||
|
||||
private:
|
||||
char* FindAlias(const char *ident);
|
||||
char* UntagText(const char *text);
|
||||
char* lastText;
|
||||
|
@ -38,18 +38,9 @@
|
||||
#ifdef AMIGA
|
||||
#include <clib/locale_protos.h>
|
||||
|
||||
static unsigned int keywordcount;
|
||||
static unsigned int textcount;
|
||||
static unsigned int identcount;
|
||||
static unsigned int helpcount;
|
||||
|
||||
AmigaLanguage::AmigaLanguage()
|
||||
AmigaLanguage::AmigaLanguage() :
|
||||
Language::Language()
|
||||
{
|
||||
keywordcount = sizeof(keywords) / sizeof(keyworddef);
|
||||
textcount = sizeof(textdefs) / sizeof(textdef);
|
||||
identcount = sizeof(identtexts) / sizeof(identhelpdef);
|
||||
helpcount = sizeof(helptexts) / sizeof(helptextdef);
|
||||
|
||||
locale = OpenLocale(NULL);
|
||||
helpcatalog = OpenCatalog(locale, CATALOG_HELP, CATALOG_DEF, TAG_DONE);
|
||||
identcatalog = OpenCatalog(locale, CATALOG_IDEN, CATALOG_DEF, TAG_DONE);
|
||||
@ -63,9 +54,6 @@ AmigaLanguage::AmigaLanguage()
|
||||
keywordsloc[j].name = GetCatalogStr(keywordcatalog, j, NULL);
|
||||
keywordsloc[j].symbol = keywords[j].symbol;
|
||||
}
|
||||
|
||||
} else {
|
||||
keywordcatalog = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -90,81 +78,21 @@ AmigaLanguage::~AmigaLanguage()
|
||||
if (locale != NULL) {
|
||||
CloseLocale(locale);
|
||||
}
|
||||
|
||||
if (keywordsloc != NULL) {
|
||||
delete [] keywordsloc;
|
||||
}
|
||||
}
|
||||
|
||||
Symbol AmigaLanguage::FindKeyword(const char* ident)
|
||||
char* AmigaLanguage::Translate(textdef def)
|
||||
{
|
||||
for (unsigned int i = 0; i < keywordcount; i++) {
|
||||
if (
|
||||
Program->Language->StrIsEqualLoc(keywords[i].name, ident) || (keywordsloc != NULL &&
|
||||
Program->Language->StrIsEqualLoc(keywordsloc[i].name, ident))) {
|
||||
return keywords[i].symbol;
|
||||
}
|
||||
}
|
||||
|
||||
return (Symbol)0;
|
||||
return (char*)GetCatalogStr(textcatalog, def->id, (char*)def->text);
|
||||
}
|
||||
|
||||
char* AmigaLanguage::GetText(int id)
|
||||
char* AmigaLanguage::Translate(helptextdef def)
|
||||
{
|
||||
textdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < textcount; i++) {
|
||||
if (textdefs[i].id == id) {
|
||||
def = (textdef*)&textdefs[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
const char *text = GetCatalogStr(textcatalog, def->id, (char*)def->text);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
return (char*)GetCatalogStr(helpcatalog, def->id, (char*)def->text);
|
||||
}
|
||||
|
||||
char* AmigaLanguage::GetHelpText(char* ident)
|
||||
char* AmigaLanguage::Translate(identhelpdef def)
|
||||
{
|
||||
char *s = FindAlias(ident);
|
||||
identhelpdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < identcount; i++) {
|
||||
if (StrIsEqual(identtexts[i].ident, s)) {
|
||||
def = (identhelpdef*)&identtexts[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
const char *text = GetCatalogStr(identcatalog, def->id, (char*)def->text);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
}
|
||||
|
||||
char* AmigaLanguage::GetHelpText(Symbol symbol)
|
||||
{
|
||||
helptextdef *def = NOMEM;
|
||||
for (unsigned int i = 0; i < helpcount; i++) {
|
||||
if (helptexts[i].symbol == symbol) {
|
||||
def = (helptextdef*)&helptexts[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (def == NOMEM) {
|
||||
return (char*)(HELPNOHELP);
|
||||
}
|
||||
|
||||
const char *text = GetCatalogStr(helpcatalog, def->id, (char*)def->text);
|
||||
char *untagged = UntagText(text);
|
||||
return untagged;
|
||||
return (char*)GetCatalogStr(identcatalog, def->id, (char*)def->text);
|
||||
}
|
||||
|
||||
char AmigaLanguage::GetFractionPoint()
|
||||
|
@ -37,9 +37,6 @@ class AmigaLanguage : public Language {
|
||||
public:
|
||||
AmigaLanguage();
|
||||
~AmigaLanguage();
|
||||
char* GetText(int id);
|
||||
char* GetHelpText(char *ident);
|
||||
char* GetHelpText(Symbol symbol);
|
||||
char GetFractionPoint();
|
||||
bool CharIsAlNum(unsigned long character);
|
||||
bool CharIsAlpha(unsigned long character);
|
||||
@ -48,7 +45,11 @@ public:
|
||||
bool CharIsSpace(unsigned long character);
|
||||
bool CharIsCntrl(unsigned long character);
|
||||
bool StrIsEqualLoc(const char *s1, const char *s2);
|
||||
Symbol FindKeyword(const char *ident);
|
||||
|
||||
protected:
|
||||
char* Translate(textdef *def);
|
||||
char* Translate(helptextdef *def);
|
||||
char* Translate(identhelpdef *def);
|
||||
|
||||
private:
|
||||
struct Locale* locale;
|
||||
@ -56,7 +57,6 @@ private:
|
||||
struct Catalog *identcatalog;
|
||||
struct Catalog *textcatalog;
|
||||
struct Catalog *keywordcatalog;
|
||||
keyworddef *keywordsloc;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -31,67 +31,55 @@
|
||||
#include "localize/text.h"
|
||||
#include "localize/ident.h"
|
||||
#include "localize/kword.h"
|
||||
#include "system/base/io.h"
|
||||
#include "system/program.h"
|
||||
#include "system/language_stdc.h"
|
||||
|
||||
StandardLanguage::StandardLanguage()
|
||||
{ }
|
||||
StandardLanguage::StandardLanguage() :
|
||||
Language::Language()
|
||||
{
|
||||
LoadCatalogs();
|
||||
}
|
||||
|
||||
StandardLanguage::~StandardLanguage()
|
||||
{ }
|
||||
|
||||
Symbol StandardLanguage::FindKeyword(const char* ident)
|
||||
{
|
||||
static const unsigned int count = sizeof(keywords) / sizeof(keyworddef);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (Program->Language->StrIsEqualLoc(keywords[i].name, ident)) {
|
||||
return keywords[i].symbol;
|
||||
}
|
||||
if (textbase != NOMEM) {
|
||||
delete textbase;
|
||||
}
|
||||
|
||||
if (helpbase != NOMEM) {
|
||||
delete helpbase;
|
||||
}
|
||||
|
||||
if (identbase != NOMEM) {
|
||||
delete identbase;
|
||||
}
|
||||
|
||||
if (kwordbase != NOMEM) {
|
||||
delete kwordbase;
|
||||
}
|
||||
|
||||
return (Symbol)0;
|
||||
}
|
||||
|
||||
char* StandardLanguage::GetText(int id)
|
||||
char* StandardLanguage::Translate(textdef* def)
|
||||
{
|
||||
const char *text = NOMEM;
|
||||
static const unsigned int count = sizeof(textdefs) / sizeof(textdef);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (textdefs[i].id == id) {
|
||||
text = textdefs[i].text;
|
||||
break;
|
||||
}
|
||||
}
|
||||
char *untagged = UntagText(text);
|
||||
return (char*)(untagged != NOMEM ? untagged : HELPNOHELP);
|
||||
return textcatalog == NOMEM ?
|
||||
(char*)def->text :
|
||||
(char*)textcatalog[def->id].text;
|
||||
}
|
||||
|
||||
char* StandardLanguage::GetHelpText(char* ident)
|
||||
char* StandardLanguage::Translate(helptextdef* def)
|
||||
{
|
||||
const char *text = NOMEM;
|
||||
static const unsigned int count = sizeof(identtexts) / sizeof(identhelpdef);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (StrIsEqual(identtexts[i].ident, ident)) {
|
||||
text = identtexts[i].text;
|
||||
break;
|
||||
}
|
||||
}
|
||||
char *untagged = UntagText(text);
|
||||
return (char*)(untagged != NOMEM ? untagged : HELPNOHELP);
|
||||
return helpcatalog == NOMEM ?
|
||||
(char*)def->text :
|
||||
(char*)helpcatalog[def->id].text;
|
||||
}
|
||||
|
||||
char* StandardLanguage::GetHelpText(Symbol symbol)
|
||||
char* StandardLanguage::Translate(identhelpdef* def)
|
||||
{
|
||||
const char *text = NOMEM;
|
||||
static const unsigned int count = sizeof(helptexts) / sizeof(helptextdef);
|
||||
for (unsigned int i = 0; i < count; i++) {
|
||||
if (helptexts[i].symbol == symbol) {
|
||||
text = helptexts[i].text;
|
||||
break;
|
||||
}
|
||||
}
|
||||
char *untagged = UntagText(text);
|
||||
return (char*)(untagged != NOMEM ? untagged : HELPNOHELP);
|
||||
return identcatalog == NOMEM ?
|
||||
(char*)def->text :
|
||||
(char*)identcatalog[def->id].text;
|
||||
}
|
||||
|
||||
char StandardLanguage::GetFractionPoint()
|
||||
@ -136,3 +124,106 @@ bool StandardLanguage::StrIsEqualLoc(const char* s1, const char* s2)
|
||||
{
|
||||
return StrIsEqual(s1, s2);
|
||||
}
|
||||
|
||||
|
||||
void StandardLanguage::LoadCatalogs()
|
||||
{
|
||||
const char* key;
|
||||
const char* value;
|
||||
|
||||
LoadCatalog(&textbase, "utext/dk-text.dict");
|
||||
if (textbase != NOMEM) {
|
||||
textcatalog = new textdef[textcount];
|
||||
for (unsigned int j = 0; j < textcount; j++) {
|
||||
GetNextPair(&key, &value);
|
||||
textcatalog[j].id = j;
|
||||
textcatalog[j].text = value;
|
||||
}
|
||||
}
|
||||
|
||||
LoadCatalog(&helpbase, "utext/dk-help.dict");
|
||||
if (helpbase != NOMEM) {
|
||||
helpcatalog = new helptextdef[helpcount];
|
||||
for (unsigned int j = 0; j < helpcount; j++) {
|
||||
GetNextPair(&key, &value);
|
||||
helpcatalog[j].id = j;
|
||||
helpcatalog[j].symbol = helptexts[j].symbol;
|
||||
helpcatalog[j].text = value;
|
||||
}
|
||||
}
|
||||
|
||||
LoadCatalog(&identbase, "utext/dk-ident.dict");
|
||||
if (identbase != NOMEM) {
|
||||
identcatalog = new identhelpdef[identcount];
|
||||
for (unsigned int j = 0; j < identcount; j++) {
|
||||
GetNextPair(&key, &value);
|
||||
identcatalog[j].id = j;
|
||||
identcatalog[j].ident = key;
|
||||
identcatalog[j].text = value;
|
||||
}
|
||||
}
|
||||
|
||||
LoadCatalog(&kwordbase, "utext/dk-keyword.dict");
|
||||
if (kwordbase != NOMEM) {
|
||||
keywordsloc = new keyworddef[keywordcount];
|
||||
for (unsigned int j = 0; j < keywordcount; j++) {
|
||||
GetNextPair(&key, &value);
|
||||
keywordsloc[j].id = j;
|
||||
keywordsloc[j].name = value;
|
||||
keywordsloc[j].symbol = keywords[j].symbol;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StandardLanguage::LoadCatalog(char **dest, const char *file)
|
||||
{
|
||||
FilesystemBase *filesystem = CreateFilesystem();
|
||||
CharBuffer *cbuf = filesystem->LoadTextFile(file);
|
||||
|
||||
if (cbuf != NOMEM) {
|
||||
AllocAndCopy(dest, cbuf->GetString());
|
||||
ptr = *dest;
|
||||
delete cbuf;
|
||||
} else {
|
||||
*dest = NOMEM;
|
||||
}
|
||||
|
||||
delete filesystem;
|
||||
}
|
||||
|
||||
void StandardLanguage::GetNextPair(const char **key, const char **value)
|
||||
{
|
||||
SkipComments();
|
||||
*key = ptr;
|
||||
GetNextLine();
|
||||
SkipComments();
|
||||
*value = ptr;
|
||||
GetNextLine();
|
||||
}
|
||||
|
||||
void StandardLanguage::GetNextLine()
|
||||
{
|
||||
while ((*ptr) != '\0' && (*ptr) != '\n') {
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if ((*ptr) == '\n') {
|
||||
*ptr++ = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
void StandardLanguage::SkipComments()
|
||||
{
|
||||
bool skipping;
|
||||
do {
|
||||
if ((*ptr) == ';') {
|
||||
GetNextLine();
|
||||
skipping = true;
|
||||
} else if ((*ptr) == '#' && *(ptr + sizeof(char)) == '#') {
|
||||
GetNextLine();
|
||||
skipping = true;
|
||||
} else {
|
||||
skipping = false;
|
||||
}
|
||||
} while (skipping);
|
||||
}
|
||||
|
@ -36,9 +36,6 @@ class StandardLanguage : public Language {
|
||||
public:
|
||||
StandardLanguage();
|
||||
~StandardLanguage();
|
||||
char* GetText(int id);
|
||||
char* GetHelpText(char *ident);
|
||||
char* GetHelpText(Symbol symbol);
|
||||
char GetFractionPoint();
|
||||
bool CharIsAlNum(unsigned long character);
|
||||
bool CharIsAlpha(unsigned long character);
|
||||
@ -47,7 +44,28 @@ public:
|
||||
bool CharIsSpace(unsigned long character);
|
||||
bool CharIsCntrl(unsigned long character);
|
||||
bool StrIsEqualLoc(const char *s1, const char *s2);
|
||||
Symbol FindKeyword(const char *ident);
|
||||
|
||||
protected:
|
||||
char* Translate(textdef *def);
|
||||
char* Translate(helptextdef *def);
|
||||
char* Translate(identhelpdef *def);
|
||||
|
||||
private:
|
||||
void LoadCatalogs();
|
||||
void LoadCatalog(char **dest, const char *file);
|
||||
void GetNextPair(const char **key, const char **value);
|
||||
void GetNextLine();
|
||||
void SkipComments();
|
||||
|
||||
char *textbase;
|
||||
char *helpbase;
|
||||
char *identbase;
|
||||
char *kwordbase;
|
||||
|
||||
char *ptr;
|
||||
textdef *textcatalog;
|
||||
helptextdef *helpcatalog;
|
||||
identhelpdef *identcatalog;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user