Skip to content

Commit b5f0dc2

Browse files
committed
Release 0.9.13
1 parent 41435aa commit b5f0dc2

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.9.13 (April 25, 2022)
2+
- `BUGFIX` - Writer now understands UTF-8 BOM-encoded files. ([#7](https://github.com/pulzed/mINI/issues/17))
3+
- `BUGFIX` - Fixes a bug introduced in 0.9.12 where reader would break when reading empty files.
4+
15
## 0.9.12 (April 24, 2022)
26
- `BUGFIX` - Fixes parser breaking for UTF-8 BOM-encoded files. ([#7](https://github.com/pulzed/mINI/issues/17))
37

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# mINI
22

3-
v0.9.12
3+
v0.9.13
44

55
## Info
66

src/mini/ini.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
///////////////////////////////////////////////////////////////////////////////
2525
//
26-
// /mINI/ v0.9.12
26+
// /mINI/ v0.9.13
2727
// An INI file reader and writer for the modern age.
2828
//
2929
///////////////////////////////////////////////////////////////////////////////
@@ -332,19 +332,31 @@ namespace mINI
332332
using T_LineData = std::vector<std::string>;
333333
using T_LineDataPtr = std::shared_ptr<T_LineData>;
334334

335+
bool isBOM = false;
336+
335337
private:
336338
std::ifstream fileReadStream;
337339
T_LineDataPtr lineData;
338340

339341
T_LineData readFile()
340342
{
341-
const char header[3] = {(char)fileReadStream.get(), (char)fileReadStream.get(), (char)fileReadStream.get()};
342-
const bool isBOM = header[0] == (char)0xEF && header[1] == (char)0xBB && header[2] == (char)0xBF;
343-
std::string fileContents;
344343
fileReadStream.seekg(0, std::ios::end);
345-
fileContents.resize(static_cast<std::size_t>(fileReadStream.tellg()));
344+
const std::size_t fileSize = static_cast<std::size_t>(fileReadStream.tellg());
345+
fileReadStream.seekg(0, std::ios::beg);
346+
if (fileSize >= 3) {
347+
const char header[3] = {
348+
static_cast<char>(fileReadStream.get()),
349+
static_cast<char>(fileReadStream.get()),
350+
static_cast<char>(fileReadStream.get())
351+
};
352+
isBOM = header[0] == (char)0xEF && header[1] == (char)0xBB && header[2] == (char)0xBF;
353+
}
354+
else {
355+
isBOM = false;
356+
}
357+
std::string fileContents;
358+
fileContents.resize(fileSize);
346359
fileReadStream.seekg(isBOM ? 3 : 0, std::ios::beg);
347-
std::size_t fileSize = fileContents.size();
348360
fileReadStream.read(&fileContents[0], fileSize);
349361
fileReadStream.close();
350362
T_LineData output;
@@ -678,11 +690,13 @@ namespace mINI
678690
INIStructure originalData;
679691
T_LineDataPtr lineData;
680692
bool readSuccess = false;
693+
bool fileIsBOM = false;
681694
{
682695
INIReader reader(filename, true);
683696
if ((readSuccess = reader >> originalData))
684697
{
685698
lineData = reader.getLines();
699+
fileIsBOM = reader.isBOM;
686700
}
687701
}
688702
if (!readSuccess)
@@ -693,6 +707,10 @@ namespace mINI
693707
std::ofstream fileWriteStream(filename, std::ios::out | std::ios::binary);
694708
if (fileWriteStream.is_open())
695709
{
710+
if (fileIsBOM) {
711+
const char utf8_BOM[3] = {(char)0xEF, (char)0xBB, (char)0xBF};
712+
fileWriteStream.write(utf8_BOM, 3);
713+
}
696714
if (output.size())
697715
{
698716
auto line = output.begin();

tests/testutf8.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ const lest::test mINI_tests[] = {
109109
EXPECT(ini["section"]["key"] == "value");
110110
EXPECT(ini["section"]["key2"] == "value2");
111111
EXPECT(ini["section2"]["key"] == "value");
112+
},
113+
CASE("Test: Write to UTF-8 BOM encoded file")
114+
{
115+
mINI::INIFile file("utf8bom.ini");
116+
mINI::INIStructure ini;
117+
EXPECT(file.read(ini) == true);
118+
// update
119+
ini["section"]["key"] = "something else";
120+
// write
121+
EXPECT(file.write(ini) == true);
122+
// todo expect BOM encoding
112123
}
113124
};
114125

0 commit comments

Comments
 (0)