mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 00:38:08 +08:00
fix: open file as binary-mode #16
to avoid inconsistency between file size (obtained by tellg) and the size of the actual contents that would be read later
This commit is contained in:
@@ -42,8 +42,14 @@ In the case of file open error, it will throw `std::runtime_error`.
|
|||||||
|
|
||||||
You can also pass a `stream` to the `toml::parse` function after checking the status.
|
You can also pass a `stream` to the `toml::parse` function after checking the status.
|
||||||
|
|
||||||
|
Note that on __Windows OS__, stream that is opened as text-mode automatically converts
|
||||||
|
CRLF ("\r\n") into LF ("\n") and this leads inconsistency between file size and
|
||||||
|
the contents that would be read. This causes weird error. To use a file stream
|
||||||
|
with `toml::parse`, don't forget to pass binary mode flag when you open the
|
||||||
|
stream.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
std::ifstream ifs("sample.toml");
|
std::ifstream ifs("sample.toml", std::ios_base::binary);
|
||||||
assert(ifs.good());
|
assert(ifs.good());
|
||||||
const auto data = toml::parse(ifs /*, "filename" (optional)*/);
|
const auto data = toml::parse(ifs /*, "filename" (optional)*/);
|
||||||
```
|
```
|
||||||
|
@@ -213,6 +213,20 @@ BOOST_AUTO_TEST_CASE(test_file_with_BOM)
|
|||||||
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
||||||
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
const std::string table(
|
||||||
|
"\xEF\xBB\xBF" // BOM
|
||||||
|
"key = \"value\"\n"
|
||||||
|
"[table]\n"
|
||||||
|
"key = \"value\"\n"
|
||||||
|
);
|
||||||
|
std::ofstream ofs("tmp.toml");
|
||||||
|
ofs << table;
|
||||||
|
const auto data = toml::parse("tmp.toml");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
||||||
|
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
||||||
|
}
|
||||||
{
|
{
|
||||||
const std::string table(
|
const std::string table(
|
||||||
"\xEF\xBB\xBF" // BOM
|
"\xEF\xBB\xBF" // BOM
|
||||||
@@ -223,6 +237,20 @@ BOOST_AUTO_TEST_CASE(test_file_with_BOM)
|
|||||||
std::istringstream iss(table);
|
std::istringstream iss(table);
|
||||||
const auto data = toml::parse(iss, "test_file_with_BOM_CRLF.toml");
|
const auto data = toml::parse(iss, "test_file_with_BOM_CRLF.toml");
|
||||||
|
|
||||||
|
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
||||||
|
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const std::string table(
|
||||||
|
"\xEF\xBB\xBF" // BOM
|
||||||
|
"key = \"value\"\r\n"
|
||||||
|
"[table]\r\n"
|
||||||
|
"key = \"value\"\r\n"
|
||||||
|
);
|
||||||
|
std::ofstream ofs("tmp.toml");
|
||||||
|
ofs << table;
|
||||||
|
const auto data = toml::parse("tmp.toml");
|
||||||
|
|
||||||
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
BOOST_CHECK_EQUAL(toml::get <std::string>(data.at("key")), "value");
|
||||||
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
BOOST_CHECK_EQUAL(toml::find<std::string>(data.at("table"), "key"), "value");
|
||||||
}
|
}
|
||||||
|
@@ -1517,7 +1517,7 @@ inline table parse(std::istream& is, std::string fname = "unknown file")
|
|||||||
|
|
||||||
inline table parse(const std::string& fname)
|
inline table parse(const std::string& fname)
|
||||||
{
|
{
|
||||||
std::ifstream ifs(fname.c_str());
|
std::ifstream ifs(fname.c_str(), std::ios_base::binary);
|
||||||
if(!ifs.good())
|
if(!ifs.good())
|
||||||
{
|
{
|
||||||
throw std::runtime_error("toml::parse: file open error -> " + fname);
|
throw std::runtime_error("toml::parse: file open error -> " + fname);
|
||||||
|
Reference in New Issue
Block a user