flycast/core/cfg/ini.cpp

281 lines
5.5 KiB
C++
Raw Normal View History

#include "ini.h"
#include <sstream>
wchar* trim_ws(wchar* str);
/* ConfigEntry */
string ConfigEntry::get_string()
{
return this->value;
}
int ConfigEntry::get_int()
{
if (strstr(this->value.c_str(), "0x") != NULL)
{
return strtol(this->value.c_str(), NULL, 16);
}
else
{
return atoi(this->value.c_str());
}
}
bool ConfigEntry::get_bool()
{
2015-08-23 01:12:20 +00:00
if (stricmp(this->value.c_str(), "yes") == 0 ||
stricmp(this->value.c_str(), "true") == 0 ||
stricmp(this->value.c_str(), "on") == 0 ||
stricmp(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;
};
2015-08-19 02:43:54 +00:00
void ConfigSection::set(string name, string value)
{
2015-08-19 02:43:54 +00:00
ConfigEntry new_entry = { value };
this->entries[name] = new_entry;
};
/* ConfigFile */
2015-08-19 02:43:54 +00:00
ConfigSection* ConfigFile::add_section(string name, bool is_virtual)
{
2015-08-19 02:43:54 +00:00
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];
};
2015-08-19 02:43:54 +00:00
bool ConfigFile::has_section(string name)
{
2015-08-19 02:43:54 +00:00
return (this->virtual_sections.count(name) == 1 || this->sections.count(name) == 1);
}
2015-08-19 02:43:54 +00:00
bool ConfigFile::has_entry(string section_name, string entry_name)
{
2015-08-19 02:43:54 +00:00
ConfigSection* section = this->get_section(section_name, true);
if ((section != NULL) && section->has_entry(entry_name))
{
return true;
}
section = this->get_section(section_name, false);
return ((section != NULL) && section->has_entry(entry_name));
}
2015-08-19 02:43:54 +00:00
ConfigSection* ConfigFile::get_section(string name, bool is_virtual)
{
2015-08-19 02:43:54 +00:00
if(is_virtual)
{
2015-08-19 02:43:54 +00:00
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)
{
2015-08-19 02:43:54 +00:00
ConfigSection* section = this->get_section(section_name, true);
if(section != NULL)
{
return section->get_entry(entry_name);
}
section = this->get_section(section_name, false);
if(section != NULL)
{
2015-08-19 02:43:54 +00:00
return section->get_entry(entry_name);
}
2015-08-19 02:43:54 +00:00
return NULL;
}
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();
}
}
2015-08-19 02:43:54 +00:00
void ConfigFile::set(string section_name, string entry_name, string value, bool is_virtual)
{
2015-08-19 02:43:54 +00:00
ConfigSection* section = this->get_section(section_name, is_virtual);
if(section == NULL)
{
2015-08-19 02:43:54 +00:00
section = this->add_section(section_name, is_virtual);
}
2015-08-19 02:43:54 +00:00
section->set(entry_name, value);
};
2015-08-19 02:43:54 +00:00
void ConfigFile::set_int(string section_name, string entry_name, int value, bool is_virtual)
{
std::stringstream str_value;
str_value << value;
2015-08-19 02:43:54 +00:00
this->set(section_name, entry_name, str_value.str(), is_virtual);
}
2015-08-19 02:43:54 +00:00
void ConfigFile::set_bool(string section_name, string entry_name, bool value, bool is_virtual)
{
string str_value = (value ? "yes" : "no");
2015-08-19 02:43:54 +00:00
this->set(section_name, entry_name, str_value, is_virtual);
}
void ConfigFile::parse(FILE* file)
{
if(file == NULL)
{
return;
}
char line[512];
char current_section[512] = { '\0' };
int cline = 0;
while(file && !feof(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';
}
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
{
if (strlen(current_section) == 0)
{
continue; //no open section
}
char* separator = strstr(tl, "=");
if (!separator)
{
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline, tl);
continue;
}
*separator = '\0';
char* name = trim_ws(tl);
char* value = trim_ws(separator + 1);
if (name == NULL || value == NULL)
{
printf("Malformed entry on config - ignoring @ %d(%s)\n",cline, tl);
continue;
}
else
{
this->set(string(current_section), string(name), string(value));
}
}
}
}
void ConfigFile::save(FILE* file)
{
for(std::map<string, ConfigSection>::iterator section_it = this->sections.begin();
section_it != this->sections.end(); section_it++)
{
string section_name = section_it->first;
ConfigSection section = section_it->second;
2015-08-19 02:43:54 +00:00
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());
}
fputs("\n", file);
}
}