From 66d8e991c2452748375e5a4dc8bda2b984a0d5df Mon Sep 17 00:00:00 2001 From: Nicolas Hoeft Date: Thu, 14 Dec 2006 19:31:19 +0000 Subject: [PATCH] bugfix (thx to Yodalf); more options --- CIniFile.cpp | 25 +++++++++++++++---------- CIniFile.h | 12 ++++++------ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/CIniFile.cpp b/CIniFile.cpp index bf92256..e1021ad 100644 --- a/CIniFile.cpp +++ b/CIniFile.cpp @@ -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; @@ -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; @@ -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); } } @@ -240,8 +240,14 @@ 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; @@ -249,7 +255,7 @@ void CIniFile::SetStr(genstr val, genstr strSection, genstr strKey) 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); @@ -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) @@ -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; } diff --git a/CIniFile.h b/CIniFile.h index 9d7e05a..90af9a8 100644 --- a/CIniFile.h +++ b/CIniFile.h @@ -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 @@ -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 @@ -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); @@ -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);