cfg: Add virtual entry support

This commit is contained in:
Jan Holthuis 2015-08-19 04:43:54 +02:00 committed by Stefanos Kornilios Mitsis Poiitidis
parent 89e4bf78cc
commit 42f7919c48
4 changed files with 65 additions and 66 deletions

View File

@ -10,15 +10,6 @@
string cfgPath;
struct vitem
{
string s;
string n;
string v;
vitem(string a,string b,string c){s=a;n=b;v=c;}
};
vector<vitem> vlist;
ConfigFile cfgdb;
void savecfgf()
@ -98,11 +89,6 @@ bool cfgOpen()
cfgdb.parse(cfgfile);
/*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.save(cfgfile);
@ -160,7 +146,5 @@ s32 cfgLoadInt(const wchar * Section, const wchar * Key,s32 Default)
void cfgSetVirtual(const wchar * Section, const wchar * Key, const wchar * String)
{
//vlist.push_back(vitem(Section,Key,String));
//cfgdb.GetEntry(Section,CEM_VIRTUAL)->SetEntry(Key,String,CEM_VIRTUAL);
cfgdb.set(string(Section), string(Key), string(String), true);
}

View File

@ -53,49 +53,76 @@ ConfigEntry* ConfigSection::get_entry(string name)
return NULL;
};
void ConfigSection::set(string name, string value, int flags)
void ConfigSection::set(string name, string value)
{
ConfigEntry new_entry = { value, flags };
ConfigEntry new_entry = { value };
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) && section->has_entry(entry_name));
}
ConfigSection* ConfigFile::add_section(string name)
ConfigSection* ConfigFile::add_section(string name, bool is_virtual)
{
ConfigSection new_section;
if (is_virtual)
{
this->virtual_sections.insert(std::make_pair(name, new_section));
return &this->virtual_sections[name];
}
this->sections.insert(std::make_pair(name, new_section));
return &this->sections[name];
};
ConfigSection* ConfigFile::get_section(string name)
bool ConfigFile::has_section(string name)
{
if(this->has_section(name))
return (this->virtual_sections.count(name) == 1 || this->sections.count(name) == 1);
}
bool ConfigFile::has_entry(string section_name, string entry_name)
{
ConfigSection* section = this->get_section(section_name, true);
if ((section != NULL) && section->has_entry(entry_name))
{
return &this->sections[name];
return true;
}
section = this->get_section(section_name, false);
return ((section != NULL) && section->has_entry(entry_name));
}
ConfigSection* ConfigFile::get_section(string name, bool is_virtual)
{
if(is_virtual)
{
if (this->virtual_sections.count(name) == 1)
{
return &this->virtual_sections[name];
}
}
else
{
if (this->sections.count(name) == 1)
{
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)
ConfigSection* section = this->get_section(section_name, true);
if(section != NULL)
{
return NULL;
return section->get_entry(entry_name);
}
return section->get_entry(entry_name);
section = this->get_section(section_name, false);
if(section != NULL)
{
return section->get_entry(entry_name);
}
return NULL;
}
string ConfigFile::get(string section_name, string entry_name, string default_value)
@ -137,27 +164,27 @@ bool ConfigFile::get_bool(string section_name, string entry_name, bool default_v
}
}
void ConfigFile::set(string section_name, string entry_name, string value, int flags)
void ConfigFile::set(string section_name, string entry_name, string value, bool is_virtual)
{
ConfigSection* section = this->get_section(section_name);
ConfigSection* section = this->get_section(section_name, is_virtual);
if(section == NULL)
{
section = this->add_section(section_name);
section = this->add_section(section_name, is_virtual);
}
section->set(entry_name, value, flags);
section->set(entry_name, value);
};
void ConfigFile::set_int(string section_name, string entry_name, int value, int flags)
void ConfigFile::set_int(string section_name, string entry_name, int value, bool is_virtual)
{
std::stringstream str_value;
str_value << value;
this->set(section_name, entry_name, str_value.str());
this->set(section_name, entry_name, str_value.str(), is_virtual);
}
void ConfigFile::set_bool(string section_name, string entry_name, bool value, int flags)
void ConfigFile::set_bool(string section_name, string entry_name, bool value, bool is_virtual)
{
string str_value = (value ? "yes" : "no");
this->set(section_name, entry_name, str_value);
this->set(section_name, entry_name, str_value, is_virtual);
}
void ConfigFile::parse(FILE* file)
@ -237,6 +264,7 @@ void ConfigFile::save(FILE* file)
{
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();
@ -250,4 +278,3 @@ void ConfigFile::save(FILE* file)
fputs("\n", file);
}
}

View File

@ -2,21 +2,8 @@
#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
//Is this a virtual entry ?
#define CEM_VIRTUAL 1
//Should the value be saved ?
#define CEM_SAVE 2
//is this entry readonly ?
#define CEM_READONLY 4
//the move is from loading ?
#define CEM_LOAD 8
struct ConfigEntry {
string value;
int flags; //TODO: These have no effect right now
string get_string();
int get_int();
bool get_bool();
@ -25,15 +12,16 @@ struct ConfigEntry {
struct ConfigSection {
std::map<string, ConfigEntry> entries;
bool has_entry(string name);
void set(string name, string value, int flags);
void set(string name, string value);
ConfigEntry* get_entry(string name);
};
struct ConfigFile {
private:
std::map<string, ConfigSection> sections;
ConfigSection* add_section(string name);
ConfigSection* get_section(string name);
std::map<string, ConfigSection> virtual_sections;
ConfigSection* add_section(string name, bool is_virtual);
ConfigSection* get_section(string name, bool is_virtual);
ConfigEntry* get_entry(string section_name, string entry_name);
@ -49,7 +37,7 @@ struct ConfigFile {
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);
void set(string section_name, string entry_name, string value, bool is_virtual = false);
void set_int(string section_name, string entry_name, int value, bool is_virtual = false);
void set_bool(string section_name, string entry_name, bool value, bool is_virtual = false);
};

BIN
core/oslib/logtest Executable file

Binary file not shown.