• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Juliean

Reading/Writing UTF8 with filestreams

7 posts in this topic

Hello,

 

I've just recently come across the problem that I cannot load certain vowels like ä, ü, ö from my xml-files. I'm using a std::wfstream, so how do I configure it to load those characters? (when I load it now, I always get 2 nonsense-chars from the one supposed-to-be widechar).

 

I tried:

std::wfstream m_stream;

std::locale locale("");
m_stream.imbue(locale);

m_stream.open(stFilename.c_str(), std::ios::in);

but it doesn't work, regardless of whether I open the stream before or after, even though my machine is set to german. Any ideas?

0

Share this post


Link to post
Share on other sites

What exactly do you mean by "nonsense-chars"? It's expected for ä, ü or ö to be encoded as two bytes in UTF8.

 

If you know your file is encoded in UTF8, you should open it as an std::fstream, not an std::wfstream. Load your text as an std::string (that's the nice thing about UTF8 - everything fits into normal chars). If you need it as something else (like UTF16 or UTF32) use an UTF conversion library of your choice.

2

Share this post


Link to post
Share on other sites


What exactly do you mean by "nonsense-chars"? It's expected for ä, ü or ö to be encoded as two bytes in UTF8.

 

Yeah, I meant just that, I got one char with value 195 (A with ~ on top) and another one with 132. I was not aware that this was supposed to be that way.

 

I am using std::wstring throughout my engine though, and do a lot of manual text parsing, where I require those chars to fit in one wchar_t.

 

So I quess I have to use a conversion libary? Do you recall any on top of your head whose source is available & whose licence allows me to compile the code within my engine? Don't really want to add another external dependency...

0

Share this post


Link to post
Share on other sites

195 means the top three bits are 110. That's the start of a two byte UTF8 for everything from U+0080 to U+07FF ([url=http://en.wikipedia.org/wiki/Utf8]Wikipedia[/url] has an explanation of that).

 

I'm usually not converting away from UTF8 but shouldn't [url=http://en.cppreference.com/w/cpp/locale/codecvt]codecvt[/url] int C++11 do the job?

1

Share this post


Link to post
Share on other sites

I'm usually not converting away from UTF8 but shouldn't codecvt int C++11 do the job?

 

I tried it, but without success:

 

See anything wrong about my usage? the std::u16string still contains 195,132 in the first two entries though, for loading I'm using a std::fstream now. From what I've read though std::wfstream should already use the right codecvt-overload anyways... any ideas?

 

EDIT: Got it, this does the trick:

		void Parser::ParseValue(Node& node)
		{
			std::string stValue("");

			// parse value until next token is encountered
			while(m_stream)
			{
				char c = m_stream.peek();
				if(c == '<')
					break;

				m_stream.get(c);

				if(c == '\n')
					m_line++;
				else
					stValue += c;
			}

			std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conversion;
			const auto stValueW = conversion.from_bytes(stValue);

			node.SetValue(stValueW);
		}

was supposed to use wstring_convert. This still mystifies me as the translated char of 'Ä' e.g. translates to 196, which would fit nicely into the UFT8-space, also the thing about std::wfstream being supposed to do the conversion themselfes... now I have some more workload when loading these files, plus I've got to apply this everywhere. Now, Ok, I don't have that many file accesses except that XML parser of mine, shouldn't be much of a trouble...

Edited by Juliean
0

Share this post


Link to post
Share on other sites


Down this path lies madness. wstring is in a compiler/vendor-specific format with no standards. The vendor formats are often either broken (UCS-2) or wasteful (UCS-4) or just useless (ASCII/EBCDIC). Just use a regular 8-bit std::string and interpret it as UTF-8 anywhere it matters (which is surprisingly few places outside of display). Convert to wide strings only in places you have to, like when talking to native OS path routines on certain platforms (or better, use a library that abstracts those details and just works in UTF-8 itself). The price of converting a path here and there is completely swamped by the price of the actual I/O itself, so don't worry about perf.

 

Sounds plausible, I quess I'll switch to std::string once I've got those few things I currently want to get done finished. I really hope global search and replace doesn't let me down, otherwise its going to be another fun afternoon doing nothing than repeatetly compiling, correcting few compile errors, and so on... (aren't those great)? Actually, I've already got wrappers for most file/path accesses, so its not a problem changing the underlying routines globally. I quess I'll just add a wrapper for file loading instead of using plain std::fstream for that matter, also I'm thinking about making a custom string wrapper. I can implement the underlying storage using std::string anyways, but than at least I can switch the implementation without hanging everything, plus I could add custom stuff like conversion routines, etc...

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0