1 | /* |
---|
2 | * mecab.c |
---|
3 | * |
---|
4 | * 2009.3.13 by naoya_t |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include <gauche.h> |
---|
9 | #include <gauche/extend.h> |
---|
10 | |
---|
11 | #include <mecab.h> |
---|
12 | #include "mecab.h" |
---|
13 | |
---|
14 | // mecab_t |
---|
15 | mecab_t* unwrap_mecab_t(ScmObj obj) |
---|
16 | { |
---|
17 | return SCM_MECAB(obj)->m; |
---|
18 | } |
---|
19 | ScmObj wrap_mecab_t(mecab_t *m) |
---|
20 | { |
---|
21 | ScmMeCab *obj = SCM_NEW(ScmMeCab); |
---|
22 | SCM_SET_CLASS(obj, SCM_CLASS_MECAB); |
---|
23 | obj->m = m; |
---|
24 | return SCM_OBJ(obj); |
---|
25 | } |
---|
26 | |
---|
27 | // mecab_node_t |
---|
28 | const mecab_node_t* unwrap_mecab_node_t(ScmObj obj) |
---|
29 | { |
---|
30 | return SCM_MECAB_NODE(obj)->mn; |
---|
31 | } |
---|
32 | ScmObj wrap_mecab_node_t(const mecab_node_t *mn) |
---|
33 | { |
---|
34 | ScmMeCabNode *obj = SCM_NEW(ScmMeCabNode); |
---|
35 | SCM_SET_CLASS(obj, SCM_CLASS_MECAB_NODE); |
---|
36 | obj->mn = mn; |
---|
37 | return SCM_OBJ(obj); |
---|
38 | } |
---|
39 | |
---|
40 | // mecab_dictionary_info_t |
---|
41 | const mecab_dictionary_info_t* unwrap_mecab_dictionary_info_t(ScmObj obj) |
---|
42 | { |
---|
43 | return SCM_MECAB_DICTIONARY_INFO(obj)->mdi; |
---|
44 | } |
---|
45 | ScmObj wrap_mecab_dictionary_info_t(const mecab_dictionary_info_t *mdi) |
---|
46 | { |
---|
47 | ScmMeCabDictionaryInfo *obj = SCM_NEW(ScmMeCabDictionaryInfo); |
---|
48 | SCM_SET_CLASS(obj, SCM_CLASS_MECAB_DICTIONARY_INFO); |
---|
49 | obj->mdi = mdi; |
---|
50 | return SCM_OBJ(obj); |
---|
51 | } |
---|
52 | |
---|
53 | /* APIs with (int argc, char **argv) */ |
---|
54 | #define mecab_call_func(result_type,fn,args) do{ \ |
---|
55 | result_type result; \ |
---|
56 | if (SCM_NULLP(args)) { \ |
---|
57 | char *argv[] = {""}; \ |
---|
58 | result = (fn)(1,argv); \ |
---|
59 | } else { \ |
---|
60 | int argc, i=0; \ |
---|
61 | ScmObj argl; \ |
---|
62 | char **argv = NULL; \ |
---|
63 | if (SCM_INTEGERP(Scm_Car(args))) { \ |
---|
64 | argc = SCM_INT_VALUE(Scm_Car(args)); argl = Scm_Cadr(args); \ |
---|
65 | } else { \ |
---|
66 | argc = 1 + Scm_Length(args); i++; argl = args; \ |
---|
67 | } \ |
---|
68 | argv = (char**)malloc(sizeof(char*) * argc); \ |
---|
69 | if (i) argv[0] = ""; \ |
---|
70 | if (argv) { \ |
---|
71 | if (SCM_VECTORP(argl)) { \ |
---|
72 | for (;i<argc;i++) \ |
---|
73 | argv[i] = (char *)Scm_GetStringConst((ScmString *)SCM_VECTOR_ELEMENT(argl,i)); \ |
---|
74 | } else { \ |
---|
75 | for (;i<argc && !SCM_NULLP(argl); argl=Scm_Cdr(argl),i++) \ |
---|
76 | argv[i] = (char *)Scm_GetStringConst((ScmString *)Scm_Car(argl)); \ |
---|
77 | } \ |
---|
78 | result = (fn)(argc,argv); \ |
---|
79 | } \ |
---|
80 | free((void *)argv); \ |
---|
81 | } \ |
---|
82 | return result; \ |
---|
83 | } while(0) |
---|
84 | |
---|
85 | mecab_t *mecab_call_mecab_func(mecab_func_with_args fn, ScmObj args) |
---|
86 | { |
---|
87 | mecab_call_func(mecab_t*,fn,args); |
---|
88 | } |
---|
89 | int mecab_call_int_func(int_func_with_args fn, ScmObj args) |
---|
90 | { |
---|
91 | mecab_call_func(int,fn,args); |
---|
92 | } |
---|
93 | |
---|
94 | /* |
---|
95 | * Module initialization function. |
---|
96 | */ |
---|
97 | extern void Scm_Init_mecablib(ScmModule*); |
---|
98 | |
---|
99 | void Scm_Init_mecab(void) |
---|
100 | { |
---|
101 | ScmModule *mod; |
---|
102 | |
---|
103 | /* Register this DSO to Gauche */ |
---|
104 | SCM_INIT_EXTENSION(mecab); |
---|
105 | |
---|
106 | /* Create the module if it doesn't exist yet. */ |
---|
107 | mod = SCM_MODULE(SCM_FIND_MODULE("mecab", TRUE)); |
---|
108 | |
---|
109 | /* Register stub-generated procedures */ |
---|
110 | Scm_Init_mecablib(mod); |
---|
111 | } |
---|