Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle newlines in cIniFile #5447

Merged
merged 7 commits into from Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Server/Plugins/APIDump/APIDesc.lua
Expand Up @@ -5372,6 +5372,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
12 changes: 9 additions & 3 deletions src/IniFile.cpp
Expand Up @@ -56,7 +56,7 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect)
// a few bugs with ifstream. So ... fstream used.
fstream f;
AString line;
AString keyname, valuename, value;
AString keyname, rawvalue, valuename, value;
AString::size_type pLeft, pRight;
bool IsFromExampleRedirect = false;

Expand Down Expand Up @@ -146,7 +146,9 @@ bool cIniFile::ReadFile(const AString & a_FileName, bool a_AllowExampleRedirect)
case '=':
{
valuename = line.substr(0, pLeft);
value = TrimString(line.substr(pLeft + 1));
rawvalue = TrimString(line.substr(pLeft + 1));
value = rawvalue;
ReplaceString(value, "\\n", "\n");
AddValue(keyname, valuename, value);
break;
}
Expand Down Expand Up @@ -191,6 +193,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 runvalue, writevalue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the point of having two separate strings here is - since writevalue is always set to the value of runvalue! Same for rawvalue up above.

Please remove one.


f.open((a_FileName).c_str(), ios::out);
if (f.fail())
Expand Down Expand Up @@ -223,7 +226,10 @@ 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;
runvalue = m_Keys[keyID].m_Values[valueID];
writevalue = runvalue;
ReplaceString(writevalue, "\n", "\\n");
f << m_Keys[keyID].m_Names[valueID] << '=' << writevalue << iniEOL;
}
f << iniEOL;
}
Expand Down