/Modules/cjkcodecs/multibytecodec.h

http://unladen-swallow.googlecode.com/ · C Header · 138 lines · 113 code · 19 blank · 6 comment · 6 complexity · 1c1b11d03bb48e7ed6da4bd39f605284 MD5 · raw file

  1. /*
  2. * multibytecodec.h: Common Multibyte Codec Implementation
  3. *
  4. * Written by Hye-Shik Chang <perky@FreeBSD.org>
  5. */
  6. #ifndef _PYTHON_MULTIBYTECODEC_H_
  7. #define _PYTHON_MULTIBYTECODEC_H_
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #ifdef uint32_t
  12. typedef uint32_t ucs4_t;
  13. #else
  14. typedef unsigned int ucs4_t;
  15. #endif
  16. #ifdef uint16_t
  17. typedef uint16_t ucs2_t, DBCHAR;
  18. #else
  19. typedef unsigned short ucs2_t, DBCHAR;
  20. #endif
  21. typedef union {
  22. void *p;
  23. int i;
  24. unsigned char c[8];
  25. ucs2_t u2[4];
  26. ucs4_t u4[2];
  27. } MultibyteCodec_State;
  28. typedef int (*mbcodec_init)(const void *config);
  29. typedef Py_ssize_t (*mbencode_func)(MultibyteCodec_State *state,
  30. const void *config,
  31. const Py_UNICODE **inbuf, Py_ssize_t inleft,
  32. unsigned char **outbuf, Py_ssize_t outleft,
  33. int flags);
  34. typedef int (*mbencodeinit_func)(MultibyteCodec_State *state,
  35. const void *config);
  36. typedef Py_ssize_t (*mbencodereset_func)(MultibyteCodec_State *state,
  37. const void *config,
  38. unsigned char **outbuf, Py_ssize_t outleft);
  39. typedef Py_ssize_t (*mbdecode_func)(MultibyteCodec_State *state,
  40. const void *config,
  41. const unsigned char **inbuf, Py_ssize_t inleft,
  42. Py_UNICODE **outbuf, Py_ssize_t outleft);
  43. typedef int (*mbdecodeinit_func)(MultibyteCodec_State *state,
  44. const void *config);
  45. typedef Py_ssize_t (*mbdecodereset_func)(MultibyteCodec_State *state,
  46. const void *config);
  47. typedef struct {
  48. const char *encoding;
  49. const void *config;
  50. mbcodec_init codecinit;
  51. mbencode_func encode;
  52. mbencodeinit_func encinit;
  53. mbencodereset_func encreset;
  54. mbdecode_func decode;
  55. mbdecodeinit_func decinit;
  56. mbdecodereset_func decreset;
  57. } MultibyteCodec;
  58. typedef struct {
  59. PyObject_HEAD
  60. MultibyteCodec *codec;
  61. } MultibyteCodecObject;
  62. #define MultibyteCodec_Check(op) ((op)->ob_type == &MultibyteCodec_Type)
  63. #define _MultibyteStatefulCodec_HEAD \
  64. PyObject_HEAD \
  65. MultibyteCodec *codec; \
  66. MultibyteCodec_State state; \
  67. PyObject *errors;
  68. typedef struct {
  69. _MultibyteStatefulCodec_HEAD
  70. } MultibyteStatefulCodecContext;
  71. #define MAXENCPENDING 2
  72. #define _MultibyteStatefulEncoder_HEAD \
  73. _MultibyteStatefulCodec_HEAD \
  74. Py_UNICODE pending[MAXENCPENDING]; \
  75. Py_ssize_t pendingsize;
  76. typedef struct {
  77. _MultibyteStatefulEncoder_HEAD
  78. } MultibyteStatefulEncoderContext;
  79. #define MAXDECPENDING 8
  80. #define _MultibyteStatefulDecoder_HEAD \
  81. _MultibyteStatefulCodec_HEAD \
  82. unsigned char pending[MAXDECPENDING]; \
  83. Py_ssize_t pendingsize;
  84. typedef struct {
  85. _MultibyteStatefulDecoder_HEAD
  86. } MultibyteStatefulDecoderContext;
  87. typedef struct {
  88. _MultibyteStatefulEncoder_HEAD
  89. } MultibyteIncrementalEncoderObject;
  90. typedef struct {
  91. _MultibyteStatefulDecoder_HEAD
  92. } MultibyteIncrementalDecoderObject;
  93. typedef struct {
  94. _MultibyteStatefulDecoder_HEAD
  95. PyObject *stream;
  96. } MultibyteStreamReaderObject;
  97. typedef struct {
  98. _MultibyteStatefulEncoder_HEAD
  99. PyObject *stream;
  100. } MultibyteStreamWriterObject;
  101. /* positive values for illegal sequences */
  102. #define MBERR_TOOSMALL (-1) /* insufficient output buffer space */
  103. #define MBERR_TOOFEW (-2) /* incomplete input buffer */
  104. #define MBERR_INTERNAL (-3) /* internal runtime error */
  105. #define ERROR_STRICT (PyObject *)(1)
  106. #define ERROR_IGNORE (PyObject *)(2)
  107. #define ERROR_REPLACE (PyObject *)(3)
  108. #define ERROR_ISCUSTOM(p) ((p) < ERROR_STRICT || ERROR_REPLACE < (p))
  109. #define ERROR_DECREF(p) do { \
  110. if (p != NULL && ERROR_ISCUSTOM(p)) { \
  111. Py_DECREF(p); \
  112. } \
  113. } while (0);
  114. #define MBENC_FLUSH 0x0001 /* encode all characters encodable */
  115. #define MBENC_MAX MBENC_FLUSH
  116. #ifdef __cplusplus
  117. }
  118. #endif
  119. #endif