Skip to content

Commit

Permalink
Release 0.9.13
Browse files Browse the repository at this point in the history
  • Loading branch information
metayeti committed Apr 25, 2022
1 parent 41435aa commit b5f0dc2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.13 (April 25, 2022)
- `BUGFIX` - Writer now understands UTF-8 BOM-encoded files. ([#7](https://github.com/pulzed/mINI/issues/17))
- `BUGFIX` - Fixes a bug introduced in 0.9.12 where reader would break when reading empty files.

## 0.9.12 (April 24, 2022)
- `BUGFIX` - Fixes parser breaking for UTF-8 BOM-encoded files. ([#7](https://github.com/pulzed/mINI/issues/17))

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# mINI

v0.9.12
v0.9.13

## Info

Expand Down
30 changes: 24 additions & 6 deletions src/mini/ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

///////////////////////////////////////////////////////////////////////////////
//
// /mINI/ v0.9.12
// /mINI/ v0.9.13
// An INI file reader and writer for the modern age.
//
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -332,19 +332,31 @@ namespace mINI
using T_LineData = std::vector<std::string>;
using T_LineDataPtr = std::shared_ptr<T_LineData>;

bool isBOM = false;

private:
std::ifstream fileReadStream;
T_LineDataPtr lineData;

T_LineData readFile()
{
const char header[3] = {(char)fileReadStream.get(), (char)fileReadStream.get(), (char)fileReadStream.get()};
const bool isBOM = header[0] == (char)0xEF && header[1] == (char)0xBB && header[2] == (char)0xBF;
std::string fileContents;
fileReadStream.seekg(0, std::ios::end);
fileContents.resize(static_cast<std::size_t>(fileReadStream.tellg()));
const std::size_t fileSize = static_cast<std::size_t>(fileReadStream.tellg());
fileReadStream.seekg(0, std::ios::beg);
if (fileSize >= 3) {
const char header[3] = {
static_cast<char>(fileReadStream.get()),
static_cast<char>(fileReadStream.get()),
static_cast<char>(fileReadStream.get())
};
isBOM = header[0] == (char)0xEF && header[1] == (char)0xBB && header[2] == (char)0xBF;
}
else {
isBOM = false;
}
std::string fileContents;
fileContents.resize(fileSize);
fileReadStream.seekg(isBOM ? 3 : 0, std::ios::beg);
std::size_t fileSize = fileContents.size();
fileReadStream.read(&fileContents[0], fileSize);
fileReadStream.close();
T_LineData output;
Expand Down Expand Up @@ -678,11 +690,13 @@ namespace mINI
INIStructure originalData;
T_LineDataPtr lineData;
bool readSuccess = false;
bool fileIsBOM = false;
{
INIReader reader(filename, true);
if ((readSuccess = reader >> originalData))
{
lineData = reader.getLines();
fileIsBOM = reader.isBOM;
}
}
if (!readSuccess)
Expand All @@ -693,6 +707,10 @@ namespace mINI
std::ofstream fileWriteStream(filename, std::ios::out | std::ios::binary);
if (fileWriteStream.is_open())
{
if (fileIsBOM) {
const char utf8_BOM[3] = {(char)0xEF, (char)0xBB, (char)0xBF};
fileWriteStream.write(utf8_BOM, 3);
}
if (output.size())
{
auto line = output.begin();
Expand Down
11 changes: 11 additions & 0 deletions tests/testutf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ const lest::test mINI_tests[] = {
EXPECT(ini["section"]["key"] == "value");
EXPECT(ini["section"]["key2"] == "value2");
EXPECT(ini["section2"]["key"] == "value");
},
CASE("Test: Write to UTF-8 BOM encoded file")
{
mINI::INIFile file("utf8bom.ini");
mINI::INIStructure ini;
EXPECT(file.read(ini) == true);
// update
ini["section"]["key"] = "something else";
// write
EXPECT(file.write(ini) == true);
// todo expect BOM encoding
}
};

Expand Down

0 comments on commit b5f0dc2

Please sign in to comment.