Skip to content

Commit

Permalink
Handle newlines in cIniFile (#5447)
Browse files Browse the repository at this point in the history
* Handle newlines during read and write in cIniFile

When reading the ini file, replace \n with newline. When writing,
replace the newline with \n.

* Use ReplaceString instead of regex in IniFile

* Update cIniFile description

* Removed duplicate variable

* Revert "Removed duplicate variable"

This reverts commit de11bac.

* Removed duplicate variable

Now without plugin changes

---------

Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com>
  • Loading branch information
MacaylaMarvelous81 and 12xx12 committed Mar 22, 2023
1 parent c747b49 commit fddbf65
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Server/Plugins/APIDump/APIDesc.lua
Expand Up @@ -5375,6 +5375,10 @@ ValueName0=SomeOtherValue
insert values by hand. Then you can store the object's contents to a disk file using WriteFile(), or
just forget everything by destroying the object. Note that the file operations are quite slow.</p>
<p>
Cuberite will write the characters '\n' in place of line breaks in the values of the cIniFile when
it is being stored into a file. It will also replace '\n' with line breaks when it reads an INI
file.
<p>
For storing high-volume low-latency data, use the {{sqlite3}} class. For storing
hierarchically-structured data, use the XML format, using the LuaExpat parser in the {{lxp}} class.
]],
Expand Down
6 changes: 5 additions & 1 deletion src/IniFile.cpp
Expand Up @@ -147,6 +147,7 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect)
{
valuename = line.substr(0, pLeft);
value = TrimString(line.substr(pLeft + 1));
ReplaceString(value, "\\n", "\n");
AddValue(keyname, valuename, value);
break;
}
Expand Down Expand Up @@ -191,6 +192,7 @@ bool cIniFile::WriteFile(const AString & a_FileName) const
// Normally you would use ofstream, but the SGI CC compiler has
// a few bugs with ofstream. So ... fstream used.
fstream f;
AString writevalue;

f.open((a_FileName).c_str(), ios::out);
if (f.fail())
Expand Down Expand Up @@ -223,7 +225,9 @@ bool cIniFile::WriteFile(const AString & a_FileName) const
// Values.
for (size_t valueID = 0; valueID < m_Keys[keyID].m_Names.size(); ++valueID)
{
f << m_Keys[keyID].m_Names[valueID] << '=' << m_Keys[keyID].m_Values[valueID] << iniEOL;
writevalue = m_Keys[keyID].m_Values[valueID];
ReplaceString(writevalue, "\n", "\\n");
f << m_Keys[keyID].m_Names[valueID] << '=' << writevalue << iniEOL;
}
f << iniEOL;
}
Expand Down

0 comments on commit fddbf65

Please sign in to comment.