3 2009.6.2 Remove WISH_TRUE, use get_guessed_code() for nkf-2.0.9
4 by SATOH Fumiyasu (fumiyas @ osstech co jp)
5 2008.7.17 Change the type of strlen from long to int, by SATOH Fumiyasu.
6 2007.2.1 Add guess() function.
7 2007.1.13 Remove pynkf_parseopts(), by SATOH Fumiyasu.
9 /** Python Interface to NKF
10 ***************************************************************************
11 ** Copyright (c) 2005 Matsumoto, Tadashi <ma2@city.plala.jp>
12 ** All Rights Reserved.
14 ** Everyone is permitted to do anything on this program
15 ** including copying, modifying, improving,
16 ** as long as you don't try to pretend that you wrote it.
17 ** i.e., the above copyright notice has to appear in all copies.
18 ** Binary distribution requires original version messages.
19 ** You don't have to ask before copying, redistribution or publishing.
20 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
21 ***************************************************************************/
23 #define PY_SSIZE_T_CLEAN
30 #define getc(f) pynkf_getc(f)
31 #define ungetc(c,f) pynkf_ungetc(c,f)
36 #define putchar(c) pynkf_putchar(c)
38 static Py_ssize_t pynkf_ibufsize, pynkf_obufsize;
39 static unsigned char *pynkf_inbuf, *pynkf_outbuf;
40 static int pynkf_icount,pynkf_ocount;
41 static unsigned char *pynkf_iptr, *pynkf_optr;
43 static int pynkf_guess_flag;
49 if (pynkf_icount >= pynkf_ibufsize) return EOF;
56 pynkf_ungetc(int c, FILE *f)
70 if (pynkf_guess_flag) {
77 size = pynkf_obufsize + pynkf_obufsize;
78 p = (unsigned char *)PyMem_Realloc(pynkf_outbuf, size + 1);
79 if (pynkf_outbuf == NULL){ longjmp(env, 1); }
81 pynkf_optr = pynkf_outbuf + pynkf_obufsize;
82 pynkf_ocount = pynkf_obufsize;
83 pynkf_obufsize = size;
90 #include "../utf8tbl.c"
94 pynkf_convert(unsigned char* str, Py_ssize_t strlen, char* opts, Py_ssize_t optslen)
98 pynkf_ibufsize = strlen + 1;
99 pynkf_obufsize = pynkf_ibufsize * 1.5 + 256;
100 pynkf_outbuf = (unsigned char *)PyMem_Malloc(pynkf_obufsize);
101 if (pynkf_outbuf == NULL){
105 pynkf_outbuf[0] = '\0';
106 pynkf_ocount = pynkf_obufsize;
107 pynkf_optr = pynkf_outbuf;
110 pynkf_iptr = pynkf_inbuf;
111 pynkf_guess_flag = 0;
113 if (setjmp(env) == 0){
122 PyMem_Free(pynkf_outbuf);
128 res = PyBytes_FromString(pynkf_outbuf);
129 PyMem_Free(pynkf_outbuf);
134 pynkf_convert_guess(unsigned char* str, int strlen)
137 const char *codename;
139 pynkf_ibufsize = strlen + 1;
142 pynkf_iptr = pynkf_inbuf;
144 pynkf_guess_flag = 1;
150 codename = get_guessed_code();
152 res = PyUnicode_FromString(codename);
159 PyObject *pynkf_nkf(PyObject *self, PyObject *args)
167 if (!PyArg_ParseTuple(args, "s#s#", &opts, &optslen, &str, &strlen)) {
170 res = pynkf_convert(str, strlen, opts, optslen);
177 PyObject *pynkf_guess(PyObject *self, PyObject *args)
183 if (!PyArg_ParseTuple(args, "s#", &str, &strlen)) {
186 res = pynkf_convert_guess(str, strlen);
193 {"nkf", pynkf_nkf, METH_VARARGS, ""},
194 {"guess", pynkf_guess, METH_VARARGS, ""},
195 {NULL, NULL, 0, NULL}
198 static struct PyModuleDef nkfmodule = {
199 PyModuleDef_HEAD_INIT,
206 /* Module initialization function */
210 return PyModule_Create(&nkfmodule);