Skip to content

Commit

Permalink
Merge pull request simpleledger#25 from ToruNiina/hotfix
Browse files Browse the repository at this point in the history
fix: open file as binary-mode simpleledger#16
  • Loading branch information
ToruNiina authored Feb 13, 2019
2 parents 53f6b82 + 46569da commit 60c81d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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
std::ifstream ifs("sample.toml");
std::ifstream ifs("sample.toml", std::ios_base::binary);
assert(ifs.good());
const auto data = toml::parse(ifs /*, "filename" (optional)*/);
```
Expand Down
35 changes: 35 additions & 0 deletions tests/test_parse_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ BOOST_AUTO_TEST_CASE(test_file_with_BOM)
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\"\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(
"\xEF\xBB\xBF" // BOM
Expand All @@ -223,6 +239,25 @@ BOOST_AUTO_TEST_CASE(test_file_with_BOM)
std::istringstream iss(table);
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"
);
{
// with text-mode, "\n" is converted to "\r\n" and the resulting
// value will be "\r\r\n". To avoid the additional "\r", use binary
// mode.
std::ofstream ofs("tmp.toml", std::ios_base::binary);
ofs.write(table.data(), table.size());
}
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");
}
Expand Down
2 changes: 1 addition & 1 deletion toml/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1517,7 +1517,7 @@ inline table parse(std::istream& is, std::string fname = "unknown file")

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())
{
throw std::runtime_error("toml::parse: file open error -> " + fname);
Expand Down

0 comments on commit 60c81d0

Please sign in to comment.