summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2009-04-26 15:31:18 +0200
committerBruno Haible <bruno@clisp.org>2009-04-26 15:31:18 +0200
commit7723389f98b6159511dea9bd60bf41ec99f0ae14 (patch)
tree85c45c416585ebb55d87397625683a33497363b3 /lib
parenta5bd4a560247e63db562041f9d136ba83a8b95fb (diff)
downloadgnulib-7723389f98b6159511dea9bd60bf41ec99f0ae14.tar.gz
Simplify calling convention of u*_conv_to_encoding.
Diffstat (limited to 'lib')
-rw-r--r--lib/unicase/u-casexfrm.h14
-rw-r--r--lib/uniconv.h26
-rw-r--r--lib/uniconv/u-conv-to-enc.h61
-rw-r--r--lib/uniconv/u8-conv-to-enc.c49
-rw-r--r--lib/uninorm/u-normxfrm.h14
-rw-r--r--lib/vasnprintf.c36
6 files changed, 117 insertions, 83 deletions
diff --git a/lib/unicase/u-casexfrm.h b/lib/unicase/u-casexfrm.h
index a7298a9701..e36aff3b9e 100644
--- a/lib/unicase/u-casexfrm.h
+++ b/lib/unicase/u-casexfrm.h
@@ -26,7 +26,6 @@ FUNC (const UNIT *s, size_t n, const char *iso639_language, uninorm_t nf,
char convsbuf[2048];
char *convs;
size_t convs_length;
- int ret;
char *result;
/* Casefold and normalize the Unicode string. */
@@ -37,14 +36,13 @@ FUNC (const UNIT *s, size_t n, const char *iso639_language, uninorm_t nf,
return NULL;
/* Convert it to locale encoding. */
- convs = convsbuf;
convs_length = sizeof (convsbuf) - 1;
- ret = U_CONV_TO_ENCODING (locale_charset (),
- iconveh_error,
- foldeds, foldeds_length,
- NULL,
- &convs, &convs_length);
- if (ret < 0)
+ convs = U_CONV_TO_ENCODING (locale_charset (),
+ iconveh_error,
+ foldeds, foldeds_length,
+ NULL,
+ convsbuf, &convs_length);
+ if (convs == NULL)
{
if (foldeds != foldedsbuf)
{
diff --git a/lib/uniconv.h b/lib/uniconv.h
index 4c565ad4f0..d364202e60 100644
--- a/lib/uniconv.h
+++ b/lib/uniconv.h
@@ -84,32 +84,32 @@ extern int
array is filled with offsets into the result, i.e. the character starting
at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
and other offsets are set to (size_t)(-1).
- *RESULTP and *LENGTHP should initially be a scratch buffer and its size,
- or *RESULTP can initially be NULL.
+ RESULTBUF and *LENGTHP should initially be a scratch buffer and its size,
+ or RESULTBUF can be NULL.
May erase the contents of the memory at *RESULTP.
- Return value: 0 if successful, otherwise -1 and errno set.
- If successful: The resulting string is stored in *RESULTP and its length
- in *LENGTHP. *RESULTP is set to a freshly allocated memory block, or is
- unchanged if no dynamic memory allocation was necessary.
- Particular errno values: EINVAL, EILSEQ, ENOMEM. */
-extern int
+ If successful: The resulting string (non-NULL) is returned and its length
+ stored in *LENGTHP. The resulting string is RESULTBUF if no dynamic memory
+ allocation was necessary, or a freshly allocated memory block otherwise.
+ In case of error: NULL is returned and errno is set. Particular errno
+ values: EINVAL, EILSEQ, ENOMEM. */
+extern char *
u8_conv_to_encoding (const char *tocode,
enum iconv_ilseq_handler handler,
const uint8_t *src, size_t srclen,
size_t *offsets,
- char **resultp, size_t *lengthp);
-extern int
+ char *resultbuf, size_t *lengthp);
+extern char *
u16_conv_to_encoding (const char *tocode,
enum iconv_ilseq_handler handler,
const uint16_t *src, size_t srclen,
size_t *offsets,
- char **resultp, size_t *lengthp);
-extern int
+ char *resultbuf, size_t *lengthp);
+extern char *
u32_conv_to_encoding (const char *tocode,
enum iconv_ilseq_handler handler,
const uint32_t *src, size_t srclen,
size_t *offsets,
- char **resultp, size_t *lengthp);
+ char *resultbuf, size_t *lengthp);
/* Converts a NUL terminated string from a given encoding.
The result is malloc allocated, or NULL (with errno set) in case of error.
diff --git a/lib/uniconv/u-conv-to-enc.h b/lib/uniconv/u-conv-to-enc.h
index 144ea40171..f8840c2577 100644
--- a/lib/uniconv/u-conv-to-enc.h
+++ b/lib/uniconv/u-conv-to-enc.h
@@ -14,15 +14,17 @@
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
+char *
FUNC (const char *tocode,
enum iconv_ilseq_handler handler,
const UNIT *src, size_t srclen,
size_t *offsets,
- char **resultp, size_t *lengthp)
+ char *resultbuf, size_t *lengthp)
{
#if HAVE_UTF_NAME
size_t *scaled_offsets;
+ char *result;
+ size_t length;
int retval;
if (offsets != NULL && srclen > 0)
@@ -32,42 +34,59 @@ FUNC (const char *tocode,
if (scaled_offsets == NULL)
{
errno = ENOMEM;
- return -1;
+ return NULL;
}
}
else
scaled_offsets = NULL;
+ result = resultbuf;
+ length = *lengthp;
retval = mem_iconveha ((const char *) src, srclen * sizeof (UNIT),
UTF_NAME, tocode,
handler == iconveh_question_mark, handler,
- scaled_offsets, resultp, lengthp);
+ scaled_offsets, &result, &length);
+ if (retval < 0)
+ {
+ int saved_errno = errno;
+ free (scaled_offsets);
+ errno = saved_errno;
+ return NULL;
+ }
if (offsets != NULL)
{
- if (retval >= 0)
- {
- /* Convert scaled_offsets[srclen * sizeof (UNIT)] to
- offsets[srclen]. */
- size_t i;
+ /* Convert scaled_offsets[srclen * sizeof (UNIT)] to
+ offsets[srclen]. */
+ size_t i;
- for (i = 0; i < srclen; i++)
- offsets[i] = scaled_offsets[i * sizeof (UNIT)];
- }
+ for (i = 0; i < srclen; i++)
+ offsets[i] = scaled_offsets[i * sizeof (UNIT)];
free (scaled_offsets);
}
- return retval;
+
+ if (result == NULL) /* when (resultbuf == NULL && length == 0) */
+ {
+ result = (char *) malloc (1);
+ if (result == NULL)
+ {
+ errno = ENOMEM;
+ return NULL;
+ }
+ }
+ *lengthp = length;
+ return result;
#else
uint8_t tmpbuf[4096];
size_t tmpbufsize = SIZEOF (tmpbuf);
uint8_t *utf8_src;
size_t utf8_srclen;
size_t *scaled_offsets;
- int retval;
+ char *result;
utf8_src = U_TO_U8 (src, srclen, tmpbuf, &tmpbufsize);
if (utf8_src == NULL)
- return -1;
+ return NULL;
utf8_srclen = tmpbufsize;
if (offsets != NULL && utf8_srclen > 0)
@@ -78,22 +97,22 @@ FUNC (const char *tocode,
if (utf8_src != tmpbuf)
free (utf8_src);
errno = ENOMEM;
- return -1;
+ return NULL;
}
}
else
scaled_offsets = NULL;
- retval = u8_conv_to_encoding (tocode, handler, utf8_src, utf8_srclen,
- scaled_offsets, resultp, lengthp);
- if (retval < 0)
+ result = u8_conv_to_encoding (tocode, handler, utf8_src, utf8_srclen,
+ scaled_offsets, resultbuf, lengthp);
+ if (result == NULL)
{
int saved_errno = errno;
free (scaled_offsets);
if (utf8_src != tmpbuf)
free (utf8_src);
errno = saved_errno;
- return -1;
+ return NULL;
}
if (offsets != NULL)
{
@@ -134,6 +153,6 @@ FUNC (const char *tocode,
}
if (utf8_src != tmpbuf)
free (utf8_src);
- return retval;
+ return result;
#endif
}
diff --git a/lib/uniconv/u8-conv-to-enc.c b/lib/uniconv/u8-conv-to-enc.c
index 85404d4be9..5aec163a11 100644
--- a/lib/uniconv/u8-conv-to-enc.c
+++ b/lib/uniconv/u8-conv-to-enc.c
@@ -29,47 +29,66 @@
#include "striconveha.h"
#include "unistr.h"
-int
+char *
u8_conv_to_encoding (const char *tocode,
enum iconv_ilseq_handler handler,
const uint8_t *src, size_t srclen,
size_t *offsets,
- char **resultp, size_t *lengthp)
+ char *resultbuf, size_t *lengthp)
{
- char *result;
-
if (STRCASEEQ (tocode, "UTF-8", 'U','T','F','-','8',0,0,0,0))
{
+ char *result;
+
/* Conversion from UTF-8 to UTF-8. No need to go through iconv(). */
#if CONFIG_UNICODE_SAFETY
if (u8_check (src, srclen))
{
errno = EILSEQ;
- return -1;
+ return NULL;
}
#endif
/* Memory allocation. */
- if ((*resultp != NULL && *lengthp >= srclen) || srclen == 0)
- result = *resultp;
+ if (resultbuf != NULL && *lengthp >= srclen)
+ result = resultbuf;
else
{
- result = (char *) malloc (srclen);
+ result = (char *) malloc (srclen > 0 ? srclen : 1);
if (result == NULL)
{
errno = ENOMEM;
- return -1;
+ return NULL;
}
}
memcpy (result, (const char *) src, srclen);
- *resultp = result;
*lengthp = srclen;
- return 0;
+ return result;
}
else
- return mem_iconveha ((const char *) src, srclen,
- "UTF-8", tocode,
- handler == iconveh_question_mark, handler,
- offsets, resultp, lengthp);
+ {
+ char *result = resultbuf;
+ size_t length = *lengthp;
+ int retval =
+ mem_iconveha ((const char *) src, srclen,
+ "UTF-8", tocode,
+ handler == iconveh_question_mark, handler,
+ offsets, &result, &length);
+
+ if (retval < 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 result;
+ }
}
diff --git a/lib/uninorm/u-normxfrm.h b/lib/uninorm/u-normxfrm.h
index 60036b1058..6ed1e3c360 100644
--- a/lib/uninorm/u-normxfrm.h
+++ b/lib/uninorm/u-normxfrm.h
@@ -25,7 +25,6 @@ FUNC (const UNIT *s, size_t n, uninorm_t nf,
char convsbuf[2048];
char *convs;
size_t convs_length;
- int ret;
char *result;
/* Normalize the Unicode string. */
@@ -36,14 +35,13 @@ FUNC (const UNIT *s, size_t n, uninorm_t nf,
return NULL;
/* Convert it to locale encoding. */
- convs = convsbuf;
convs_length = sizeof (convsbuf) - 1;
- ret = U_CONV_TO_ENCODING (locale_charset (),
- iconveh_error,
- norms, norms_length,
- NULL,
- &convs, &convs_length);
- if (ret < 0)
+ convs = U_CONV_TO_ENCODING (locale_charset (),
+ iconveh_error,
+ norms, norms_length,
+ NULL,
+ convsbuf, &convs_length);
+ if (convs == NULL)
{
if (norms != normsbuf)
{
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index ce4e44b7b3..51375eb4e5 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -1799,18 +1799,18 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
size_t converted_len = allocated - length;
# if DCHAR_IS_TCHAR
/* Convert from UTF-8 to locale encoding. */
- if (u8_conv_to_encoding (locale_charset (),
- iconveh_question_mark,
- arg, arg_end - arg, NULL,
- &converted, &converted_len)
- < 0)
+ converted =
+ u8_conv_to_encoding (locale_charset (),
+ iconveh_question_mark,
+ arg, arg_end - arg, NULL,
+ converted, &converted_len);
# else
/* Convert from UTF-8 to UTF-16/UTF-32. */
converted =
U8_TO_DCHAR (arg, arg_end - arg,
converted, &converted_len);
- if (converted == NULL)
# endif
+ if (converted == NULL)
{
int saved_errno = errno;
if (!(result == resultbuf || result == NULL))
@@ -1927,18 +1927,18 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
size_t converted_len = allocated - length;
# if DCHAR_IS_TCHAR
/* Convert from UTF-16 to locale encoding. */
- if (u16_conv_to_encoding (locale_charset (),
- iconveh_question_mark,
- arg, arg_end - arg, NULL,
- &converted, &converted_len)
- < 0)
+ converted =
+ u16_conv_to_encoding (locale_charset (),
+ iconveh_question_mark,
+ arg, arg_end - arg, NULL,
+ converted, &converted_len);
# else
/* Convert from UTF-16 to UTF-8/UTF-32. */
converted =
U16_TO_DCHAR (arg, arg_end - arg,
converted, &converted_len);
- if (converted == NULL)
# endif
+ if (converted == NULL)
{
int saved_errno = errno;
if (!(result == resultbuf || result == NULL))
@@ -2055,18 +2055,18 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
size_t converted_len = allocated - length;
# if DCHAR_IS_TCHAR
/* Convert from UTF-32 to locale encoding. */
- if (u32_conv_to_encoding (locale_charset (),
- iconveh_question_mark,
- arg, arg_end - arg, NULL,
- &converted, &converted_len)
- < 0)
+ converted =
+ u32_conv_to_encoding (locale_charset (),
+ iconveh_question_mark,
+ arg, arg_end - arg, NULL,
+ converted, &converted_len);
# else
/* Convert from UTF-32 to UTF-8/UTF-16. */
converted =
U32_TO_DCHAR (arg, arg_end - arg,
converted, &converted_len);
- if (converted == NULL)
# endif
+ if (converted == NULL)
{
int saved_errno = errno;
if (!(result == resultbuf || result == NULL))