From 851839cdea28406c916bffef019c009db15d5469 Mon Sep 17 00:00:00 2001 From: OV2 Date: Thu, 30 Sep 2010 23:43:05 +0200 Subject: [PATCH] faster config file saving --- conffile.cpp | 33 +++++++++++++++++---------------- conffile.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/conffile.cpp b/conffile.cpp index daa38551..94983e72 100644 --- a/conffile.cpp +++ b/conffile.cpp @@ -186,11 +186,6 @@ #define snprintf _snprintf // needs ANSI compliant name #endif -#ifndef MAX -# define MAX(a,b) ((a) > (b)? (a) : (b)) -# define MIN(a,b) ((a) < (b)? (a) : (b)) -#endif - #define SORT_SECTIONS_BY_SIZE // output using namespace std; @@ -208,6 +203,7 @@ ConfigFile::ConfigFile(void) { void ConfigFile::Clear(void){ data.clear(); + sectionSizes.ClearSections(); linectr = 0; } @@ -287,8 +283,10 @@ void ConfigFile::LoadFile(Reader *r, const char *name){ ConfigEntry e(line, section, key, val); e.comment = comment; - data.erase(e); + if(data.erase(e)) + sectionSizes.DecreaseSectionSize(e.section); data.insert(e); + sectionSizes.IncreaseSectionSize(e.section); } while(!eof); curConfigFile = NULL; } @@ -470,12 +468,14 @@ bool ConfigFile::SetString(const char *key, string val, const char *comment){ if(i!=data.end()){ e.line=i->line; data.erase(e); + sectionSizes.DecreaseSectionSize(e.section); ret=true; } if((i==data.end() && (!alphaSort || timeSort)) || (!alphaSort && timeSort)) e.line = linectr++; data.insert(e); + sectionSizes.IncreaseSectionSize(e.section); return ret; } @@ -562,7 +562,12 @@ const char* ConfigFile::GetComment(const char *key) } bool ConfigFile::DeleteKey(const char *key){ - return (data.erase(ConfigEntry(key))>0); + ConfigEntry e = ConfigEntry(key); + if(data.erase(e)) { + sectionSizes.DecreaseSectionSize(e.section); + return true; + } + return false; } /***********************************************/ @@ -574,6 +579,7 @@ bool ConfigFile::DeleteSection(const char *section){ if(s==data.end()) return false; for(e=s; e!=data.end() && e->section==section; e++) ; data.erase(s, e); + sectionSizes.DeleteSection(section); return true; } @@ -589,13 +595,8 @@ ConfigFile::secvec_t ConfigFile::GetSection(const char *section){ return v; } -int ConfigFile::GetSectionSize(const char *section){ - int count=0; - const unsigned int seclen=strlen(section); - set::iterator i; - for(i=data.begin(); i!=data.end(); i++) - if(i->section==section || !strncasecmp(section,i->section.c_str(),MIN(seclen,i->section.size()))) count++; - return count; +int ConfigFile::GetSectionSize(const std::string section){ + return sectionSizes.GetSectionSize(section); } // Clears all key-value pairs that didn't receive a "Get" or "Exists" command @@ -621,8 +622,8 @@ void ConfigFile::ClearLines() bool ConfigFile::ConfigEntry::section_then_key_less::operator()(const ConfigEntry &a, const ConfigEntry &b) { if(curConfigFile && a.section!=b.section){ - const int sva = curConfigFile->GetSectionSize(a.section.c_str()); - const int svb = curConfigFile->GetSectionSize(b.section.c_str()); + const int sva = curConfigFile->GetSectionSize(a.section); + const int svb = curConfigFile->GetSectionSize(b.section); if(svasvb) return false; return a.section +#include #include #include @@ -188,6 +189,11 @@ #include "snes9x.h" #include "reader.h" +#ifndef MAX +# define MAX(a,b) ((a) > (b)? (a) : (b)) +# define MIN(a,b) ((a) < (b)? (a) : (b)) +#endif + class ConfigFile { public: ConfigFile(void); @@ -222,7 +228,7 @@ class ConfigFile { bool DeleteSection(const char *section); typedef std::vector > secvec_t; secvec_t GetSection(const char *section); - int GetSectionSize(const char *section); + int GetSectionSize(const std::string section); // Clears all key-value pairs that didn't receive a Set command, or a Get command with autoAdd on void ClearUnused(void); @@ -349,7 +355,47 @@ class ConfigFile { friend struct key_less; friend struct line_less; }; + class SectionSizes { + protected: + std::map sections; + + public: + uint32 GetSectionSize(const std::string section) { + uint32 count=0; + uint32 seclen; + std::map::iterator it; + for(it=sections.begin(); it!=sections.end(); it++) { + seclen = MIN(section.size(),it->first.size()); + if(it->first==section || !section.compare(0,seclen,it->first,0,seclen)) count+=it->second; + } + return count; + } + + void IncreaseSectionSize(const std::string section) { + std::map::iterator it=sections.find(section); + if(it!=sections.end()) + it->second++; + else + sections.insert(std::pair(section,1)); + } + + void DecreaseSectionSize(const std::string section) { + std::map::iterator it=sections.find(section); + if(it!=sections.end()) + it->second--; + } + + void ClearSections() { + sections.clear(); + } + + void DeleteSection(const std::string section) { + sections.erase(section); + } + + }; std::set data; + SectionSizes sectionSizes; int linectr; static bool defaultAutoAdd; static bool niceAlignment;