openat: don’t close (-1)
[gnulib.git] / lib / xstriconv.h
blobd8abc2032e938827f44f835f5c609ba4c0955684
1 /* Charset conversion with out-of-memory checking.
2 Copyright (C) 2001-2004, 2006-2007, 2009-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible and Simon Josefsson.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _XSTRICONV_H
19 #define _XSTRICONV_H
21 /* This file uses _GL_ATTRIBUTE_MALLOC, HAVE_ICONV. */
22 #if !_GL_CONFIG_H_INCLUDED
23 #error "Please include config.h first."
24 #endif
26 #include <stdlib.h>
27 #if HAVE_ICONV
28 #include <iconv.h>
29 #endif
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
37 #if HAVE_ICONV
39 /* Convert an entire string from one encoding to another, using iconv.
40 The original string is at [SRC,...,SRC+SRCLEN-1].
41 The conversion descriptor is passed as CD.
42 *RESULTP and *LENGTH should initially be a scratch buffer and its size,
43 or *RESULTP can initially be NULL.
44 May erase the contents of the memory at *RESULTP.
45 Upon memory allocation failure, report the error and exit.
46 Return value: 0 if successful, otherwise -1 and errno set.
47 If successful: The resulting string is stored in *RESULTP and its length
48 in *LENGTHP. *RESULTP is set to a freshly allocated memory block, or is
49 unchanged if no dynamic memory allocation was necessary. */
50 extern int xmem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
51 char **resultp, size_t *lengthp);
53 /* Convert an entire string from one encoding to another, using iconv.
54 The original string is the NUL-terminated string starting at SRC.
55 The conversion descriptor is passed as CD. Both the "from" and the "to"
56 encoding must use a single NUL byte at the end of the string (i.e. not
57 UCS-2, UCS-4, UTF-16, UTF-32).
58 Allocate a malloced memory block for the result.
59 Upon memory allocation failure, report the error and exit.
60 Return value: the freshly allocated resulting NUL-terminated string if
61 successful, otherwise NULL and errno set. */
62 extern char * xstr_cd_iconv (const char *src, iconv_t cd)
63 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
65 #endif
67 /* Convert an entire string from one encoding to another, using iconv.
68 The original string is the NUL-terminated string starting at SRC.
69 Both the "from" and the "to" encoding must use a single NUL byte at the
70 end of the string (i.e. not UCS-2, UCS-4, UTF-16, UTF-32).
71 Allocate a malloced memory block for the result.
72 Upon memory allocation failure, report the error and exit.
73 Return value: the freshly allocated resulting NUL-terminated string if
74 successful, otherwise NULL and errno set. */
75 extern char * xstr_iconv (const char *src,
76 const char *from_codeset, const char *to_codeset)
77 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
80 #ifdef __cplusplus
82 #endif
85 #endif /* _XSTRICONV_H */