cmake/Source/kwsys/FStream.cxx

56 lines
1.4 KiB
C++
Raw Normal View History

2017-04-14 19:02:05 +02:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
2015-04-27 22:25:09 +02:00
#include "kwsysPrivate.h"
#include KWSYS_HEADER(FStream.hxx)
// Work-around CMake dependency scanning limitation. This must
// duplicate the above list of headers.
#if 0
2018-08-09 18:06:22 +02:00
# include "FStream.hxx.in"
2015-04-27 22:25:09 +02:00
#endif
2017-04-14 19:02:05 +02:00
namespace KWSYS_NAMESPACE {
namespace FStream {
2015-04-27 22:25:09 +02:00
BOM ReadBOM(std::istream& in)
{
2017-04-14 19:02:05 +02:00
if (!in.good()) {
2015-04-27 22:25:09 +02:00
return BOM_None;
2017-04-14 19:02:05 +02:00
}
2015-04-27 22:25:09 +02:00
unsigned long orig = in.tellg();
unsigned char bom[4];
in.read(reinterpret_cast<char*>(bom), 2);
2017-04-14 19:02:05 +02:00
if (!in.good()) {
2016-03-13 13:35:51 +01:00
in.clear();
2015-04-27 22:25:09 +02:00
in.seekg(orig);
return BOM_None;
2017-04-14 19:02:05 +02:00
}
if (bom[0] == 0xEF && bom[1] == 0xBB) {
in.read(reinterpret_cast<char*>(bom + 2), 1);
if (in.good() && bom[2] == 0xBF) {
2015-04-27 22:25:09 +02:00
return BOM_UTF8;
}
2017-04-14 19:02:05 +02:00
} else if (bom[0] == 0xFE && bom[1] == 0xFF) {
2015-04-27 22:25:09 +02:00
return BOM_UTF16BE;
2017-04-14 19:02:05 +02:00
} 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) {
2015-04-27 22:25:09 +02:00
return BOM_UTF32BE;
}
2017-04-14 19:02:05 +02:00
} else if (bom[0] == 0xFF && bom[1] == 0xFE) {
2015-04-27 22:25:09 +02:00
unsigned long p = in.tellg();
2017-04-14 19:02:05 +02:00
in.read(reinterpret_cast<char*>(bom + 2), 2);
if (in.good() && bom[2] == 0x00 && bom[3] == 0x00) {
2015-04-27 22:25:09 +02:00
return BOM_UTF32LE;
2017-04-14 19:02:05 +02:00
}
2015-04-27 22:25:09 +02:00
in.seekg(p);
return BOM_UTF16LE;
2017-04-14 19:02:05 +02:00
}
2016-03-13 13:35:51 +01:00
in.clear();
2015-04-27 22:25:09 +02:00
in.seekg(orig);
return BOM_None;
}
} // FStream namespace
2017-04-14 19:02:05 +02:00
} // KWSYS_NAMESPACE