11/**
22 * @file cppp/reiconv.hpp
33 * @author ChenPi11
4- * @brief C++ Plus base definitions
4+ * @brief C++ Plus cppp-reiconv package.
55 * @version 2.1.0
66 * @date 2023-7-28
77 * @copyright Copyright (C) 1999-2023 Free Software Foundation, Inc.
2828#ifndef _CPPP_REICONV_HPP
2929#define _CPPP_REICONV_HPP
3030
31- #ifdef _MSC_VER
31+ #if _MSC_VER >= 1600
3232/* Use UTF-8 to decode this file. */
3333#pragma execution_character_set("utf-8")
3434#endif
3535
36- #include <errno.h>
3736#include <exception>
3837#include <iostream>
38+ #include <errno.h>
3939#include <time.h>
4040#include <vector>
4141#include <wchar.h>
@@ -216,7 +216,20 @@ extern "C++"
216216 void *data);
217217
218218 /**
219- * @brief Converts an entire string from one encoding to another, using iconv. Easier to use than iconv() itself,
219+ * @brief Converts an entire string from one encoding to another, using iconv. Easier to use than iconv() itself.
220+ * @note This function does not treat zero characters specially.
221+ * @param cd Conversion descriptor.
222+ * @param start Source string start pointer.
223+ * @param end Source string end pointer.
224+ * @param resultp Result memory pointer.
225+ * @param lengthp Pointer to a variable that stores the length of the result.
226+ * @return Return 0 if successful, otherwise return errno and errno set. Particular errno values are EILSEQ and ENOMEM.
227+ */
228+ extern @DLL_VARIABLE@ int iconv_string(const iconv_t& cd, const char *start,
229+ const char *end, char **resultp, size_t *lengthp);
230+
231+ /**
232+ * @brief Converts an entire string from one encoding to another,
220233 * and supports autodetect encodings on input when extra features enabled.
221234 *
222235 * @note Converts a memory region given in encoding FROMCODE to a new memory
@@ -231,25 +244,47 @@ extern "C++"
231244 * *resultp; malloc/realloc is used to allocate the result.
232245 *
233246 * @note This function does not treat zero characters specially.
234- *
235247 * @param tocode Target encoding.
236248 * @param fromcode From encoding.
237249 * @param start Source string start pointer.
238250 * @param end Source string end pointer.
239251 * @param resultp Result memory pointer.
240252 * @param lengthp Pointer to a variable that stores the length of the result.
241- * @return Return 0 if successful, otherwise -1 and errno set. Particular errno values: EILSEQ and ENOMEM.
253+ * @return Return 0 if successful, otherwise return errno and errno set. Particular errno values: EILSEQ and ENOMEM.
242254 *
243255 * @example
244256 * const char* s = ...;
245257 * char* result = NULL;
246258 * if (iconv_string("UCS-4-INTERNAL", "GBK",
247- * s, s+strlen(s)+1, &result, NULL) < 0)
259+ * s, s+strlen(s)+1, &result, NULL) != 0)
248260 * perror("iconv_string");
249261 *
250262 */
251- extern @DLL_VARIABLE@ int iconv_string(const char *tocode, const char *fromcode, const char *start,
252- const char *end, char **resultp, size_t *lengthp);
263+ extern @DLL_VARIABLE@ int iconv_string(const char* tocode, const char* fromcode, const char* start,
264+ const char* end, char** resultp, size_t* lengthp);
265+
266+ /**
267+ * @brief Converts an entire string from one encoding to another.
268+ * @note This function does not treat zero characters specially.
269+ * @param tocode_cp Target encoding codepage.
270+ * @param fromcode_cp From encoding codepage.
271+ * @param start Source string start pointer.
272+ * @param end Source string end pointer.
273+ * @param resultp Result memory pointer.
274+ * @param lengthp Pointer to a variable that stores the length of the result.
275+ * @param strict Strict mode, if false, will ignore the invalid characters.
276+ * @return Return 0 if successful, otherwise return errno and errno set. Particular errno values: EILSEQ and ENOMEM.
277+ *
278+ * @example
279+ * const char* s = ...;
280+ * char* result = NULL;
281+ * if (iconv_string(65001, 936,
282+ * s, s+strlen(s)+1, &result, NULL) != 0)
283+ * perror("iconv_string");
284+ *
285+ */
286+ extern @DLL_VARIABLE@ int iconv_string(int tocode_cp, int fromcode_cp, const char* start,
287+ const char* end, char** resultp, size_t* lengthp, bool strict=true);
253288
254289 /**
255290 * @brief Encode source string to another encoding.
@@ -259,7 +294,7 @@ extern "C++"
259294 * @param ignore Ignore invalid characters.
260295 * @throw int: when conversation error, throw the 'errno', you can catch the errno for more infomation.
261296 */
262- inline std::vector<char> encode(std::string from, std::string to, const std::vector<char> & data,
297+ inline std::vector<char> encode(const std::string& from, const std::string& to, const std::vector<char>& data,
263298 bool ignore = false)
264299 {
265300 char *result = NULL;
0 commit comments