D
Dancefire
Hi, everyone,
I'm trying to use std::codecvt<> to do the encoding conversion. I am
using following code for encoding conversion between wchar_t string
and char string(MBCS). I am not sure am I right. The code works, but
I'm not familiar with the codecvt, and I don't know my way is the
right way to do the job. Could you help me to review the code?
This function try to convert a wide string to a MBCS in the loc's
charset. I hardcode multiple by 2 here which is not correct, but I
don't know how to get the buf length.
==================================================================
static string to_string(const wstring& str, const locale& loc)
{
typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
const codecvt_t& cc = use_facet<codecvt_t>(loc);
mbstate_t state = mbstate_t();
int buf_size = static_cast<int>(str.length() * 2);
char* buf = new char[buf_size+1]; // FIXME: it's hardcode : "* 2"
const wchar_t* char_next;
char* byte_next;
int res = cc.out(state,
str.c_str(), str.c_str() + str.length(), char_next,
&buf[0], &buf[buf_size], byte_next);
if (res == codecvt_base::error) {
cerr << "codecvt convert fail. locale=" << loc.name() << endl;
return string();
}
*byte_next = 0;
string result(buf);
delete buf;
return result;
}
==================================================================
The following code is trying to convert a MBCS string to a wide string
which encoded by loc's charset. I don't know this time I use
cc.length() is right or not.
==================================================================
static wstring to_wstring(const string& str, const locale& loc)
{
typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
const codecvt_t& cc = use_facet<codecvt_t>(loc);
mbstate_t state = mbstate_t();
int buf_size = cc.length(state, str.c_str(), str.c_str() +
str.length(), str.length());
wchar_t* buf = new wchar_t[buf_size+1];
wchar_t* char_next;
const char* byte_next;
int res = cc.in(state,
str.c_str(), str.c_str() + str.length(), byte_next,
&buf[0], &buf[buf_size], char_next);
if (res == codecvt_base::error) {
cerr << "codecvt convert fail. locale=" << loc.name() << endl;
return wstring();
}
*char_next = 0;
wstring result(buf);
delete buf;
return result;
}
==================================================================
Thanks.
I'm trying to use std::codecvt<> to do the encoding conversion. I am
using following code for encoding conversion between wchar_t string
and char string(MBCS). I am not sure am I right. The code works, but
I'm not familiar with the codecvt, and I don't know my way is the
right way to do the job. Could you help me to review the code?
This function try to convert a wide string to a MBCS in the loc's
charset. I hardcode multiple by 2 here which is not correct, but I
don't know how to get the buf length.
==================================================================
static string to_string(const wstring& str, const locale& loc)
{
typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
const codecvt_t& cc = use_facet<codecvt_t>(loc);
mbstate_t state = mbstate_t();
int buf_size = static_cast<int>(str.length() * 2);
char* buf = new char[buf_size+1]; // FIXME: it's hardcode : "* 2"
const wchar_t* char_next;
char* byte_next;
int res = cc.out(state,
str.c_str(), str.c_str() + str.length(), char_next,
&buf[0], &buf[buf_size], byte_next);
if (res == codecvt_base::error) {
cerr << "codecvt convert fail. locale=" << loc.name() << endl;
return string();
}
*byte_next = 0;
string result(buf);
delete buf;
return result;
}
==================================================================
The following code is trying to convert a MBCS string to a wide string
which encoded by loc's charset. I don't know this time I use
cc.length() is right or not.
==================================================================
static wstring to_wstring(const string& str, const locale& loc)
{
typedef codecvt<wchar_t, char, mbstate_t> codecvt_t;
const codecvt_t& cc = use_facet<codecvt_t>(loc);
mbstate_t state = mbstate_t();
int buf_size = cc.length(state, str.c_str(), str.c_str() +
str.length(), str.length());
wchar_t* buf = new wchar_t[buf_size+1];
wchar_t* char_next;
const char* byte_next;
int res = cc.in(state,
str.c_str(), str.c_str() + str.length(), byte_next,
&buf[0], &buf[buf_size], char_next);
if (res == codecvt_base::error) {
cerr << "codecvt convert fail. locale=" << loc.name() << endl;
return wstring();
}
*char_next = 0;
wstring result(buf);
delete buf;
return result;
}
==================================================================
Thanks.