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