testEncoding.cxx 1.3 KB

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