cfg: Reimplement ConfigFile using C++ maps and strings

This commit is contained in:
Jan Holthuis 2015-08-18 19:36:19 +02:00 committed by Stefanos Kornilios Mitsis Poiitidis
parent f065bc5289
commit 7a5c90ccca
3 changed files with 255 additions and 284 deletions

View File

@ -28,13 +28,13 @@ void savecfgf()
printf("Error : Unable to open file for saving \n");
else
{
cfgdb.SaveFile(cfgfile);
cfgdb.save(cfgfile);
fclose(cfgfile);
}
}
void cfgSaveStr(const wchar * Section, const wchar * Key, const wchar * String)
{
cfgdb.GetEntry(Section)->SetEntry(Key,String,CEM_SAVE);
cfgdb.set(string(Section), string(Key), string(String));
savecfgf();
//WritePrivateProfileString(Section,Key,String,cfgPath);
}
@ -96,16 +96,16 @@ bool cfgOpen()
}
}
cfgdb.ParseFile(cfgfile);
cfgdb.parse(cfgfile);
for (size_t i=0;i<vlist.size();i++)
/*for (size_t i=0;i<vlist.size();i++)
{
cfgdb.GetEntry(vlist[i].s)->SetEntry(vlist[i].n,vlist[i].v,CEM_VIRTUAL);
}
}*/
if (cfgfile)
{
cfgdb.SaveFile(cfgfile);
cfgdb.save(cfgfile);
fclose(cfgfile);
}
return true;
@ -119,32 +119,48 @@ bool cfgOpen()
//2 : found section & key
s32 cfgExists(const wchar * Section, const wchar * Key)
{
return cfgdb.Exists(Section, Key);
if(cfgdb.has_entry(string(Section), string(Key)))
{
return 2;
}
else
{
return (cfgdb.has_section(string(Section)) ? 1 : 0);
}
}
void cfgLoadStr(const wchar * Section, const wchar * Key, wchar * Return,const wchar* Default)
{
return cfgdb.LoadStr(Section, Key, Return, Default);
string value = cfgdb.get(Section, Key, Default);
strcpy(Return, value.c_str());
}
string cfgLoadStr(const wchar * Section, const wchar * Key, const wchar* Default)
{
return cfgdb.LoadStr(Section, Key, Default);
if(!cfgdb.has_entry(string(Section), string(Key)))
{
cfgSaveStr(Section, Key, Default);
}
return cfgdb.get(string(Section), string(Key), string(Default));
}
//These are helpers , mainly :)
s32 cfgLoadInt(const wchar * Section, const wchar * Key,s32 Default)
{
return cfgdb.LoadInt(Section, Key, Default);
}
void cfgSaveInt(const wchar * Section, const wchar * Key, s32 Int)
{
wchar tmp[32];
sprintf(tmp,"%d", Int);
cfgSaveStr(Section,Key,tmp);
return cfgdb.set_int(string(Section), string(Key), Int);
}
s32 cfgLoadInt(const wchar * Section, const wchar * Key,s32 Default)
{
if(!cfgdb.has_entry(string(Section), string(Key)))
{
cfgSaveInt(Section, Key, Default);
}
return cfgdb.get_int(string(Section), string(Key), Default);
}
void cfgSetVirtual(const wchar * Section, const wchar * Key, const wchar * String)
{
vlist.push_back(vitem(Section,Key,String));
//vlist.push_back(vitem(Section,Key,String));
//cfgdb.GetEntry(Section,CEM_VIRTUAL)->SetEntry(Key,String,CEM_VIRTUAL);
}

View File

@ -1,299 +1,252 @@
#include "ini.h"
#include <sstream>
wchar* trim_ws(wchar* str);
ConfigEntry::ConfigEntry(ConfigEntry* pp)
/* ConfigEntry */
string ConfigEntry::get_string()
{
next=pp;
flags=0;
return this->value;
}
void ConfigEntry::SaveFile(FILE* file)
int ConfigEntry::get_int()
{
if (flags & CEM_SAVE)
fprintf(file,"%s=%s\n",name.c_str(),value.c_str());
}
string ConfigEntry::GetValue()
{
if (flags&CEM_VIRTUAL)
return valueVirtual;
else
return value;
}
ConfigSection::ConfigSection(ConfigSection* pp)
{
next=pp;
flags=0;
entrys=0;
}
ConfigEntry* ConfigSection::FindEntry(string name)
{
ConfigEntry* c= entrys;
while(c)
if (strstr(this->value.c_str(), "0x") != NULL)
{
if (stricmp(name.c_str(),c->name.c_str())==0)
return c;
c=c->next;
return strtol(this->value.c_str(), NULL, 16);
}
else
{
return atoi(this->value.c_str());
}
return 0;
}
void ConfigSection::SetEntry(string name,string value,u32 eflags)
bool ConfigEntry::get_bool()
{
ConfigEntry* c=FindEntry(name);
if (c)
if (strcmp(this->value.c_str(), "yes") == 0 ||
strcmp(this->value.c_str(), "true") == 0 ||
strcmp(this->value.c_str(), "on") == 0 ||
strcmp(this->value.c_str(), "1") == 0)
{
return true;
}
else
{
return false;
}
}
/* ConfigSection */
bool ConfigSection::has_entry(string name)
{
return (this->entries.count(name) == 1);
};
ConfigEntry* ConfigSection::get_entry(string name)
{
if(this->has_entry(name))
{
return &this->entries[name];
}
return NULL;
};
void ConfigSection::set(string name, string value, int flags)
{
ConfigEntry new_entry = { value, flags };
this->entries[name] = new_entry;
};
/* ConfigFile */
bool ConfigFile::has_section(string name)
{
return (this->sections.count(name) == 1);
};
bool ConfigFile::has_entry(string section_name, string entry_name)
{
ConfigSection* section = this->get_section(section_name);
return ((section == NULL) ? false : section->has_entry(entry_name));
}
ConfigSection* ConfigFile::add_section(string name)
{
ConfigSection new_section;
this->sections.insert(std::make_pair(name, new_section));
return &this->sections[name];
};
ConfigSection* ConfigFile::get_section(string name)
{
if(this->has_section(name))
{
return &this->sections[name];
}
return NULL;
};
ConfigEntry* ConfigFile::get_entry(string section_name, string entry_name)
{
ConfigSection* section = this->get_section(section_name);
if(section == NULL)
{
return NULL;
}
return section->get_entry(entry_name);
}
string ConfigFile::get(string section_name, string entry_name, string default_value)
{
ConfigEntry* entry = this->get_entry(section_name, entry_name);
if (entry == NULL)
{
return default_value;
}
else
{
return entry->get_string();
}
}
int ConfigFile::get_int(string section_name, string entry_name, int default_value)
{
ConfigEntry* entry = this->get_entry(section_name, entry_name);
if (entry == NULL)
{
return default_value;
}
else
{
return entry->get_int();
}
}
bool ConfigFile::get_bool(string section_name, string entry_name, bool default_value)
{
ConfigEntry* entry = this->get_entry(section_name, entry_name);
if (entry == NULL)
{
return default_value;
}
else
{
return entry->get_bool();
}
}
void ConfigFile::set(string section_name, string entry_name, string value, int flags)
{
ConfigSection* section = this->get_section(section_name);
if(section == NULL)
{
section = this->add_section(section_name);
}
section->set(entry_name, value, flags);
};
void ConfigFile::set_int(string section_name, string entry_name, int value, int flags)
{
std::stringstream str_value;
str_value << value;
this->set(section_name, entry_name, str_value.str());
}
void ConfigFile::set_bool(string section_name, string entry_name, bool value, int flags)
{
string str_value = (value ? "yes" : "no");
this->set(section_name, entry_name, str_value);
}
void ConfigFile::parse(FILE* file)
{
if(file == NULL)
{
//readonly is read only =)
if (c->flags & CEM_READONLY)
return;
//virtual : save only if different value
if (c->flags & CEM_VIRTUAL)
{
if(stricmp(c->valueVirtual.c_str(),value.c_str())==0)
return;
c->flags&=~CEM_VIRTUAL;
}
}
else
{
entrys=c= new ConfigEntry(entrys);
c->name=name;
}
verify(!(c->flags&(CEM_VIRTUAL|CEM_READONLY)));
//Virtual
//Virtual | ReadOnly
//Save
if (eflags & CEM_VIRTUAL)
{
verify(!(eflags & CEM_SAVE));
c->flags|=eflags;
c->valueVirtual=value;
}
else if (eflags & CEM_SAVE)
{
verify(!(eflags & (CEM_VIRTUAL|CEM_READONLY)));
flags|=CEM_SAVE;
c->flags|=CEM_SAVE;
c->value=value;
}
else
{
die("Invalid eflags value");
}
}
ConfigSection::~ConfigSection()
{
ConfigEntry* n=entrys;
while(n)
{
ConfigEntry* p=n;
n=n->next;
delete p;
}
}
void ConfigSection::SaveFile(FILE* file)
{
if (flags&CEM_SAVE)
{
fprintf(file,"[%s]\n",name.c_str());
vector<ConfigEntry*> stuff;
ConfigEntry* n=entrys;
while(n)
{
stuff.push_back(n);
n=n->next;
}
for (int i=stuff.size()-1;i>=0;i--)
{
stuff[i]->SaveFile(file);
}
fprintf(file,"\n");
}
}
ConfigSection* ConfigFile::FindSection(string name)
{
ConfigSection* c= entrys;
while(c)
{
if (stricmp(name.c_str(),c->name.c_str())==0)
return c;
c=c->next;
}
return 0;
}
ConfigSection* ConfigFile::GetEntry(string name)
{
ConfigSection* c=FindSection(name);
if (!c)
{
entrys=c= new ConfigSection(entrys);
c->name=name;
}
return c;
}
ConfigFile::~ConfigFile()
{
ConfigSection* n=entrys;
while(n)
{
ConfigSection* p=n;
n=n->next;
delete p;
}
}
void ConfigFile::ParseFile(FILE* file)
{
wchar line[512];
wchar cur_sect[512]={0};
int cline=0;
char line[512];
char current_section[512] = { '\0' };
int cline = 0;
while(file && !feof(file))
{
fgets(line,512,file);
fgets(line, 512, file);
if (feof(file))
{
break;
}
cline++;
if (strlen(line)<3)
continue;
if (line[strlen(line)-1]=='\r' || line[strlen(line)-1]=='\n')
line[strlen(line)-1]=0;
wchar* tl=trim_ws(line);
if (tl[0]=='[' && tl[strlen(tl)-1]==']')
if (strlen(line) < 3)
{
tl[strlen(tl)-1]=0;
strcpy(cur_sect,tl+1);
trim_ws(cur_sect);
}
else
{
if (cur_sect[0]==0)
continue;//no open section
wchar* str1=strstr(tl,"=");
if (!str1)
{
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline,tl);
continue;
}
*str1=0;
str1++;
wchar* v=trim_ws(str1);
wchar* k=trim_ws(tl);
if (v && k)
{
ConfigSection*cs=this->GetEntry(cur_sect);
//if (!cs->FindEntry(k))
cs->SetEntry(k,v,CEM_SAVE|CEM_LOAD);
if (line[strlen(line)-1] == '\r' ||
line[strlen(line)-1] == '\n')
{
line[strlen(line)-1] = '\0';
}
char* tl = trim_ws(line);
if (tl[0] == '[' && tl[strlen(tl)-1] == ']')
{
tl[strlen(tl)-1] = '\0';
strcpy(current_section, tl+1);
trim_ws(current_section);
}
else
{
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline,tl);
}
}
}
}
void ConfigFile::SaveFile(FILE* file)
{
vector<ConfigSection*> stuff;
ConfigSection* n=entrys;
while(n)
if (strlen(current_section) == 0)
{
stuff.push_back(n);
n=n->next;
continue; //no open section
}
for (int i=stuff.size()-1;i>=0;i--)
char* separator = strstr(tl, "=");
if (!separator)
{
if (stuff[i]->name!="emu")
stuff[i]->SaveFile(file);
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline, tl);
continue;
}
}
s32 ConfigFile::Exists(const wchar * Section, const wchar * Key)
{
if (Section==0)
return -1;
//return cfgRead(Section,Key,0);
ConfigSection*cs= this->FindSection(Section);
if (cs == 0)
return 0;
*separator = '\0';
if (Key==0)
return 1;
ConfigEntry* ce=cs->FindEntry(Key);
if (ce!=0)
return 2;
else
return 0;
}
void ConfigFile::LoadStr(const wchar * Section, const wchar * Key, wchar * Return,const wchar* Default)
{
verify(Return!=0);
string value = this->LoadStr(Section, Key, Default);
strcpy(Return, value.c_str());
}
string ConfigFile::LoadStr(const wchar * Section, const wchar * Key, const wchar* Default)
{
verify(Section != 0 && strlen(Section) != 0);
verify(Key != 0 && strlen(Key) != 0);
if (Default == 0)
Default = "";
ConfigSection* cs = this->GetEntry(Section);
ConfigEntry* ce = cs->FindEntry(Key);
if (!ce)
char* name = trim_ws(tl);
char* value = trim_ws(separator + 1);
if (name == NULL || value == NULL)
{
cs->SetEntry(Key, Default, CEM_SAVE);
return Default;
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline, tl);
continue;
}
else
{
return ce->GetValue();
this->set(string(current_section), string(name), string(value));
}
}
}
}
s32 ConfigFile::LoadInt(const wchar * Section, const wchar * Key,s32 Default)
void ConfigFile::save(FILE* file)
{
wchar temp_d[30];
wchar temp_o[30];
sprintf(temp_d,"%d",Default);
this->LoadStr(Section,Key,temp_o,temp_d);
if (strstr(temp_o, "0x") != NULL)
for(std::map<string, ConfigSection>::iterator section_it = this->sections.begin();
section_it != this->sections.end(); section_it++)
{
return strtol(temp_o, NULL, 16);
string section_name = section_it->first;
ConfigSection section = section_it->second;
fprintf(file, "[%s]\n", section_name.c_str());
for(std::map<string, ConfigEntry>::iterator entry_it = section.entries.begin();
entry_it != section.entries.end(); entry_it++)
{
string entry_name = entry_it->first;
ConfigEntry entry = entry_it->second;
fprintf(file, "%s = %s\n", entry_name.c_str(), entry.get_string().c_str());
}
else
{
return atoi(temp_o);
fputs("\n", file);
}
}

View File

@ -1,5 +1,6 @@
#pragma once
#include "types.h"
#include <map>
//A config remains virtual only as long as a write at it
//doesn't override the virtual value.While a config is virtual, a copy of its 'real' value is held and preserved
@ -13,41 +14,42 @@
//the move is from loading ?
#define CEM_LOAD 8
struct ConfigEntry
{
u32 flags;
string name;
struct ConfigEntry {
string value;
string valueVirtual;
ConfigEntry* next;
ConfigEntry(ConfigEntry*);
string GetValue();
void SaveFile(FILE*);
int flags; //TODO: These have no effect right now
string get_string();
int get_int();
bool get_bool();
};
struct ConfigSection
{
u32 flags;
string name;
ConfigEntry* entrys;
ConfigSection* next;
~ConfigSection();
ConfigSection(ConfigSection*);
ConfigEntry* FindEntry(string);
void SetEntry(string, string, u32);
void SaveFile(FILE*);
struct ConfigSection {
std::map<string, ConfigEntry> entries;
bool has_entry(string name);
void set(string name, string value, int flags);
ConfigEntry* get_entry(string name);
};
struct ConfigFile
{
ConfigSection* entrys;
~ConfigFile();
void ParseFile(FILE*);
void SaveFile(FILE*);
ConfigSection* FindSection(string);
ConfigSection* GetEntry(string);
s32 Exists(const wchar *, const wchar *);
void LoadStr(const wchar *, const wchar *, wchar *,const wchar*);
string LoadStr(const wchar *, const wchar *, const wchar*);
s32 LoadInt(const wchar *, const wchar *,s32);
struct ConfigFile {
private:
std::map<string, ConfigSection> sections;
ConfigSection* add_section(string name);
ConfigSection* get_section(string name);
ConfigEntry* get_entry(string section_name, string entry_name);
public:
bool has_section(string name);
bool has_entry(string section_name, string entry_name);
void parse(FILE* fd);
void save(FILE* fd);
/* getting values */
string get(string section_name, string entry_name, string default_value = "");
int get_int(string section_name, string entry_name, int default_value = 0);
bool get_bool(string section_name, string entry_name, bool default_value = false);
/* setting values */
void set(string section_name, string entry_name, string value, int flags = 0);
void set_int(string section_name, string entry_name, int value, int flags = 0);
void set_bool(string section_name, string entry_name, bool value, int flags = 0);
};