testEncoding.cxx 1.3 KB

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