• 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
Ectara

Outputting A Custom String To A Standard Stream

9 posts in this topic

I have a custom string class that takes a character type and a traits class as template parameters, and I am looking to input/output the string to a std::basic_stream. One of the problem is, my traits class definition is different from the standard traits type, and thus it isn't possible for it to be 100% compatible.

Since the string is allowed to contain a custom transformation format, where a code point could comprise multiple code units, it is possible that the string is a variable-width text encoding. For this reason, I presume that I should output code units, rather than code points. As an example, a terminal that is expecting UTF-8 output would expect a stream of 8bit code units, rather than a 32bit code point.

So, what would anybody suggest for accomplishing this?

0

Share this post


Link to post
Share on other sites

Have you looked at how various standard library implementations do this for std::string? It should be a pretty analogous process.

Looking through GCC's implementation, running into the problem that it handles everything through locales, and such; this runs the issue that the custom traits class can essentially define its own locale, so the standard library attempting to convert code units to code points (or the opposite direction) before it ever reaches me is a problem. This subverts what I am doing, and thus, I must try to read raw data, since the format might be one that the custom string expects, but not one that std::basic_stream and the standard locales would.
 

Also, it seems that Windows internally converts wchar_t string to a char stream using ANSI codepages, so I can imagine that it'd butcher my data if I outputted data from different locales through the same stream. My point is, I feel like the standard implementation might be barking up the wrong tree, if I, for example, want to implement outputting UTF-16 data, assuming that the OS doesn't have built-in UTF-16 support.

 

as ApochPiQ mentioned, the standard templated std::string method is basically what you are after. If that seems a bit of a hurdle to wade through, a google search of "overloading << operator c++" came back with a "couple" results, like:

http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

I do have an overload for the operators, I'm wondering how to implement it properly. You might notice that the link does not modify the behavior of reading in the data; rather it just batches together a couple standard operations. What I'm doing does not adhere to the conventional idea of a character, so I can't just std::cin >> str.char_array.
 

Edited by Ectara
0

Share this post


Link to post
Share on other sites
If the OS doesn't understand UTF-16, what good does it do you to output to a stream in UTF-16? Obviously if you're talking about a stringstream or fstream or other non-console stream target, fine; but if you want to write to the console, you're kind of a slave to doing what the OS expects.
0

Share this post


Link to post
Share on other sites

I understand, a console interprets what a console is capable of handling; my terminal emulator uses UTF-8. However, in this case it would substitute UTF-8 for UTF-16; I picked UTF-16 as an example because it doesn't have code units that are a single byte. But, let's go with the example of writing one of these encodings to an fstream, as this is closer to the actual purpose.

0

Share this post


Link to post
Share on other sites

If you want binary serialization, you just dump the bytes to disk in whatever form you can re-read them later.

 

If you want something else, you dump something else.

 

 

I'm not sure what the question is?

0

Share this post


Link to post
Share on other sites

For example, how do I output the code units through standard streams in a way that (1) the implementation of the streams won't mangle it completely, and (2) other programs would be able to access it in more or less the expected format (no locale transformations for UTF-8 code units, UTF-16 doesn't get piped through Window's underlying implementation that converts it to char array using your ANSI code page)?

Sort of like how I'd expect to just use fwrite() in C, and call it a day.

0

Share this post


Link to post
Share on other sites

The equivalent would be basic_ostream::write().

Hm... it does seem like it would be that easy, doesn't it?

I guess my next question is a matter of the basic_ostream's charT parameter. If the code unit is larger than the width of charT, it seems like I'd have to break each code point up, and write it in pieces, and likewise, if charT is larger than a code unit, then there are extra bits in the written value that are unaccounted for.

Seems like I need to somehow add a partial specialization for char streams only, and make it be a compile error if you try to output the string with a stream that's any wider. Otherwise, I'd have to enforce that both the application that reads and the application that writes uses a stream of the same width, to account for the idiosyncrasies.

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