summaryrefslogtreecommitdiff
path: root/src/libguess/guess.c
blob: 2a3845c7e64a38ded323cb090338ce206dca422d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "libguess.h"

mowgli_patricia_t *guess_impl_list = NULL;

void strcasecanon(char *str)
{
    while (*str)
    {
        *str = toupper(*str);
        str++;
    }
}

static void
guess_impl_register(const char *lang, guess_impl_f impl)
{
    return_if_fail(guess_impl_list != NULL);

    mowgli_patricia_add(guess_impl_list, lang, impl);
}

void
libguess_init(void)
{
    /* check if already initialized */
    if (guess_impl_list != NULL)
        return;

    guess_impl_list = mowgli_patricia_create(strcasecanon);

    guess_impl_register(GUESS_REGION_JP, guess_jp);
    guess_impl_register(GUESS_REGION_TW, guess_tw);
    guess_impl_register(GUESS_REGION_CN, guess_cn);
    guess_impl_register(GUESS_REGION_KR, guess_kr);
    guess_impl_register(GUESS_REGION_RU, guess_ru);
    guess_impl_register(GUESS_REGION_AR, guess_ar);
    guess_impl_register(GUESS_REGION_TR, guess_tr);
    guess_impl_register(GUESS_REGION_GR, guess_gr);
    guess_impl_register(GUESS_REGION_HW, guess_hw);
    guess_impl_register(GUESS_REGION_PL, guess_pl);
    guess_impl_register(GUESS_REGION_BL, guess_bl);
}

const char *
libguess_determine_encoding(const char *inbuf, int buflen, const char *lang)
{
    guess_impl_f impl;

    libguess_init();

    impl = mowgli_patricia_retrieve(guess_impl_list, lang);
    if (impl != NULL)
        return impl(inbuf, buflen);

    /* TODO: try other languages as fallback? */
    return NULL;
}