diff options
-rw-r--r-- | src/curconfig.c | 101 | ||||
-rw-r--r-- | src/fs.c | 75 | ||||
-rw-r--r-- | src/fs.h | 2 | ||||
-rw-r--r-- | src/internal.h | 8 | ||||
-rw-r--r-- | src/librcc.c | 54 | ||||
-rw-r--r-- | src/librcc.h | 29 | ||||
-rw-r--r-- | src/lng.c | 62 | ||||
-rw-r--r-- | src/lngconfig.c | 58 | ||||
-rw-r--r-- | src/opt.c | 25 | ||||
-rw-r--r-- | src/rcciconv.c | 10 | ||||
-rw-r--r-- | src/rcciconv.h | 2 | ||||
-rw-r--r-- | src/rcclist.c | 48 | ||||
-rw-r--r-- | src/rccstring.c | 88 | ||||
-rw-r--r-- | src/rccstring.h | 18 | ||||
-rw-r--r-- | src/rccxml.c | 10 | ||||
-rw-r--r-- | src/recode.c | 102 | ||||
-rw-r--r-- | src/recode.h | 8 |
17 files changed, 499 insertions, 201 deletions
diff --git a/src/curconfig.c b/src/curconfig.c index 4a26a33..49cddee 100644 --- a/src/curconfig.c +++ b/src/curconfig.c @@ -2,94 +2,161 @@ #include "internal.h" const char *rccGetEngineName(rcc_context ctx, rcc_engine_id engine_id) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return rccConfigGetEngineName(ctx->current_config, engine_id); } const char *rccGetCharsetName(rcc_context ctx, rcc_charset_id charset_id) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return rccConfigGetCharsetName(ctx->current_config, charset_id); } const char *rccGetAutoCharsetName(rcc_context ctx, rcc_charset_id charset_id) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return rccConfigGetAutoCharsetName(ctx->current_config, charset_id); } rcc_engine_id rccGetEngineByName(rcc_context ctx, const char *name) { - if (!ctx) return (rcc_engine_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_engine_id)-1; + } + return rccConfigGetEngineByName(ctx->current_config, name); } rcc_charset_id rccGetCharsetByName(rcc_context ctx, const char *name) { - if (!ctx) return (rcc_charset_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_charset_id)-1; + } + return rccConfigGetCharsetByName(ctx->current_config, name); } rcc_charset_id rccGetAutoCharsetByName(rcc_context ctx, const char *name) { - if (!ctx) return (rcc_charset_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_charset_id)-1; + } + return rccConfigGetAutoCharsetByName(ctx->current_config, name); } rcc_engine_id rccGetSelectedEngine(rcc_context ctx) { - if (!ctx) return (rcc_engine_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_engine_id)-1; + } + return rccConfigGetSelectedEngine(ctx->current_config); } const char *rccGetSelectedEngineName(rcc_context ctx) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return rccConfigGetSelectedEngineName(ctx->current_config); } rcc_engine_id rccGetCurrentEngine(rcc_context ctx) { - if (!ctx) return (rcc_engine_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_engine_id)-1; + } return rccConfigGetCurrentEngine(ctx->current_config); } const char *rccGetCurrentEngineName(rcc_context ctx) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } return rccConfigGetCurrentEngineName(ctx->current_config); } rcc_charset_id rccGetSelectedCharset(rcc_context ctx, rcc_class_id class_id) { + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_charset_id)-1; + } return rccConfigGetSelectedCharset(ctx->current_config, class_id); } const char *rccGetSelectedCharsetName(rcc_context ctx, rcc_class_id class_id) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } return rccConfigGetSelectedCharsetName(ctx->current_config, class_id); } rcc_charset_id rccGetCurrentCharset(rcc_context ctx, rcc_class_id class_id) { + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_charset_id)-1; + } return rccConfigGetCurrentCharset(ctx->current_config, class_id); } const char *rccGetCurrentCharsetName(rcc_context ctx, rcc_class_id class_id) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } return rccConfigGetCurrentCharsetName(ctx->current_config, class_id); } int rccSetEngine(rcc_context ctx, rcc_engine_id engine_id) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } return rccConfigSetEngine(ctx->current_config, engine_id); } int rccSetCharset(rcc_context ctx, rcc_class_id class_id, rcc_charset_id charset_id) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } return rccConfigSetCharset(ctx->current_config, class_id, charset_id); } int rccSetEngineByName(rcc_context ctx, const char *name) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } return rccConfigSetEngineByName(ctx->current_config, name); } int rccSetCharsetByName(rcc_context ctx, rcc_class_id class_id, const char *name) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } return rccConfigSetCharsetByName(ctx->current_config, class_id, name); } rcc_charset_id rccGetLocaleCharset(rcc_context ctx, const char *locale_variable) { - if (!ctx) return (rcc_charset_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } return rccConfigGetLocaleCharset(ctx->current_config, locale_variable); } @@ -64,29 +64,45 @@ static char *rccCheckFile(const char *prefix, const char *name) { } /* Converts: 'filename' to 'prefix/name' using 'fspath' */ -int rccFS0(const char *fspath, const char *filename, char **prefix, char **name) { +int rccFS0(rcc_context ctx, const char *fspath, const char *filename, char **prefix, char **name) { FILE *mtab; struct mntent *fsentry; - const char *tmp; + const char *tmp = NULL; + size_t len; if (fspath) { - tmp = strstr(filename, fspath); - if (tmp) tmp = filename + strlen(fspath); + len = strlen(fspath); + if (!len) return 1; + + if (!strncmp(filename, fspath, len)) tmp = filename + strlen(fspath); } else { - mtab = setmntent(_PATH_MNTTAB, "r"); + /* only required with non-english mount directories */ + len = strlen(ctx->lastprefix); + if ((len)&&(!strncmp(filename, ctx->lastprefix, len))) { + tmp = filename + len; + } + + if (tmp) mtab = NULL; + else mtab = setmntent(_PATH_MNTTAB, "r"); if (mtab) { while (!feof(mtab)) { fsentry = getmntent(mtab); if ((fsentry)&&(fsentry->mnt_dir)) { - tmp = strstr(filename, fsentry->mnt_dir); - if (tmp) tmp = filename + strlen(fsentry->mnt_dir); + len = strlen(fsentry->mnt_dir); + if (len) { + if (!strncmp(filename, fsentry->mnt_dir, len)) { + tmp = filename + len; + if (len<RCC_MAX_PREFIX_CHARS) strcpy(ctx->lastprefix, fsentry->mnt_dir); + break; + } + } } } endmntent(mtab); } } - if (!tmp) tmp = filename; + if (!tmp) return 1; *name = strdup(tmp); *prefix = strndup(filename, (tmp-filename)); @@ -100,7 +116,13 @@ int rccFS0(const char *fspath, const char *filename, char **prefix, char **name) return 0; } -/* Normalizes 'prefix/name' using 'fspath' */ +/* Normalizes 'prefix/name' using 'fspath' +returns: + -1 Error + 0 Okey + bit 1 Exact Match + bit 2 Memory cleanup isn't required +*/ int rccFS1(rcc_context ctx, const char *fspath, char **prefix, char **name) { int err; int prefix_size; @@ -122,17 +144,27 @@ 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(result)) { - if ((path)&&(filename)) *name = result; - else if (filename) *name = strdup(filename); - else *name = strdup(path); - return 1; + *prefix = NULL; + *name = result; + + if ((path)&&(filename)) return 1; + return 3; } } + + puts("--"); + if (rccFS0(ctx, fspath, result, prefix, name)) { + *prefix = NULL; + *name = result; + + if ((path)&&(filename)) return 0; + return 2; + } + puts("++"); - err = rccFS0(fspath, result, prefix, name); - if ((path)&&(filename)) free(name); + if ((path)&&(filename)) free(result); - return err; + return 0; } /* Checks if 'prefix/name' is accessible using 'icnv' recoding. In case of @@ -142,12 +174,15 @@ const char *rccFS2(rcc_context ctx, iconv_t icnv, const char *prefix, const char if (icnv == (iconv_t)-1) return NULL; if (icnv == (iconv_t)-2) { - strcpy(ctx->tmpbuffer, name); + puts("-1"); + strncpy(ctx->tmpbuffer, name, RCC_MAX_STRING_CHARS); + ctx->tmpbuffer[RCC_MAX_STRING_CHARS] = 0; } else { + puts("-2"); err = rccIConv(ctx, icnv, name, 0); if (err<=0) return NULL; } - + puts("ok"); return rccCheckFile(prefix, ctx->tmpbuffer); } @@ -167,7 +202,7 @@ const char *rccFS3(rcc_context ctx, rcc_language_id language_id, rcc_class_id cl result = rccFS2(ctx, ctx->iconv_to[class_id], prefix, name); if (result) { - if ((icnv != (iconv_t)-1)||(icnv != (iconv_t)-2)) iconv_close(icnv); + if ((icnv != (iconv_t)-1)&&(icnv != (iconv_t)-2)) iconv_close(icnv); ctx->fsiconv = (iconv_t)-1; return result; } @@ -181,7 +216,7 @@ const char *rccFS3(rcc_context ctx, rcc_language_id language_id, rcc_class_id cl if ((icnv != (iconv_t)-1)&&(icnv != (iconv_t)-2)) iconv_close(icnv); - if (strcmp(charset, "UTF-8")&&strcmp(charset, "UTF8")) icnv = (iconv_t)-2; + if ((!strcmp(charset, "UTF-8"))||(!strcmp(charset, "UTF8"))) icnv = (iconv_t)-2; else icnv = iconv_open(charset, "UTF-8"); result = rccFS2(ctx, icnv, prefix, name); @@ -1,7 +1,7 @@ #ifndef _RCC_FS_H #define _RCC_FS_H -int rccFS0(const char *fspath, const char *filename, char **prefix, char **name); +int rccFS0(rcc_context ctx, 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); diff --git a/src/internal.h b/src/internal.h index 1706ff5..ac9a74e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -6,9 +6,13 @@ #include "recode.h" #include "engine.h" #include "lngconfig.h" +#include "rccstring.h" #define STRNLEN(str,n) (n?strnlen(str,n):strlen(str)) +#define RCC_MAX_STRING_CHARS 1024 +#define RCC_MAX_PREFIX_CHARS 32 + struct rcc_context_t { char locale_variable[RCC_MAX_VARIABLE_CHARS+1]; @@ -33,6 +37,7 @@ struct rcc_context_t { iconv_t iconv_auto[RCC_MAX_CHARSETS]; char tmpbuffer[RCC_MAX_STRING_CHARS+sizeof(rcc_string_header)+1]; + char lastprefix[RCC_MAX_PREFIX_CHARS+1]; iconv_t fsiconv; unsigned char configure; @@ -44,6 +49,7 @@ struct rcc_context_t { typedef struct rcc_context_t rcc_context_s; int rccConfigure(rcc_context ctx); -char *rccCreateResult(rcc_context ctx, int len, int *rlen); +char *rccCreateResult(rcc_context ctx, size_t len, size_t *rlen); +extern rcc_context rcc_default_ctx; #endif /* _RCC_INTERNAL_H */ diff --git a/src/librcc.c b/src/librcc.c index 5a68b5f..d29ba8b 100644 --- a/src/librcc.c +++ b/src/librcc.c @@ -27,6 +27,7 @@ static int initialized = 0; char *rcc_home_dir = NULL; +rcc_context rcc_default_ctx = NULL; int rccInit() { int err; @@ -65,6 +66,11 @@ int rccInit() { } void rccFree() { + if (rcc_default_ctx) { + rccFreeContext(rcc_default_ctx); + rcc_default_ctx = NULL; + } + rccXmlFree(); rccEncaFree(); @@ -136,6 +142,7 @@ rcc_context rccCreateContext(const char *locale_variable, unsigned int max_langu classes[0] = NULL; ctx->fsiconv = (iconv_t)-1; + ctx->lastprefix[0] = 0; ctx->iconv_from = from; ctx->iconv_to = to; @@ -204,6 +211,14 @@ rcc_context rccCreateContext(const char *locale_variable, unsigned int max_langu return ctx; } +int rccInitDefaultContext(const char *locale_variable, unsigned int max_languages, unsigned int max_classes, rcc_class_ptr defclasses, rcc_init_flags flags) { + if (rcc_default_ctx) return -1; + rcc_default_ctx = rccCreateContext(locale_variable, max_languages, max_classes, defclasses, flags); + if (rcc_default_ctx) return 0; + return -1; +} + + static void rccFreeIConv(rcc_context ctx) { unsigned int i; @@ -253,21 +268,33 @@ void rccFreeContext(rcc_context ctx) { } int rccLockConfiguration(rcc_context ctx, unsigned int lock_code) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if (ctx->configuration_lock) return -1; ctx->configuration_lock = lock_code; return 0; } int rccUnlockConfiguration(rcc_context ctx, unsigned int lock_code) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if (ctx->configuration_lock != lock_code) return -1; ctx->configuration_lock = 0; return 0; } rcc_language_id rccRegisterLanguage(rcc_context ctx, rcc_language *language) { - if ((!ctx)||(!language)) return (rcc_language_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_language_id)-1; + } + if (!language) return (rcc_language_id)-1; if (ctx->configuration_lock) return (rcc_language_id)-1; if (ctx->n_languages == ctx->max_languages) return (rcc_language_id)-1; @@ -305,7 +332,11 @@ rcc_engine_id rccLanguageRegisterEngine(rcc_language *language, rcc_engine *engi rcc_alias_id rccRegisterLanguageAlias(rcc_context ctx, rcc_language_alias *alias) { unsigned int i; - if ((!ctx)||(!alias)) return (rcc_alias_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_alias_id)-1; + } + if (!alias) return (rcc_alias_id)-1; for (i=0;ctx->aliases[i];i++) if (i>=RCC_MAX_ALIASES) return (rcc_alias_id)-1; @@ -317,7 +348,11 @@ rcc_alias_id rccRegisterLanguageAlias(rcc_context ctx, rcc_language_alias *alias } rcc_class_id rccRegisterClass(rcc_context ctx, rcc_class *cl) { - if ((!ctx)||(!cl)) return (rcc_class_id)-1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_class_id)-1; + } + if (!cl) return (rcc_class_id)-1; if (ctx->configuration_lock) return (rcc_class_id)-1; if (ctx->n_classes == ctx->max_classes) return (rcc_class_id)-1; @@ -329,7 +364,12 @@ rcc_class_id rccRegisterClass(rcc_context ctx, rcc_class *cl) { 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; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return RCC_CLASS_INVALID; + } + + if ((class_id<0)||(class_id>=ctx->n_classes)) return RCC_CLASS_INVALID; return ctx->classes[class_id]->class_type; } @@ -374,7 +414,7 @@ int rccConfigure(rcc_context ctx) { return 0; } -char *rccCreateResult(rcc_context ctx, int len, int *rlen) { +char *rccCreateResult(rcc_context ctx, size_t len, size_t *rlen) { char *res; if (!len) len = strlen(ctx->tmpbuffer); diff --git a/src/librcc.h b/src/librcc.h index 1f5ee72..79aab7e 100644 --- a/src/librcc.h +++ b/src/librcc.h @@ -17,7 +17,6 @@ #define RCC_MAX_LANGUAGE_CHARS 16 #define RCC_MAX_VARIABLE_CHARS 16 -#define RCC_MAX_STRING_CHARS 1024 /* ID's */ typedef char rcc_language_id; @@ -45,7 +44,8 @@ void rccFree(); *******************************************************************************/ typedef unsigned int rcc_init_flags; #define RCC_NO_DEFAULT_CONFIGURATION 1 -rcc_context rccCreateContext(const char *locale_variable, unsigned int max_languages, unsigned int max_classes, rcc_class_ptr classes, rcc_init_flags flags); +rcc_context rccCreateContext(const char *locale_variable, unsigned int max_languages, unsigned int max_classes, rcc_class_ptr defclasses, rcc_init_flags flags); +int rccInitDefaultContext(const char *locale_variable, unsigned int max_languages, unsigned int max_classes, rcc_class_ptr defclasses, rcc_init_flags flags); void rccFreeContext(rcc_context ctx); int rccLockConfiguration(rcc_context ctx, unsigned int lock_code); @@ -248,24 +248,29 @@ const char *rccGetLanguageFullName(const char *lang); /* string.c */ 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); +size_t rccStringCheck(const char *str); +size_t rccStringSizedCheck(const char *str, size_t len); -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); +rcc_language_id rccStringGetLanguage(const rcc_string str); +const char *rccStringGetString(const rcc_string str); +char *rccStringExtractString(const rcc_string str); +const char *rccGetString(const char *str); +const char *rccSizedGetString(const char *str, size_t len, size_t *rlen); + +int rccStringCmp(const char *str1, const char *str2); +int rccStringNCmp(const char *str1, const char *str2, size_t n); +int rccStringCaseCmp(const char *str1, const char *str2); +int rccStringNCaseCmp(const char *str1, const char *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); +rcc_string rccFrom(rcc_context ctx, rcc_class_id class_id, const char *buf, size_t len, size_t *rlen); +char *rccTo(rcc_context ctx, rcc_class_id class_id, const rcc_string buf, size_t len, size_t *rlen); +char *rccRecode(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *buf, size_t len, size_t *rlen); char *rccFS(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *fspath, const char *path, const char *filename); /******************************************************************************* @@ -6,7 +6,11 @@ #include "rcclocale.h" rcc_language_ptr rccGetLanguagePointer(rcc_context ctx, rcc_language_id language_id) { - if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + if ((language_id<0)||(language_id>=ctx->n_languages)) return NULL; return ctx->languages[language_id]; } @@ -19,12 +23,17 @@ const char *rccGetLanguageName(rcc_context ctx, rcc_language_id language_id) { rcc_language_id rccGetLanguageByName(rcc_context ctx, const char *name) { unsigned int i; - if ((!ctx)||(!name)) return 0; + + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_language_id)-1; + } + if (!name) return (rcc_language_id)-1; for (i=0;ctx->languages[i];i++) if (!strcmp(ctx->languages[i]->sn, name)) return i; - return 0; + return (rcc_language_id)-1; } static rcc_language_id rccGetDefaultLanguage(rcc_context ctx) { @@ -32,36 +41,43 @@ static rcc_language_id rccGetDefaultLanguage(rcc_context ctx) { unsigned int i; char stmp[RCC_MAX_LANGUAGE_CHARS+1]; - if (!ctx) return -1; - err = rccLocaleGetLanguage(stmp, ctx->locale_variable, RCC_MAX_LANGUAGE_CHARS); if (err) { - if (ctx->n_languages>1) return 1; - return -1; + if (ctx->n_languages>1) return (rcc_language_id)1; + return (rcc_language_id)-1; } for (i=0;ctx->languages[i];i++) - if (!strcmp(ctx->languages[i]->sn, stmp)) return i; + if (!strcmp(ctx->languages[i]->sn, stmp)) return (rcc_language_id)i; - if (i>1) return 1; - return -1; + if (i>1) return (rcc_language_id)1; + return (rcc_language_id)-1; } 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 (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_language_id)-1; + } + if ((language_id<0)||(language_id>=ctx->n_languages)) return (rcc_language_id)-1; + if (language_id) return language_id; return rccGetDefaultLanguage(ctx); } const char *rccGetRealLanguageName(rcc_context ctx, rcc_language_id language_id) { language_id = rccGetRealLanguage(ctx, language_id); - if (language_id<0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetLanguageName(ctx, language_id); } rcc_language_id rccGetSelectedLanguage(rcc_context ctx) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_language_id)-1; + } + return ctx->current_language; } @@ -69,13 +85,17 @@ const char *rccGetSelectedLanguageName(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetSelectedLanguage(ctx); - if (language_id<0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetLanguageName(ctx, language_id); } rcc_language_id rccGetCurrentLanguage(rcc_context ctx) { - if (!ctx) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_language_id)-1; + } + return rccGetRealLanguage(ctx, ctx->current_language); } @@ -83,7 +103,7 @@ const char *rccGetCurrentLanguageName(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetCurrentLanguage(ctx); - if (language_id<0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetLanguageName(ctx, language_id); } @@ -91,8 +111,12 @@ const char *rccGetCurrentLanguageName(rcc_context ctx) { 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; + + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((language_id < 0)||(language_id >= ctx->n_languages)) return -1; if ((!ctx->languages[language_id]->engines[0])||(!ctx->languages[language_id]->charsets[0])) return -2; if (ctx->current_language != language_id) { @@ -109,7 +133,7 @@ int rccSetLanguageByName(rcc_context ctx, const char *name) { rcc_language_id language_id; language_id = rccGetLanguageByName(ctx, name); - if (language_id < 0) return -1; + if (language_id == (rcc_language_id)-1) return -1; return rccSetLanguage(ctx, language_id); } diff --git a/src/lngconfig.c b/src/lngconfig.c index 73cf527..9748629 100644 --- a/src/lngconfig.c +++ b/src/lngconfig.c @@ -68,20 +68,20 @@ rcc_engine_id rccConfigGetEngineByName(rcc_language_config config, const char *n for (i=0;engines[i];i++) if (!strcasecmp(engines[i]->title,name)) return i; - return -1; + return (rcc_engine_id)-1; } rcc_charset_id rccConfigGetCharsetByName(rcc_language_config config, const char *name) { unsigned int i; rcc_charset *charsets; - if ((!config)||(!config->language)||(!name)) return -1; + if ((!config)||(!config->language)||(!name)) return (rcc_charset_id)-1; charsets = config->language->charsets; for (i=0;charsets[i];i++) - if (!strcasecmp(charsets[i],name)) return i; + if (!strcasecmp(charsets[i],name)) return (rcc_charset_id)i; - return 0; + return (rcc_charset_id)-1; } rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config config, const char *name) { @@ -89,15 +89,15 @@ rcc_charset_id rccConfigGetAutoCharsetByName(rcc_language_config config, const c rcc_charset *charsets; rcc_engine_ptr *engines; - if ((!config)||(!config->language)||(!name)) return -1; + if ((!config)||(!config->language)||(!name)) return (rcc_charset_id)-1; engines = config->language->engines; charsets = engines[config->engine]->charsets; for (i=0;charsets[i];i++) - if (!strcasecmp(charsets[i],name)) return i; + if (!strcasecmp(charsets[i],name)) return (rcc_charset_id)i; - return -1; + return (rcc_charset_id)-1; } int rccConfigInit(rcc_language_config config, rcc_context ctx) { @@ -132,7 +132,7 @@ rcc_language_config rccCheckConfig(rcc_context ctx, rcc_language_id language_id) int err; new_language_id = rccGetRealLanguage(ctx, language_id); - if ((language_id<=0)||(new_language_id != language_id)) return NULL; + if ((language_id == (rcc_language_id)-1)||(new_language_id != language_id)) return NULL; if (!ctx->configs[language_id].charset) return NULL; if (!strcasecmp(ctx->languages[language_id]->sn, "off")) return NULL; @@ -143,7 +143,7 @@ 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 (language_id == (rcc_language_id)-1) return NULL; if (!ctx->configs[language_id].charset) { if (rccConfigInit(ctx->configs+language_id, ctx)) return NULL; } @@ -156,7 +156,7 @@ rcc_language_config rccGetConfigByName(rcc_context ctx, const char *name) { rcc_language_id language_id; language_id = rccGetLanguageByName(ctx, name); - if (language_id < 0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetConfig(ctx, language_id); } @@ -165,13 +165,13 @@ rcc_language_config rccGetCurrentConfig(rcc_context ctx) { rcc_language_id language_id; language_id = rccGetCurrentLanguage(ctx); - if (language_id < 0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetConfig(ctx, language_id); } rcc_engine_id rccConfigGetSelectedEngine(rcc_language_config config) { - if (!config) return -1; + if (!config) return (rcc_engine_id)-1; return config->engine; } @@ -180,7 +180,7 @@ const char *rccConfigGetSelectedEngineName(rcc_language_config config) { rcc_engine_id engine_id; engine_id = rccConfigGetSelectedEngine(config); - if (engine_id == -1) return rcc_engine_nonconfigured; + if (engine_id == (rcc_engine_id)-1) return rcc_engine_nonconfigured; if ((engine_id < 0)||(!config->language)) return NULL; return rccConfigGetEngineName(config, engine_id); @@ -191,23 +191,23 @@ rcc_engine_id rccConfigGetCurrentEngine(rcc_language_config config) { rcc_engine_id engine_id; engine_id = rccConfigGetSelectedEngine(config); - if (engine_id>=0) return engine_id; + if (engine_id != (rcc_engine_id)-1) return engine_id; - if (!config->language) return -1; + if (!config->language) return (rcc_engine_id)-1; else enginelist = config->language->engines; if (enginelist[0]) { - if (enginelist[1]) return 1; - return 0; + if (enginelist[1]) return (rcc_engine_id)1; + return (rcc_engine_id)0; } - return -1; + return (rcc_engine_id)-1; } const char *rccConfigGetCurrentEngineName(rcc_language_config config) { rcc_engine_id engine_id; engine_id = rccConfigGetCurrentEngine(config); - if ((engine_id < 0)||(!config->language)) return NULL; + if ((engine_id == (rcc_engine_id)-1)||(!config->language)) return NULL; return rccConfigGetEngineName(config, engine_id); } @@ -222,7 +222,7 @@ const char *rccConfigGetSelectedCharsetName(rcc_language_config config, rcc_clas rcc_charset_id charset_id; charset_id = rccConfigGetSelectedCharset(config, class_id); - if ((charset_id < 0)||(!config->language)) return NULL; + if ((charset_id == (rcc_charset_id)-1)||(!config->language)) return NULL; return rccConfigGetCharsetName(config, charset_id); } @@ -246,7 +246,7 @@ rcc_charset_id rccConfigGetCurrentCharset(rcc_language_config config, rcc_class_ charset_id = config->charset[class_id]; if (charset_id) return charset_id; - if (!config->language) return -1; + if (!config->language) return (rcc_charset_id)-1; else language = config->language; classes = config->ctx->classes; @@ -264,15 +264,15 @@ rcc_charset_id rccConfigGetCurrentCharset(rcc_language_config config, rcc_class_ if ((charset_id != 0)&&(charset_id != (rcc_charset_id)-1)) return charset_id; charsets=language->charsets; - if ((charsets[0])&&(charsets[1])) return 1; - return -1; + if ((charsets[0])&&(charsets[1])) return (rcc_charset_id)1; + return (rcc_charset_id)-1; } const char *rccConfigGetCurrentCharsetName(rcc_language_config config, rcc_class_id class_id) { rcc_charset_id charset_id; charset_id = rccConfigGetCurrentCharset(config, class_id); - if ((charset_id < 0)||(!config->language)) return NULL; + if ((charset_id == (rcc_charset_id)-1)||(!config->language)) return NULL; return rccConfigGetCharsetName(config, charset_id); } @@ -304,7 +304,7 @@ int rccConfigSetEngineByName(rcc_language_config config, const char *name) { return rccConfigSetEngine(config, (rcc_engine_id)-1); engine_id = rccConfigGetEngineByName(config, name); - if (engine_id < 0) return -1; + if (engine_id == (rcc_engine_id)-1) return -1; return rccConfigSetEngine(config, engine_id); } @@ -329,7 +329,7 @@ int rccConfigSetCharsetByName(rcc_language_config config, rcc_class_id class_id, rcc_charset_id charset_id; charset_id = rccConfigGetCharsetByName(config, name); - if (charset_id < 0) return -1; + if (charset_id == (rcc_charset_id)-1) return -1; return rccConfigSetCharset(config, class_id, charset_id); } @@ -340,7 +340,7 @@ rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config config, const char rcc_language_id language_id; char stmp[RCC_MAX_CHARSET_CHARS+1]; - if ((!config)||(!config->language)) return -1; + if ((!config)||(!config->language)) return (rcc_charset_id)-1; language_id = rccGetCurrentLanguage(config->ctx); if (language_id) err = rccLocaleGetLanguage(stmp, locale_variable?locale_variable:config->ctx->locale_variable, RCC_MAX_CHARSET_CHARS); @@ -352,8 +352,8 @@ rcc_charset_id rccConfigGetLocaleCharset(rcc_language_config config, const char if (err) { charsets=config->language->charsets; - if ((charsets[0])&&(charsets[1])) return 1; - return -1; + if ((charsets[0])&&(charsets[1])) return (rcc_charset_id)1; + return (rcc_charset_id)-1; } return rccConfigGetCharsetByName(config, stmp); @@ -4,19 +4,31 @@ #include "opt.h" rcc_option_value rccGetOption(rcc_context ctx, rcc_option option) { - if ((!ctx)||(option<0)||(option>=RCC_MAX_OPTIONS)) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return (rcc_option_value)0; + } + if ((option<0)||(option>=RCC_MAX_OPTIONS)) return 0; return ctx->options[option]; } int rccOptionIsDefault(rcc_context ctx, rcc_option option) { - if ((!ctx)||(option<0)||(option>=RCC_MAX_OPTIONS)) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1; return ctx->default_options[option]; } int rccSetOption(rcc_context ctx, rcc_option option, rcc_option_value value) { - if ((!ctx)||(option>=RCC_MAX_OPTIONS)) return -1; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1; ctx->default_options[option] = 0; @@ -30,7 +42,12 @@ int rccSetOption(rcc_context ctx, rcc_option option, rcc_option_value value) { int rccOptionSetDefault(rcc_context ctx, rcc_option option) { rcc_option_value value; - if ((!ctx)||(option>=RCC_MAX_OPTIONS)) return -1; + + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((option<0)||(option>=RCC_MAX_OPTIONS)) return -1; ctx->default_options[option] = 1; value = rccGetOptionDefaultValue(option); diff --git a/src/rcciconv.c b/src/rcciconv.c index 50f827e..aaf655f 100644 --- a/src/rcciconv.c +++ b/src/rcciconv.c @@ -28,17 +28,17 @@ static int rccIConvUTFBytes(unsigned char c) { return 6-j; } -int rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, int len) { +size_t rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, size_t len) { char *in_buf, *out_buf, *res, err; int in_left, out_left, olen; int ub, utf_mode=0; int errors=0; - if ((!buf)||(!ctx)||(icnv == (iconv_t)-1)) return -1; + if ((!buf)||(!ctx)||(icnv == (iconv_t)-1)) return (size_t)-1; len = STRNLEN(buf,len); - if (iconv(icnv, NULL, NULL, NULL, NULL) == -1) return -1; + if (iconv(icnv, NULL, NULL, NULL, NULL) == -1) return (size_t)-1; loop_restart: errors = 0; @@ -61,10 +61,10 @@ loop: utf_mode = 1; goto loop_restart; } else { - return -1; + return (size_t)-1; } } else { - return -1; + return (size_t)-1; } } diff --git a/src/rcciconv.h b/src/rcciconv.h index 1c899a9..fd72486 100644 --- a/src/rcciconv.h +++ b/src/rcciconv.h @@ -1,6 +1,6 @@ #ifndef _RCC_ICONV_H #define _RCC_ICONV_H -int rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, int len); +size_t rccIConv(rcc_context ctx, iconv_t icnv, const char *buf, size_t len); #endif /* _RCC_ICONV_H */ diff --git a/src/rcclist.c b/src/rcclist.c index 25e6d07..2664ed6 100644 --- a/src/rcclist.c +++ b/src/rcclist.c @@ -2,19 +2,33 @@ #include "internal.h" rcc_language_ptr *rccGetLanguageList(rcc_context ctx) { - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return ctx->languages; } rcc_charset *rccGetCharsetList(rcc_context ctx, rcc_language_id language_id) { - if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + + if ((language_id<0)||(language_id>=ctx->n_languages)) return NULL; if (!language_id) language_id = rccGetCurrentLanguage(ctx); return ctx->languages[language_id]->charsets; } rcc_engine_ptr *rccGetEngineList(rcc_context ctx, rcc_language_id language_id) { - if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + + if ((language_id<0)||(language_id>=ctx->n_languages)) return NULL; if (!language_id) language_id = rccGetCurrentLanguage(ctx); return ctx->languages[language_id]->engines; @@ -23,10 +37,13 @@ rcc_engine_ptr *rccGetEngineList(rcc_context ctx, rcc_language_id language_id) { rcc_charset *rccGetCurrentCharsetList(rcc_context ctx) { rcc_language_id language_id; - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } language_id = rccGetCurrentLanguage(ctx); - if (language_id<0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetCharsetList(ctx, language_id); } @@ -34,10 +51,13 @@ rcc_charset *rccGetCurrentCharsetList(rcc_context ctx) { rcc_engine_ptr *rccGetCurrentEngineList(rcc_context ctx) { rcc_language_id language_id; - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } language_id = rccGetCurrentLanguage(ctx); - if (language_id<0) return NULL; + if (language_id == (rcc_language_id)-1) return NULL; return rccGetEngineList(ctx, language_id); } @@ -46,18 +66,24 @@ rcc_charset *rccGetCurrentAutoCharsetList(rcc_context ctx) { rcc_language_id language_id; rcc_engine_id engine_id; - if (!ctx) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } language_id = rccGetCurrentLanguage(ctx); engine_id = rccGetCurrentEngine(ctx); - if ((language_id<0)||(engine_id<0)) return NULL; + if ((language_id == (rcc_language_id)-1)||(engine_id == (rcc_engine_id)-1)) return NULL; return ctx->languages[language_id]->engines[engine_id]->charsets; } rcc_class_ptr *rccGetClassList(rcc_context ctx) { - if (!ctx) return NULL; - + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + return ctx->classes; } diff --git a/src/rccstring.c b/src/rccstring.c index 66cb6a9..0df20d6 100644 --- a/src/rccstring.c +++ b/src/rccstring.c @@ -4,7 +4,7 @@ #include "internal.h" #include "rccstring.h" -rcc_string rccCreateString(rcc_language_id language_id, const char *buf, int len, int *rlen) { +rcc_string rccCreateString(rcc_language_id language_id, const char *buf, size_t len, size_t *rlen) { char *res; rcc_string_header header = {RCC_STRING_MAGIC, language_id}; @@ -26,53 +26,93 @@ void rccStringFree(rcc_string str) { if (str) free(str); } -rcc_language_id rccStringCheck(const rcc_string str) { - int len; - rcc_string_header *header; +size_t rccStringCheck(const char *str) { + size_t len; + if (!str) return 0; + len = strlen(str); + if (len>sizeof(rcc_string_header)) { + len-=sizeof(rcc_string_header); + if (((rcc_string_header*)str)->magic == RCC_STRING_MAGIC) return len; + } + + return 0; +} + +size_t rccStringSizedCheck(const char *str, size_t len) { + size_t newlen; + + if (!str) return 0; - if ((!str)||(len<=sizeof(unsigned int))||(*((unsigned int*)(str))!=RCC_STRING_MAGIC)) return 0; + newlen = STRNLEN(str, len); + if (newlen>sizeof(rcc_string_header)) { + if ((len==newlen)&&(str[newlen-1])) return 0; + newlen-=sizeof(rcc_string_header); + } else return 0; + + if (((rcc_string_header*)str)->magic == RCC_STRING_MAGIC) return newlen; + return 0; +} + - header = (rcc_string_header*)(str); - return header->language_id; +rcc_language_id rccStringGetLanguage(const rcc_string str) { + if (!str) return (rcc_language_id)-1; + return ((rcc_string_header*)str)->language_id; } -const char *rccStringGet(const rcc_string str) { - if (rccStringCheck(str)) return (const char *)str + sizeof(rcc_string_header); - return (const char *)str; +const char *rccStringGetString(const rcc_string str) { + return (const char *)str + sizeof(rcc_string_header); } -char *rccStringExtract(const rcc_string buf, int len, int *rlen) { +char *rccStringExtractString(const rcc_string str) { + size_t len; char *res; - len = STRNLEN(buf, len) - sizeof(rcc_string_header); - if (len<0) return NULL; + len = rccStringCheck(str); + if (!len) return NULL; res = (char*)malloc(len+1); if (!res) return NULL; - strncpy(res, buf + sizeof(rcc_string_header), len); + strncpy(res, rccStringGetString(str), len); res[len] = 0; - if (rlen) *rlen = len; - return res; } -int rccStringCmp(const rcc_string str1, const rcc_string str2) { - return strcmp(rccStringGet(str1), rccStringGet(str2)); + +const char *rccGetString(const char *str) { + if (rccStringCheck(str)) return rccStringGetString((const rcc_string)str); + return str; +} + +const char *rccSizedGetString(const char *str, size_t len, size_t *rlen) { + size_t newlen; + + newlen = rccStringSizedCheck(str, len); + if (newlen) { + if (rlen) *rlen = newlen; + return rccStringGetString((const rcc_string)str); + } + + return (const char *)str; +} + + +int rccStringCmp(const char *str1, const char *str2) { + return strcmp(rccGetString(str1), rccGetString(str2)); } -int rccStringNCmp(const rcc_string str1, const rcc_string str2, size_t n) { - return strncmp(rccStringGet(str1), rccStringGet(str2), n); +int rccStringNCmp(const char *str1, const char *str2, size_t n) { + return strncmp(rccGetString(str1), rccGetString(str2), n); } -int rccStringCaseCmp(const rcc_string str1, const rcc_string str2) { - return strcasecmp(rccStringGet(str1), rccStringGet(str2)); +int rccStringCaseCmp(const char *str1, const char *str2) { + return strcasecmp(rccGetString(str1), rccGetString(str2)); } -int rccStringNCaseCmp(const rcc_string str1, const rcc_string str2, size_t n) { - return strncasecmp(rccStringGet(str1), rccStringGet(str2), n); +int rccStringNCaseCmp(const char *str1, const char *str2, size_t n) { + return strncasecmp(rccGetString(str1), rccGetString(str2), n); } diff --git a/src/rccstring.h b/src/rccstring.h index 4f7d411..a9cfc9b 100644 --- a/src/rccstring.h +++ b/src/rccstring.h @@ -1,16 +1,16 @@ #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); +#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; -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); -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); +rcc_string rccCreateString(rcc_language_id language_id, const char *buf, size_t len, size_t *rlen); +void rccStringFree(rcc_string str); #endif /* _RCC_STRING_H */ diff --git a/src/rccxml.c b/src/rccxml.c index f40853f..a64e759 100644 --- a/src/rccxml.c +++ b/src/rccxml.c @@ -216,6 +216,11 @@ int rccSave(rcc_context ctx, const char *name) { int memsize; xmlChar *mem; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((!name)||(!strcmp(name, "rcc"))||(strlen(rcc_home_dir)<3)) name = "default"; size = strlen(rcc_home_dir) + strlen(name) + 32; @@ -371,6 +376,11 @@ int rccLoad(rcc_context ctx, const char *name) { xmlDocPtr doc = NULL; xmlNodePtr node, lnode; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return -1; + } + if ((!name)||(!strcmp(name, "rcc"))||(strlen(rcc_home_dir)<3)) name = "default"; size = strlen(rcc_home_dir) + strlen(name) + 32; diff --git a/src/recode.c b/src/recode.c index f10221f..4c1c5bd 100644 --- a/src/recode.c +++ b/src/recode.c @@ -16,7 +16,7 @@ static rcc_charset_id rccIConvAuto(rcc_context ctx, rcc_class_id class_id, const rcc_class_type class_type; rcc_engine_ptr engine; - if ((!ctx)||(!buf)) return -1; + if (!buf) return (rcc_charset_id)-1; class_type = rccGetClassType(ctx, class_id); if ((class_type == RCC_CLASS_STANDARD)||((class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_TITLES)))) { @@ -25,24 +25,29 @@ static rcc_charset_id rccIConvAuto(rcc_context ctx, rcc_class_id class_id, const return engine->func(&ctx->engine_ctx, buf, len); } - return -1; + return (rcc_charset_id)-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, size_t len, size_t *rlen) { int err; + size_t ret; rcc_language_id language_id; rcc_charset_id charset_id; iconv_t icnv = (iconv_t)-1; rcc_string result; - if ((!ctx)||(class_id<0)||(class_id>=ctx->n_classes)||(!buf)) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + if ((class_id<0)||(class_id>=ctx->n_classes)||(!buf)) return NULL; err = rccConfigure(ctx); if (err) return NULL; // Checking if rcc_string passed - language_id = rccStringCheck((const rcc_string)buf); - if (language_id) return NULL; + ret = rccStringSizedCheck(buf, len); + if (ret) return NULL; language_id = rccGetCurrentLanguage(ctx); // DS: Learning. check database (language_id) @@ -58,9 +63,9 @@ rcc_string rccFrom(rcc_context ctx, rcc_class_id class_id, const char *buf, int if (icnv == (iconv_t)-2) { result = rccCreateString(language_id, buf, len, rlen); } else { - err = rccIConv(ctx, icnv, buf, len); - if (err<=0) return NULL; - result = rccCreateString(language_id, ctx->tmpbuffer, err, rlen); + ret = rccIConv(ctx, icnv, buf, len); + if (ret == (size_t)-1) return NULL; + result = rccCreateString(language_id, ctx->tmpbuffer, ret, rlen); } // DS: Learning. write database @@ -68,19 +73,28 @@ 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, size_t len, size_t *rlen) { int err; + size_t newlen; char *result; char *prefix, *name; + const char *utfstring; 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; - language_id = rccStringCheck(buf); - if (!language_id) return NULL; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + if ((class_id<0)||(class_id>=ctx->n_classes)||(!buf)) return NULL; + + newlen = rccStringSizedCheck((const char*)buf, len); + if (!newlen) return NULL; + + language_id = rccStringGetLanguage(buf); + utfstring = rccStringGetString(buf); err = rccConfigure(ctx); if (err) return NULL; @@ -88,11 +102,17 @@ char *rccTo(rcc_context ctx, rcc_class_id class_id, const rcc_string buf, int le icnv = ctx->iconv_to[class_id]; class_type = rccGetClassType(ctx, class_id); - if ((class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES))) { - prefix = NULL; name = buf + sizeof(rcc_string_header); - err = rccFS0(NULL, buf, &prefix, &name); - if (!err) { + if ((language_id)&&(class_type == RCC_CLASS_FS)&&(rccGetOption(ctx, RCC_AUTODETECT_FS_NAMES))) { + name = (char*)utfstring; + prefix = NULL; + + err = rccFS0(ctx, NULL, buf, &prefix, &name); + if (err>=0) { result = rccFS3(ctx, language_id, class_id, prefix, name); + if (!err) { + if (prefix) free(prefix); + free(name); + } if ((rlen)&&(result)) *rlen = strlen(result); return result; } @@ -100,32 +120,37 @@ char *rccTo(rcc_context ctx, rcc_class_id class_id, const rcc_string buf, int le if (icnv == (iconv_t)-1) return NULL; if (icnv == (iconv_t)-2) { - result = rccStringExtract(buf, len, rlen); + result = rccStringExtractString(buf); + if (rlen) *rlen = newlen; } else { - err = rccIConv(ctx, icnv, buf + sizeof(rcc_string_header), len?len-sizeof(rcc_string_header):0); - if (err<=0) return NULL; + newlen = rccIConv(ctx, icnv, rccStringGetString(buf), len?newlen:0); + if (newlen == (size_t)-1) return NULL; - result = rccCreateResult(ctx, err, rlen); + result = rccCreateResult(ctx, newlen, rlen); } return result; } -char *rccRecode(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *buf, int len, int *rlen) { - int nlen; +char *rccRecode(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *buf, size_t len, size_t *rlen) { + size_t nlen; rcc_string stmp; 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; + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + if ((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, from, buf, len); - if (from_charset_id>0) { + if (from_charset_id != (rcc_charset_id)-1) { from_charset = rccGetAutoCharsetName(ctx, from_charset_id); to_charset = rccGetCurrentCharsetName(ctx, to); if ((from_charset)&&(to_charset)&&(!strcasecmp(from_charset, to_charset))) return NULL; @@ -155,23 +180,34 @@ char *rccFS(rcc_context ctx, rcc_class_id from, rcc_class_id to, const char *fsp char *stmp; char *result = NULL; - + + if (!ctx) { + if (rcc_default_ctx) ctx = rcc_default_ctx; + else return NULL; + } + if ((from<0)||(from>=ctx->n_classes)||(to<0)||(to>=ctx->n_classes)||(!filename)) return NULL; err = rccFS1(ctx, fspath, &prefix, &name); if (err) { - if (err<0) return NULL; - return name; + if (err < 0) return NULL; + + if (err&1) { + if (err&2) return NULL; + return name; + } } 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)); + result = rccFS3(ctx, language_id, to, prefix, rccStringGetString(string)); free(string); - } + } else result = NULL; - free(prefix); - free(name); + if (!(err&2)) { + if (prefix) free(prefix); + free(name); + } return result; } diff --git a/src/recode.h b/src/recode.h index a70f354..0e31759 100644 --- a/src/recode.h +++ b/src/recode.h @@ -1,13 +1,5 @@ #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 */ |