Boost logo

Boost :

Subject: [boost] [locale] Composing asymmetric locale for character encoding conversion
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2013-03-02 05:56:52


Hi,

Suppose I have a logging application that writes log records in wide
(wchar_t, UTF-16) and narrow (char, UTF-8) encodings and I want these
logs to be stored in a UTF-16LE encoded file. For simplicity, let's
assume that I write log files with std::wofstream. Now, the standard
says that the file stream buffer is supposed to convert wide
characters to byte sequences using the locale imbued into the buffer.
However, it seems that the locale should be the same as the one imbued
into the stream (basic_ostream::imbue makes sure of that). What this
leads to is that in order to achieve my goal the locale should be able
to convert narrow characters of UTF-8 to wide characters of UTF-16 and
wide characters of UTF-16 to narrow characters representing byte
sequence of UTF16LE. Is it possible to make such an asymmetric locale
with Boost.Locale? Or maybe there is another way of doing this?

An additional question. Is it possible to to achieve my goal with
std::ofstream (as opposed to std::wofstream)? I have a very strong
suspicion that the answer is no because the narrow characters will
pass on unconverted to the file instead of being translated from UTF-8
to UTF-16LE, but maybe I'm missing something.

Thank you.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk