Hye-Shik Chang | 3e2a306 | 2004-01-17 14:29:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * multibytecodec.h: Common Multibyte Codec Implementation |
| 3 | * |
| 4 | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
| 5 | * $CJKCodecs: multibytecodec.h,v 1.2 2003/12/31 05:46:55 perky Exp $ |
| 6 | */ |
| 7 | |
| 8 | #ifndef _PYTHON_MULTIBYTECODEC_H_ |
| 9 | #define _PYTHON_MULTIBYTECODEC_H_ |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
| 14 | #include "cjkcommon.h" |
| 15 | |
| 16 | typedef union { |
| 17 | void *p; |
| 18 | int i; |
| 19 | unsigned char c[8]; |
| 20 | ucs2_t u2[4]; |
| 21 | ucs4_t u4[2]; |
| 22 | } MultibyteCodec_State; |
| 23 | |
| 24 | typedef int (*mbencode_func)(MultibyteCodec_State *state, |
| 25 | const Py_UNICODE **inbuf, size_t inleft, |
| 26 | unsigned char **outbuf, size_t outleft, |
| 27 | int flags); |
| 28 | typedef int (*mbencodeinit_func)(MultibyteCodec_State *state); |
| 29 | typedef int (*mbencodereset_func)(MultibyteCodec_State *state, |
| 30 | unsigned char **outbuf, size_t outleft); |
| 31 | typedef int (*mbdecode_func)(MultibyteCodec_State *state, |
| 32 | const unsigned char **inbuf, size_t inleft, |
| 33 | Py_UNICODE **outbuf, size_t outleft); |
| 34 | typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state); |
| 35 | typedef int (*mbdecodereset_func)(MultibyteCodec_State *state); |
| 36 | |
| 37 | typedef struct { |
| 38 | const char *encoding; |
| 39 | mbencode_func encode; |
| 40 | mbencodeinit_func encinit; |
| 41 | mbencodereset_func encreset; |
| 42 | mbdecode_func decode; |
| 43 | mbdecodeinit_func decinit; |
| 44 | mbdecodereset_func decreset; |
| 45 | } MultibyteCodec; |
| 46 | |
| 47 | typedef struct { |
| 48 | PyObject_HEAD |
| 49 | MultibyteCodec *codec; |
| 50 | } MultibyteCodecObject; |
| 51 | |
| 52 | #define MAXDECPENDING 8 |
| 53 | typedef struct { |
| 54 | PyObject_HEAD |
| 55 | MultibyteCodec *codec; |
| 56 | MultibyteCodec_State state; |
| 57 | unsigned char pending[MAXDECPENDING]; |
| 58 | int pendingsize; |
| 59 | PyObject *stream, *errors; |
| 60 | } MultibyteStreamReaderObject; |
| 61 | |
| 62 | #define MAXENCPENDING 2 |
| 63 | typedef struct { |
| 64 | PyObject_HEAD |
| 65 | MultibyteCodec *codec; |
| 66 | MultibyteCodec_State state; |
| 67 | Py_UNICODE pending[MAXENCPENDING]; |
| 68 | int pendingsize; |
| 69 | PyObject *stream, *errors; |
| 70 | } MultibyteStreamWriterObject; |
| 71 | |
| 72 | /* positive values for illegal sequences */ |
| 73 | #define MBERR_TOOSMALL (-1) /* insufficient output buffer space */ |
| 74 | #define MBERR_TOOFEW (-2) /* incomplete input buffer */ |
| 75 | #define MBERR_INTERNAL (-3) /* internal runtime error */ |
| 76 | |
| 77 | #define ERROR_STRICT (PyObject *)(1) |
| 78 | #define ERROR_IGNORE (PyObject *)(2) |
| 79 | #define ERROR_REPLACE (PyObject *)(3) |
| 80 | #define ERROR_MAX ERROR_REPLACE |
| 81 | |
| 82 | #define MBENC_FLUSH 0x0001 /* encode all characters encodable */ |
| 83 | #define MBENC_MAX MBENC_FLUSH |
| 84 | |
| 85 | #ifdef __cplusplus |
| 86 | } |
| 87 | #endif |
| 88 | #endif |