| re: InputStream Read - how to ignore comma
Rakesh Sinha wrote:[color=blue]
> I have a question though. Once we change the locale using 'imbue' ,
> would it be still valid / possible to return to the default locale ?[/color]
Yes. You can simply 'imbue()' another locale. There is only a minor
restriction that you cannot change the code conversion facet of a
file stream once you have opened it (actually, the restriction is
that you cannot change the code conversion facet once you have read
or written to an opened stream but a popular implementation makes the
stricter assumption) but there is rarely a need to change the code
conversion facet at all and different locales can share common facets.
The only possible problem could be obtaining the original locale: you
can either obtain it prior to a call to 'imbue()' using 'getloc()' or
you can rely on the default, i.e. the global locale, not being
changed. The global locale can simply be obtained with locale's default
constructor: 'std::locale()'.
Note that switching locales can be a relatively costly operation since
the stream might cache values obtained from locale facets: assuming
that locales rarelsy change, it is reasonable to prepare various data
just once rather than obtaining it for many IOStream operations.
Anyway,
if you need different locales with the same stream, you are probably
better off creating multiple streams sharing a common stream buffer.
For example:
| // ...
| std::istringstream input1("some string");
| std::istream input2(input1.rdbuf());
| input2.imbue(some_locale);
| // ...
The stream objects themselves don't buffer any characters: this is
done only by the stream buffers. Thus, you can alternatingly obtain
characters from either 'std::istream' avoiding the cost of switching
locales back and forth.
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting |