diff options
author | Bruno Haible <bruno@clisp.org> | 2009-04-26 16:43:25 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2009-04-26 16:43:25 +0200 |
commit | d4ca6455614829e611b5488421bb4462990e50ad (patch) | |
tree | f702824d7283147cf7c0d659c11777ff29c33e0a /lib/uniconv | |
parent | b16d2ef530105fa88aebd525b63551cab0ef87b5 (diff) | |
download | gnulib-d4ca6455614829e611b5488421bb4462990e50ad.tar.gz |
Simplify calling convention of u*_conv_from_encoding.
Diffstat (limited to 'lib/uniconv')
-rw-r--r-- | lib/uniconv/u-conv-from-enc.h | 37 | ||||
-rw-r--r-- | lib/uniconv/u-strconv-from-enc.h | 14 | ||||
-rw-r--r-- | lib/uniconv/u8-conv-from-enc.c | 39 |
3 files changed, 53 insertions, 37 deletions
diff --git a/lib/uniconv/u-conv-from-enc.h b/lib/uniconv/u-conv-from-enc.h index 4fc566d330..fa0f0d3dcb 100644 --- a/lib/uniconv/u-conv-from-enc.h +++ b/lib/uniconv/u-conv-from-enc.h @@ -1,5 +1,5 @@ /* Conversion to UTF-16/UTF-32 from legacy encodings. - Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2002, 2006-2007, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -14,19 +14,20 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -int +UNIT * FUNC (const char *fromcode, enum iconv_ilseq_handler handler, const char *src, size_t srclen, size_t *offsets, - UNIT **resultp, size_t *lengthp) + UNIT *resultbuf, size_t *lengthp) { #if HAVE_UTF_NAME - size_t length; + char *result = (char *) resultbuf; + size_t length = *lengthp * sizeof (UNIT); if (mem_iconveha (src, srclen, fromcode, UTF_NAME, true, handler, - offsets, (char **) resultp, &length) < 0) - return -1; + offsets, &result, &length) < 0) + return NULL; if (offsets != NULL) { /* Convert 'char *' offsets to 'UNIT *' offsets. */ @@ -40,27 +41,24 @@ FUNC (const char *fromcode, if ((length % sizeof (UNIT)) != 0) abort (); *lengthp = length / sizeof (UNIT); - return 0; + return (UNIT *) result; #else - uint8_t *utf8_string = NULL; - size_t utf8_length = 0; + uint8_t *utf8_string; + size_t utf8_length; UNIT *result; - if (u8_conv_from_encoding (fromcode, handler, src, srclen, offsets, - &utf8_string, &utf8_length) < 0) - return -1; + utf8_string = + u8_conv_from_encoding (fromcode, handler, src, srclen, offsets, + NULL, &utf8_length); if (utf8_string == NULL) - { - *lengthp = 0; - return 0; - } - result = U8_TO_U (utf8_string, utf8_length, *resultp, lengthp); + return NULL; + result = U8_TO_U (utf8_string, utf8_length, resultbuf, lengthp); if (result == NULL) { int saved_errno = errno; free (utf8_string); errno = saved_errno; - return -1; + return NULL; } if (offsets != NULL) { @@ -88,7 +86,6 @@ FUNC (const char *fromcode, } } free (utf8_string); - *resultp = result; - return 0; + return result; #endif } diff --git a/lib/uniconv/u-strconv-from-enc.h b/lib/uniconv/u-strconv-from-enc.h index 19d8b3e6fc..25813c76dc 100644 --- a/lib/uniconv/u-strconv-from-enc.h +++ b/lib/uniconv/u-strconv-from-enc.h @@ -1,5 +1,5 @@ /* Conversion to UTF-8/UTF-16/UTF-32 from legacy encodings. - Copyright (C) 2002, 2006-2007 Free Software Foundation, Inc. + Copyright (C) 2002, 2006-2007, 2009 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published @@ -19,12 +19,14 @@ FUNC (const char *string, const char *fromcode, enum iconv_ilseq_handler handler) { - UNIT *result = NULL; - size_t length = 0; + UNIT *result; + size_t length; - if (U_CONV_FROM_ENCODING (fromcode, handler, - string, strlen (string) + 1, NULL, - &result, &length) < 0) + result = + U_CONV_FROM_ENCODING (fromcode, handler, + string, strlen (string) + 1, NULL, + NULL, &length); + if (result == NULL) return NULL; /* Verify the result has exactly one NUL unit, at the end. */ if (!(length > 0 && result[length-1] == 0 diff --git a/lib/uniconv/u8-conv-from-enc.c b/lib/uniconv/u8-conv-from-enc.c index 09eba1e519..7605e63e75 100644 --- a/lib/uniconv/u8-conv-from-enc.c +++ b/lib/uniconv/u8-conv-from-enc.c @@ -29,12 +29,12 @@ #include "striconveha.h" #include "unistr.h" -int +uint8_t * u8_conv_from_encoding (const char *fromcode, enum iconv_ilseq_handler handler, const char *src, size_t srclen, size_t *offsets, - uint8_t **resultp, size_t *lengthp) + uint8_t *resultbuf, size_t *lengthp) { if (STRCASEEQ (fromcode, "UTF-8", 'U','T','F','-','8',0,0,0,0)) { @@ -44,7 +44,7 @@ u8_conv_from_encoding (const char *fromcode, if (u8_check ((const uint8_t *) src, srclen)) { errno = EILSEQ; - return -1; + return NULL; } if (offsets != NULL) @@ -65,24 +65,41 @@ u8_conv_from_encoding (const char *fromcode, } /* Memory allocation. */ - if ((*resultp != NULL && *lengthp >= srclen) || srclen == 0) - result = *resultp; + if (resultbuf != NULL && *lengthp >= srclen) + result = resultbuf; else { - result = (uint8_t *) malloc (srclen); + result = (uint8_t *) malloc (srclen > 0 ? srclen : 1); if (result == NULL) { errno = ENOMEM; - return -1; + return NULL; } } memcpy ((char *) result, src, srclen); - *resultp = result; *lengthp = srclen; - return 0; + return result; } else - return mem_iconveha (src, srclen, fromcode, "UTF-8", true, handler, - offsets, (char **) resultp, lengthp); + { + char *result = (char *) resultbuf; + size_t length = *lengthp; + + if (mem_iconveha (src, srclen, fromcode, "UTF-8", true, handler, + offsets, &result, &length) < 0) + return NULL; + + if (result == NULL) /* when (resultbuf == NULL && length == 0) */ + { + result = (char *) malloc (1); + if (result == NULL) + { + errno = ENOMEM; + return NULL; + } + } + *lengthp = length; + return (uint8_t *) result; + } } |