mirror of
https://github.com/ChaiScript/ChaiScript.git
synced 2026-02-11 04:39:58 +08:00
Catch BOM at the beginning of file.
This commit is contained in:
parent
efbebee9da
commit
a024db040d
@ -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();
|
||||||
|
|||||||
@ -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'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user