|
| 1 | +/* Copyright (C) 1999-2023 Free Software Foundation, Inc. |
| 2 | + This file is part of the cppp-reiconv library. |
| 3 | + |
| 4 | + The cppp-reiconv library is free software; you can redistribute it |
| 5 | + and/or modify it under the terms of the GNU Lesser General Public |
| 6 | + License as published by the Free Software Foundation; either version 2.1 |
| 7 | + of the License, or (at your option) any later version. |
| 8 | + |
| 9 | + The cppp-reiconv library is distributed in the hope that it will be |
| 10 | + useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | + Lesser General Public License for more details. |
| 13 | + |
| 14 | + You should have received a copy of the GNU Lesser General Public |
| 15 | + License along with the cppp-reiconv library; see the file COPYING. |
| 16 | + If not, see <https://www.gnu.org/licenses/>. */ |
| 17 | + |
| 18 | +/* When installed, this file is called "cppp/reiconv.hpp". */ |
| 19 | + |
| 20 | +#ifndef _CPPP_REICONV_HPP |
| 21 | +#define _CPPP_REICONV_HPP |
| 22 | + |
| 23 | +#include <stddef.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <time.h> |
| 26 | +#include <stddef.h> |
| 27 | +#include <errno.h> |
| 28 | +#include <wchar.h> |
| 29 | + |
| 30 | +extern "C++" |
| 31 | +{ |
| 32 | + namespace cppp{namespace base{namespace reiconv |
| 33 | + { |
| 34 | + extern @DLL_VARIABLE@ int reiconv_version; /* Likewise */ |
| 35 | + |
| 36 | + /* Define iconv_t ourselves. */ |
| 37 | + #undef iconv_t |
| 38 | + typedef void* iconv_t; |
| 39 | + |
| 40 | + /* Allocates descriptor for code conversion from encoding ‘fromcode’ to |
| 41 | + encoding ‘tocode’. */ |
| 42 | + extern @DLL_VARIABLE@ iconv_t iconv_open (const char* tocode, const char* fromcode); |
| 43 | + |
| 44 | + /* Converts, using conversion descriptor ‘cd’, at most ‘*inbytesleft’ bytes |
| 45 | + starting at ‘*inbuf’, writing at most ‘*outbytesleft’ bytes starting at |
| 46 | + ‘*outbuf’. |
| 47 | + Decrements ‘*inbytesleft’ and increments ‘*inbuf’ by the same amount. |
| 48 | + Decrements ‘*outbytesleft’ and increments ‘*outbuf’ by the same amount. */ |
| 49 | + extern @DLL_VARIABLE@ size_t iconv (iconv_t cd, char* * inbuf, size_t *inbytesleft, char* * outbuf, size_t *outbytesleft); |
| 50 | + |
| 51 | + /* Frees resources allocated for conversion descriptor ‘cd’. */ |
| 52 | + extern @DLL_VARIABLE@ int iconv_close (iconv_t cd); |
| 53 | + |
| 54 | + /* A type that holds all memory needed by a conversion descriptor. |
| 55 | + A pointer to such an object can be used as an iconv_t. */ |
| 56 | + typedef struct |
| 57 | + { |
| 58 | + void* dummy1[28]; |
| 59 | + mbstate_t dummy2; |
| 60 | + } iconv_allocation_t; |
| 61 | + |
| 62 | + /* Allocates descriptor for code conversion from encoding ‘fromcode’ to |
| 63 | + encoding ‘tocode’ into preallocated memory. Returns an error indicator |
| 64 | + (0 or -1 with errno set). */ |
| 65 | + extern @DLL_VARIABLE@ int iconv_open_into (const char* tocode, const char* fromcode, |
| 66 | + iconv_allocation_t* resultp); |
| 67 | + |
| 68 | + /* Control of attributes. */ |
| 69 | + extern @DLL_VARIABLE@ int iconvctl (iconv_t cd, int request, void* argument); |
| 70 | + |
| 71 | + /* Hook performed after every successful conversion of a Unicode character. */ |
| 72 | + typedef void (*iconv_unicode_char_hook) (unsigned int uc, void* data); |
| 73 | + /* Hook performed after every successful conversion of a wide character. */ |
| 74 | + typedef void (*iconv_wide_char_hook) (wchar_t wc, void* data); |
| 75 | + /* Set of hooks. */ |
| 76 | + struct iconv_hooks |
| 77 | + { |
| 78 | + iconv_unicode_char_hook uc_hook; |
| 79 | + iconv_wide_char_hook wc_hook; |
| 80 | + void* data; |
| 81 | + }; |
| 82 | + |
| 83 | + /* Fallback function. Invoked when a small number of bytes could not be |
| 84 | + converted to a Unicode character. This function should process all |
| 85 | + bytes from inbuf and may produce replacement Unicode characters by calling |
| 86 | + the write_replacement callback repeatedly. */ |
| 87 | + typedef void (*iconv_unicode_mb_to_uc_fallback) |
| 88 | + (const char* inbuf, size_t inbufsize, |
| 89 | + void (*write_replacement) (const unsigned int *buf, size_t buflen, |
| 90 | + void* callback_arg), |
| 91 | + void* callback_arg, |
| 92 | + void* data); |
| 93 | + |
| 94 | + /* Fallback function. Invoked when a Unicode character could not be converted |
| 95 | + to the target encoding. This function should process the character and |
| 96 | + may produce replacement bytes (in the target encoding) by calling the |
| 97 | + write_replacement callback repeatedly. */ |
| 98 | + typedef void (*iconv_unicode_uc_to_mb_fallback) |
| 99 | + (unsigned int code, |
| 100 | + void (*write_replacement) (const char *buf, size_t buflen, |
| 101 | + void* callback_arg), |
| 102 | + void* callback_arg, |
| 103 | + void* data); |
| 104 | + |
| 105 | + /* Fallback function. Invoked when a number of bytes could not be converted to |
| 106 | + a wide character. This function should process all bytes from inbuf and may |
| 107 | + produce replacement wide characters by calling the write_replacement |
| 108 | + callback repeatedly. */ |
| 109 | + typedef void (*iconv_wchar_mb_to_wc_fallback) |
| 110 | + (const char* inbuf, size_t inbufsize, |
| 111 | + void (*write_replacement) (const wchar_t *buf, size_t buflen, |
| 112 | + void* callback_arg), |
| 113 | + void* callback_arg, |
| 114 | + void* data); |
| 115 | + |
| 116 | + /* Fallback function. Invoked when a wide character could not be converted to |
| 117 | + the target encoding. This function should process the character and may |
| 118 | + produce replacement bytes (in the target encoding) by calling the |
| 119 | + write_replacement callback repeatedly. */ |
| 120 | + typedef void (*iconv_wchar_wc_to_mb_fallback) |
| 121 | + (wchar_t code, |
| 122 | + void (*write_replacement) (const char *buf, size_t buflen, |
| 123 | + void* callback_arg), |
| 124 | + void* callback_arg, |
| 125 | + void* data); |
| 126 | + |
| 127 | + /* Set of fallbacks. */ |
| 128 | + struct iconv_fallbacks |
| 129 | + { |
| 130 | + iconv_unicode_mb_to_uc_fallback mb_to_uc_fallback; |
| 131 | + iconv_unicode_uc_to_mb_fallback uc_to_mb_fallback; |
| 132 | + iconv_wchar_mb_to_wc_fallback mb_to_wc_fallback; |
| 133 | + iconv_wchar_wc_to_mb_fallback wc_to_mb_fallback; |
| 134 | + void* data; |
| 135 | + }; |
| 136 | + |
| 137 | + /* Surfaces. |
| 138 | + The concept of surfaces is described in the 'recode' manual. */ |
| 139 | + #define ICONV_SURFACE_NONE 0 |
| 140 | + /* In EBCDIC encodings, 0x15 (which encodes the "newline function", see the |
| 141 | + Unicode standard, chapter 5) maps to U+000A instead of U+0085. This is |
| 142 | + for interoperability with C programs and Unix environments on z/OS. */ |
| 143 | + #define ICONV_SURFACE_EBCDIC_ZOS_UNIX 1 |
| 144 | + |
| 145 | + /* Requests for iconvctl. */ |
| 146 | + #define ICONV_TRIVIALP 0 /* int *argument */ |
| 147 | + #define ICONV_GET_TRANSLITERATE 1 /* int *argument */ |
| 148 | + #define ICONV_SET_TRANSLITERATE 2 /* const int *argument */ |
| 149 | + #define ICONV_GET_DISCARD_ILSEQ 3 /* int *argument */ |
| 150 | + #define ICONV_SET_DISCARD_ILSEQ 4 /* const int *argument */ |
| 151 | + #define ICONV_SET_HOOKS 5 /* const struct iconv_hooks *argument */ |
| 152 | + #define ICONV_SET_FALLBACKS 6 /* const struct iconv_fallbacks *argument */ |
| 153 | + #define ICONV_GET_FROM_SURFACE 7 /* unsigned int *argument */ |
| 154 | + #define ICONV_SET_FROM_SURFACE 8 /* const unsigned int *argument */ |
| 155 | + #define ICONV_GET_TO_SURFACE 9 /* unsigned int *argument */ |
| 156 | + #define ICONV_SET_TO_SURFACE 10 /* const unsigned int *argument */ |
| 157 | + |
| 158 | + /* Listing of locale independent encodings. */ |
| 159 | + extern @DLL_VARIABLE@ void iconvlist (int (*do_one) (unsigned int namescount, |
| 160 | + const char * const * names, |
| 161 | + void* data), |
| 162 | + void* data); |
| 163 | + |
| 164 | + /* Canonicalize an encoding name. |
| 165 | + The result is either a canonical encoding name, or name itself. */ |
| 166 | + extern @DLL_VARIABLE@ const char * iconv_canonicalize (const char * name); |
| 167 | + |
| 168 | + /* |
| 169 | + * This C function converts an entire string from one encoding to another, |
| 170 | + * using iconv. Easier to use than iconv() itself, and supports autodetect |
| 171 | + * encodings on input. |
| 172 | + * |
| 173 | + * int iconv_string (const char* tocode, const char* fromcode, |
| 174 | + * const char* start, const char* end, |
| 175 | + * char** resultp, size_t* lengthp) |
| 176 | + * |
| 177 | + * Converts a memory region given in encoding FROMCODE to a new memory |
| 178 | + * region in encoding TOCODE. FROMCODE and TOCODE are as for iconv_open(3), |
| 179 | + * except that FROMCODE may be one of the values |
| 180 | + * "autodetect_utf8" supports ISO-8859-1 and UTF-8 |
| 181 | + * "autodetect_jp" supports EUC-JP, ISO-2022-JP-2 and SHIFT_JIS |
| 182 | + * "autodetect_kr" supports EUC-KR and ISO-2022-KR |
| 183 | + * The input is in the memory region between start (inclusive) and end |
| 184 | + * (exclusive). If resultp is not NULL, the output string is stored in |
| 185 | + * *resultp; malloc/realloc is used to allocate the result. |
| 186 | + * |
| 187 | + * This function does not treat zero characters specially. |
| 188 | + * |
| 189 | + * Return value: 0 if successful, otherwise -1 and errno set. Particular |
| 190 | + * errno values: EILSEQ and ENOMEM. |
| 191 | + * |
| 192 | + * Example: |
| 193 | + * const char* s = ...; |
| 194 | + * char* result = NULL; |
| 195 | + * if (iconv_string("UCS-4-INTERNAL", "autodetect_utf8", |
| 196 | + * s, s+strlen(s)+1, &result, NULL) < 0) |
| 197 | + * perror("iconv_string"); |
| 198 | + * |
| 199 | + */ |
| 200 | + extern @DLL_VARIABLE@ int iconv_string (const char* tocode, const char* fromcode, const char* start, const char* end, char** resultp, size_t* lengthp); |
| 201 | + |
| 202 | + }}} |
| 203 | +} |
| 204 | + |
| 205 | +#endif /* _CPPP_REICONV_HPP */ |
0 commit comments