summaryrefslogtreecommitdiffstats
path: root/src/librcc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/librcc.c')
-rw-r--r--src/librcc.c98
1 files changed, 58 insertions, 40 deletions
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);