473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

I need help for std::codecvt<>

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_tcodecvt_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_tcodecvt_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.

May 2 '07 #1
1 3630
* 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.
Essentially you need to loop until the result isn't "partial". That's
what the "char_next" arguments are for -- continuing.

================================================== ================
static string to_string(const wstring& str, const locale& loc)
Declaring a function "static" is a bit old-fashioned.

The modern style is to use an anonymous namespace.

But, oh well, I do it too: these newfangled ways are much too verbose.

{
typedef codecvt<wchar_t, char, mbstate_tcodecvt_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"
An initial assumption of sizeof(wchar_t) is OK, as an estimate. Perhaps
multiply by a little fudge factor. If the first conversion call yields
a partial conversion (check the result) you can use the xxx_next
pointers to improve the estimate and increase the buffer.

For a buffer, use a std::string (use a suitable initial size).

Note: you can safely assume that a std::string's internal buffer (from
data()) is contiguous, and C++0x will require that.

>
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);
Would possibly be better to use data() than c_str(), although as I
recall most discussion have concluded that data() in practice has no
option other than doing the same as c_str().

If you declare the result variable as codecvt_t::result instead of int a
debugger can help you see what the result value means (symbolic name).

Anyway, make it const.

>
if (res == codecvt_base::error) {
cerr << "codecvt convert fail. locale=" << loc.name() << endl;
return string();
}
Throw an exception instead of doing output or other nasty things.

*byte_next = 0;
No need to add a null-terminator.

string result(buf);
No need for this if you use a string as buffer. For the current buffer
management scheme just do

string result( buf, byte_next );
delete buf;

return result;
}
I've never used this stuff. One reason is that it's not well-defined
what result you actually get. Thus, it's useless in the first place.
Another reason is that it's complicated and suspected of being hugely
inefficient. But the main reason is that it takes (much!) less time to
just code e.g. a conversion to UTF-8 directly, assuming UTF-16 or UTF-32
for wchar_t, or use platform-specific functionality, than to figure this
out and provide reasonable wrappers and interfaces.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 2 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: davihigh | last post by:
My Friends: I am using std::ofstream (as well as ifstream), I hope that when i wrote in some std::string(...) with locale, ofstream can convert to UTF-8 encoding and save file to disk. So does...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.