FStream.cxx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*============================================================================
  2. KWSys - Kitware System Library
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "kwsysPrivate.h"
  11. #include KWSYS_HEADER(FStream.hxx)
  12. // Work-around CMake dependency scanning limitation. This must
  13. // duplicate the above list of headers.
  14. #if 0
  15. # include "FStream.hxx.in"
  16. #endif
  17. namespace KWSYS_NAMESPACE
  18. {
  19. namespace FStream
  20. {
  21. BOM ReadBOM(std::istream& in)
  22. {
  23. if(!in.good())
  24. {
  25. return BOM_None;
  26. }
  27. unsigned long orig = in.tellg();
  28. unsigned char bom[4];
  29. in.read(reinterpret_cast<char*>(bom), 2);
  30. if(!in.good())
  31. {
  32. in.clear();
  33. in.seekg(orig);
  34. return BOM_None;
  35. }
  36. if(bom[0] == 0xEF && bom[1] == 0xBB)
  37. {
  38. in.read(reinterpret_cast<char*>(bom+2), 1);
  39. if(in.good() && bom[2] == 0xBF)
  40. {
  41. return BOM_UTF8;
  42. }
  43. }
  44. else if(bom[0] == 0xFE && bom[1] == 0xFF)
  45. {
  46. return BOM_UTF16BE;
  47. }
  48. else if(bom[0] == 0x00 && bom[1] == 0x00)
  49. {
  50. in.read(reinterpret_cast<char*>(bom+2), 2);
  51. if(in.good() && bom[2] == 0xFE && bom[3] == 0xFF)
  52. {
  53. return BOM_UTF32BE;
  54. }
  55. }
  56. else if(bom[0] == 0xFF && bom[1] == 0xFE)
  57. {
  58. unsigned long p = in.tellg();
  59. in.read(reinterpret_cast<char*>(bom+2), 2);
  60. if(in.good() && bom[2] == 0x00 && bom[3] == 0x00)
  61. {
  62. return BOM_UTF32LE;
  63. }
  64. in.seekg(p);
  65. return BOM_UTF16LE;
  66. }
  67. in.clear();
  68. in.seekg(orig);
  69. return BOM_None;
  70. }
  71. } // FStream namespace
  72. } //KWSYS_NAMESPACE