Do an indirect conversion if iconv_open does not support a direct conversion.
[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    CD is the conversion descriptor from FROMCODE to TOCODE, or (iconv_t)(-1) if
46    the system does not support a direct conversion from FROMCODE to TOCODE.
47    CD1 is the conversion descriptor from FROM_CODESET to UTF-8 (or
48    (iconv_t)(-1) if FROM_CODESET is UTF-8).
49    CD2 is the conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1)
50    if TO_CODESET is UTF-8).
51    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
52    array is filled with offsets into the result, i.e. the character starting
53    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
54    and other offsets are set to (size_t)(-1).
55    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
56    or *RESULTP can initially be NULL.
57    May erase the contents of the memory at *RESULTP.
58    Return value: 0 if successful, otherwise -1 and errno set.
59    If successful: The resulting string is stored in *RESULTP and its length
60    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
61    unchanged if no dynamic memory allocation was necessary.  */
62 extern int
63        mem_cd_iconveh (const char *src, size_t srclen,
64                        iconv_t cd, iconv_t cd1, iconv_t cd2,
65                        enum iconv_ilseq_handler handler,
66                        size_t *offsets,
67                        char **resultp, size_t *lengthp);
68
69 /* Convert an entire string from one encoding to another, using iconv.
70    The original string is the NUL-terminated string starting at SRC.
71    CD is the conversion descriptor from FROMCODE to TOCODE, or (iconv_t)(-1) if
72    the system does not support a direct conversion from FROMCODE to TOCODE.
73    Both the "from" and the "to" encoding must use a single NUL byte at the end
74    of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
75    CD1 is the conversion descriptor from FROM_CODESET to UTF-8 (or
76    (iconv_t)(-1) if FROM_CODESET is UTF-8).
77    CD2 is the conversion descriptor from UTF-8 to TO_CODESET (or (iconv_t)(-1)
78    if TO_CODESET is UTF-8).
79    Allocate a malloced memory block for the result.
80    Return value: the freshly allocated resulting NUL-terminated string if
81    successful, otherwise NULL and errno set.  */
82 extern char *
83        str_cd_iconveh (const char *src,
84                        iconv_t cd, iconv_t cd1, iconv_t cd2,
85                        enum iconv_ilseq_handler handler);
86
87 #endif
88
89 /* Convert an entire string from one encoding to another, using iconv.
90    The original string is at [SRC,...,SRC+SRCLEN-1].
91    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
92    array is filled with offsets into the result, i.e. the character starting
93    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
94    and other offsets are set to (size_t)(-1).
95    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
96    or *RESULTP can initially be NULL.
97    May erase the contents of the memory at *RESULTP.
98    Return value: 0 if successful, otherwise -1 and errno set.
99    If successful: The resulting string is stored in *RESULTP and its length
100    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
101    unchanged if no dynamic memory allocation was necessary.  */
102 extern int
103        mem_iconveh (const char *src, size_t srclen,
104                     const char *from_codeset, const char *to_codeset,
105                     enum iconv_ilseq_handler handler,
106                     size_t *offsets,
107                     char **resultp, size_t *lengthp);
108
109 /* Convert an entire string from one encoding to another, using iconv.
110    The original string is the NUL-terminated string starting at SRC.
111    Both the "from" and the "to" encoding must use a single NUL byte at the
112    end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
113    Allocate a malloced memory block for the result.
114    Return value: the freshly allocated resulting NUL-terminated string if
115    successful, otherwise NULL and errno set.  */
116 extern char *
117        str_iconveh (const char *src,
118                     const char *from_codeset, const char *to_codeset,
119                     enum iconv_ilseq_handler handler);
120
121
122 #ifdef __cplusplus
123 }
124 #endif
125
126
127 #endif /* _STRICONVEH_H */