x::iconviofilter
is an IO filter,
virtually derived from
x::iofilter<char, char>
that implements the
iconv(3)
interface as an IO filter.
The constructor takes two parameters, the input character set and the
output character set. The filter
() function consumes
available input and/or writes to the available output buffer, performing the
requested character conversion:
#include <fstream> #include <iterator> #include <algorithm> #include <x/basicstreambufiofilter.H> #include <x/iconviofilter.H> class utf8toucs4 : public x::basic_istreambufiofilter<char, char>, public x::iconviofilter, virtual public x::iofilter<char, char> { public: utf8toucs4(std::istream &i) : x::basic_istreambufiofilter<char, char>(i), x::iconviofilter("UTF-8", "UCS-4BE") { } ~utf8toucs4() { } void filter() { x::iconviofilter::filter(); } }; int main(int argc, char **argv) { if (argc > 1) { std::vector<unsigned char> buf; try { std::ifstream i(argv[1]); if (i.is_open()) { utf8toucs4 conv(i); std::istream i2(&conv); std::copy(std::istreambuf_iterator<char>(i2), std::istreambuf_iterator<char>(), std::back_insert_iterator <std::vector<unsigned char> >(buf)); } } catch (x::exception &e) { std::cerr << e << std::endl; return 1; } size_t cnt=0; uint32_t n=0; for (std::vector<unsigned char>::const_iterator b(buf.begin()), e(buf.end()); b != e; ++b) { n= (n << 8) | *b; if ((++cnt % 4) == 0) { std::cout << n << std::endl; } } } return 0; }