FStream.cxx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(FStream.hxx)
  5. // Work-around CMake dependency scanning limitation. This must
  6. // duplicate the above list of headers.
  7. #if 0
  8. #include "FStream.hxx.in"
  9. #endif
  10. namespace KWSYS_NAMESPACE {
  11. namespace FStream {
  12. BOM ReadBOM(std::istream& in)
  13. {
  14. if (!in.good()) {
  15. return BOM_None;
  16. }
  17. unsigned long orig = in.tellg();
  18. unsigned char bom[4];
  19. in.read(reinterpret_cast<char*>(bom), 2);
  20. if (!in.good()) {
  21. in.clear();
  22. in.seekg(orig);
  23. return BOM_None;
  24. }
  25. if (bom[0] == 0xEF && bom[1] == 0xBB) {
  26. in.read(reinterpret_cast<char*>(bom + 2), 1);
  27. if (in.good() && bom[2] == 0xBF) {
  28. return BOM_UTF8;
  29. }
  30. } else if (bom[0] == 0xFE && bom[1] == 0xFF) {
  31. return BOM_UTF16BE;
  32. } else if (bom[0] == 0x00 && bom[1] == 0x00) {
  33. in.read(reinterpret_cast<char*>(bom + 2), 2);
  34. if (in.good() && bom[2] == 0xFE && bom[3] == 0xFF) {
  35. return BOM_UTF32BE;
  36. }
  37. } else if (bom[0] == 0xFF && bom[1] == 0xFE) {
  38. unsigned long p = in.tellg();
  39. in.read(reinterpret_cast<char*>(bom + 2), 2);
  40. if (in.good() && bom[2] == 0x00 && bom[3] == 0x00) {
  41. return BOM_UTF32LE;
  42. }
  43. in.seekg(p);
  44. return BOM_UTF16LE;
  45. }
  46. in.clear();
  47. in.seekg(orig);
  48. return BOM_None;
  49. }
  50. } // FStream namespace
  51. } // KWSYS_NAMESPACE