changeset 3201:e1470a536417 trunk

Allow libguess to be extended for other languages using a common API.
author William Pitcock <nenolod@atheme-project.org>
date Wed, 01 Aug 2007 07:11:38 -0500
parents 95cfda3878da
children e9f66c3905ec be19736de36b
files src/libguess/guess.c src/libguess/libguess.h
diffstat 2 files changed, 59 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/libguess/guess.c	Wed Aug 01 03:38:35 2007 -0500
+++ b/src/libguess/guess.c	Wed Aug 01 07:11:38 2007 -0500
@@ -426,3 +426,52 @@
         return "JOHAB";
     return NULL;
 }
+
+typedef struct _guess_impl {
+    struct _guess_impl *next;
+    const char *name;
+    const char *(*impl)(const char *buf, int len);
+} guess_impl;
+
+static guess_impl *guess_impl_list = NULL;
+
+void guess_impl_register(const char *lang,
+    const char *(*impl)(const char *buf, int len))
+{
+    guess_impl *iptr = calloc(sizeof(guess_impl), 1);
+
+    iptr->name = lang;
+    iptr->impl = impl;
+    iptr->next = guess_impl_list;
+
+    guess_impl_list = iptr;
+}
+
+void guess_init(void)
+{
+    /* check if already initialized */
+    if (guess_impl_list != NULL)
+        return;
+
+    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);
+}
+
+const char *guess_encoding(const char *inbuf, int buflen, const char *lang)
+{
+    guess_impl *iter;
+
+    guess_init();
+
+    for (iter = guess_impl_list; iter != NULL; iter = iter->next)
+    {
+        if (!strcasecmp(lang, iter->name))
+           return iter->impl(inbuf, buflen);
+    }
+
+    /* TODO: try other languages as fallback? */
+
+    return NULL;
+}
--- a/src/libguess/libguess.h	Wed Aug 01 03:38:35 2007 -0500
+++ b/src/libguess/libguess.h	Wed Aug 01 07:11:38 2007 -0500
@@ -45,4 +45,14 @@
 const char *guess_kr(const char *buf, int buflen);
 int dfa_validate_utf8(const char *buf, int buflen);
 
+#define GUESS_REGION_JP		"japanese"
+#define GUESS_REGION_TW		"taiwanese"
+#define GUESS_REGION_CN		"chinese"
+#define GUESS_REGION_KR		"korean"
+
+const char *guess_encoding(const char *buf, int buflen, const char *lang);
+void guess_init(void);
+void guess_impl_register(const char *name,
+	const char *(impl)(const char *buf, int buflen));
+
 #endif