0
|
1 #include "libguess.h"
|
|
2
|
|
3 typedef struct _guess_impl {
|
|
4 struct _guess_impl *next;
|
|
5 const char *name;
|
|
6 const char *(*impl)(const char *buf, int len);
|
|
7 } guess_impl;
|
|
8
|
|
9 static guess_impl *guess_impl_list = NULL;
|
|
10
|
|
11 void guess_impl_register(const char *lang,
|
|
12 const char *(*impl)(const char *buf, int len))
|
|
13 {
|
|
14 guess_impl *iptr = calloc(sizeof(guess_impl), 1);
|
|
15
|
|
16 iptr->name = lang;
|
|
17 iptr->impl = impl;
|
|
18 iptr->next = guess_impl_list;
|
|
19
|
|
20 guess_impl_list = iptr;
|
|
21 }
|
|
22
|
|
23 void guess_init(void)
|
|
24 {
|
|
25 /* check if already initialized */
|
|
26 if (guess_impl_list != NULL)
|
|
27 return;
|
|
28
|
|
29 guess_impl_register(GUESS_REGION_JP, guess_jp);
|
|
30 guess_impl_register(GUESS_REGION_TW, guess_tw);
|
|
31 guess_impl_register(GUESS_REGION_CN, guess_cn);
|
|
32 guess_impl_register(GUESS_REGION_KR, guess_kr);
|
|
33 guess_impl_register(GUESS_REGION_RU, guess_ru);
|
|
34 guess_impl_register(GUESS_REGION_AR, guess_ar);
|
|
35 guess_impl_register(GUESS_REGION_TR, guess_tr);
|
|
36 guess_impl_register(GUESS_REGION_GR, guess_gr);
|
|
37 guess_impl_register(GUESS_REGION_HW, guess_hw);
|
|
38 }
|
|
39
|
|
40 const char *guess_encoding(const char *inbuf, int buflen, const char *lang)
|
|
41 {
|
|
42 guess_impl *iter;
|
|
43
|
|
44 guess_init();
|
|
45
|
|
46 for (iter = guess_impl_list; iter != NULL; iter = iter->next)
|
|
47 {
|
|
48 if (!strcasecmp(lang, iter->name))
|
|
49 return iter->impl(inbuf, buflen);
|
|
50 }
|
|
51
|
|
52 /* TODO: try other languages as fallback? */
|
|
53
|
|
54 return NULL;
|
|
55 }
|