Catch BOM at the beginning of file.

This commit is contained in:
Alek Mosingiewicz 2018-05-13 12:24:34 +02:00
parent efbebee9da
commit a024db040d
2 changed files with 23 additions and 3 deletions

View File

@ -52,6 +52,7 @@
#include "../dispatchkit/exception_specification.hpp" #include "../dispatchkit/exception_specification.hpp"
#include "chaiscript_parser.hpp"
namespace chaiscript namespace chaiscript
{ {
@ -204,6 +205,23 @@ namespace chaiscript
m_engine.add(fun([this](const std::string& t_namespace_name) { import(t_namespace_name); }), "import"); m_engine.add(fun([this](const std::string& t_namespace_name) { import(t_namespace_name); }), "import");
} }
/// Skip BOM at the beginning of file
static bool skip_bom(std::ifstream &infile) {
char buffer[4];
memset(buffer, '\0', 4);
infile.readsome(buffer, 3);
std::string buffer_string(buffer);
if (chaiscript::parser::detail::Char_Parser_Helper<std::string>::has_utf8_bom(buffer_string)) {
infile.seekg(3);
return true;
}
infile.seekg(0);
return false;
}
/// Helper function for loading a file /// Helper function for loading a file
static std::string load_file(const std::string &t_filename) { static std::string load_file(const std::string &t_filename) {
@ -218,6 +236,8 @@ namespace chaiscript
assert(size >= 0); assert(size >= 0);
skip_bom(infile);
if (size == std::streampos(0)) if (size == std::streampos(0))
{ {
return std::string(); return std::string();

View File

@ -118,7 +118,7 @@ namespace chaiscript
static bool has_utf8_bom(const std::string &t_input) static bool has_utf8_bom(const std::string &t_input)
{ {
//skip UTF-8 BOM //skip UTF-8 BOM
return ((t_input.size() > 3) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf')); return ((t_input.size() > 2) && (t_input[0] == '\xef') && (t_input[1] == '\xbb' && t_input[2] == '\xbf'));
} }
}; };
} }