/rusxmms/librcc

To get this branch, use:
bzr branch http://suren.me/webbzr/rusxmms/librcc

« back to all changes in this revision

Viewing changes to src/librcc.c

  • Committer: Suren A. Chilingaryan
  • Date: 2005-06-16 23:14:30 UTC
  • Revision ID: Arch-1:ds@dside.dyndns.org--darksoft-2004%librcc--main--0.1--base-0
Initial Import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <string.h>
 
3
 
 
4
#include <librcd.h>
 
5
#include "librcc.h"
 
6
#include "enca.h"
 
7
 
 
8
#include "config.h"
 
9
 
 
10
int rccInit() {
 
11
    return rccEncaInit();
 
12
}
 
13
 
 
14
void rccFree() {
 
15
    rccEncaFree();
 
16
}
 
17
 
 
18
rcc_context rccInitContext(rcc_init_flags flags, unsigned int max_languages, unsigned int max_classes, const char *locale) {
 
19
    unsigned int i;
 
20
    
 
21
    rcc_context *ctx;
 
22
    rcc_language_ptr *languages;
 
23
    rcc_class_ptr *classes;
 
24
    rcc_language_config *configs;
 
25
    iconv_t *from, *to;
 
26
    
 
27
    if (!max_languages) max_languages = RCC_MAX_LANGUAGES;
 
28
    if (!max_classes) max_classes = RCC_MAX_CLASSES;
 
29
 
 
30
    ctx = (rcc_context*)malloc(sizeof(rcc_context));
 
31
    languages = (rcc_language_ptr*)malloc((max_languages+1)*sizeof(rcc_language_ptr));
 
32
    classes = (rcc_class_ptr*)malloc((max_classes+1)*sizeof(rcc_class_ptr));
 
33
    from = (iconv_t*)malloc((max_classes)*sizeof(iconv_t));
 
34
    to = (iconv_t*)malloc((max_classes)*sizeof(iconv_t));
 
35
 
 
36
    configs = (rcc_language_config*)malloc((max_languages)*sizeof(rcc_language_config));
 
37
    
 
38
    if ((!ctx)||(!languages)||(!classes)) {
 
39
        if (from) free(from);
 
40
        if (to) free(to);
 
41
        if (configs) free(configs);
 
42
        if (classes) free(classes);
 
43
        if (languages) free(languages);
 
44
        if (ctx) free(ctx);
 
45
        return NULL;
 
46
    }
 
47
 
 
48
    ctx->languages = languages;
 
49
    ctx->max_languages = max_languages;
 
50
    ctx->n_languages = 0;
 
51
    languages[0] = NULL;
 
52
 
 
53
    ctx->classes = classes;
 
54
    ctx->max_classes = max_classes;
 
55
    ctx->n_classes = 0;
 
56
    classes[0] = NULL;
 
57
 
 
58
    ctx->fsiconv = (iconv_t)-1;
 
59
    
 
60
    ctx->iconv_from = from;
 
61
    ctx->iconv_to = to;
 
62
    for (i=0;i<max_classes;i++) {
 
63
        from[i] = (iconv_t)-1;
 
64
        to[i] = (iconv_t)-1;
 
65
    }
 
66
    
 
67
    for (i=0;i<RCC_MAX_CHARSETS;i++)
 
68
        ctx->iconv_auto[i] = (iconv_t)-1;
 
69
    
 
70
    ctx->configs = configs;
 
71
    for (i=0;i<max_languages;i++)
 
72
        configs[i].charset = NULL;
 
73
 
 
74
    err = rccEngineInit(&ctx->engine_ctx, ctx);
 
75
    if (err) {
 
76
        rccFree(ctx);
 
77
        return NULL;
 
78
    }
 
79
    
 
80
    ctx->current_language = 0;
 
81
 
 
82
    if (locale) {
 
83
        if (strlen(locale)>=RCC_MAX_VARIABLE_CHARS) {
 
84
            rccFree(ctx);
 
85
            return NULL;
 
86
        }
 
87
        strcpy(ctx->locale_variable, locale);
 
88
    } else {
 
89
        strcpy(ctx->locale_variable, RCC_LOCALE_VARIABLE);
 
90
    }
 
91
    
 
92
    if (flags&RCC_DEFAULT_CONFIGURATION) {
 
93
        if (sizeof(languages)<sizeof(rcc_default_languages)) {
 
94
            rccFree(ctx);
 
95
            return NULL;
 
96
        }
 
97
        
 
98
        for (i=0;rcc_default_languages[i];i++)
 
99
            rccRegisterLanguage(ctx, rcc_default_language[i]);
 
100
 
 
101
        ctx->current_config = rccGetCurrentConfig(ctx);
 
102
    } else {
 
103
        rccRegisterLanguage(ctx, rcc_default_language[0]);
 
104
        ctx->current_config = NULL;
 
105
    } 
 
106
    
 
107
    ctx->configure = 1;
 
108
    
 
109
    return ctx;
 
110
}
 
111
 
 
112
static void rccFreeIConv(rcc_context *ctx) {
 
113
    unsigned int i;
 
114
    
 
115
    if ((!ctx)||(!ctx->iconv_from)||(!ctx->iconv_to)) return;
 
116
 
 
117
    if ((ctx->fsiconv_t != (iconv_t)-1)&&(ctx->fsiconv_t != (iconv_t)-2)) {
 
118
        iconv_close(ctx->fsiconv);
 
119
        ctx->fsiconv = (iconv_t)-1;
 
120
    }
 
121
    
 
122
    for (i=0;i<ctx->n_classes;i++) {
 
123
        if ((ctx->iconv_from[i] != (iconv_t)-1)&&(ctx->iconv_from[i] != (iconv_t)-2)) {
 
124
            iconv_close(ctx->iconv_from[i]);
 
125
            ctx->iconv_from[i] = (iconv_t)-1;
 
126
        }
 
127
        if ((ctx->iconv_to[i] != (iconv_t)-1)&&(ctx->iconv_to[i] != (iconv_t)-2)) {
 
128
            iconv_close(ctx->iconv_to[i]);
 
129
            ctx->iconv_to[i] = (iconv_t)-1;
 
130
        }
 
131
    }
 
132
    for (i=0;i<RCC_MAX_CHARSETS;i++) {
 
133
        if ((ctx->iconv_auto[i] != (iconv_t)-1)&&(ctx->iconv_auto[i] != (iconv_t)-2)) {
 
134
            iconv_close(ctx->iconv_auto[i]);
 
135
            ctx->iconv_auto[i] = (iconv_t)-1;
 
136
        }
 
137
    }    
 
138
}
 
139
 
 
140
void rccFreeContext(rcc_context *ctx) {
 
141
    if (ctx) {
 
142
        rccFreeEngine(&ctx->engine_ctx);
 
143
        rccFreeIConv(ctx);
 
144
        if (ctx->iconv_from) free(ctx->iconv_from);
 
145
        if (ctx->iconv_to) free(ctx->iconv_to);
 
146
        
 
147
        if (ctx->configs) {
 
148
            for (i=0;i<ctx->max_languages;i++)
 
149
                rccFreeConfig(configs+i);
 
150
            free(ctx->configs);
 
151
        }
 
152
        if (ctx->charsets) free(ctx->charsets);
 
153
        if (ctx->classes) free(ctx->classes);
 
154
        if (ctx->languages) free(ctx->languages);
 
155
        free(ctx);
 
156
    }
 
157
}
 
158
 
 
159
rcc_language_id rccRegisterLanguage(rcc_context *ctx, rcc_language *language) {
 
160
    if ((!ctx)||(!language)) return -1;
 
161
    if (ctx->n_languages == ctx->max_languages) return -2;
 
162
    ctx->languages[ctx->n_languages++] = language;
 
163
    ctx->languages[ctx->n_languages] = NULL;
 
164
    
 
165
    if (!ctx->current_language)
 
166
        ctx->current_config = rccGetCurrentConfig(ctx);
 
167
    
 
168
    return ctx->n_languages-1;
 
169
}
 
170
 
 
171
rcc_charset_id rccLanguageRegisterCharset(rcc_language *language, rcc_charset charset) {
 
172
    unsigned int i;
 
173
    
 
174
    if ((!language)||(!charset)) return -1;
 
175
    for (i=0;language->charsets[i];i++);
 
176
    if (i>=RCC_MAX_CHARSETS) return -2;
 
177
    language->charsets[i++] = charset;
 
178
    language->charsets[i] = NULL;
 
179
    return i-1;
 
180
}
 
181
 
 
182
rcc_engine_id rccLanguageRegisterEngine(rcc_language *language, rcc_engine *engine) {
 
183
    unsigned int i;
 
184
    
 
185
    if ((!language)||(!engine)) return -1;
 
186
    for (i=0;language->engines[i];i++);
 
187
    if (i>=RCC_MAX_ENGINES) return -2;
 
188
    language->engines[i++] = engine;
 
189
    language->engines[i] = NULL;
 
190
    return i-1;
 
191
}
 
192
 
 
193
rcc_class_id rccRegisterClass(rcc_context *ctx, rcc_class *cl) {
 
194
    if ((!ctx)||(!cl)) return -1;
 
195
    if (ctx->n_classes == ctx->max_classes) return -2;
 
196
    ctx->configure = 1;
 
197
    ctx->classes[ctx->n_languages++] = cl;
 
198
    ctx->classes[ctx->n_languages] = NULL;
 
199
    return ctx->n_classes-1;
 
200
}
 
201
 
 
202
 
 
203
rcc_class_type rccGetClassType(rcc_context *ctx, rcc_class_id class_id) {
 
204
    rcc_class cl;
 
205
    
 
206
    if (!ctx)||(class_id<0)||(class_id>=ctx->n_classes)) return RCC_CLASS_INVALID;
 
207
    
 
208
    cl = rcc->classes[class_id];
 
209
    return cl->class_type;
 
210
}
 
211
 
 
212
static rcc_language *rccGetLanguageList(rcc_context *ctx) {
 
213
    if (!ctx) return NULL;
 
214
    return ctx->languages;
 
215
}
 
216
 
 
217
static rcc_charset *rccGetCharsetList(rcc_context *ctx, rcc_language_id language_id) {
 
218
    if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL;
 
219
    return ctx->languages[language_id]->charsets;
 
220
}
 
221
 
 
222
static rcc_engine *rccGetEngineList(rcc_context *ctx, rcc_language_id language_id) {
 
223
    if ((!ctx)||(language_id<0)||(language_id>=ctx->n_languages)) return NULL;
 
224
    return ctx->languages[language_id]->engines;
 
225
}
 
226
 
 
227
static rcc_charset *rccGetCurrentCharsetList(rcc_context *ctx) {
 
228
    rcc_language_id language_id;
 
229
 
 
230
    if (!ctx) return NULL;
 
231
 
 
232
    language_id = rccGetCurrentLanguage(ctx);
 
233
    if (language_id<0) return NULL;
 
234
 
 
235
    return rccGetCharsetList(ctx, language_id);
 
236
}
 
237
 
 
238
static rcc_charset *rccGetCurrentEngineList(rcc_context *ctx) {
 
239
    rcc_language_id language_id;
 
240
 
 
241
    if (!ctx) return NULL;
 
242
 
 
243
    language_id = rccGetCurrentLanguage(ctx);
 
244
    if (language_id<0) return NULL;
 
245
 
 
246
    return rccGetEngineList(ctx, language_id); 
 
247
}
 
248
 
 
249
static rcc_charset *rccGetCurrentAutoCharsetList(rcc_context *ctx) {
 
250
    rcc_language_id language_id;
 
251
    rcc_engine_id engine_id;
 
252
 
 
253
    if (!ctx) return NULL;
 
254
 
 
255
    language_id = rccGetCurrentLanguage(ctx);
 
256
    engine_id = rccGetCurrentEngine(ctx);
 
257
    if ((language_id<0)||(engine_id<0)) return NULL;
 
258
    
 
259
    
 
260
    return ctx->languages[language_id]->engine[engine_id]->charsets;
 
261
}
 
262
 
 
263
 
 
264
int rccConfigure(rcc_engine_context *ctx) {
 
265
    unsigned int i;
 
266
    rcc_charset *charsets;
 
267
    char *charset;
 
268
    
 
269
    if (!ctx) return -1;
 
270
    if (!ctx->configure) return 0;
 
271
    
 
272
    rccFreeIConv(ctx);
 
273
    for (i=0;i<ctx->n_classes;i++) {
 
274
        charset = rccGetCurrentCharsetName(ctx, i);
 
275
        if (strcmp(charset, "UTF-8")&&strcmp(charset, "UTF8")) {
 
276
            iconv_from = iconv_open("UTF-8", charset);
 
277
            iconv_to = iconv_open(charset, "UTF-8");
 
278
        } else {
 
279
            iconv_from = (iconv_t)-2;
 
280
            iconv_to = (iconv_t)-2;
 
281
        }
 
282
    }
 
283
 
 
284
    charsets = rccGetCurrentAutoCharsetList(ctx);
 
285
    for (i=0;charsets[i];i++) {
 
286
        charset = charsets[i];
 
287
        if (strcmp(charset, "UTF-8")&&strcmp(charset, "UTF8"))
 
288
            iconv_auto = iconv_open("UTF-8", charset);
 
289
        else
 
290
            iconv_auto = (iconv_t)-2;
 
291
    }
 
292
    
 
293
    rccEngineConfigure(&ctx->engine_ctx);
 
294
    
 
295
    return 0;
 
296
}
 
297
 
 
298
char *rccCreateResult(rcc_context *ctx, int len, int *rlen) {
 
299
    char *res;
 
300
 
 
301
    if (!len) len = strlen(ctx->tmpbuffer);
 
302
 
 
303
    res = (char*)malloc(len+1);
 
304
    if (!res) return NULL;
 
305
 
 
306
    memcpy(res, ctx->tmpbuffer, len);
 
307
    res[len] = 0;
 
308
    
 
309
    if (rlen) *rlen = len;
 
310
    
 
311
    return res;    
 
312
}