/src/CMake/Source/kwsys/FStream.cxx
Line | Count | Source |
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 | | |
6 | | // Work-around CMake dependency scanning limitation. This must |
7 | | // duplicate the above list of headers. |
8 | | #if 0 |
9 | | # include "FStream.hxx.in" |
10 | | #endif |
11 | | |
12 | | namespace KWSYS_NAMESPACE { |
13 | | namespace FStream { |
14 | | |
15 | | BOM ReadBOM(std::istream& in) |
16 | 41.9k | { |
17 | 41.9k | if (!in.good()) { |
18 | 0 | return BOM_None; |
19 | 0 | } |
20 | 41.9k | unsigned long orig = in.tellg(); |
21 | 41.9k | unsigned char bom[4]; |
22 | 41.9k | in.read(reinterpret_cast<char*>(bom), 2); |
23 | 41.9k | if (!in.good()) { |
24 | 233 | in.clear(); |
25 | 233 | in.seekg(orig); |
26 | 233 | return BOM_None; |
27 | 233 | } |
28 | 41.7k | if (bom[0] == 0xEF && bom[1] == 0xBB) { |
29 | 159 | in.read(reinterpret_cast<char*>(bom + 2), 1); |
30 | 159 | if (in.good() && bom[2] == 0xBF) { |
31 | 84 | return BOM_UTF8; |
32 | 84 | } |
33 | 41.5k | } else if (bom[0] == 0xFE && bom[1] == 0xFF) { |
34 | 5 | return BOM_UTF16BE; |
35 | 41.5k | } else if (bom[0] == 0x00 && bom[1] == 0x00) { |
36 | 113 | in.read(reinterpret_cast<char*>(bom + 2), 2); |
37 | 113 | if (in.good() && bom[2] == 0xFE && bom[3] == 0xFF) { |
38 | 5 | return BOM_UTF32BE; |
39 | 5 | } |
40 | 41.4k | } else if (bom[0] == 0xFF && bom[1] == 0xFE) { |
41 | 93 | unsigned long p = in.tellg(); |
42 | 93 | in.read(reinterpret_cast<char*>(bom + 2), 2); |
43 | 93 | if (in.good() && bom[2] == 0x00 && bom[3] == 0x00) { |
44 | 5 | return BOM_UTF32LE; |
45 | 5 | } |
46 | 88 | in.seekg(p); |
47 | 88 | return BOM_UTF16LE; |
48 | 93 | } |
49 | 41.5k | in.clear(); |
50 | 41.5k | in.seekg(orig); |
51 | 41.5k | return BOM_None; |
52 | 41.7k | } |
53 | | |
54 | | } // FStream namespace |
55 | | } // KWSYS_NAMESPACE |