testEncoding.cxx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. #include <cmsys/ConsoleBuf.hxx>
  5. #ifdef _WIN32
  6. void setEncoding(cmsys::ConsoleBuf::Manager& buf, UINT codepage)
  7. {
  8. cmsys::ConsoleBuf* cb = buf.GetConsoleBuf();
  9. if (cb) {
  10. cb->input_pipe_codepage = codepage;
  11. cb->output_pipe_codepage = codepage;
  12. cb->input_file_codepage = codepage;
  13. cb->output_file_codepage = codepage;
  14. cb->activateCodepageChange();
  15. }
  16. }
  17. #endif
  18. int main(int argc, char* argv[])
  19. {
  20. #ifdef _WIN32
  21. cmsys::ConsoleBuf::Manager consoleOut(std::cout);
  22. #endif
  23. if (argc <= 2) {
  24. std::cout << "Usage: testEncoding <encoding> <file>" << std::endl;
  25. return 1;
  26. }
  27. const std::string encoding(argv[1]);
  28. #ifdef _WIN32
  29. if (encoding == "UTF8") {
  30. setEncoding(consoleOut, CP_UTF8);
  31. } else if (encoding == "ANSI") {
  32. setEncoding(consoleOut, CP_ACP);
  33. } else if (encoding == "OEM") {
  34. setEncoding(consoleOut, CP_OEMCP);
  35. } // else AUTO
  36. #endif
  37. std::ifstream file(argv[2]);
  38. if (!file.is_open()) {
  39. std::cout << "Failed to open file: " << argv[2] << std::endl;
  40. return 2;
  41. }
  42. std::string text((std::istreambuf_iterator<char>(file)),
  43. std::istreambuf_iterator<char>());
  44. std::cout << text;
  45. return 0;
  46. }