Skip to content

Commit 9e4344d

Browse files
committed
Add function cppp::base::reiconv::encode()
修改: ../include/cppp/reiconv.hpp.in
1 parent 8713ca7 commit 9e4344d

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

include/cppp/reiconv.hpp.in

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
#ifndef _CPPP_REICONV_HPP
2121
#define _CPPP_REICONV_HPP
2222

23-
#include <stddef.h>
24-
#include <stdio.h>
23+
#include <iostream>
2524
#include <time.h>
26-
#include <stddef.h>
2725
#include <errno.h>
2826
#include <wchar.h>
27+
#include <vector>
28+
#include <exception>
2929

3030
extern "C++"
3131
{
@@ -190,6 +190,24 @@ extern "C++"
190190
*/
191191
extern @DLL_VARIABLE@ int iconv_string (const char* tocode, const char* fromcode, const char* start, const char* end, char** resultp, size_t* lengthp);
192192

193+
inline std::vector<char> encode (std::string from, std::string to,
194+
const std::vector<char>& data, bool ignore = false)
195+
{
196+
char* result = NULL;
197+
size_t length = 0;
198+
std::string ignstr = ignore?"//IGNORE":"";
199+
if (iconv_string((to + ignstr).c_str(), from.c_str(),
200+
data.data(), data.data() + data.size() + 1, &result, &length) < 0)
201+
{
202+
if(!ignore)
203+
{
204+
/* When conversation error, throw the errno, you can catch the errno for more infomation. */
205+
throw (int)(errno);
206+
}
207+
}
208+
return std::vector<char>(result, result + length);
209+
}
210+
193211
}}}
194212
}
195213

0 commit comments

Comments
 (0)