Move getline and getdelim into stdio.h, per POSIX 200x.
[gnulib.git] / lib / uniconv.h
1 /* Conversions between Unicode and legacy encodings.
2    Copyright (C) 2002, 2005, 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU Library General Public License as published
6    by the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17    USA.  */
18
19 #ifndef _UNICONV_H
20 #define _UNICONV_H
21
22 /* Get size_t.  */
23 #include <stddef.h>
24
25 #include "unitypes.h"
26
27 /* Get enum iconv_ilseq_handler.  */
28 #include "striconveh.h"
29
30 /* Get iconv_register_autodetect(), mem_iconveha() declaration.  */
31 #include "striconveha.h"
32
33 /* Get locale_charset() declaration.  */
34 #include "localcharset.h"
35
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41
42 /* Converts an entire string, possibly including NUL bytes, from one encoding
43    to a Unicode encoding.
44    Converts a memory region given in encoding FROMCODE.  FROMCODE is as for
45    iconv_open(3).
46    The input is in the memory region between SRC (inclusive) and SRC + SRCLEN
47    (exclusive).
48    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
49    array is filled with offsets into the result, i.e. the character starting
50    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
51    and other offsets are set to (size_t)(-1).
52    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
53    or *RESULTP can initially be NULL.
54    May erase the contents of the memory at *RESULTP.
55    Return value: 0 if successful, otherwise -1 and errno set.
56    If successful: The resulting string is stored in *RESULTP and its length
57    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
58    unchanged if no dynamic memory allocation was necessary.
59    Particular errno values: EINVAL, EILSEQ, ENOMEM.  */
60 extern int
61        u8_conv_from_encoding (const char *fromcode,
62                               enum iconv_ilseq_handler handler,
63                               const char *src, size_t srclen,
64                               size_t *offsets,
65                               uint8_t **resultp, size_t *lengthp);
66 extern int
67        u16_conv_from_encoding (const char *fromcode,
68                                enum iconv_ilseq_handler handler,
69                                const char *src, size_t srclen,
70                                size_t *offsets,
71                                uint16_t **resultp, size_t *lengthp);
72 extern int
73        u32_conv_from_encoding (const char *fromcode,
74                                enum iconv_ilseq_handler handler,
75                                const char *src, size_t srclen,
76                                size_t *offsets,
77                                uint32_t **resultp, size_t *lengthp);
78
79 /* Converts an entire Unicode string, possibly including NUL units, from a
80    Unicode encoding to a given encoding.
81    Converts a memory region to encoding TOCODE.  TOCODE is as for
82    iconv_open(3).
83    The input is in the memory region between SRC (inclusive) and SRC + SRCLEN
84    (exclusive).
85    If OFFSETS is not NULL, it should point to an array of SRCLEN integers; this
86    array is filled with offsets into the result, i.e. the character starting
87    at SRC[i] corresponds to the character starting at (*RESULTP)[OFFSETS[i]],
88    and other offsets are set to (size_t)(-1).
89    *RESULTP and *LENGTH should initially be a scratch buffer and its size,
90    or *RESULTP can initially be NULL.
91    May erase the contents of the memory at *RESULTP.
92    Return value: 0 if successful, otherwise -1 and errno set.
93    If successful: The resulting string is stored in *RESULTP and its length
94    in *LENGTHP.  *RESULTP is set to a freshly allocated memory block, or is
95    unchanged if no dynamic memory allocation was necessary.
96    Particular errno values: EINVAL, EILSEQ, ENOMEM.  */
97 extern int
98        u8_conv_to_encoding (const char *tocode,
99                             enum iconv_ilseq_handler handler,
100                             const uint8_t *src, size_t srclen,
101                             size_t *offsets,
102                             char **resultp, size_t *lengthp);
103 extern int
104        u16_conv_to_encoding (const char *tocode,
105                              enum iconv_ilseq_handler handler,
106                              const uint16_t *src, size_t srclen,
107                              size_t *offsets,
108                              char **resultp, size_t *lengthp);
109 extern int
110        u32_conv_to_encoding (const char *tocode,
111                              enum iconv_ilseq_handler handler,
112                              const uint32_t *src, size_t srclen,
113                              size_t *offsets,
114                              char **resultp, size_t *lengthp);
115
116 /* Converts a NUL terminated string from a given encoding.
117    The result is malloc allocated, or NULL (with errno set) in case of error.
118    Particular errno values: EILSEQ, ENOMEM.  */
119 extern uint8_t *
120        u8_strconv_from_encoding (const char *string,
121                                  const char *fromcode,
122                                  enum iconv_ilseq_handler handler);
123 extern uint16_t *
124        u16_strconv_from_encoding (const char *string,
125                                   const char *fromcode,
126                                   enum iconv_ilseq_handler handler);
127 extern uint32_t *
128        u32_strconv_from_encoding (const char *string,
129                                   const char *fromcode,
130                                   enum iconv_ilseq_handler handler);
131
132 /* Converts a NUL terminated string to a given encoding.
133    The result is malloc allocated, or NULL (with errno set) in case of error.
134    Particular errno values: EILSEQ, ENOMEM.  */
135 extern char *
136        u8_strconv_to_encoding (const uint8_t *string,
137                                const char *tocode,
138                                enum iconv_ilseq_handler handler);
139 extern char *
140        u16_strconv_to_encoding (const uint16_t *string,
141                                 const char *tocode,
142                                 enum iconv_ilseq_handler handler);
143 extern char *
144        u32_strconv_to_encoding (const uint32_t *string,
145                                 const char *tocode,
146                                 enum iconv_ilseq_handler handler);
147
148 /* Converts a NUL terminated string from the locale encoding.
149    The result is malloc allocated, or NULL (with errno set) in case of error.
150    Particular errno values: ENOMEM.  */
151 extern uint8_t *
152        u8_strconv_from_locale (const char *string);
153 extern uint16_t *
154        u16_strconv_from_locale (const char *string);
155 extern uint32_t *
156        u32_strconv_from_locale (const char *string);
157
158 /* Converts a NUL terminated string to the locale encoding.
159    The result is malloc allocated, or NULL (with errno set) in case of error.
160    Particular errno values: ENOMEM.  */
161 extern char *
162        u8_strconv_to_locale (const uint8_t *string);
163 extern char *
164        u16_strconv_to_locale (const uint16_t *string);
165 extern char *
166        u32_strconv_to_locale (const uint32_t *string);
167
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173 #endif /* _UNICONV_H */