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 ***************************************************************************/
28 #define getc(f) pynkf_getc(f)
29 #define ungetc(c,f) pynkf_ungetc(c,f)
34 #define putchar(c) pynkf_putchar(c)
36 static int pynkf_ibufsize, pynkf_obufsize;
37 static unsigned char *pynkf_inbuf, *pynkf_outbuf;
38 static int pynkf_icount,pynkf_ocount;
39 static unsigned char *pynkf_iptr, *pynkf_optr;
41 static int pynkf_guess_flag;
47 if (pynkf_icount >= pynkf_ibufsize) return EOF;
54 pynkf_ungetc(int c, FILE *f)
68 if (pynkf_guess_flag) {
75 size = pynkf_obufsize + pynkf_obufsize;
76 p = (unsigned char *)PyMem_Realloc(pynkf_outbuf, size + 1);
77 if (pynkf_outbuf == NULL){ longjmp(env, 1); }
79 pynkf_optr = pynkf_outbuf + pynkf_obufsize;
80 pynkf_ocount = pynkf_obufsize;
81 pynkf_obufsize = size;
88 #include "../utf8tbl.c"
92 pynkf_convert(unsigned char* str, int strlen, char* opts, int optslen)
96 pynkf_ibufsize = strlen + 1;
97 pynkf_obufsize = pynkf_ibufsize * 1.5 + 256;
98 pynkf_outbuf = (unsigned char *)PyMem_Malloc(pynkf_obufsize);
99 if (pynkf_outbuf == NULL){
103 pynkf_outbuf[0] = '\0';
104 pynkf_ocount = pynkf_obufsize;
105 pynkf_optr = pynkf_outbuf;
108 pynkf_iptr = pynkf_inbuf;
109 pynkf_guess_flag = 0;
111 if (setjmp(env) == 0){
120 PyMem_Free(pynkf_outbuf);
126 res = PyBytes_FromString(pynkf_outbuf);
127 PyMem_Free(pynkf_outbuf);
132 pynkf_convert_guess(unsigned char* str, int strlen)
135 const char *codename;
137 pynkf_ibufsize = strlen + 1;
140 pynkf_iptr = pynkf_inbuf;
142 pynkf_guess_flag = 1;
148 codename = get_guessed_code();
150 res = PyUnicode_FromString(codename);
157 PyObject *pynkf_nkf(PyObject *self, PyObject *args)
165 if (!PyArg_ParseTuple(args, "s#y#", &opts, &optslen, &str, &strlen)) {
168 res = pynkf_convert(str, strlen, opts, optslen);
175 PyObject *pynkf_guess(PyObject *self, PyObject *args)
181 if (!PyArg_ParseTuple(args, "y#", &str, &strlen)) {
184 res = pynkf_convert_guess(str, strlen);
191 {"nkf", pynkf_nkf, METH_VARARGS, ""},
192 {"guess", pynkf_guess, METH_VARARGS, ""},
193 {NULL, NULL, 0, NULL}
196 static struct PyModuleDef nkfmodule = {
197 PyModuleDef_HEAD_INIT,
204 /* Module initialization function */
208 return PyModule_Create(&nkfmodule);