Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > wide codecvt no...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 3 Topic 9577 of 9593
Post > Topic >>

wide codecvt not used unless sync_with_stdio(false)

by Frank Birbacher <bloodymir.crap@[EMAIL PROTECTED] > May 7, 2008 at 11:43 AM

Hi!

I'm facing a problem with the following code that uses a custom codecvt
facet for wide streams. The facet is taken from Boost (actually a recent
svn trunk checkout, but the considered code did not change a while, a
1.34 or 1.35 release would do) and converts wchar_t internally and UTF-8
externally.

#include <iostream>
#include <ostream>
#include <fstream>

#define BOOST_UTF8_BEGIN_NAMESPACE
#define BOOST_UTF8_DECL
#define BOOST_UTF8_END_NAMESPACE
#include <libs/detail/utf8_codecvt_facet.cpp>
#undef BOOST_UTF8_DECL
#undef BOOST_UTF8_BEGIN_NAMESPACE
#undef BOOST_UTF8_END_NAMESPACE

using namespace std;

void setUTF8Locale()
{
	//try *with* and *without*
	//ios_base::sync_with_stdio(false);
	locale utf8Locale(locale(), new utf8_codecvt_facet);
	wcout.imbue(utf8Locale);
	locale::global(utf8Locale);
}

int main()
{
	setUTF8Locale();
	
	wcout << L"wertüäö" << endl;

	wofstream out("test");
	out << L"wertüäö\n";
}

The code is in a UTF-8 encoded source file and is compiled using gcc
4.1.2 on Gentoo Linux which accepts UTF-8 input. Well the output of the
program in an UTF-8 console is different from the content of the file
"test" written by the same program (called cvttest):

$./cvttest | hexdump -C
00000000  77 65 72 74 3f 3f 3f 0a                           |wert???.|
00000008
$hexdump -C test
00000000  77 65 72 74 c3 bc c3 a4  c3 b6 0a                 |wert.......|
0000000b
$

The wcout output converted every non-ASCII char into a question mark. A
simple cout << "I'm here" in the source code of the codecvt shows the
code is never called.

When I use sync_with_stdio(false), however, the output is the same as
the contents of the test file.

The "Standard C++ IOStreams and Locales" [Langer&Kreft] state "The first
operation that is performed on a C file determines its orientation"
(page 59) meaning: deciding between narrow and wide output. As I'm not
using the stdio here, sould sync_with_stdio possibly affect the use of a
code conversion facet?

Regards, Frank

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]




 3 Posts in Topic:
wide codecvt not used unless sync_with_stdio(false)
Frank Birbacher <blood  2008-05-07 11:43:28 
Re: wide codecvt not used unless sync_with_stdio(false)
Oncaphillis <ocaphilli  2008-05-08 21:30:56 
Re: wide codecvt not used unless sync_with_stdio(false)
"sebor@[EMAIL PROTEC  2008-05-09 09:15:01 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 17:06:28 CDT 2008.