New function mem_iconveh.
[gnulib.git] / lib / striconveh.h
1 /* Character set conversion with error handling.
2    Copyright (C) 2001-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible and Simon Josefsson.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _STRICONVEH_H
20 #define _STRICONVEH_H
21
22 #include <stddef.h>
23 #if HAVE_ICONV
24 #include <iconv.h>
25 #endif
26
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32
33 /* Handling of unconvertible characters.  */
34 enum iconv_ilseq_handler
35 {
36   iconveh_error,                /* return and set errno = EILSEQ */
37   iconveh_question_mark,        /* use one '?' per unconvertible character */
38   iconveh_escape_sequence       /* use escape sequence \uxxxx or \Uxxxxxxxx */
39 };
40
41 #if HAVE_ICONV
42
43 /* Convert an entire string from one encoding to another, using iconv.
44    The original string is at [SRC,...,SRC+SRCLEN-1].
45    The conversion descriptor from FROMCODE to TOCODE is passed as CD.
46    CD1 is the conversion descriptor from FROM_CODESET to UTF-8 (or
47    (iconv_t)(-1) if FROM_CODESET is UTF-8).
48    CD2 is the conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1)
49    if TO_CODESET is UTF-8).
50    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
51    or *RESULTP can initially be NULL.
52    May erase the contents of the memory at *RESULTP.
53    Return value: 0 if successful, otherwise -1 and errno set.
54    If successful: The resulting string is stored in *RESULTP and its length
55    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
56    unchanged if no dynamic memory allocation was necessary.  */
57 extern int
58        mem_cd_iconveh (const char *src, size_t srclen,
59                        iconv_t cd, iconv_t cd1, iconv_t cd2,
60                        enum iconv_ilseq_handler handler,
61                        char **resultp, size_t *lengthp);
62
63 /* Convert an entire string from one encoding to another, using iconv.
64    The original string is the NUL-terminated string starting at SRC.
65    The conversion descriptor is passed as CD.  Both the "from" and the "to"
66    encoding must use a single NUL byte at the end of the string (i.e. not
67    UCS-2, UCS-4, UTF-16, UTF-32).
68    CD1 is the conversion descriptor from FROM_CODESET to UTF-8 (or
69    (iconv_t)(-1) if FROM_CODESET is UTF-8).
70    CD2 is the conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1)
71    if TO_CODESET is UTF-8).
72    Allocate a malloced memory block for the result.
73    Return value: the freshly allocated resulting NUL-terminated string if
74    successful, otherwise NULL and errno set.  */
75 extern char *
76        str_cd_iconveh (const char *src,
77                        iconv_t cd, iconv_t cd1, iconv_t cd2,
78                        enum iconv_ilseq_handler handler);
79
80 #endif
81
82 /* Convert an entire string from one encoding to another, using iconv.
83    The original string is at [SRC,...,SRC+SRCLEN-1].
84    Both the "from" and the "to" encoding must use a single NUL byte at the
85    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
86    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
87    or *RESULTP can initially be NULL.
88    May erase the contents of the memory at *RESULTP.
89    Return value: 0 if successful, otherwise -1 and errno set.
90    If successful: The resulting string is stored in *RESULTP and its length
91    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
92    unchanged if no dynamic memory allocation was necessary.  */
93 extern int
94        mem_iconveh (const char *src, size_t srclen,
95                     const char *from_codeset, const char *to_codeset,
96                     enum iconv_ilseq_handler handler,
97                     char **resultp, size_t *lengthp);
98
99 /* Convert an entire string from one encoding to another, using iconv.
100    The original string is the NUL-terminated string starting at SRC.
101    Both the "from" and the "to" encoding must use a single NUL byte at the
102    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
103    Allocate a malloced memory block for the result.
104    Return value: the freshly allocated resulting NUL-terminated string if
105    successful, otherwise NULL and errno set.  */
106 extern char *
107        str_iconveh (const char *src,
108                     const char *from_codeset, const char *to_codeset,
109                     enum iconv_ilseq_handler handler);
110
111
112 #ifdef __cplusplus
113 }
114 #endif
115
116
117 #endif /* _STRICONVEH_H */