Skip to content

Commit

Permalink
bugfix (thx to Yodalf); more options
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Hoeft committed Dec 14, 2006
1 parent 7da4a9a commit 66d8e99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
25 changes: 15 additions & 10 deletions CIniFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ CIniFile::~CIniFile()
{
}

CIniFile::CIniFile(const char* file, bool remove_leading_spaces, bool remove_trailing_spaces)
CIniFile::CIniFile(const char* file, bool remove_leading_spaces, bool remove_trailing_spaces, bool overwrite_existing /*= true*/)
{
m_DelFirstSpaces = remove_leading_spaces;
m_DelLastSpaces = remove_trailing_spaces;
ReadIniFile(file);
ReadIniFile(file, false, overwrite_existing);
}

bool CIniFile::ReadIniFile(const char* file, bool append)
bool CIniFile::ReadIniFile(const char* file, bool append, bool overwrite_existing /*= true*/)
{
#ifdef LINUX
stat f_stats;
Expand Down Expand Up @@ -86,7 +86,7 @@ bool CIniFile::ReadIniFile(const char* file, bool append)
else if((f = line.find(_T("="))) != genstr::npos)
{
// get key and value
genstr key = RemoveSpaces(line.substr(0, f), true, true);
genstr key = RemoveSpaces(line.substr(0, f), m_DelFirstSpaces, m_DelFirstSpaces);
if((f2 = key.find(_T(";"))) != genstr::npos)
continue;

Expand All @@ -95,7 +95,7 @@ bool CIniFile::ReadIniFile(const char* file, bool append)
if ((f = value.find(_T(";"))) != genstr::npos) {
value.erase(f);
}
SetStr(value, cur_section, key);
SetStr(value, cur_section, key, overwrite_existing);
}
}

Expand Down Expand Up @@ -240,16 +240,22 @@ bool CIniFile::WriteIniFile(const char* file, bool write_unicode)
}


void CIniFile::SetStr(genstr val, genstr strSection, genstr strKey)
void CIniFile::SetStr(genstr val, genstr strSection, genstr strKey, bool overwrite_existing /*= true*/)
{
if(!overwrite_existing)
{
genstr tmp;
if(GetStr(tmp, strSection, strKey))
return;
}
m_mRealSecNames[ChgCase(strSection, false)] = strSection;
strSection = ChgCase(strSection, false);
m_mRealKeyNames[ChgCase(strKey, false)] = strKey;
strKey = ChgCase(strKey, false);
m_mIniData[strSection][strKey] = val;
}

void CIniFile::SetLong(long val, genstr strSection, genstr strKey)
void CIniFile::SetLong(long val, genstr strSection, genstr strKey, bool overwrite_existing /*= true*/)
{
char longStr[64];
sprintf(longStr, "%d", val);
Expand All @@ -261,7 +267,7 @@ void CIniFile::SetLong(long val, genstr strSection, genstr strKey)
#else
strncpy(str, longStr, 63);
#endif
SetStr(str, strSection, strKey);
SetStr(str, strSection, strKey, overwrite_existing);
}

genstr CIniFile::RemoveSpaces(genstr str, bool remove_first, bool remove_last)
Expand All @@ -276,10 +282,9 @@ genstr CIniFile::RemoveSpaces(genstr str, bool remove_first, bool remove_last)
if(remove_last)
{
int i = str.length();
while(str[i] == ' ')
while(str[i - 1] == ' ')
i--;
str.erase(i);

}
return str;
}
Expand Down
12 changes: 6 additions & 6 deletions CIniFile.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
SpeedSim - a OGame (www.ogame.org) combat simulator
Copyright (C) 2004-2006 Maximialian Matth�& Nicolas H�t
Copyright (C) 2004-2006 Maximialian Matthé & Nicolas Höft
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
Expand Down Expand Up @@ -33,7 +33,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#undef _tcslen
#undef _tcstol

//#define UNICODE
#define UNICODE
#ifdef UNICODE
typedef wchar_t TCHAR;
#define _T(x) L ## x
Expand Down Expand Up @@ -72,11 +72,11 @@ class CINIFILE_API CIniFile
{
public:
CIniFile();
CIniFile(const char* file, bool remove_leading_spaces = false, bool remove_trailing_spaces = true);
CIniFile(const char* file, bool remove_leading_spaces = false, bool remove_trailing_spaces = true, bool overwrite_existing = true);
~CIniFile();

// reads out all keys from a file
bool ReadIniFile(const char* file, bool append = false);
bool ReadIniFile(const char* file, bool append = false, bool overwrite_existing = true);
// writes current ini data into file
bool WriteIniFile(const char* file, bool write_unicode = false);

Expand All @@ -85,8 +85,8 @@ class CINIFILE_API CIniFile
bool RemoveKey(genstr strSection, genstr strKey);
bool RemoveSection(genstr strSection);

void SetStr(genstr val, genstr strSection, genstr strKey);
void SetLong(long val, genstr strSection, genstr strKey);
void SetStr(genstr val, genstr strSection, genstr strKey, bool overwrite_existing = true);
void SetLong(long val, genstr strSection, genstr strKey, bool overwrite_existing = true);

void SetRemoveSpaces(bool remove_leading, bool remove_trailing);

Expand Down

0 comments on commit 66d8e99

Please sign in to comment.