473,802 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_tcodecv t_t;
const codecvt_t& cc = use_facet<codec vt_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::e rror) {
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(cons t string& str, const locale& loc)
{
typedef codecvt<wchar_t , char, mbstate_tcodecv t_t;
const codecvt_t& cc = use_facet<codec vt_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::e rror) {
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 3671
* 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_tcodecv t_t;
const codecvt_t& cc = use_facet<codec vt_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::resu lt instead of int a
debugger can help you see what the result value means (symbolic name).

Anyway, make it const.

>
if (res == codecvt_base::e rror) {
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
14333
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 ifstream. Something I found shows that, I need to have a proper codecvt to set it. I need more information, maybe a small piece of code sample. Thank you!
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.