Rafa? Maj Raf256 wrote:
[character encoding/decoding by fiddling with streams]
Or do std allow better way?
Certainly! The first thing to note is that character encoding and
decoding moves between different things: encoding turns characters
into bytes and decoding turns bytes into characters. It is important
to distinguish between bytes and characters: characters do not care
about their encoding and you can investigate characters to determine
their semantics. Bytes on the other hand are encoded characters (in
this context; bytes might represent other stuff, too) which are
useless when taken out of context. The only reasonable thing to do
to them, except of passing the whole sequence around, of course, is
to decode them and use the resulting characters.
OK, this sets the stage for the 'std::codecvt' facets: these turn
bytes into characters or vice versa. Each file stream, well, actually
the 'std::filebuf' stream buffer, internally uses the code conversion
facets to blockwise encode characters or decode bytes. Unfortunately,
the same mechanism is not readily available for other streams although
Dinkumware's standard library ships with a class which can be used to
do the conversions. It isn't too hard to implement a simple filtering
stream buffer which converts bytes into a characters using an
appropriate 'std::codecvt' facet (the aspect which makes the code
conversion stuff pretty complex e.g. for 'std::basic_filebuf' is
support for positioning which is rarely necessary on streams
representing characters). This would be the way to go: create a
filtering stream buffer which internally uses code conversion facets,
e.g. the ones you provide to implement the Unicode encodings. These
filtering stream buffer is then used with stream classes to actually
use the encoding.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence