diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 16 | ||||
-rw-r--r-- | src/config.c | 48 | ||||
-rw-r--r-- | src/enca.c | 148 | ||||
-rw-r--r-- | src/enca.h | 2 | ||||
-rw-r--r-- | src/engine.c | 14 | ||||
-rw-r--r-- | src/engine.h | 19 | ||||
-rw-r--r-- | src/fs.c | 47 | ||||
-rw-r--r-- | src/fs.h | 6 | ||||
-rw-r--r-- | src/internal.h | 42 | ||||
-rw-r--r-- | src/librcc.c | 98 | ||||
-rw-r--r-- | src/librcc.h | 266 | ||||
-rw-r--r-- | src/lng.c | 68 | ||||
-rw-r--r-- | src/lng.h | 10 | ||||
-rw-r--r-- | src/lngconfig.c | 126 | ||||
-rw-r--r-- | src/lngconfig.h | 16 | ||||
-rw-r--r-- | src/plugin.c | 5 | ||||
-rw-r--r-- | src/rcclocale.c | 82 | ||||
-rw-r--r-- | src/rcclocale.h | 7 | ||||
-rw-r--r-- | src/rccstring.c (renamed from src/string.c) | 14 | ||||
-rw-r--r-- | src/rccstring.h | 7 | ||||
-rw-r--r-- | src/recode.c | 80 | ||||
-rw-r--r-- | src/recode.h | 13 | ||||
-rw-r--r-- | src/xml.c | 6 | ||||
-rw-r--r-- | src/xml.h | 4 |
24 files changed, 687 insertions, 457 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index d555299..a0ad875 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,6 +1,20 @@ lib_LTLIBRARIES = librcc.la -librcc_la_SOURCES = librcc.c lng.c lngconfig.c recode.c config.c config.h plugin.c plugin.h enca.c enca.h engine.c engine.h +librcc_la_SOURCES = librcc.c \ + rcclocale.c rcclocale.h \ + lng.c lng.h \ + lngconfig.c lngconfig.h \ + config.c config.h \ + plugin.c plugin.h \ + enca.c enca.h \ + engine.c engine.h \ + rccstring.c rccstring.h \ + xml.c xml.h \ + fs.c fs.h \ + recode.c recode.h \ + internal.h include_HEADERS = librcc.h +INCLUDES = @XML_INCLUDES@ +librcc_la_LIBADD = @XML_LIBS@ librcc_la_LDFLAGS = -version-info @LIBRCC_VERSION_INFO@ diff --git a/src/config.c b/src/config.c index 885f416..89a8bde 100644 --- a/src/config.c +++ b/src/config.c @@ -1,6 +1,8 @@ #include <stdio.h> #include <librcd.h> +#include "config.h" + static rcc_charset_id rcc_autoengine_russian(rcc_engine_context ctx, char *buf, int len) { return (int)get_russian_charset(buf,len); } @@ -22,73 +24,73 @@ rcc_engine rcc_russian_engine = { }; rcc_language rcc_default_languages[] = { -{"default", "Autodetect", {"Default", NULL} { - &default_engine, +{"default", "Autodetect", {"Default", NULL}, { + &rcc_default_engine, NULL }}, -{"off", "Dissable", {"Default", NULL} { - &default_engine, +{"off", "Dissable", {"Default", NULL}, { + &rcc_default_engine, NULL }}, -{"ru","Russian",{"Default","KOI8-R","CP1251","UTF-8","IBM866","MACCYRILLIC","ISO8859-5", NULL},{ - &default_engine, - &russian_engine, +{"ru","Russian", {"Default","KOI8-R","CP1251","UTF-8","IBM866","MACCYRILLIC","ISO8859-5", NULL}, { + &rcc_russian_engine, + &rcc_default_engine, NULL }}, -{"uk","Ukrainian",{"Default","KOI8-U","CP1251","UTF-8","IBM855","MACCYRILLIC","ISO8859-5","CP1125", NULL},{ - &default_engine, - &russian_engine, +{"uk","Ukrainian", {"Default","KOI8-U","CP1251","UTF-8","IBM855","MACCYRILLIC","ISO8859-5","CP1125", NULL}, { + &rcc_russian_engine, + &rcc_default_engine, NULL }}, {"be","Belarussian",{"Default", "UTF-8", "CP1251", "IBM866", "ISO-8859-5", "KOI8-UNI", "maccyr" "IBM855", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"bg","Bulgarian",{"Default", "UTF-8", "CP1251", "ISO-8859-5", "IBM855", "maccyr", "ECMA-113", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"cz","Czech",{"Default", "UTF-8", "ISO-8859-2", "CP1250", "IBM852", "KEYBCS2", "macce", "KOI-8_CS_2", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"es","Estonian",{"Default", "UTF-8", "ISO-8859-4", "CP1257", "IBM775", "ISO-8859-13", "macce", "baltic", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"hr","Croatian",{"Default", "UTF-8", "CP1250", "ISO-8859-2", "IBM852", "macce", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"hu","Hungarian",{"Default", "UTF-8", "ISO-8859-2", "CP1250", "IBM852", "macce", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"lt","Lithuanian",{"Default", "UTF-8", "CP1257", "ISO-8859-4", "IBM775", "ISO-8859-13", "macce", "baltic", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"lv","Latvian",{"Default", "UTF-8", "CP1257", "ISO-8859-4", "IBM775", "ISO-8859-13", "macce", "baltic", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"pl","Polish",{"Default", "UTF-8", "ISO-8859-2", "CP1250", "IBM852", "macce", "ISO-8859-13", "ISO-8859-16", "baltic", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"sk","Slovak",{"Default", "UTF-8", "CP1250", "ISO-8859-2", "IBM852", "KEYBCS2", "macce", "KOI-8_CS_2", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"sl","Slovenian",{"Default", "UTF-8", "ISO-8859-2", "CP1250", "IBM852", "macce", "CORK", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, {"zh","Chinese",{"Default", "UTF-8", "GB2312", "GBK", "GB18030", "BIG5", NULL},{ - &default_engine, + &rcc_default_engine, NULL }}, -NULL +{NULL} }; /* @@ -1,23 +1,82 @@ #include <stdio.h> -#include "librcc.h" - +#include "internal.h" #include "plugin.h" +#include "engine.h" +#include "config.h" + #include "enca.h" +#ifdef RCC_ENCA_DYNAMIC +# include "fake_enca.h" +#endif /* RCC_ENCA_DYNAMIC */ + +static rcc_library_handle enca_handle = NULL; +static rcc_engine *enca_engines = NULL; + +rcc_engine_internal rccEncaInitContext(rcc_engine_context ctx) { +#ifdef RCC_ENCA_SUPPORT + EncaAnalyser enca; + + if ((!ctx)||(!ctx->language)) return NULL; + + enca = enca_analyser_alloc(ctx->language->sn); + if (!enca) return NULL; + + enca_set_threshold(enca, 1); + enca_set_multibyte(enca, 1); + enca_set_ambiguity(enca, 1); + enca_set_garbage_test(enca, 0); + enca_set_filtering(enca, 0); + enca_set_significant(enca,1); + enca_set_termination_strictness(enca,0); + + return (rcc_engine_internal)enca; +#else /* RCC_ENCA_SUPPORT */ + return NULL; +#endif /* RCC_ENCA_SUPPORT */ +} + +void rccEncaFreeContext(rcc_engine_context ctx) { + rcc_engine_internal internal; +#ifdef RCC_ENCA_SUPPORT + internal = rccEngineGetInternal(ctx); + if (internal) + enca_analyser_free(internal); +#endif /* RCC_ENCA_SUPPORT */ +} + +rcc_charset_id rccEnca(rcc_engine_context ctx, char *buf, int len) { +#ifdef RCC_ENCA_SUPPORT + rcc_engine_internal internal; + const char *charset; + EncaEncoding ee; + + internal = rccEngineGetInternal(ctx); + if ((!internal)||(!buf)) return -1; + + + len = STRLEN(buf, len); + + ee = enca_analyse_const((EncaAnalyser)ctx->internal,buf,len); + if (ee.charset<0) return -1; + + charset = enca_charset_name(ee.charset, ENCA_NAME_STYLE_ICONV); + return rccGetAutoCharsetByName(ctx->ctx, charset); +#else /* RCC_ENCA_SUPPORT */ + return -1; +#endif /* RCC_ENCA_SUPPORT */ +} rcc_engine rcc_enca_engine = { "Enca Library", &rccEncaInitContext, &rccEncaFreeContext, &rccEnca, {"UTF-8", NULL} }; -static rcc_library_handle enca_handle = NULL; -static rcc_engine enca_engines[sizeof(rcc_default_languages)/sizeof(rcc_language)]; - static int rccEncaLibraryLoad() { #ifdef RCC_ENCA_DYNAMIC if (enca_handle) return 0; - enca_handle = rccLibraryLoad(RCC_ENCA_LIB); + enca_handle = rccLibraryOpen(RCC_ENCA_LIB); if (!enca_handle) return -1; enca_set_multibyte=rccLibraryFind(enca_handle,"enca_set_multibyte"); @@ -59,34 +118,38 @@ static void rccEncaLibraryUnload() { #endif /* RCC_ENCA_DYNAMIC */ } - int rccEncaInit() { int err; unsigned int i,j,k,l; - rcc_engine_list engines; + rcc_engine **engines; int *charsets; - size_t ncharsets; + size_t n_charsets; #ifdef RCC_ENCA_SUPPORT + if (enca_engines) return -1; + for (i=0;rcc_default_languages[i].sn;i++); + enca_engines = (rcc_engine*)malloc(i*sizeof(rcc_engine)); + if (!enca_engines) return -1; + err = rccEncaLibraryLoad(); if (err) return err; - for (i=0;rcc_default_languages[i];i++) { + for (i=0;rcc_default_languages[i].sn;i++) { engines = rcc_default_languages[i].engines; for (j=0;engines[j];j++) if (j >= RCC_MAX_ENGINES) continue; - charsets = enca_get_language_charsets(rcc_default_languages[i].sn, &ncharsets); + charsets = enca_get_language_charsets(rcc_default_languages[i].sn, &n_charsets); if (charsets) { memcpy(enca_engines+i, &rcc_enca_engine, sizeof(rcc_engine)); for (k=0;enca_engines[i].charsets[k];k++); if (n_charsets+k>=RCC_MAX_CHARSETS) n_charsets = RCC_MAX_CHARSETS-k; for (l=0;l<n_charsets;l++) - enca_engines[j].charset[k++] = enca_charset_name(charsets[l], ENCA_NAME_STYLE_ICONV); - enca_engines[j].charset[k] = NULL; + enca_engines[j].charsets[k++] = enca_charset_name(charsets[l], ENCA_NAME_STYLE_ICONV); + enca_engines[j].charsets[k] = NULL; engines[j] = enca_engines + i; engines[j+1] = NULL; @@ -102,60 +165,9 @@ int rccEncaInit() { void rccEncaFree() { #ifdef RCC_ENCA_SUPPORT rccEncaLibraryUnload(); + if (enca_engines) { + free(enca_engines); + enca_engines = NULL; + } #endif /* RCC_ENCA_SUPPORT */ } - - -rcc_engine_internal rccEncaInitContext(rcc_engine_context *ctx) { -#ifdef RCC_ENCA_SUPPORT - EncaAnalyser enca; - - if ((!ctx)||(!ctx->language)) return NULL; - - enca = enca_analyser_alloc(ctx->lanuage->sn); - if (!enca) return NULL; - - enca_set_threshold(enca, 1); - enca_set_multibyte(enca, 1); - enca_set_ambiguity(enca, 1); - enca_set_garbage_test(enca, 0); - enca_set_filtering(enca, 0); - enca_set_significant(enca,1); - enca_set_termination_strictness(enca,0); - - return (rcc_engine_internal)enca; -#else /* RCC_ENCA_SUPPORT */ - return NULL; -#endif /* RCC_ENCA_SUPPORT */ -} - -void rccEncaFreeContext(rcc_engine_context ctx) { - rcc_engine_internal internal; -#ifdef RCC_ENCA_SUPPORT - internal = rccEngineGetInternal(ctx); - if (internal) - enca_analyser_free(internal); -#endif /* RCC_ENCA_SUPPORT */ -} - -rcc_charset_id rccEnca(rcc_engine_context ctx, char *buf, int len) { -#ifdef RCC_ENCA_SUPPORT - rcc_engine_internal internal; - const char *charset; - EncaEncoding ee; - - internal = rccEngineGetInternal(ctx); - if ((!internal)||(!buf)) return -1; - - - len = STRLEN(buf, len); - - ee = enca_analyse_const((EncaAnalyser)ctx->internal,buf,len); - if (ee.charset<0) return -1; - - charset = enca_charset_name(ee.charset, ENCA_NAME_STYLE_ICONV); - return rccGetAutoCharsetByName(ctx->ctx, charset); -#else /* RCC_ENCA_SUPPORT */ - return -1; -#endif /* RCC_ENCA_SUPPORT */ -} @@ -16,4 +16,4 @@ int rccEncaInit(); void rccEncaFree(); -#endif /* _RCC_ENCA_H */
\ No newline at end of file +#endif /* _RCC_ENCA_H */ diff --git a/src/engine.c b/src/engine.c index 646d46e..219a098 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,9 +1,9 @@ #include <stdio.h> #include <string.h> -#include "librcc.h" +#include "internal.h" -int rccEngineInit(rcc_engine_context *engine_ctx, rcc_context *ctx) { +int rccEngineInit(rcc_engine_context engine_ctx, rcc_context ctx) { if ((!ctx)||(!engine_ctx)) return -1; engine_ctx->ctx = ctx; @@ -12,7 +12,7 @@ int rccEngineInit(rcc_engine_context *engine_ctx, rcc_context *ctx) { return 0; } -void rccFreeEngine(rcc_engine_context *engine_ctx) { +void rccEngineFree(rcc_engine_context engine_ctx) { if (!engine_ctx) return; if (engine_ctx->free_func) { @@ -24,14 +24,14 @@ void rccFreeEngine(rcc_engine_context *engine_ctx) { engine_ctx->internal = NULL; } -int rccEngineConfigure(rcc_engine_context *ctx) { +int rccEngineConfigure(rcc_engine_context ctx) { rcc_language_id language_id; rcc_engine_id engine_id; rcc_engine *engine; if ((!ctx)||(!ctx->ctx)) return -1; - rccEngineFree(&ctx); + rccEngineFree(ctx); language_id = rccGetCurrentLanguage(ctx->ctx); if (language_id<0) return -1; @@ -43,13 +43,13 @@ int rccEngineConfigure(rcc_engine_context *ctx) { ctx->free_func = engine->free_func; ctx->func = engine->func; - ctx->language = ctx->languages[language_id]; + ctx->language = ctx->ctx->languages[language_id]; ctx->internal = engine->init_func(ctx); return 0; } -rcc_engine_internal rccEngineGetInternal(rcc_engine_context *ctx) { +rcc_engine_internal rccEngineGetInternal(rcc_engine_context ctx) { if (!ctx) return NULL; return ctx->internal; diff --git a/src/engine.h b/src/engine.h index 8f7400a..c0cb57d 100644 --- a/src/engine.h +++ b/src/engine.h @@ -1,11 +1,20 @@ #ifndef _RCC_ENGINE_H -#defien _RCC_ENGINE_H +#define _RCC_ENGINE_H -int rccEngineInit(rcc_engine_context *engine_ctx, rcc_context *ctx); -void rccFreeEngine(rcc_engine_context *engine_ctx); +struct rcc_engine_context_t { + rcc_context ctx; + rcc_language *language; + + rcc_engine_function func; + rcc_engine_free_function free_func; -int rccConfigure(rcc_engine_context *ctx); + rcc_engine_internal internal; +}; +typedef struct rcc_engine_context_t rcc_engine_context_s; -rcc_engine_internal rccEngineGetInternal(rcc_engine_context *ctx); +int rccEngineInit(rcc_engine_context engine_ctx, rcc_context ctx); +void rccEngineFree(rcc_engine_context engine_ctx); + +rcc_engine_internal rccEngineGetInternal(rcc_engine_context ctx); #endif /* _RCC_ENGINE_H */ @@ -1,11 +1,26 @@ #include <stdio.h> +#include <string.h> #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <mntent.h> -#include "librcc.h" +#include "internal.h" + +#ifndef strndup +static char *rccStrndup(const char *str, size_t n) { + char *res; + + n = STRNLEN(str, n); + res = (char*)malloc((n+1)*sizeof(char)); + if (!res) return res; + strncpy(res, str, n); + res[n] = 0; + return res; +} +#define strndup rccStrndup +#endif /* !strndup */ static char *rccCreateFullName(const char *path, const char *filename) { unsigned int i; @@ -18,7 +33,7 @@ static char *rccCreateFullName(const char *path, const char *filename) { i = strlen(path); - name = (char*)malloc(i+strlen(filename)+2)*sizeof(char)); + name = (char*)malloc((i+strlen(filename)+2)*sizeof(char)); if (!name) return NULL; if ((path[i-1]=='/')||(filename[0]=='/')) @@ -47,10 +62,11 @@ static char *rccCheckFile(const char *prefix, const char *name) { return NULL; } +/* Converts: 'filename' to 'prefix/name' using 'fspath' */ int rccFS0(const char *fspath, const char *filename, char **prefix, char **name) { FILE *mtab; struct mntent *fsentry; - char *tmp; + const char *tmp; if (fspath) { tmp = strstr(filename, fspath); @@ -83,7 +99,9 @@ int rccFS0(const char *fspath, const char *filename, char **prefix, char **name) return 0; } -int rccFS1(rcc_context *ctx, const char *fspath, char **prefix, char **name) { +/* Normalizes 'prefix/name' using 'fspath' */ +int rccFS1(rcc_context ctx, const char *fspath, char **prefix, char **name) { + int err; int prefix_size; char *result, *tmp; char *path, *filename; @@ -102,7 +120,7 @@ int rccFS1(rcc_context *ctx, const char *fspath, char **prefix, char **name) { // Checking without recoding in case of autodetection if (rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES)) { - if (rccIsFile(name)) { + if (rccIsFile(result)) { if ((path)&&(filename)) *name = result; else if (filename) *name = strdup(filename); else *name = strdup(path); @@ -110,17 +128,20 @@ int rccFS1(rcc_context *ctx, const char *fspath, char **prefix, char **name) { } } - err = rccFS0(fspath, result, &prefix, &name); + err = rccFS0(fspath, result, prefix, name); if ((path)&&(filename)) free(name); return err; } -char *rccFS2(rcc_context *ctx, iconv_t icnv, const char *prefix, const char *name) { +/* Checks if 'prefix/name' is accessible using 'icnv' recoding. In case of +sucess returns pointer on statically allocated memory, and NULL overwise */ +const char *rccFS2(rcc_context ctx, iconv_t icnv, const char *prefix, const char *name) { + int err; + if (icnv == (iconv_t)-1) return NULL; if (icnv == (iconv_t)-2) { strcpy(ctx->tmpbuffer, name); - ctx->tmpbuffer[len] = 0; } else { err = rccIConv(ctx, icnv, name, 0); if (err<=0) return NULL; @@ -129,7 +150,11 @@ char *rccFS2(rcc_context *ctx, iconv_t icnv, const char *prefix, const char *nam return rccCheckFile(prefix, ctx->tmpbuffer); } -char *rccFS3(rcc_context *ctx, rcc_language_id language_id, rcc_class_id class_id, const char *prefix, const char *name) { +/* Tries to find 'name' encoding in 'prefix/name' file. Returns pointer on +statically allocated string with correct filename or NULL. */ +const char *rccFS3(rcc_context ctx, rcc_language_id language_id, rcc_class_id class_id, const char *prefix, const char *name) { + unsigned int i; + const char *result; rcc_charset charset; rcc_language *language; iconv_t icnv = ctx->fsiconv; @@ -147,8 +172,8 @@ char *rccFS3(rcc_context *ctx, rcc_language_id language_id, rcc_class_id class_i } if (rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES)) { - language = ctx->language[language_id]; - if (language->charset[0]) { + language = ctx->languages[language_id]; + if (language->charsets[0]) { for (i=1;(!result);i++) { charset = language->charsets[i]; if (!charset) break; @@ -2,8 +2,8 @@ #define _RCC_FS_H int rccFS0(const char *fspath, const char *filename, char **prefix, char **name); -int rccFS1(rcc_context *ctx, const char *fspath, char **prefix, char **name); -char *rccFS2(rcc_context *ctx, iconv_t icnv, const char *prefix, const char *name); -char *rccFS3(rcc_context *ctx, rcc_language_id language_id, rcc_class_id class_id, const char *prefix, const char *name); +int rccFS1(rcc_context ctx, const char *fspath, char **prefix, char **name); +char *rccFS2(rcc_context ctx, iconv_t icnv, const char *prefix, const char *name); +char *rccFS3(rcc_context ctx, rcc_language_id language_id, rcc_class_id class_id, const char *prefix, const char *name); #endif /* _RCC_FS_H */ diff --git a/src/internal.h b/src/internal.h new file mode 100644 index 0000000..7d40547 --- /dev/null +++ b/src/internal.h @@ -0,0 +1,42 @@ +#ifndef _RCC_INTERNAL_H +#define _RCC_INTERNAL_H + +#include <iconv.h> +#include "librcc.h" +#include "recode.h" +#include "engine.h" +#include "lngconfig.h" + +struct rcc_context_t { + char locale_variable[RCC_MAX_VARIABLE_CHARS+1]; + + rcc_language_alias_list aliases; + + unsigned int max_languages; + unsigned int n_languages; + rcc_language_ptr *languages; + rcc_language_config configs; + + unsigned int max_classes; + unsigned int n_classes; + rcc_class_ptr *classes; + + rcc_engine_context_s engine_ctx; + + iconv_t *iconv_from; + iconv_t *iconv_to; + iconv_t iconv_auto[RCC_MAX_CHARSETS]; + + char tmpbuffer[RCC_MAX_STRING_CHARS+sizeof(rcc_string_header)+1]; + iconv_t fsiconv; + + unsigned char configure; + rcc_language_config current_config; + rcc_language_id current_language; +}; +typedef struct rcc_context_t rcc_context_s; + +int rccConfigure(rcc_context ctx); +char *rccCreateResult(rcc_context ctx, int len, int *rlen); + +#endif /* _RCC_INTERNAL_H */ diff --git a/src/librcc.c b/src/librcc.c index 6f621fc..e582c4c 100644 --- a/src/librcc.c +++ b/src/librcc.c @@ -2,10 +2,11 @@ #include <string.h> #include <librcd.h> -#include "librcc.h" -#include "enca.h" +#include "internal.h" #include "config.h" +#include "enca.h" + int rccInit() { return rccEncaInit(); @@ -16,24 +17,25 @@ void rccFree() { } rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, unsigned int max_classes, const char *locale) { + int err; unsigned int i; - rcc_context *ctx; + rcc_context ctx; rcc_language_ptr *languages; rcc_class_ptr *classes; - rcc_language_config *configs; + rcc_language_config configs; iconv_t *from, *to; if (!max_languages) max_languages = RCC_MAX_LANGUAGES; if (!max_classes) max_classes = RCC_MAX_CLASSES; - ctx = (rcc_context*)malloc(sizeof(rcc_context)); + ctx = (rcc_context)malloc(sizeof(struct rcc_context_t)); languages = (rcc_language_ptr*)malloc((max_languages+1)*sizeof(rcc_language_ptr)); classes = (rcc_class_ptr*)malloc((max_classes+1)*sizeof(rcc_class_ptr)); from = (iconv_t*)malloc((max_classes)*sizeof(iconv_t)); to = (iconv_t*)malloc((max_classes)*sizeof(iconv_t)); - configs = (rcc_language_config*)malloc((max_languages)*sizeof(rcc_language_config)); + configs = (rcc_language_config)malloc((max_languages)*sizeof(struct rcc_language_config_t)); if ((!ctx)||(!languages)||(!classes)) { if (from) free(from); @@ -45,6 +47,10 @@ rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, uns return NULL; } + ctx->aliases[0] = NULL; + for (i=0;rcc_default_aliases[i].alias;i++) + rccRegisterLanguageAlias(ctx, rcc_default_aliases + i); + ctx->languages = languages; ctx->max_languages = max_languages; ctx->n_languages = 0; @@ -90,17 +96,17 @@ rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, uns } if (flags&RCC_DEFAULT_CONFIGURATION) { - if (sizeof(languages)<sizeof(rcc_default_languages)) { + for (i=0;rcc_default_languages[i].sn;i++) + rccRegisterLanguage(ctx, rcc_default_languages+i); + + if (max_languages < i) { rccFree(ctx); return NULL; } - - for (i=0;rcc_default_languages[i];i++) - rccRegisterLanguage(ctx, rcc_default_language[i]); ctx->current_config = rccGetCurrentConfig(ctx); } else { - rccRegisterLanguage(ctx, rcc_default_language[0]); + rccRegisterLanguage(ctx, rcc_default_languages); ctx->current_config = NULL; } @@ -109,12 +115,12 @@ rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, uns return ctx; } -static void rccFreeIConv(rcc_context *ctx) { +static void rccFreeIConv(rcc_context ctx) { unsigned int i; if ((!ctx)||(!ctx->iconv_from)||(!ctx->iconv_to)) return; - if ((ctx->fsiconv_t != (iconv_t)-1)&&(ctx->fsiconv_t != (iconv_t)-2)) { + if ((ctx->fsiconv != (iconv_t)-1)&&(ctx->fsiconv != (iconv_t)-2)) { iconv_close(ctx->fsiconv); ctx->fsiconv = (iconv_t)-1; } @@ -137,7 +143,9 @@ static void rccFreeIConv(rcc_context *ctx) { } } -void rccFreeContext(rcc_context *ctx) { +void rccFreeContext(rcc_context ctx) { + unsigned int i; + if (ctx) { rccFreeEngine(&ctx->engine_ctx); rccFreeIConv(ctx); @@ -146,17 +154,16 @@ void rccFreeContext(rcc_context *ctx) { if (ctx->configs) { for (i=0;i<ctx->max_languages;i++) - rccFreeConfig(configs+i); + rccFreeConfig(ctx->configs+i); free(ctx->configs); } - if (ctx->charsets) free(ctx->charsets); if (ctx->classes) free(ctx->classes); if (ctx->languages) free(ctx->languages); free(ctx); } } -rcc_language_id rccRegisterLanguage(rcc_context *ctx, rcc_language *language) { +rcc_language_id rccRegisterLanguage(rcc_context ctx, rcc_language *language) { if ((!ctx)||(!language)) return -1; if (ctx->n_languages == ctx->max_languages) return -2; ctx->languages[ctx->n_languages++] = language; @@ -190,7 +197,21 @@ rcc_engine_id rccLanguageRegisterEngine(rcc_language *language, rcc_engine *engi return i-1; } -rcc_class_id rccRegisterClass(rcc_context *ctx, rcc_class *cl) { +rcc_alias_id rccRegisterLanguageAlias(rcc_context ctx, rcc_language_alias *alias) { + unsigned int i; + + if ((!ctx)||(!alias)) return -1; + + for (i=0;ctx->aliases[i];i++) + if (i>=RCC_MAX_ALIASES) return -2; + + ctx->aliases[i++] = alias; + ctx->aliases[i] = NULL; + + return i-1; +} + +rcc_class_id rccRegisterClass(rcc_context ctx, rcc_class *cl) { if ((!ctx)||(!cl)) return -1; if (ctx->n_classes == ctx->max_classes) return -2; ctx->configure = 1; @@ -200,31 +221,28 @@ rcc_class_id rccRegisterClass(rcc_context *ctx, rcc_class *cl) { } -rcc_class_type rccGetClassType(rcc_context *ctx, rcc_class_id class_id) { - rcc_class cl; - - if (!ctx)||(class_id<0)||(class_id>=ctx->n_classes)) return RCC_CLASS_INVALID; +rcc_class_type rccGetClassType(rcc_context ctx, rcc_class_id class_id) { + if ((!ctx)||(class_id<0)||(class_id>=ctx->n_classes)) return RCC_CLASS_INVALID; - cl = rcc->classes[class_id]; - return cl->class_type; + return ctx->classes[class_id]->class_type; } -static rcc_language *rccGetLanguageList(rcc_context *ctx) { +static rcc_language_ptr *rccGetLanguageList(rcc_context ctx) { if (!ctx) return NULL; return ctx->languages; } -static rcc_charset *rccGetCharsetList(rcc_context *ctx, rcc_language_id language_id) { +static rcc_charset *rccGetCharsetList(rcc_context ctx, rcc_language_id language_id) { if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; return ctx->languages[language_id]->charsets; } -static rcc_engine *rccGetEngineList(rcc_context *ctx, rcc_language_id language_id) { +static rcc_engine_ptr *rccGetEngineList(rcc_context ctx, rcc_language_id language_id) { if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; return ctx->languages[language_id]->engines; } -static rcc_charset *rccGetCurrentCharsetList(rcc_context *ctx) { +static rcc_charset *rccGetCurrentCharsetList(rcc_context ctx) { rcc_language_id language_id; if (!ctx) return NULL; @@ -235,7 +253,7 @@ static rcc_charset *rccGetCurrentCharsetList(rcc_context *ctx) { return rccGetCharsetList(ctx, language_id); } -static rcc_charset *rccGetCurrentEngineList(rcc_context *ctx) { +static rcc_engine_ptr *rccGetCurrentEngineList(rcc_context ctx) { rcc_language_id language_id; if (!ctx) return NULL; @@ -246,7 +264,7 @@ static rcc_charset *rccGetCurrentEngineList(rcc_context *ctx) { return rccGetEngineList(ctx, language_id); } -static rcc_charset *rccGetCurrentAutoCharsetList(rcc_context *ctx) { +static rcc_charset *rccGetCurrentAutoCharsetList(rcc_context ctx) { rcc_language_id language_id; rcc_engine_id engine_id; @@ -257,14 +275,14 @@ static rcc_charset *rccGetCurrentAutoCharsetList(rcc_context *ctx) { if ((language_id<0)||(engine_id<0)) return NULL; - return ctx->languages[language_id]->engine[engine_id]->charsets; + return ctx->languages[language_id]->engines[engine_id]->charsets; } -int rccConfigure(rcc_engine_context *ctx) { +int rccConfigure(rcc_context ctx) { unsigned int i; rcc_charset *charsets; - char *charset; + const char *charset; if (!ctx) return -1; if (!ctx->configure) return 0; @@ -273,11 +291,11 @@ int rccConfigure(rcc_engine_context *ctx) { for (i=0;i<ctx->n_classes;i++) { charset = rccGetCurrentCharsetName(ctx, i); if (strcmp(charset, "UTF-8")&&strcmp(charset, "UTF8")) { - iconv_from = iconv_open("UTF-8", charset); - iconv_to = iconv_open(charset, "UTF-8"); + ctx->iconv_from[i] = iconv_open("UTF-8", charset); + ctx->iconv_to[i] = iconv_open(charset, "UTF-8"); } else { - iconv_from = (iconv_t)-2; - iconv_to = (iconv_t)-2; + ctx->iconv_from[i] = (iconv_t)-2; + ctx->iconv_to[i] = (iconv_t)-2; } } @@ -285,9 +303,9 @@ int rccConfigure(rcc_engine_context *ctx) { for (i=0;charsets[i];i++) { charset = charsets[i]; if (strcmp(charset, "UTF-8")&&strcmp(charset, "UTF8")) - iconv_auto = iconv_open("UTF-8", charset); + ctx->iconv_auto[i] = iconv_open("UTF-8", charset); else - iconv_auto = (iconv_t)-2; + ctx->iconv_auto[i] = (iconv_t)-2; } rccEngineConfigure(&ctx->engine_ctx); @@ -295,7 +313,7 @@ int rccConfigure(rcc_engine_context *ctx) { return 0; } -char *rccCreateResult(rcc_context *ctx, int len, int *rlen) { +char *rccCreateResult(rcc_context ctx, int len, int *rlen) { char *res; if (!len) len = strlen(ctx->tmpbuffer); diff --git a/src/librcc.h b/src/librcc.h index c3d6f17..0ca55a4 100644 --- a/src/librcc.h +++ b/src/librcc.h @@ -1,11 +1,14 @@ #ifndef LIBRCC_H #define LIBRCC_H -#include <iconv.h> +/******************************************************************************* +***************************** Global Defines *********************************** +*******************************************************************************/ #define RCC_MAX_CHARSETS 16 #define RCC_MAX_ENGINES 5 #define RCC_MAX_LANGUAGES 64 +#define RCC_MAX_ALIASES 64 #define RCC_MAX_CLASSES 16 #define RCC_MAX_ERRORS 3 @@ -16,65 +19,45 @@ #define RCC_MAX_STRING_CHARS 1024 -#define RCC_STRING_MAGIC 0xFF7F01FF -/* - class = charset class - engine = auto engine - selected - which is selected - current - resolves default values -*/ +/* ID's */ +typedef char rcc_language_id; +typedef char rcc_alias_id; +typedef char rcc_charset_id; +typedef char rcc_engine_id; + +typedef int rcc_class_id; + +/* Opaque Pointer's */ +typedef struct rcc_context_t *rcc_context; +typedef struct rcc_engine_context_t *rcc_engine_context; +typedef struct rcc_language_config_t *rcc_language_config; #ifdef __cplusplus extern "C" { #endif +int rccInit(); +void rccFree(); + +/******************************************************************************* +**************************** Initialization ************************************ +*******************************************************************************/ typedef unsigned int rcc_init_flags; #define RCC_DEFAULT_CONFIGURATION 1 +rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, unsigned int max_classes, const char *locale); +void rccFreeContext(rcc_context ctx); -typedef int rcc_option_value; -typedef enum rcc_option_t { - RCC_LEARNING_MODE = 0, - RCC_AUTODETECT_FS_TITLES, - RCC_AUTODETECT_FS_NAMES, - RCC_USE_HEADERS, - RCC_MAX_OPTIONS -} rcc_option; - -typedef enum rcc_class_type_t { - RCC_CLASS_INVALID = 0, - RCC_CLASS_STANDARD, - RCC_CLASS_FS -} rcc_class_type; - - -struct rcc_string_header_t { - unsigned int magic; - rcc_language_id language_id; -}; -typedef struct rcc_string_header_t rcc_string_header; - -typedef char *rcc_string; -typedef char rcc_language_id; -typedef char rcc_charset_id; -typedef char rcc_engine_id; -typedef int rcc_class_id; - -typedef struct rcc_context_t rcc_context; -typedef struct rcc_engine_context_t rcc_engine_ctx; -typedef const struct rcc_class_t rcc_class; -typedef struct rcc_language_t rcc_language; -typedef struct rcc_engine_t rcc_engine; +/******************************************************************************* +******************* Altering Language Configuaration *************************** +*******************************************************************************/ typedef const char *rcc_charset; +typedef rcc_charset rcc_charset_list[RCC_MAX_CHARSETS+1]; -typedef struct rcc_language_config_t rcc_language_config; -typedef const struct rcc_language_alias_t rcc_language_alias; - +/* Engines */ typedef void *rcc_engine_internal; -typedef rcc_engine_internal (*rcc_engine_init_function)(rcc_engine_context *ctx); -typedef rcc_charset_id (*rcc_engine_function)(rcc_engine_context ctx, char *buf, int len); -typedef void (rcc_engine_free_function)(rcc_engine_context ctx); - -typedef rcc_charset rcc_charset_list[RCC_MAX_CHARSETS+1]; +typedef rcc_engine_internal (*rcc_engine_init_function)(rcc_engine_context ctx); +typedef rcc_charset_id (*rcc_engine_function)(rcc_engine_context ctx, const char *buf, int len); +typedef void (*rcc_engine_free_function)(rcc_engine_context ctx); struct rcc_engine_t { const char *title; @@ -83,22 +66,44 @@ struct rcc_engine_t { rcc_engine_function func; rcc_charset_list charsets; }; +typedef struct rcc_engine_t rcc_engine; typedef rcc_engine *rcc_engine_ptr; typedef rcc_engine_ptr rcc_engine_list[RCC_MAX_ENGINES+1]; +/* Language */ struct rcc_language_t { const char *sn; const char *name; rcc_charset_list charsets; rcc_engine_list engines; }; +typedef struct rcc_language_t rcc_language; typedef rcc_language *rcc_language_ptr; typedef rcc_language_ptr rcc_language_list[RCC_MAX_LANGUAGES+1]; +/* Alias */ struct rcc_language_alias_t { const char *alias; const char *lang; }; +typedef struct rcc_language_alias_t rcc_language_alias; +typedef rcc_language_alias *rcc_language_alias_ptr; +typedef rcc_language_alias_ptr rcc_language_alias_list[RCC_MAX_ALIASES+1]; + +rcc_language_id rccRegisterLanguage(rcc_context ctx, rcc_language *language); +rcc_charset_id rccLanguageRegisterCharset(rcc_language *language, rcc_charset charset); +rcc_engine_id rccLanguageRegisterEngine(rcc_language *language, rcc_engine *engine); +rcc_alias_id rccRegisterLanguageAlias(rcc_context ctx, rcc_language_alias *alias); + +/******************************************************************************* +************************ Altering Configuaration ******************************* +*******************************************************************************/ +typedef enum rcc_class_type_t { + RCC_CLASS_INVALID = 0, + RCC_CLASS_STANDARD, + RCC_CLASS_FS +} rcc_class_type; +typedef const struct rcc_class_t rcc_class; struct rcc_class_t { const char *name; @@ -108,94 +113,48 @@ struct rcc_class_t { typedef rcc_class *rcc_class_ptr; typedef rcc_class_ptr rcc_class_list[RCC_MAX_CLASSES+1]; -struct rcc_language_config_t { - rcc_context *ctx; - rcc_language *language; - - rcc_engine_id engine; - rcc_charset_id *charset; - rcc_option_value options[RCC_MAX_OPTIONS]; -}; - -struct rcc_engine_context_t { - rcc_context *ctx; - rcc_language *language; - - rcc_engine_function func; - rcc_engine_free_function free_func; - - rcc_engine_internal internal; -}; +rcc_class_id rccRegisterClass(rcc_context ctx, rcc_class *cl); +rcc_class_type rccGetClassType(rcc_context ctx, rcc_class_id class_id); -struct rcc_context_t { - char locale_variable[RCC_MAX_VARIABLE_CHARS+1]; - - unsigned int max_languages; - unsigned int n_languages; - rcc_language_ptr *languages; - rcc_language_config *configs; - - unsigned int max_classes; - unsigned int n_classes; - rcc_class_ptr *classes; - - rcc_engine_ctx engine_ctx; - - iconv_t *iconv_from; - iconv_t *iconv_to; - iconv_t iconv_auto[RCC_MAX_CHARSETS]; - - char tmpbuffer[RCC_MAX_STRING_CHARS+sizeof(rcc_string_footer)+1]; - iconv_t fsiconv; - - unsigned char configure; - rcc_language_config *current_config; - rcc_language_id current_language; -}; - -int rccInit(); -void rccFree(); - -rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, unsigned int max_classes, const char *locale); -void rccFreeContext(rcc_context *ctx); - -rcc_language_id rccRegisterLanguage(rcc_context *ctx, rcc_language *language); -rcc_charset_id rccLanguageRegisterCharset(rcc_language *language, rcc_charset charset); -rcc_engine_id rccLanguageRegisterEngine(rcc_language *language, rcc_engine *engine); -rcc_class_id rccRegisterClass(rcc_context *ctx, rcc_class *cl); - -rcc_class_type rccGetClassType(rcc_context *ctx, rcc_class_id class_id); - -int rccConfigure(rcc_context *ctx); -char *rccCreateResult(rcc_context *ctx, int len, int *rlen); +/******************************************************************************* +************************ Altering Configuaration ******************************* +*******************************************************************************/ +typedef int rcc_option_value; +typedef enum rcc_option_t { + RCC_LEARNING_MODE = 0, + RCC_AUTODETECT_FS_TITLES, + RCC_AUTODETECT_FS_NAMES, + RCC_USE_HEADERS, + RCC_MAX_OPTIONS +} rcc_option; /* lng.c */ -const char *rccGetLanguageName(rcc_context *ctx, rcc_language_id language_id); -rcc_language_id rccGetLanguageByName(rcc_context *ctx, const char *name); -rcc_language_id rccGetRealLanguage(rcc_context *ctx, rcc_language_id language_id); -const char *rccGetRealLanguageName(rcc_context *ctx, rcc_language_id language_id); -rcc_language_id rccGetSelectedLanguage(rcc_context *ctx); -const char *rccGetSelectedLanguageName(rcc_context *ctx); -rcc_language_id rccGetCurrentLanguage(rcc_context *ctx); -const char *rccGetCurrentLanguageName(rcc_context *ctx); - -int rccSetLanguage(rcc_context *ctx, rcc_language_id language_id); -int rccSetLanguageByName(rcc_context *ctx, const char *name); +const char *rccGetLanguageName(rcc_context ctx, rcc_language_id language_id); +rcc_language_id rccGetLanguageByName(rcc_context ctx, const char *name); +rcc_language_id rccGetRealLanguage(rcc_context ctx, rcc_language_id language_id); +const char *rccGetRealLanguageName(rcc_context ctx, rcc_language_id language_id); +rcc_language_id rccGetSelectedLanguage(rcc_context ctx); +const char *rccGetSelectedLanguageName(rcc_context ctx); +rcc_language_id rccGetCurrentLanguage(rcc_context ctx); +const char *rccGetCurrentLanguageName(rcc_context ctx); + +int rccSetLanguage(rcc_context ctx, rcc_language_id language_id); +int rccSetLanguageByName(rcc_context ctx, const char *name); /* lngconfig.c */ -int rccConfigInit(rcc_language_config *config, rcc_context *ctx); -int rccConfigFree(rcc_language_config *config); +int rccConfigInit(rcc_language_config config, rcc_context ctx); +int rccConfigFree(rcc_language_config config); const char *rccConfigGetEngineName(rcc_language_config config, rcc_engine_id engine_id); const char *rccConfigGetCharsetName(rcc_language_config config, rcc_charset_id charset_id); const char *rccConfigGetAutoCharsetName(rcc_language_config config, rcc_charset_id charset_id); -rcc_engine_id rccConfigGetEngineByName(rcc_language_config *config, const char *name); -rcc_charset_id rccConfigGetCharsetByName(rcc_language_config *config, const char *name); -rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config *config, const char *name); +rcc_engine_id rccConfigGetEngineByName(rcc_language_config config, const char *name); +rcc_charset_id rccConfigGetCharsetByName(rcc_language_config config, const char *name); +rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config config, const char *name); -rcc_language_config *rccGetConfig(rcc_context *ctx, rcc_language_id language_id); -rcc_language_config *rccGetConfigByName(rcc_context *ctx, const char *name); -rcc_language_config *rccGetCurrentConfig(rcc_context *ctx); +rcc_language_config rccGetConfig(rcc_context ctx, rcc_language_id language_id); +rcc_language_config rccGetConfigByName(rcc_context ctx, const char *name); +rcc_language_config rccGetCurrentConfig(rcc_context ctx); rcc_engine_id rccConfigGetSelectedEngine(rcc_language_config config); const char *rccConfigGetSelectedEngineName(rcc_language_config config); @@ -207,18 +166,18 @@ rcc_charset_id rccConfigGetCurrentCharset(rcc_language_config config, rcc_class_ const char *rccConfigGetCurrentCharsetName(rcc_language_config config, rcc_class_id class_id); rcc_option_value rccConfigGetOption(rcc_language_config config, rcc_option option); -int rccConfigSetEngine(rcc_language_config *config, rcc_engine_id engine_id); -int rccConfigSetCharset(rcc_language_config *config, rcc_class_id class_id, rcc_charset_id charset_id); -int rccConfigSetEngineByName(rcc_language_config *config, const char *name); -int rccConfigSetCharsetByName(rcc_language_config *config, rcc_class_id class_id, const char *name); -int rccConfigSetOption(rcc_language_config *config, rcc_option option, rcc_option_value value); +int rccConfigSetEngine(rcc_language_config config, rcc_engine_id engine_id); +int rccConfigSetCharset(rcc_language_config config, rcc_class_id class_id, rcc_charset_id charset_id); +int rccConfigSetEngineByName(rcc_language_config config, const char *name); +int rccConfigSetCharsetByName(rcc_language_config config, rcc_class_id class_id, const char *name); +int rccConfigSetOption(rcc_language_config config, rcc_option option, rcc_option_value value); -rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config *config, const char *locale_variable); +rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config config, const char *locale_variable); /* curconfig.c */ #define rccGetEngineName(ctx, engine_id) rccConfigGetEngineName(ctx->current_config, engine_id) #define rccGetCharsetName(ctx, charset_id) rccConfigGetCharsetName(ctx->current_config, charset_id) -#define rccGetAutoCharsetName(ctx, charset_id) rccConfGetAutoCharsetName(ctx->current_config, charset_id) +#define rccGetAutoCharsetName(ctx, charset_id) rccConfigGetAutoCharsetName(ctx->current_config, charset_id) #define rccGetEngineByName(ctx, name) rccConfigGetEngineByName(ctx->current_config, name) #define rccGetCharsetByName(ctx, name) rccConfigGetCharsetByName(ctx->current_config, name) #define rccGetAutoCharsetByName(ctx, name) rccConfigGetAutoCharsetByName(ctx->current_config, name) @@ -241,28 +200,39 @@ rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config *config, const char #define rccGetLocaleCharset(ctx, locale_variable) rccConfigGetLocaleCharset(ctx->current_config, locale_variable) -/* recode.c */ -char *rccFrom(rcc_context *ctx, rcc_class_id class_id, char *buf, int len, int *rlen); -char *rccTo(rcc_context *ctx, rcc_class_id class_id, char *buf, int len, int *rlen); -char *rccRecode(rcc_context *ctx, rcc_class_id from, rcc_class_id to, char *buf, int len, int *rlen); -char *rccFS(rcc_context *ctx, char *fspath, char *path, char *filename, int len, int *rlen); - +/******************************************************************************* +************************ RCC_STRING Manipulations ****************************** +*******************************************************************************/ /* string.c */ -rcc_string rccStringInit(rcc_language_id language_id, const char *buf, int len, int *rlen); -void rccStringFree(rcc_string str); +typedef char *rcc_string; rcc_language_id rccStringCheck(const rcc_string str); const char *rccStringGet(const rcc_string str); char *rccStringExtract(const rcc_string buf, int len, int *rlen); -char *rccStringCmp(const rcc_string str1, const rcc_string str2); -char *rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n); -char *rccStringCaseCmp(const rcc_string str1, const rcc_string str2); -char *rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n); +int rccStringCmp(const rcc_string str1, const rcc_string str2); +int rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n); +int rccStringCaseCmp(const rcc_string str1, const rcc_string str2); +int rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n); + + +/******************************************************************************* +******************************** Recoding ************************************** +*******************************************************************************/ + +/* recode.c */ +rcc_string rccFrom(rcc_context ctx, rcc_class_id class_id, const char *buf, int len, int *rlen); +char *rccTo(rcc_context ctx, rcc_class_id class_id, const rcc_string buf, int len, int *rlen); +char *rccRecode(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *buf, int len, int *rlen); +char *rccFS(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *fspath, const char *path, const char *filename); + +/******************************************************************************* +******************************** Options *************************************** +*******************************************************************************/ /* xml.c */ -int rccSave(rcc_context *ctx); -int rccLoad(rcc_context *ctx); +int rccSave(rcc_context ctx); +int rccLoad(rcc_context ctx); #ifdef __cplusplus } @@ -1,16 +1,23 @@ #include <stdio.h> #include <string.h> -#include <locale.h> -#include <librcd.h> -#include "librcc.h" +#include "internal.h" +#include "config.h" +#include "rcclocale.h" -const char *rccGetLanguageName(rcc_context *ctx, rcc_language_id language_id) { +rcc_language_ptr rccGetLanguagePointer(rcc_context ctx, rcc_language_id language_id) { if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; - return ctx->languages[language_id]->sn; + return ctx->languages[language_id]; } -language_id rccGetLanguageByName(rcc_context *ctx, const char *name) { +const char *rccGetLanguageName(rcc_context ctx, rcc_language_id language_id) { + rcc_language_ptr language; + language = rccGetLanguagePointer(ctx, language_id); + if (!language) return NULL; + return language->sn; +} + +rcc_language_id rccGetLanguageByName(rcc_context ctx, const char *name) { unsigned int i; if ((!ctx)||(!name)) return 0; @@ -20,41 +27,14 @@ language_id rccGetLanguageByName(rcc_context *ctx, const char *name) { return 0; } -static int rccGetLocaleLanguage(char *result, const char *lv, unsigned int n) { - charset_list_t *enc; - char *l; - - if (!lv) return -1; - - l = setlocale(lv, NULL); - if (!l) return -1; - else if ((strcmp(l,"C")==0)||(strcmp(l,"POSIX")==0)) return -1; - - for (i=0;((l[i])&&(l[i]!='.'));i++); - - for (i=0;rcc_default_aliases[i].alias;i++) - if (strncmp(l,rcc_default_aliases[i].alias,i)==0) { - l = rcc_default_aliases[i].alias; - break; - } - - for (i=0;((l[i])&&(l[i]!='.')&&(l[i]!='_'));i++); - if (i>=n) return -1; - - strncpy(result,l,i); - result[i]=0; - - return 0; -} - -static rcc_language_id rccGetDefaultLanguage(rc_context *ctx) { +static rcc_language_id rccGetDefaultLanguage(rcc_context ctx) { int err; unsigned int i; char stmp[RCC_MAX_LANGUAGE_CHARS+1]; if (!ctx) return -1; - err = rccGetLocaleLanguage(stmp, ctx->locale_variable, RCC_MAX_LANGUAGE_CHARS); + err = rccLocaleGetLanguage(stmp, ctx->locale_variable, RCC_MAX_LANGUAGE_CHARS); if (err) { if (ctx->n_languages>1) return 1; return -1; @@ -67,25 +47,25 @@ static rcc_language_id rccGetDefaultLanguage(rc_context *ctx) { return -1; } -rcc_language_id rccGetRealLanguage(rcc_context *ctx, rcc_language_id language_id) { +rcc_language_id rccGetRealLanguage(rcc_context ctx, rcc_language_id language_id) { if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return -1; if (language_id) return language_id; return rccGetDefaultLanguage(ctx); } -const char *rccGetRealLanguageName(rcc_context *ctx, rcc_language_id language_id) { +const char *rccGetRealLanguageName(rcc_context ctx, rcc_language_id language_id) { language_id = rccGetRealLanguage(ctx, language_id); if (language_id<0) return NULL; return rccGetLanguageName(ctx, language_id); } -rcc_language_id rccGetSelectedLanguage(rcc_context *ctx) { - if (!ctx) return NULL; +rcc_language_id rccGetSelectedLanguage(rcc_context ctx) { + if (!ctx) return -1; return ctx->current_language; } -const char *rccGetSelectedLanguageName(rcc_context *ctx) { +const char *rccGetSelectedLanguageName(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetSelectedLanguage(ctx); @@ -94,12 +74,12 @@ const char *rccGetSelectedLanguageName(rcc_context *ctx) { return rccGetLanguageName(ctx, language_id); } -rcc_language_id rccGetCurrentLanguage(rcc_context *ctx) { +rcc_language_id rccGetCurrentLanguage(rcc_context ctx) { if (!ctx) return -1; return rccGetRealLanguage(ctx, ctx->current_language); } -const char *rccGetCurrentLanguageName(rcc_context *ctx) { +const char *rccGetCurrentLanguageName(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetCurrentLanguage(ctx); @@ -109,7 +89,7 @@ const char *rccGetCurrentLanguageName(rcc_context *ctx) { } -int rccSetLanguage(rcc_context *ctx, rcc_language_id language_id) { +int rccSetLanguage(rcc_context ctx, rcc_language_id language_id) { rcc_language_config config; if ((!ctx)||(language_id < 0)||(language_id >= ctx->n_languages)) return -1; @@ -125,7 +105,7 @@ int rccSetLanguage(rcc_context *ctx, rcc_language_id language_id) { } } -int rccSetLanguageByName(rcc_context *ctx, const char *name) { +int rccSetLanguageByName(rcc_context ctx, const char *name) { rcc_language_id language_id; language_id = rccGetLanguageByName(ctx, name); diff --git a/src/lng.h b/src/lng.h new file mode 100644 index 0000000..87a9c91 --- /dev/null +++ b/src/lng.h @@ -0,0 +1,10 @@ +#ifndef _RCC_LNG_H +#define _RCC_LNG_H + +#include "lngconfig.h" + +rcc_language_ptr rccGetLanguagePointer(rcc_context ctx, rcc_language_id language_id); + +#define rccGetEnginePointer(ctx, engine_id) rccConfigGetEnginePointer(ctx->current_config, engine_id) + +#endif /* _RCC_LNG_H */ diff --git a/src/lngconfig.c b/src/lngconfig.c index ba904ec..32f454b 100644 --- a/src/lngconfig.c +++ b/src/lngconfig.c @@ -1,11 +1,12 @@ #include <stdio.h> #include <string.h> -#include <librcd.h> -#include "librcc.h" +#include "internal.h" #include "config.h" +#include "rcclocale.h" -const char *rccConfigGetEngineName(rcc_language_config config, rcc_engine_id engine_id) { +rcc_engine_ptr rccConfigGetEnginePointer(rcc_language_config config, rcc_engine_id engine_id) { + unsigned int i; rcc_engine_ptr *engines; if ((!config)||(!config->language)||(engine_id<0)) return NULL; @@ -15,11 +16,20 @@ const char *rccConfigGetEngineName(rcc_language_config config, rcc_engine_id eng for (i=0;engines[i];i++); if (engine_id>=i) return NULL; - return engines[engine_id]->title; + return engines[engine_id]; +} + +const char *rccConfigGetEngineName(rcc_language_config config, rcc_engine_id engine_id) { + rcc_engine_ptr engine; + + engine = rccConfigGetEnginePointer(config, engine_id); + if (!engine) return NULL; + return engine->title; } const char *rccConfigGetCharsetName(rcc_language_config config, rcc_charset_id charset_id) { - rcc_charset_ptr *charsets; + unsigned int i; + rcc_charset *charsets; if ((!config)||(!config->language)||(charset_id<0)) return NULL; @@ -32,10 +42,11 @@ const char *rccConfigGetCharsetName(rcc_language_config config, rcc_charset_id c } const char *rccConfigGetAutoCharsetName(rcc_language_config config, rcc_charset_id charset_id) { - rcc_charset_ptr *charsets; + unsigned int i; + rcc_charset *charsets; rcc_engine_ptr *engines; - if ((!config)||(!config->language)||(engine_id<0)) return NULL; + if ((!config)||(!config->language)||(charset_id<0)) return NULL; engines = config->language->engines; charsets = engines[config->engine]->charsets; @@ -47,9 +58,9 @@ const char *rccConfigGetAutoCharsetName(rcc_language_config config, rcc_charset_ } -rcc_engine_id rccConfigGetEngineByName(rcc_language_config *config, const char *name) { +rcc_engine_id rccConfigGetEngineByName(rcc_language_config config, const char *name) { unsigned int i; - rcc_engine *engines; + rcc_engine **engines; if ((!config)||(!config->language)||(!name)) return -1; @@ -60,7 +71,7 @@ rcc_engine_id rccConfigGetEngineByName(rcc_language_config *config, const char * return -1; } -rcc_charset_id rccConfigGetCharsetByName(rcc_language_config *config, const char *name) { +rcc_charset_id rccConfigGetCharsetByName(rcc_language_config config, const char *name) { unsigned int i; rcc_charset *charsets; @@ -73,7 +84,7 @@ rcc_charset_id rccConfigGetCharsetByName(rcc_language_config *config, const char return 0; } -rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config *config, const char *name) { +rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config config, const char *name) { unsigned int i; rcc_charset *charsets; rcc_engine_ptr *engines; @@ -89,7 +100,8 @@ rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config *config, const return -1; } -int rccConfigInit(rcc_language_config *config, rcc_context *ctx) { +int rccConfigInit(rcc_language_config config, rcc_context ctx) { + unsigned int i; rcc_charset_id *charsets; if ((!ctx)||(!config)) return -1; @@ -110,7 +122,7 @@ int rccConfigInit(rcc_language_config *config, rcc_context *ctx) { return 0; } -int rccConfigFree(rcc_language_config *config) { +int rccConfigFree(rcc_language_config config) { if (config->charset) { free(config->charset); config->charset = NULL; @@ -118,20 +130,20 @@ int rccConfigFree(rcc_language_config *config) { } -rcc_language_config *rccGetConfig(rcc_context *ctx, rcc_language_id language_id) { +rcc_language_config rccGetConfig(rcc_context ctx, rcc_language_id language_id) { int err; language_id = rccGetRealLanguage(ctx, language_id); if (language_id < 0) return NULL; - if (!ctx->configs[language_id].charsets) { + if (!ctx->configs[language_id].charset) { if (rccInitConfig(ctx->configs+language_id, ctx)) return NULL; } - ctx->configs[language_id] = ctx->languages[language_id]; + ctx->configs[language_id].language = ctx->languages[language_id]; return ctx->configs + language_id; } -rcc_language_config *rccGetConfigByName(rcc_context *ctx, const char *name) { +rcc_language_config rccGetConfigByName(rcc_context ctx, const char *name) { rcc_language_id language_id; language_id = rccGetLanguageByName(ctx, name); @@ -140,7 +152,7 @@ rcc_language_config *rccGetConfigByName(rcc_context *ctx, const char *name) { return rccGetConfig(ctx, language_id); } -rcc_language_config *rccGetCurrentConfig(rcc_context *ctx) { +rcc_language_config rccGetCurrentConfig(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetCurrentLanguage(ctx); @@ -167,13 +179,13 @@ const char *rccConfigGetSelectedEngineName(rcc_language_config config) { } rcc_engine_id rccConfigGetCurrentEngine(rcc_language_config config) { - rcc_engine_list enginelist; + rcc_engine **enginelist; rcc_engine_id engine_id; engine_id = rccConfigGetSelectedEngine(config); if (engine_id>=0) return engine_id; - if (!config->language) return NULL; + if (!config->language) return -1; else enginelist = config->language->engines; if (enginelist[0]) { @@ -192,31 +204,8 @@ const char *rccConfigGetCurrentEngineName(rcc_language_config config) { return rccConfigGetEngineName(config, engine_id); } - -static int rccGetLocaleCharset(char *result, const char *lv, unsigned int n) { - char *l; - - if (!lv) return -1; - l = setlocale(lv, NULL); - if (!l) return -1; - - for (i=0;((l[i])&&(l[i]!='.')&&(l[i]!='_'));i++); - if (i>=n) return -1; - - l = strrchr(l, '.'); - if (!l) return -1; - - for (i=0;((l[i])&&(l[i]!='@'));i++); - if (i>=n) return -1; - - strncpy(result,l,i); - result[i]=0; - - return 0; -} - rcc_charset_id rccConfigGetSelectedCharset(rcc_language_config config, rcc_class_id class_id) { - if ((!config)||(!config->ctx)||(class_id<0)||(class_id>=ctx->n_classes)) return -1; + if ((!config)||(!config->ctx)||(class_id<0)||(class_id>=config->ctx->n_classes)) return -1; return config->charset[class_id]; } @@ -233,18 +222,21 @@ const char *rccConfigGetSelectedCharsetName(rcc_language_config config, rcc_clas rcc_charset_id rccConfigGetCurrentCharset(rcc_language_config config, rcc_class_id class_id) { int err; unsigned int i; + rcc_language_id language_id; rcc_charset_id charset_id; rcc_language *language; - rcc_class *classes; + rcc_class_ptr *classes; rcc_charset *charsets; + rcc_class *cl; + char stmp[RCC_MAX_CHARSET_CHARS + 1]; - char *defvalue; + const char *defvalue; - if ((!config)||(!config->ctx)||(class_id<0)||(class_id>=ctx->n_classes)) return NULL; + if ((!config)||(!config->ctx)||(class_id<0)||(class_id>=config->ctx->n_classes)) return -1; - charset_id = ctx->config->charset[class_id]; + charset_id = config->charset[class_id]; if (charset_id) return charset_id; if (!config->language) return -1; @@ -260,15 +252,13 @@ rcc_charset_id rccConfigGetCurrentCharset(rcc_language_config config, rcc_class_ return rccConfigGetCurrentCharset(config, i); } } else defvalue = config->ctx->locale_variable; - - err = rccGetLocaleCharset(stmp, defvalue, RCC_MAX_CHARSET_CHARS); - if (err) { - charsets=ctx->languages[language_id]->charsets; - if ((charsets[0])&&(charsets[1])) return 1; - return -1; - } - return rccConfigGetCharsetByName(config, stmp); + charset_id = rccConfigGetLocaleCharset(config, defvalue); + if (charset_id > 0) return charset_id; + + charsets=config->ctx->languages[language_id]->charsets; + if ((charsets[0])&&(charsets[1])) return 1; + return -1; } const char *rccConfigGetCurrentCharsetName(rcc_language_config config, rcc_class_id class_id) { @@ -286,7 +276,7 @@ rcc_option_value rccConfigGetOption(rcc_language_config config, rcc_option optio return config->options[option]; } -int rccConfigSetEngine(rcc_language_config *config, rcc_engine_id engine_id) { +int rccConfigSetEngine(rcc_language_config config, rcc_engine_id engine_id) { unsigned int i; if ((!config)||(!config->language)||(engine_id < 0)) return -1; @@ -301,7 +291,7 @@ int rccConfigSetEngine(rcc_language_config *config, rcc_engine_id engine_id) { return 0; } -int rccConfigSetEngineByName(rcc_language_config *config, const char *name) { +int rccConfigSetEngineByName(rcc_language_config config, const char *name) { rcc_engine_id engine_id; engine_id = rccConfigGetEngineByName(config, name); @@ -310,7 +300,7 @@ int rccConfigSetEngineByName(rcc_language_config *config, const char *name) { return rccConfigSetEngine(config, engine_id); } -int rccConfigSetCharset(rcc_language_config *config, rcc_class_id class_id, rcc_charset_id charset_id) { +int rccConfigSetCharset(rcc_language_config config, rcc_class_id class_id, rcc_charset_id charset_id) { unsigned int i; if ((!config)||(!config->language)||(class_id < 0)||(class_id >= config->ctx->n_classes)||(charset_id<0)) return -1; @@ -326,7 +316,7 @@ int rccConfigSetCharset(rcc_language_config *config, rcc_class_id class_id, rcc_ return 0; } -int rccConfigSetCharsetByName(rcc_language_config *config, rcc_class_id class_id, const char *name) { +int rccConfigSetCharsetByName(rcc_language_config config, rcc_class_id class_id, const char *name) { rcc_charset_id charset_id; charset_id = rccConfigGetCharsetByName(config, name); @@ -335,7 +325,7 @@ int rccConfigSetCharsetByName(rcc_language_config *config, rcc_class_id class_id return rccConfigSetCharset(config, class_id, charset_id); } -int rccConfigSetOption(rcc_language_config *config, rcc_option option, rcc_option_value value) { +int rccConfigSetOption(rcc_language_config config, rcc_option option, rcc_option_value value) { if ((!config)||(option>=RCC_MAX_OPTIONS)) return -1; if (config->options[option] != value) { if (config->ctx->current_config == config) config->ctx->configure = 1; @@ -345,14 +335,22 @@ int rccConfigSetOption(rcc_language_config *config, rcc_option option, rcc_optio return 0; } -rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config *config, const char *locale_variable) { +rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config config, const char *locale_variable) { int err; rcc_charset *charsets; + rcc_language_id language_id; char stmp[RCC_MAX_CHARSET_CHARS+1]; if ((!config)||(!config->language)) return -1; - - err = rccGetLocaleCharset(stmp, locale_variable?locale_variable:config->ctx->locale_variable, RCC_MAX_CHARSET_CHARS); + + language_id = rccGetCurrentLanguage(config->ctx); + if (language_id) err = rccLocaleGetLanguage(stmp, locale_variable?locale_variable:config->ctx->locale_variable, RCC_MAX_CHARSET_CHARS); + + if ((language_id == 0)||((!err)&&(!strcmp(rccGetCurrentLanguageName(config->ctx), stmp)))) + err = rccLocaleGetCharset(stmp, locale_variable?locale_variable:config->ctx->locale_variable, RCC_MAX_CHARSET_CHARS); + else + err = 1; + if (err) { charsets=config->language->charsets; if ((charsets[0])&&(charsets[1])) return 1; diff --git a/src/lngconfig.h b/src/lngconfig.h new file mode 100644 index 0000000..ced4784 --- /dev/null +++ b/src/lngconfig.h @@ -0,0 +1,16 @@ +#ifndef _RCC_LNGCONFIG_H +#define _RCC_LNGCONFIG_H + +struct rcc_language_config_t { + rcc_context ctx; + rcc_language *language; + + rcc_engine_id engine; + rcc_charset_id *charset; + rcc_option_value options[RCC_MAX_OPTIONS]; +}; +typedef struct rcc_language_config_t rcc_language_config_s; + +rcc_engine_ptr rccConfigGetEnginePointer(rcc_language_config config, rcc_engine_id engine_id); + +#endif /* _RCC_LNGCONFIG_H */ diff --git a/src/plugin.c b/src/plugin.c index 53ff00c..a50bff3 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -1,3 +1,7 @@ +#include <stdio.h> + +#include "plugin.h" + #ifdef RCC_PLUGINS # include <dlfcn.h> # ifndef RTLD_NOW @@ -5,6 +9,7 @@ # endif #endif /* RCC_PLUGINS */ + rcc_library_handle rccLibraryOpen(char *filename) { #ifdef RCC_PLUGINS diff --git a/src/rcclocale.c b/src/rcclocale.c new file mode 100644 index 0000000..a16d51f --- /dev/null +++ b/src/rcclocale.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <string.h> +#include <locale.h> + +#include "config.h" + +static int rccLocaleGetClassByName(const char *locale) { + if (!strcmp(locale, "LC_CTYPE")) return LC_CTYPE; + if (!strcmp(locale, "LC_MESSAGES")) return LC_MESSAGES; + if (!strcmp(locale, "LC_COLLATE")) return LC_COLLATE; +/* + if (!strcmp(locale, "LC_ALL")) return LC_ALL; + if (!strcmp(locale, "LC_NUMERIC")) return LC_NUMERIC; + if (!strcmp(locale, "LC_MONETARY")) return LC_MONETARY; + if (!strcmp(locale, "LC_PAPER")) return LC_PAPER; + if (!strcmp(locale, "LC_NAME")) return LC_NAME; + if (!strcmp(locale, "LC_ADDRESS")) return LC_ADDRESS; + if (!strcmp(locale, "LC_TELEPHONE")) return LC_TELEPHONE; + if (!strcmp(locale, "LC_MEASURMENT")) return LC_MEASURMENT; + if (!strcmp(locale, "LC_IDENTIFICATION")) return LC_IDENTIFICATION; +*/ + return -1; +} + +static int rccLocaleGetLanguage(char *result, const char *lv, unsigned int n) { + unsigned int i; + int locale_class; + const char *l; + + if (!lv) return -1; + + locale_class = rccLocaleGetClassByName(lv); + if (locale_class >= 0) { + l = setlocale(locale_class, NULL); + if (!l) return -1; + else if ((strcmp(l,"C")==0)||(strcmp(l,"POSIX")==0)) return -1; + } return -1; + + for (i=0;((l[i])&&(l[i]!='.'));i++); + + for (i=0;rcc_default_aliases[i].alias;i++) + if (strncmp(l,rcc_default_aliases[i].alias,i)==0) { + l = rcc_default_aliases[i].alias; + break; + } + + for (i=0;((l[i])&&(l[i]!='.')&&(l[i]!='_'));i++); + if (i>=n) return -1; + + strncpy(result,l,i); + result[i]=0; + + return 0; +} + +int rccLocaleGetCharset(char *result, const char *lv, unsigned int n) { + unsigned int i; + int locale_class; + char *l; + + if (!lv) return -1; + + locale_class = rccLocaleGetClassByName(lv); + if (locale_class >= 0) { + l = setlocale(locale_class, NULL); + if (!l) return -1; + } else return -1; + + for (i=0;((l[i])&&(l[i]!='.')&&(l[i]!='_'));i++); + if (i>=n) return -1; + + l = strrchr(l, '.'); + if (!l) return -1; + + for (i=0;((l[i])&&(l[i]!='@'));i++); + if (i>=n) return -1; + + strncpy(result,l,i); + result[i]=0; + + return 0; +} diff --git a/src/rcclocale.h b/src/rcclocale.h new file mode 100644 index 0000000..fbbfc24 --- /dev/null +++ b/src/rcclocale.h @@ -0,0 +1,7 @@ +#ifndef _RCC_LOCALE_H +#define _RCC_LOCALE_H + +int rccLocaleGetLanguage(char *result, const char *lv, unsigned int n); +int rccLocaleGetCharset(char *result, const char *lv, unsigned int n); + +#endif /* _RCC_LOCALE_H */ diff --git a/src/string.c b/src/rccstring.c index 85a5767..e2db6be 100644 --- a/src/string.c +++ b/src/rccstring.c @@ -1,7 +1,11 @@ #include <stdio.h> #include <string.h> -rcc_string rccStringInit(rcc_language_id language_id, const char *buf, int len, int *rlen) { +#include "internal.h" +#include "rccstring.h" + +rcc_string rccCreateString(rcc_language_id language_id, const char *buf, int len, int *rlen) { + char *res; rcc_string_header header = {RCC_STRING_MAGIC, language_id}; len = STRNLEN(buf, len); @@ -56,19 +60,19 @@ char *rccStringExtract(const rcc_string buf, int len, int *rlen) { return res; } -char *rccStringCmp(const rcc_string str1, const rcc_string str2) { +int rccStringCmp(const rcc_string str1, const rcc_string str2) { return strcmp(rccStringGet(str1), rccStringGet(str2)); } -char *rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n) { +int rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n) { return strncmp(rccStringGet(str1), rccStringGet(str2), n); } -char *rccStringCaseCmp(const rcc_string str1, const rcc_string str2) { +int rccStringCaseCmp(const rcc_string str1, const rcc_string str2) { return strcasecmp(rccStringGet(str1), rccStringGet(str2)); } -char *rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n) { +int rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n) { return strncasecmp(rccStringGet(str1), rccStringGet(str2), n); } diff --git a/src/rccstring.h b/src/rccstring.h new file mode 100644 index 0000000..eceab96 --- /dev/null +++ b/src/rccstring.h @@ -0,0 +1,7 @@ +#ifndef _RCC_STRING_H +#define _RCC_STRING_H + +rcc_string rccCreateString(rcc_language_id language_id, const char *buf, int len, int *rlen); +void rccStringFree(rcc_string str); + +#endif /* _RCC_STRING_H */ diff --git a/src/recode.c b/src/recode.c index 6d82daa..0f6b865 100644 --- a/src/recode.c +++ b/src/recode.c @@ -2,11 +2,13 @@ #include <stdlib.h> #include <string.h> #include <locale.h> +#include <errno.h> +#include <iconv.h> -#include <librcd.h> -#include "librcc.h" - +#include "internal.h" #include "fs.h" +#include "lng.h" +#include "rccstring.h" #include "config.h" @@ -31,7 +33,7 @@ static int rccIConvUTFBytes(unsigned char c) { return 6-j; } -static int rccIConv(rcc_context *ctx, iconv_t icnv, char *buf, int len) { +static int rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, int len) { char *in_buf, *out_buf, *res, err; int in_left, out_left, olen; int ub, utf_mode=0; @@ -45,7 +47,7 @@ static int rccIConv(rcc_context *ctx, iconv_t icnv, char *buf, int len) { loop_restart: errors = 0; - in_buf = buf; + in_buf = (char*)buf; /*DS*/ in_left = len; out_buf = ctx->tmpbuffer; out_left = RCC_MAX_STRING_CHARS; @@ -54,9 +56,9 @@ loop: err=iconv(icnv, &in_buf, &in_left, &out_buf, &out_left); if (err<0) { if (errno==E2BIG) { - *(int*)(ctx->tmpbuffer+(CHARSET_MAX_STRING_SIZE-sizeof(int)))=0; + *(int*)(ctx->tmpbuffer+(RCC_MAX_STRING_CHARS-sizeof(int)))=0; } else if (errno==EILSEQ) { - if (errors++<CHARSET_MAX_ERRORS) { + if (errors++<RCC_MAX_ERRORS) { for (ub=utf_mode?rccIConvUTFBytes(*in_buf):1;ub>0;ub--) rccIConvCopySymbol(&in_buf, &in_left, &out_buf, &out_left); if (in_left>0) goto loop; @@ -71,43 +73,42 @@ loop: } } - return CHARSET_MAX_STRING_SIZE - out_left; + return RCC_MAX_STRING_CHARS - out_left; } -static charset_id rccIConvAuto(rcc_context *ctx, rcc_class_id class_id, char *buf, int len) { +static rcc_charset_id rccIConvAuto(rcc_context ctx, rcc_class_id class_id, const char *buf, int len) { rcc_class_type class_type; + rcc_engine_ptr engine; if ((!ctx)||(!buf)) return -1; class_type = rccGetClassType(ctx, class_id); if ((class_type == RCC_CLASS_STANDARD)||((class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_TITLES)))) { - engine = rccGetCurrentEngine(ctx); + engine = rccGetEnginePointer(ctx, rccGetCurrentEngine(ctx)); if ((!engine)||(!engine->func)||(!stricmp(engine->title, "off"))||(!strcmp(engine->title, "dissable"))) return -1; - - return engine->func(buf, len); + return engine->func(&ctx->engine_ctx, buf, len); } return -1; } -rcc_string rccFrom(rcc_context *ctx, rcc_class_id class_id, const char *buf, int len, int *rlen) { +rcc_string rccFrom(rcc_context ctx, rcc_class_id class_id, const char *buf, int len, int *rlen) { int err; rcc_language_id language_id; rcc_charset_id charset_id; iconv_t icnv = (iconv_t)-1; - char *result; + rcc_string result; if ((!ctx)||(class_id<0)||(class_id>=ctx->n_classes)||(!buf)) return NULL; err = rccConfigure(ctx); if (err) return NULL; - language_id = rccGetCurrentLanguage(ctx); // DS: Learning. check database (language_id) - charset_id = rccIConvAuto(ctx, buf, len); + charset_id = rccIConvAuto(ctx, class_id, buf, len); if (charset_id > 0) icnv = ctx->iconv_auto[charset_id]; if (icnv == (iconv_t)-1) { icnv = ctx->iconv_from[class_id]; @@ -127,12 +128,13 @@ rcc_string rccFrom(rcc_context *ctx, rcc_class_id class_id, const char *buf, int return result; } -char *rccTo(rcc_context *ctx, rcc_class_id class_id, const rcc_string buf, int len, int *rlen) { +char *rccTo(rcc_context ctx, rcc_class_id class_id, const rcc_string buf, int len, int *rlen) { int err; char *result; char *prefix, *name; rcc_language_id language_id; rcc_charset_id charset_id; + rcc_class_type class_type; iconv_t icnv; if ((!ctx)||(class_id<0)||(class_id>=ctx->n_classes)||(!buf)) return NULL; @@ -145,45 +147,51 @@ char *rccTo(rcc_context *ctx, rcc_class_id class_id, const rcc_string buf, int l icnv = ctx->iconv_to[class_id]; + class_type = rccGetClassType(ctx, class_id); if ((class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES))) { - // DS: file_names (autodetect fspath) + // DS: file_names (aut odetect fspath) prefix = NULL; name = buf + sizeof(rcc_string_header); err = rccFS0(NULL, buf, &prefix, &name); if (!err) { - result = rccFS3(ctx, language_id, class_id, prefix, name, 0, rlen); + result = rccFS3(ctx, language_id, class_id, prefix, name); + if ((rlen)&&(result)) *rlen = strlen(result); return result; } } if (icnv == (iconv_t)-1) return NULL; if (icnv == (iconv_t)-2) { - result = rccParseString(ctx, buf, len, rlen); + result = rccStringExtract(buf, len, rlen); } else { err = rccIConv(ctx, icnv, buf + sizeof(rcc_string_header), len?len-sizeof(rcc_string_header):0); if (err<=0) return NULL; - result = rccCreateAnswer(ctx, err, rlen); + result = rccCreateResult(ctx, err, rlen); } return result; } -char *rccRecode(rcc_context *ctx, rcc_class_id from, rcc_class_id to, const char *buf, int len, int *rlen) { +char *rccRecode(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *buf, int len, int *rlen) { int nlen; rcc_string stmp; - charset_id from_charset_id, to_charset_id; + char *result; + const char *from_charset, *to_charset; + rcc_charset_id from_charset_id, to_charset_id; + rcc_class_type class_type; if ((!ctx)||(from<0)||(from>=ctx->n_classes)||(to<0)||(to>=ctx->n_classes)||(!buf)) return NULL; + class_type = rccGetClassType(ctx, to); if ((class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES))) goto recoding; - from_charset_id = rccIConvAuto(ctx, buf, len); + from_charset_id = rccIConvAuto(ctx, from, buf, len); if (from_charset_id>0) { from_charset = rccGetAutoCharsetName(ctx, from_charset_id); to_charset = rccGetCurrentCharsetName(ctx, to); if ((from_charset)&&(to_charset)&&(!stricmp(from_charset, to_charset))) return NULL; } else { - from_charset_id = rccGetCurrentCharset(ctx, from) + from_charset_id = rccGetCurrentCharset(ctx, from); to_charset_id = rccGetCurrentCharset(ctx, to); if (from_charset_id == to_charset_id) return NULL; } @@ -191,21 +199,23 @@ char *rccRecode(rcc_context *ctx, rcc_class_id from, rcc_class_id to, const char recoding: stmp = rccFrom(ctx, from, buf, len, &nlen); if (stmp) { - buf = rccTo(ctx, to, stmp, nlen, rlen); + result = rccTo(ctx, to, stmp, nlen, rlen); free(stmp); - return buf; + return result; } - return rccTo(ctx, to, buf, len, rlen); + /*return rccTo(ctx, to, buf, len, rlen);*/ + return NULL; } -char *rccFS(rcc_context *ctx, rcc_class_id from, rcc_class_id to, const char *fspath, const char *path, const char *filename) { +char *rccFS(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *fspath, const char *path, const char *filename) { int err; - char *prefix = path, *name = filename; + rcc_language_id language_id; + char *prefix = (char*)path, *name = (char*)filename; /*DS*/ rcc_string string; - + char *stmp; - char *result_fn = NULL; + char *result = NULL; err = rccFS1(ctx, fspath, &prefix, &name); @@ -214,12 +224,12 @@ char *rccFS(rcc_context *ctx, rcc_class_id from, rcc_class_id to, const char *fs return name; } - string = rccFrom(ctx, from, name, len, rlen); + string = rccFrom(ctx, from, name, 0, NULL); if (string) { language_id = rccGetCurrentLanguage(ctx); - result = rccFS3(ctx, language_id, to, prefix, string + sizeof(rcc_string_header), 0, NULL); + result = rccFS3(ctx, language_id, to, prefix, string + sizeof(rcc_string_header)); free(string); - } else result = NULL; + } free(prefix); free(name); diff --git a/src/recode.h b/src/recode.h new file mode 100644 index 0000000..a70f354 --- /dev/null +++ b/src/recode.h @@ -0,0 +1,13 @@ +#ifndef _RCC_RECODE_H +#define _RCC_RECODE_H + +#define RCC_STRING_MAGIC 0xFF7F01FF + +struct rcc_string_header_t { + unsigned int magic; + rcc_language_id language_id; +}; +typedef struct rcc_string_header_t rcc_string_header; + + +#endif /* _RCC_RECODE_H */ @@ -1,8 +1,10 @@ +#include <stdio.h> +#include "internal.h" -int rccSave(rcc_context *ctx) { +int rccSave(rcc_context ctx) { return 0; } -int rccLoad(rcc_context *ctx) { +int rccLoad(rcc_context ctx) { return 0; } diff --git a/src/xml.h b/src/xml.h new file mode 100644 index 0000000..3286a8e --- /dev/null +++ b/src/xml.h @@ -0,0 +1,4 @@ +#ifndef _RCC_XML_H +#define _RCC_XML_H + +#endif /* _RCC_XML_H */ |