// Test case, bug 776537 - Shall Gio::PollableInputStream derive from InputStream?

#include <giomm.h>
#include <iostream>
#include <cstdlib>

int main(int, char**)
{
  Glib::init();
  Gio::init();

  try
  {
    char result[256];
    auto memory_stream = Gio::MemoryOutputStream::create(result, 256, nullptr, nullptr);
    auto converter = Gio::CharsetConverter::create("ASCII", "UTF-8");
    converter->set_use_fallback(true);
#if 0
    // Temporary hack, until CharsetConverter has been fixed.
    // (Add public Initable and call Initable::init() in the constructor!)
    if (!g_initable_init((GInitable*)converter->gobj(), nullptr, nullptr))
    {
      std::cout << "Could not initialize the converter." << std::endl;
      return EXIT_FAILURE;
    }
#endif
    auto converter_stream = Gio::ConverterOutputStream::create(memory_stream, converter);

    std::string data = "Tschüs!";

    converter_stream->set_close_base_stream(true);
    converter_stream->write(data);
    converter_stream->close();

    std::cout << "Original data: " << data << std::endl;
    std::cout << "ASCII-encoded data: "  << result << std::endl;
  }
  catch (const Gio::Error& e)
  {
    std::cout << "Gio error: " << e.what() << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}
