- Timestamp:
- 11/06/10 03:15:51 (14 years ago)
- Location:
- trunk/pymigemo
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/pymigemo/README.ja_JP.UTF-8
r30 r34 108 108 * ãªã 109 109 * è¿ãå€ 110 * çåœå€ (int): æ£åžžã«èªã¿èŸŒããŠããå Žåã«ç110 * çåœå€ : æ£åžžã«èªã¿èŸŒããŠããå Žåã«ç 111 111 * load(dict_id, dict_file) 112 112 * Migemo ãªããžã§ã¯ãã«èŸæžãè¿œå ã§èªã¿èŸŒã … … 131 131 * op: ã¡ã¿æåå (str) 132 132 * è¿ãå€ 133 * çåœå€ (int): æåæã«ç133 * çåœå€ : æåæã«ç 134 134 135 135 -
trunk/pymigemo/pymigemo.c
r30 r34 9 9 #include <structmember.h> 10 10 #include <migemo.h> 11 #include <stdbool.h> 11 12 #include <string.h> 12 13 13 #define PYMIGEMO_VERSION "0. 2"14 #define PYMIGEMO_VERSION "0.3" 14 15 15 16 /* for dereference migemo object members */ … … 32 33 } Migemo; 33 34 34 static void 35 Migemo_dealloc(Migemo *self) 36 { 37 if (self->migemo_obj) { 38 migemo_close(self->migemo_obj); 39 } 40 41 self->ob_type->tp_free((PyObject *)self); 42 } 43 44 static PyObject * 45 Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 46 { 47 Migemo *self; 48 49 self = (Migemo *)type->tp_alloc(type, 0); 50 51 if (self != NULL) { 52 self->migemo_obj = NULL; 53 } 54 55 return (PyObject *)self; 56 } 57 58 static int 59 Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) 60 { 61 migemo *migemo_obj; 62 char *dictionary; 63 64 static char *kwlist[] = {"dictionary", NULL}; 65 66 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) { 67 return -1; 68 } 69 70 if (dictionary) { 71 if (self->migemo_obj) { 72 migemo_close(self->migemo_obj); 73 } 74 75 migemo_obj = migemo_open(dictionary); 76 77 if (migemo_obj) { 78 self->migemo_obj = migemo_obj; 79 } 80 else { 81 return -1; 82 } 83 } 84 85 return 0; 86 } 87 88 static int 35 static bool 89 36 get_encoding(char *encoding, size_t size, int charset) 90 37 { … … 107 54 if (strlen(enc) < size) { 108 55 strcpy(encoding, enc); 109 return 1; 56 return true; 57 } 58 59 return false; 60 } 61 62 static void 63 Migemo_dealloc(Migemo *self) 64 { 65 if (self->migemo_obj) { 66 migemo_close(self->migemo_obj); 67 } 68 69 self->ob_type->tp_free((PyObject *)self); 70 } 71 72 static PyObject * 73 Migemo_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 74 { 75 Migemo *self; 76 77 self = (Migemo *)type->tp_alloc(type, 0); 78 79 if (self != NULL) { 80 self->migemo_obj = NULL; 81 } 82 83 return (PyObject *)self; 84 } 85 86 static int 87 Migemo_init(Migemo *self, PyObject *args, PyObject *kwds) 88 { 89 migemo *migemo_obj; 90 char *dictionary; 91 92 static char *kwlist[] = {"dictionary", NULL}; 93 94 if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dictionary)) { 95 return -1; 96 } 97 98 if (dictionary) { 99 if (self->migemo_obj) { 100 migemo_close(self->migemo_obj); 101 } 102 103 migemo_obj = migemo_open(dictionary); 104 105 if (migemo_obj) { 106 self->migemo_obj = migemo_obj; 107 } 108 else { 109 PyErr_SetString(PyExc_AssertionError, "migemo_open() failed"); 110 return -1; 111 } 110 112 } 111 113 … … 119 121 120 122 if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 123 PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 121 124 return NULL; 122 125 } … … 128 131 Migemo_query(Migemo *self, PyObject *args, PyObject *kwds) 129 132 { 130 PyObject *result, * query_obj, *query_str = NULL, *regex_strobj = NULL;133 PyObject *result, *pyquery, *pyrestr; 131 134 char *query, encoding[7]; 132 135 unsigned char *regex; … … 134 137 static char *kwlist[] = {"query", NULL}; 135 138 136 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, & query_obj)) {139 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, &pyquery)) { 137 140 return NULL; 138 141 } 139 142 140 143 if (!get_encoding(encoding, sizeof(encoding), self->migemo_obj->charset)) { 141 return NULL; 142 } 143 144 if (PyUnicode_Check(query_obj)) { 145 query_str = PyUnicode_AsEncodedString(query_obj, encoding, "strict"); 146 query = PyString_AS_STRING(query_str); 147 } 148 else if (PyString_Check(query_obj)) { 149 query = PyString_AS_STRING(query_obj); 144 PyErr_SetString(PyExc_AssertionError, "get_encoding() failed"); 145 return NULL; 146 } 147 148 if (PyUnicode_Check(pyquery)) { 149 PyObject *q = PyUnicode_AsEncodedString(pyquery, encoding, "strict"); 150 151 if (q == NULL) { 152 return NULL; 153 } 154 155 query = PyString_AS_STRING(q); 156 Py_DECREF(q); 157 } 158 else if (PyString_Check(pyquery)) { 159 query = PyString_AS_STRING(pyquery); 150 160 } 151 161 else { 152 return NULL; 153 } 154 155 if (query) { 156 regex = migemo_query(self->migemo_obj, query); 157 158 if (regex) { 159 regex_strobj = PyString_FromString(regex); 160 161 if (regex_strobj) { 162 result = PyUnicode_FromEncodedObject(regex_strobj, encoding, "strict"); 163 } 164 165 migemo_release(self->migemo_obj, regex); 166 } 167 } 168 169 Py_XDECREF(regex_strobj); 170 Py_XDECREF(query_str); 171 172 if (!result) { 173 return NULL; 174 } 175 162 PyErr_SetString(PyExc_ValueError, "argument must be string"); 163 return NULL; 164 } 165 if (query == NULL) { 166 return NULL; 167 } 168 169 regex = migemo_query(self->migemo_obj, query); 170 if (regex == NULL) { 171 PyErr_SetString(PyExc_AssertionError, "migemo_query() failed"); 172 return NULL; 173 } 174 175 pyrestr = PyString_FromString(regex); 176 migemo_release(self->migemo_obj, regex); 177 if (pyrestr == NULL) { 178 return NULL; 179 } 180 181 result = PyUnicode_FromEncodedObject(pyrestr, encoding, "strict"); 182 Py_DECREF(pyrestr); 176 183 return result; 177 184 } … … 191 198 192 199 if (op) { 193 result = Py Int_FromLong((long)migemo_set_operator(self->migemo_obj, index, op));200 result = PyBool_FromLong((long)migemo_set_operator(self->migemo_obj, index, op)); 194 201 } 195 202 … … 204 211 Migemo_get_operator(Migemo *self, PyObject *args, PyObject *kwds) 205 212 { 206 PyObject *result;207 unsigned char *op;208 int index;213 PyObject *result; 214 const unsigned char *op; 215 int index; 209 216 210 217 static char *kwlist[] = {"index", NULL}; … … 252 259 Migemo_is_enable(Migemo *self) 253 260 { 254 return Py Int_FromLong((long)migemo_is_enable(self->migemo_obj));261 return PyBool_FromLong((long)migemo_is_enable(self->migemo_obj)); 255 262 } 256 263
Note: See TracChangeset
for help on using the changeset viewer.