FStream.cxx 1.42 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4 5 6 7 8
#include "kwsysPrivate.h"
#include KWSYS_HEADER(FStream.hxx)

// Work-around CMake dependency scanning limitation.  This must
// duplicate the above list of headers.
#if 0
9
#include "FStream.hxx.in"
10 11
#endif

12 13
namespace KWSYS_NAMESPACE {
namespace FStream {
14 15 16

BOM ReadBOM(std::istream& in)
{
17
  if (!in.good()) {
18
    return BOM_None;
19
  }
20 21 22
  unsigned long orig = in.tellg();
  unsigned char bom[4];
  in.read(reinterpret_cast<char*>(bom), 2);
23
  if (!in.good()) {
24
    in.clear();
25 26
    in.seekg(orig);
    return BOM_None;
27 28 29 30
  }
  if (bom[0] == 0xEF && bom[1] == 0xBB) {
    in.read(reinterpret_cast<char*>(bom + 2), 1);
    if (in.good() && bom[2] == 0xBF) {
31 32
      return BOM_UTF8;
    }
33
  } else if (bom[0] == 0xFE && bom[1] == 0xFF) {
34
    return BOM_UTF16BE;
35 36 37
  } else if (bom[0] == 0x00 && bom[1] == 0x00) {
    in.read(reinterpret_cast<char*>(bom + 2), 2);
    if (in.good() && bom[2] == 0xFE && bom[3] == 0xFF) {
38 39
      return BOM_UTF32BE;
    }
40
  } else if (bom[0] == 0xFF && bom[1] == 0xFE) {
41
    unsigned long p = in.tellg();
42 43
    in.read(reinterpret_cast<char*>(bom + 2), 2);
    if (in.good() && bom[2] == 0x00 && bom[3] == 0x00) {
44
      return BOM_UTF32LE;
45
    }
46 47
    in.seekg(p);
    return BOM_UTF16LE;
48
  }
49
  in.clear();
50 51 52 53 54
  in.seekg(orig);
  return BOM_None;
}

} // FStream namespace
55
} // KWSYS_NAMESPACE