34c21fa0ecaf6c34e0c9f0d2476e532494c63627
[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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 #ifndef _STRICONVEH_H
19 #define _STRICONVEH_H
20
21 #include <stddef.h>
22 #if HAVE_ICONV
23 #include <iconv.h>
24 #endif
25
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31
32 /* Handling of unconvertible characters.  */
33 enum iconv_ilseq_handler
34 {
35   iconveh_error,                /* return and set errno = EILSEQ */
36   iconveh_question_mark,        /* use one '?' per unconvertible character */
37   iconveh_escape_sequence       /* use escape sequence \uxxxx or \Uxxxxxxxx */
38 };
39
40 #if HAVE_ICONV
41
42 /* Convert an entire string from one encoding to another, using iconv.
43    The original string is at [SRC,...,SRC+SRCLEN-1].
44    CD is the conversion descriptor from FROMCODE to TOCODE, or (iconv_t)(-1) if
45    the system does not support a direct conversion from FROMCODE to TOCODE.
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    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
51    array is filled with offsets into the result, i.e. the character starting
52    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
53    and other offsets are set to (size_t)(-1).
54    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
55    or *RESULTP can initially be NULL.
56    May erase the contents of the memory at *RESULTP.
57    Return value: 0 if successful, otherwise -1 and errno set.
58    If successful: The resulting string is stored in *RESULTP and its length
59    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
60    unchanged if no dynamic memory allocation was necessary.  */
61 extern int
62        mem_cd_iconveh (const char *src, size_t srclen,
63                        iconv_t cd, iconv_t cd1, iconv_t cd2,
64                        enum iconv_ilseq_handler handler,
65                        size_t *offsets,
66                        char **resultp, size_t *lengthp);
67
68 /* Convert an entire string from one encoding to another, using iconv.
69    The original string is the NUL-terminated string starting at SRC.
70    CD is the conversion descriptor from FROMCODE to TOCODE, or (iconv_t)(-1) if
71    the system does not support a direct conversion from FROMCODE to TOCODE.
72    Both the "from" and the "to" encoding must use a single NUL byte at the end
73    of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
74    CD1 is the conversion descriptor from FROM_CODESET to UTF-8 (or
75    (iconv_t)(-1) if FROM_CODESET is UTF-8).
76    CD2 is the conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1)
77    if TO_CODESET is UTF-8).
78    Allocate a malloced memory block for the result.
79    Return value: the freshly allocated resulting NUL-terminated string if
80    successful, otherwise NULL and errno set.  */
81 extern char *
82        str_cd_iconveh (const char *src,
83                        iconv_t cd, iconv_t cd1, iconv_t cd2,
84                        enum iconv_ilseq_handler handler);
85
86 #endif
87
88 /* Convert an entire string from one encoding to another, using iconv.
89    The original string is at [SRC,...,SRC+SRCLEN-1].
90    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
91    array is filled with offsets into the result, i.e. the character starting
92    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
93    and other offsets are set to (size_t)(-1).
94    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
95    or *RESULTP can initially be NULL.
96    May erase the contents of the memory at *RESULTP.
97    Return value: 0 if successful, otherwise -1 and errno set.
98    If successful: The resulting string is stored in *RESULTP and its length
99    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
100    unchanged if no dynamic memory allocation was necessary.  */
101 extern int
102        mem_iconveh (const char *src, size_t srclen,
103                     const char *from_codeset, const char *to_codeset,
104                     enum iconv_ilseq_handler handler,
105                     size_t *offsets,
106                     char **resultp, size_t *lengthp);
107
108 /* Convert an entire string from one encoding to another, using iconv.
109    The original string is the NUL-terminated string starting at SRC.
110    Both the "from" and the "to" encoding must use a single NUL byte at the
111    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
112    Allocate a malloced memory block for the result.
113    Return value: the freshly allocated resulting NUL-terminated string if
114    successful, otherwise NULL and errno set.  */
115 extern char *
116        str_iconveh (const char *src,
117                     const char *from_codeset, const char *to_codeset,
118                     enum iconv_ilseq_handler handler);
119
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125
126 #endif /* _STRICONVEH_H */