[KoCo-CVS] [Commit] iconvcodec _iconv_codec.c
Brought to you by:
perky
From: Hye-Shik C. <pe...@us...> - 2003-06-16 19:12:46
|
perky 03/06/16 12:12:41 Modified: . _iconv_codec.c Log: Minor code clean ups Revision Changes Path 1.15 +43 -49 iconvcodec/_iconv_codec.c Index: _iconv_codec.c =================================================================== RCS file: /cvsroot/koco/iconvcodec/_iconv_codec.c,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- _iconv_codec.c 12 Jun 2003 05:51:33 -0000 1.14 +++ _iconv_codec.c 16 Jun 2003 19:12:41 -0000 1.15 @@ -24,7 +24,7 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * $Id: _iconv_codec.c,v 1.14 2003/06/12 05:51:33 perky Exp $ + * $Id: _iconv_codec.c,v 1.15 2003/06/16 19:12:41 perky Exp $ */ #include "Python.h" @@ -127,10 +127,10 @@ const char *unicode_encoding; int unitype; size_t (*iconvwrap)( - iconv_t cd, iconv_arg2_t inbuf, size_t * inbytesleft, - char* *outbuf, size_t * outbytesleft); + iconv_t cd, iconv_arg2_t inbuf, size_t *inbytesleft, + char **outbuf, size_t *outbytesleft); size_t (*iconvwrap2)(iconv_t cd, IconvDecoderBuffer *buf, - int inleft, int outleft); + size_t inleft, size_t outleft); /* an alternative wrapper: for utf8 backend */ } IconvDecoderObject; @@ -187,9 +187,6 @@ { PyObject *v, *w; - if (unicode == NULL) - return NULL; - v = PyTuple_New(2); if (v == NULL) { Py_DECREF(unicode); @@ -225,7 +222,7 @@ } #define UTF8NEXTCHAR(p) \ - if (*(p) < 128) (p)++; \ + if (*(p) < 0x80) (p)++; \ else if (*(p) < 0xe0) (p) += 2; \ else if (*(p) < 0xf0) (p) += 3; \ else if (*(p) < 0xf8) (p) += 4; \ @@ -374,7 +371,7 @@ #if Py_UNICODE_SIZE == 2 "'%s' codec can't encode byte '\\u%04x' in position %d: %s", #else - "'%s' codec can't encode byte '\\u%08lx' in position %d: %s", + "'%s' codec can't encode byte '\\U%08lx' in position %d: %s", #endif self->encoding, *buf->inbuf, start, reason); else @@ -391,14 +388,11 @@ start, end, reason); if (buf->excobj == NULL) goto errorexit; - } else { - if (PyUnicodeEncodeError_SetStart(buf->excobj, start) != 0) - goto errorexit; - if (PyUnicodeEncodeError_SetEnd(buf->excobj, end) != 0) - goto errorexit; - if (PyUnicodeEncodeError_SetReason(buf->excobj, reason) != 0) - goto errorexit; - } + } else + if (PyUnicodeEncodeError_SetStart(buf->excobj, start) || + PyUnicodeEncodeError_SetEnd(buf->excobj, end) || + PyUnicodeEncodeError_SetReason(buf->excobj, reason)) + goto errorexit; if (errors == ERROR_STRICT) { PyCodec_StrictErrors(buf->excobj); @@ -739,9 +733,9 @@ iconvencoder_makestream(IconvEncoderObject *self, PyObject *args, PyObject *kwargs) { - static char *stream_kwarglist[] = {"stream", "errors", NULL}; - PyObject *stream; - char *errors = NULL; + static char *stream_kwarglist[] = {"stream", "errors", NULL}; + PyObject *stream; + char *errors = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|s:makestream", stream_kwarglist, &stream, &errors)) @@ -820,7 +814,7 @@ static int expand_decodebuffer(IconvDecoderBuffer *buf, int esize) { - int orgpos, orgsize; + int orgpos, orgsize; orgpos = (int)(buf->outbuf - PyUnicode_AS_UNICODE(buf->outobj)); orgsize = PyUnicode_GET_SIZE(buf->outobj); @@ -906,14 +900,11 @@ (size_t)(buf->inbuf_end - buf->inbuf_top), start, end, reason); if (buf->excobj == NULL) goto errorexit; - } else { - if (PyUnicodeDecodeError_SetStart(buf->excobj, start) != 0) - goto errorexit; - if (PyUnicodeDecodeError_SetEnd(buf->excobj, end) != 0) - goto errorexit; - if (PyUnicodeDecodeError_SetReason(buf->excobj, reason) != 0) - goto errorexit; - } + } else + if (PyUnicodeDecodeError_SetStart(buf->excobj, start) || + PyUnicodeDecodeError_SetEnd(buf->excobj, end) || + PyUnicodeDecodeError_SetReason(buf->excobj, reason)) + goto errorexit; if (errors == ERROR_STRICT) { PyCodec_StrictErrors(buf->excobj); @@ -983,7 +974,8 @@ } static size_t -iconvwrap_utf8(iconv_t ic, IconvDecoderBuffer *buf, int inleft, int outleft) +iconvwrap_utf8(iconv_t ic, IconvDecoderBuffer *buf, + size_t inleft, size_t outleft) { unsigned char *ubuf, *ubuf_top, *ubuf_end; size_t r; @@ -994,7 +986,7 @@ return -1; ubuf_top = ubuf; - if (inleft != -1) + if (inleft > 0) r = iconv(ic, (iconv_arg2_t)&(buf->inbuf), &inleft, (char **)&ubuf, &outleft); else @@ -1100,7 +1092,7 @@ iconvdecoder_conv(IconvDecoderObject *self, iconv_t ic, IconvDecoderBuffer *buf, PyObject *errors) { - size_t r, inleft, outleft; + size_t r, inleft, outleft; for (;;) { inleft = (size_t)(buf->inbuf_end - buf->inbuf); @@ -1133,7 +1125,7 @@ iconv_t ic, IconvDecoderBuffer *buf, PyObject *errors) { - size_t r, outleft; + size_t r, outleft; if (buf->inbuf < buf->inbuf_end) /* assumes as left by EINVAL */ if (iconvdecoder_error(self, ic, buf, errors, EINVAL, @@ -1147,7 +1139,7 @@ r = self->iconvwrap(ic, NULL, NULL, (char **)&(buf->outbuf), &outleft); else - r = self->iconvwrap2(ic, buf, -1, outleft); + r = self->iconvwrap2(ic, buf, 0, outleft); if (r == (size_t)-1) { if (errno == E2BIG) { @@ -1242,9 +1234,9 @@ iconvdecoder_makestream(IconvDecoderObject *self, PyObject *args, PyObject *kwargs) { - static char *stream_kwarglist[] = {"stream", "errors", NULL}; - PyObject *stream; - char *errors = NULL; + static char *stream_kwarglist[] = {"stream", "errors", NULL}; + PyObject *stream; + char *errors = NULL; if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|s:makestream", stream_kwarglist, &stream, &errors)) @@ -1325,8 +1317,8 @@ const char *method, int sizehint) { IconvDecoderBuffer buf; - PyObject *cres; - int rsize, r, finalsize = 0; + PyObject *cres; + int rsize, r, finalsize = 0; if (sizehint == 0) return PyUnicode_FromUnicode(NULL, 0); @@ -1848,9 +1840,9 @@ iconvcodec_makeencoder(PyObject *spam, PyObject *args) { IconvEncoderObject *self; - iconv_t ic; - char *encoding; - int i; + iconv_t ic; + char *encoding; + int i; if (!PyArg_ParseTuple(args, "s:makeencoder", &encoding)) return NULL; @@ -1898,9 +1890,9 @@ iconvcodec_makedecoder(PyObject *spam, PyObject *args) { IconvDecoderObject *self; - iconv_t ic; - char *encoding; - int i; + iconv_t ic; + char *encoding; + int i; if (!PyArg_ParseTuple(args, "s:makedecoder", &encoding)) return NULL; @@ -1930,9 +1922,11 @@ self->unitype = um[i].type; switch (self->unitype) { case UNIINTERNAL_UCS: - self->iconvwrap = iconv; break; + self->iconvwrap = iconv; + break; case UNIINTERNAL_UCS_SWAPPED: - self->iconvwrap = iconvwrap_ucsswapped; break; + self->iconvwrap = iconvwrap_ucsswapped; + break; case UNIINTERNAL_UTF_8: self->iconvwrap = NULL; self->iconvwrap2= iconvwrap_utf8; @@ -1968,8 +1962,8 @@ static void detect_iconv_endian(void) { - iconv_t ic; - int i; + iconv_t ic; + int i; #define um uniinternal_modes for (i = 0; um[i].encoding[0]; i++) { |