mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-06-13 00:58:29 +02:00
Warp back to 5578. Sorry for the lost changes, please re-apply. Reason: 5579 is a complete disaster.
Not only does it change tons of files to switch to a new and non-working (it doesn't parse my ini files, at least) ini parser, it also reshuffles a lot of code and removes a plugin. The latter part is fine, but doing these two major switches in one revision, one of which is broken, is completely unacceptable. I said to merge tiny changes, not massive reworkings. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5589 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
@ -16,239 +16,510 @@
|
||||
// http://code.google.com/p/dolphin-emu/
|
||||
// see IniFile.h
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <algorithm>
|
||||
|
||||
#include "StringUtil.h"
|
||||
#include "IniFile.h"
|
||||
|
||||
template <typename S>
|
||||
void StripChars(std::string& str, const S space)
|
||||
{
|
||||
const size_t start = str.find_first_not_of(space);
|
||||
IniFile::IniFile()
|
||||
{}
|
||||
|
||||
if (str.npos == start)
|
||||
str.clear();
|
||||
else
|
||||
str = str.substr(start, str.find_last_not_of(space) - start + 1);
|
||||
IniFile::~IniFile()
|
||||
{}
|
||||
|
||||
Section::Section()
|
||||
: lines(), name(""), comment("") {}
|
||||
|
||||
|
||||
Section::Section(const std::string& _name)
|
||||
: lines(), name(_name), comment("") {}
|
||||
|
||||
|
||||
Section::Section(const Section& other)
|
||||
{
|
||||
name = other.name;
|
||||
comment = other.comment;
|
||||
lines = other.lines;
|
||||
}
|
||||
|
||||
bool Section::Get(const std::string& key, std::string* const val, const std::string& def) const
|
||||
const Section* IniFile::GetSection(const char* sectionName) const
|
||||
{
|
||||
const const_iterator f = find(key);
|
||||
if (f != end())
|
||||
for (std::vector<Section>::const_iterator iter = sections.begin(); iter != sections.end(); ++iter)
|
||||
if (!strcasecmp(iter->name.c_str(), sectionName))
|
||||
return (&(*iter));
|
||||
return 0;
|
||||
}
|
||||
|
||||
Section* IniFile::GetSection(const char* sectionName)
|
||||
{
|
||||
for (std::vector<Section>::iterator iter = sections.begin(); iter != sections.end(); ++iter)
|
||||
if (!strcasecmp(iter->name.c_str(), sectionName))
|
||||
return (&(*iter));
|
||||
return 0;
|
||||
}
|
||||
|
||||
Section* IniFile::GetOrCreateSection(const char* sectionName)
|
||||
{
|
||||
Section* section = GetSection(sectionName);
|
||||
|
||||
if (!section)
|
||||
{
|
||||
*val = f->second;
|
||||
return true;
|
||||
sections.push_back(Section(sectionName));
|
||||
section = §ions[sections.size() - 1];
|
||||
}
|
||||
if (false == def.empty())
|
||||
*val = def;
|
||||
|
||||
return(section);
|
||||
}
|
||||
|
||||
|
||||
bool IniFile::DeleteSection(const char* sectionName)
|
||||
{
|
||||
Section* s = GetSection(sectionName);
|
||||
|
||||
if (!s)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::vector<Section>::iterator iter = sections.begin(); iter != sections.end(); ++iter)
|
||||
{
|
||||
if (&(*iter) == s)
|
||||
{
|
||||
sections.erase(iter);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Section::Set(const std::string& key, const std::string& val, const std::string& def)
|
||||
void IniFile::ParseLine(const std::string& line, std::string* keyOut, std::string* valueOut, std::string* commentOut) const
|
||||
{
|
||||
if (val != def)
|
||||
operator[](key) = val;
|
||||
else
|
||||
{
|
||||
iterator f = find(key);
|
||||
if (f != end())
|
||||
erase(f);
|
||||
}
|
||||
}
|
||||
//
|
||||
int FirstEquals = (int)line.find("=", 0);
|
||||
int FirstCommentChar = -1;
|
||||
// Comments
|
||||
//if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find(";", FirstEquals > 0 ? FirstEquals : 0);}
|
||||
if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find("#", FirstEquals > 0 ? FirstEquals : 0);}
|
||||
if (FirstCommentChar < 0) {FirstCommentChar = (int)line.find("//", FirstEquals > 0 ? FirstEquals : 0);}
|
||||
|
||||
bool IniFile::Save(const std::string& filename) const
|
||||
{
|
||||
return Save(filename.c_str());
|
||||
}
|
||||
|
||||
bool IniFile::Save(const char filename[]) const
|
||||
{
|
||||
std::ofstream file;
|
||||
file.open(filename);
|
||||
if (file.is_open())
|
||||
// Allow preservation of spacing before comment
|
||||
if (FirstCommentChar > 0)
|
||||
{
|
||||
Save(file);
|
||||
file.close();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void IniFile::Save(std::ostream& file) const
|
||||
{
|
||||
const_iterator
|
||||
si = begin(),
|
||||
se = end();
|
||||
for ( ; si != se; ++si )
|
||||
{
|
||||
// skip a line at new sections
|
||||
file << "\n[" << si->first << "]\n";
|
||||
si->second.Save(file);
|
||||
}
|
||||
}
|
||||
|
||||
void Section::Save(std::ostream& file) const
|
||||
{
|
||||
if (m_use_lines) // this is used when GetLines or SetLines has been called
|
||||
{
|
||||
std::vector<std::string>::const_iterator
|
||||
i = m_lines.begin(),
|
||||
e = m_lines.end();
|
||||
for ( ; i!=e; ++i)
|
||||
file << *i << '\n';
|
||||
}
|
||||
else
|
||||
{
|
||||
Section::const_iterator
|
||||
vi = begin(),
|
||||
ve = end();
|
||||
for ( ; vi!=ve; ++vi)
|
||||
while (line[FirstCommentChar - 1] == ' ' || line[FirstCommentChar - 1] == 9) // 9 == tab
|
||||
{
|
||||
file << vi->first << " = ";
|
||||
// if value has quotes or whitespace, surround it with quotes
|
||||
if (vi->second.find_first_of("\"\t ") != std::string::npos)
|
||||
file << '"' << vi->second << '"';
|
||||
else
|
||||
file << vi->second;
|
||||
file << '\n';
|
||||
FirstCommentChar--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool IniFile::Load(const std::string& filename)
|
||||
{
|
||||
return Load(filename.c_str());
|
||||
}
|
||||
|
||||
bool IniFile::Load(const char filename[])
|
||||
{
|
||||
std::ifstream file;
|
||||
file.open(filename);
|
||||
if (file.is_open())
|
||||
if ((FirstEquals >= 0) && ((FirstCommentChar < 0) || (FirstEquals < FirstCommentChar)))
|
||||
{
|
||||
Load(file);
|
||||
file.close();
|
||||
return true;
|
||||
// Yes, a valid line!
|
||||
*keyOut = StripSpaces(line.substr(0, FirstEquals));
|
||||
if (commentOut) *commentOut = FirstCommentChar > 0 ? line.substr(FirstCommentChar) : std::string("");
|
||||
if (valueOut) *valueOut = StripQuotes(StripSpaces(line.substr(FirstEquals + 1, FirstCommentChar - FirstEquals - 1)));
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void IniFile::Load(std::istream& file)
|
||||
std::string* IniFile::GetLine(Section* section, const char* key, std::string* valueOut, std::string* commentOut)
|
||||
{
|
||||
std::vector<std::string> lines;
|
||||
|
||||
Section sectmp;
|
||||
Section* section = §mp;
|
||||
|
||||
std::string line;
|
||||
while (std::getline(file, line)) // read a line
|
||||
for (std::vector<std::string>::iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter)
|
||||
{
|
||||
if (line.size())
|
||||
std::string& line = *iter;
|
||||
std::string lineKey;
|
||||
ParseLine(line, &lineKey, valueOut, commentOut);
|
||||
|
||||
if (!strcasecmp(lineKey.c_str(), key))
|
||||
{
|
||||
switch (line[0])
|
||||
return &line;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool IniFile::Exists(const char* const sectionName, const char* key) const
|
||||
{
|
||||
|
||||
const Section* const section = GetSection(sectionName);
|
||||
if (!section)
|
||||
return false;
|
||||
|
||||
for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter)
|
||||
{
|
||||
std::string lineKey;
|
||||
ParseLine(*iter, &lineKey, NULL, NULL);
|
||||
|
||||
if (!strcasecmp(lineKey.c_str(), key))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void IniFile::SetLines(const char* sectionName, const std::vector<std::string> &lines)
|
||||
{
|
||||
Section* section = GetOrCreateSection(sectionName);
|
||||
section->lines.clear();
|
||||
|
||||
for (std::vector<std::string>::const_iterator iter = lines.begin(); iter != lines.end(); ++iter)
|
||||
{
|
||||
section->lines.push_back(*iter);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool IniFile::DeleteKey(const char* sectionName, const char* key)
|
||||
{
|
||||
Section* section = GetSection(sectionName);
|
||||
|
||||
if (!section)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string* line = GetLine(section, key, 0, 0);
|
||||
|
||||
for (std::vector<std::string>::iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter)
|
||||
{
|
||||
if (line == &(*liter))
|
||||
{
|
||||
section->lines.erase(liter);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false; //shouldn't happen
|
||||
}
|
||||
|
||||
// Return a list of all keys in a section
|
||||
bool IniFile::GetKeys(const char* sectionName, std::vector<std::string>& keys) const
|
||||
{
|
||||
const Section* section = GetSection(sectionName);
|
||||
|
||||
if (!section)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
keys.clear();
|
||||
|
||||
for (std::vector<std::string>::const_iterator liter = section->lines.begin(); liter != section->lines.end(); ++liter)
|
||||
{
|
||||
std::string key;
|
||||
ParseLine(*liter, &key, 0, 0);
|
||||
keys.push_back(key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Return a list of all lines in a section
|
||||
bool IniFile::GetLines(const char* sectionName, std::vector<std::string>& lines) const
|
||||
{
|
||||
const Section* section = GetSection(sectionName);
|
||||
if (!section)
|
||||
return false;
|
||||
|
||||
lines.clear();
|
||||
for (std::vector<std::string>::const_iterator iter = section->lines.begin(); iter != section->lines.end(); ++iter)
|
||||
{
|
||||
std::string line = StripSpaces(*iter);
|
||||
int commentPos = (int)line.find('#');
|
||||
if (commentPos == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (commentPos != (int)std::string::npos)
|
||||
{
|
||||
line = StripSpaces(line.substr(0, commentPos));
|
||||
}
|
||||
|
||||
lines.push_back(line);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void IniFile::SortSections()
|
||||
{
|
||||
std::sort(sections.begin(), sections.end());
|
||||
}
|
||||
|
||||
bool IniFile::Load(const char* filename)
|
||||
{
|
||||
// Maximum number of letters in a line
|
||||
static const int MAX_BYTES = 1024*32;
|
||||
|
||||
sections.clear();
|
||||
sections.push_back(Section(""));
|
||||
// first section consists of the comments before the first real section
|
||||
|
||||
// Open file
|
||||
std::ifstream in;
|
||||
in.open(filename, std::ios::in);
|
||||
|
||||
if (in.fail()) return false;
|
||||
|
||||
while (!in.eof())
|
||||
{
|
||||
char templine[MAX_BYTES];
|
||||
in.getline(templine, MAX_BYTES);
|
||||
std::string line = templine;
|
||||
|
||||
#ifndef _WIN32
|
||||
// Check for CRLF eol and convert it to LF
|
||||
if (!line.empty() && line.at(line.size()-1) == '\r')
|
||||
{
|
||||
line.erase(line.size()-1);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (in.eof()) break;
|
||||
|
||||
if (line.size() > 0)
|
||||
{
|
||||
if (line[0] == '[')
|
||||
{
|
||||
// section
|
||||
case '[' :
|
||||
section->m_lines = lines;
|
||||
// kinda odd trimming
|
||||
StripChars(line, "][\t\r ");
|
||||
section = &(*this)[line];
|
||||
lines.clear();
|
||||
break;
|
||||
size_t endpos = line.find("]");
|
||||
|
||||
// key/value
|
||||
default :
|
||||
if (endpos != std::string::npos)
|
||||
{
|
||||
std::istringstream ss(line);
|
||||
// New section!
|
||||
std::string sub = line.substr(1, endpos - 1);
|
||||
sections.push_back(Section(sub));
|
||||
|
||||
std::string key; std::getline(ss, key, '=');
|
||||
std::string val; std::getline(ss, val);
|
||||
|
||||
StripChars(val, "\t\r ");
|
||||
// handle quote surrounded values
|
||||
if (val.length() > 1)
|
||||
if ('"' == val[0])
|
||||
val.assign(val.begin()+1, val.end()-1);
|
||||
|
||||
StripChars(key, "\t\r ");
|
||||
(*section)[key] = val;
|
||||
if (endpos + 1 < line.size())
|
||||
{
|
||||
sections[sections.size() - 1].comment = line.substr(endpos + 1);
|
||||
}
|
||||
}
|
||||
//break; // no break
|
||||
|
||||
// comment
|
||||
case '#' :
|
||||
case ';' :
|
||||
lines.push_back(line);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
sections[sections.size() - 1].lines.push_back(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Clean();
|
||||
|
||||
in.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
//
|
||||
// IniFile :: Clean
|
||||
//
|
||||
// remove empty key/values and sections
|
||||
// after trying to access ini sections/values with the [] operator, they are automatically allocated
|
||||
// this deletes the empty stuff
|
||||
//
|
||||
void IniFile::Clean()
|
||||
bool IniFile::Save(const char* filename)
|
||||
{
|
||||
iterator
|
||||
i = begin(),
|
||||
e = end();
|
||||
for ( ; i != e; )
|
||||
std::ofstream out;
|
||||
out.open(filename, std::ios::out);
|
||||
|
||||
if (out.fail())
|
||||
{
|
||||
Section::iterator
|
||||
si = i->second.begin(),
|
||||
se = i->second.end();
|
||||
for ( ; si != se; )
|
||||
{
|
||||
if (si->second.empty())
|
||||
i->second.erase( si++ );
|
||||
else
|
||||
++si;
|
||||
return false;
|
||||
}
|
||||
|
||||
for (std::vector<Section>::const_iterator iter = sections.begin(); iter != sections.end(); ++iter)
|
||||
{
|
||||
const Section& section = *iter;
|
||||
|
||||
if (section.name != "")
|
||||
{
|
||||
out << "[" << section.name << "]" << section.comment << std::endl;
|
||||
}
|
||||
if (i->second.empty() && i->second.m_lines.empty())
|
||||
erase( i++ );
|
||||
else
|
||||
++i;
|
||||
|
||||
for (std::vector<std::string>::const_iterator liter = section.lines.begin(); liter != section.lines.end(); ++liter)
|
||||
{
|
||||
std::string s = *liter;
|
||||
out << s << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
out.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
void IniFile::Set(const char* sectionName, const char* key, const char* newValue)
|
||||
{
|
||||
Section* section = GetOrCreateSection(sectionName);
|
||||
std::string value, comment;
|
||||
std::string* line = GetLine(section, key, &value, &comment);
|
||||
|
||||
if (line)
|
||||
{
|
||||
// Change the value - keep the key and comment
|
||||
*line = StripSpaces(key) + " = " + newValue + comment;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The key did not already exist in this section - let's add it.
|
||||
section->lines.push_back(std::string(key) + " = " + newValue);
|
||||
}
|
||||
}
|
||||
|
||||
bool IniFile::Exists(const std::string& section) const
|
||||
void IniFile::Set(const char* sectionName, const char* key, const std::vector<std::string>& newValues)
|
||||
{
|
||||
return find(section) != end();
|
||||
std::string temp;
|
||||
|
||||
// Join the strings with ,
|
||||
std::vector<std::string>::const_iterator it;
|
||||
for (it = newValues.begin(); it != newValues.end(); ++it) {
|
||||
|
||||
temp = (*it) + ",";
|
||||
}
|
||||
|
||||
// remove last ,
|
||||
temp.resize(temp.length() - 1);
|
||||
|
||||
Set(sectionName, key, temp.c_str());
|
||||
}
|
||||
|
||||
void IniFile::Delete(const std::string& section)
|
||||
void IniFile::Set(const char* sectionName, const char* key, u32 newValue)
|
||||
{
|
||||
const iterator f = find(section);
|
||||
if (end() != f)
|
||||
erase(f);
|
||||
Set(sectionName, key, StringFromFormat("0x%08x", newValue).c_str());
|
||||
}
|
||||
|
||||
bool Section::Exists(const std::string& key) const
|
||||
|
||||
void IniFile::Set(const char* sectionName, const char* key, int newValue)
|
||||
{
|
||||
return find(key) != end();
|
||||
Set(sectionName, key, StringFromInt(newValue).c_str());
|
||||
}
|
||||
|
||||
void Section::Delete(const std::string& key)
|
||||
|
||||
void IniFile::Set(const char* sectionName, const char* key, bool newValue)
|
||||
{
|
||||
const iterator f = find(key);
|
||||
if (end() != f)
|
||||
erase(f);
|
||||
Set(sectionName, key, StringFromBool(newValue).c_str());
|
||||
}
|
||||
|
||||
void Section::SetLines(const std::vector<std::string>& lines)
|
||||
bool IniFile::Get(const char* sectionName, const char* key, std::string* value, const char* defaultValue)
|
||||
{
|
||||
m_lines = lines;
|
||||
m_use_lines = true;
|
||||
Section* section = GetSection(sectionName);
|
||||
|
||||
if (!section)
|
||||
{
|
||||
if (defaultValue)
|
||||
{
|
||||
*value = defaultValue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string* line = GetLine(section, key, value, 0);
|
||||
|
||||
if (!line)
|
||||
{
|
||||
if (defaultValue)
|
||||
{
|
||||
*value = defaultValue;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Section::GetLines(std::vector<std::string>& lines)
|
||||
|
||||
bool IniFile::Get(const char* sectionName, const char* key, std::vector<std::string>& values)
|
||||
{
|
||||
lines = m_lines;
|
||||
m_use_lines = true;
|
||||
|
||||
std::string temp;
|
||||
bool retval = Get(sectionName, key, &temp, 0);
|
||||
|
||||
if (! retval || temp.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// ignore starting , if any
|
||||
size_t subStart = temp.find_first_not_of(",");
|
||||
size_t subEnd;
|
||||
|
||||
// split by ,
|
||||
while (subStart != std::string::npos) {
|
||||
|
||||
// Find next ,
|
||||
subEnd = temp.find_first_of(",", subStart);
|
||||
if (subStart != subEnd)
|
||||
// take from first char until next ,
|
||||
values.push_back(StripSpaces(temp.substr(subStart, subEnd - subStart)));
|
||||
|
||||
// Find the next non , char
|
||||
subStart = temp.find_first_not_of(",", subEnd);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IniFile::Get(const char* sectionName, const char* key, int* value, int defaultValue)
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(sectionName, key, &temp, 0);
|
||||
|
||||
if (retval && TryParseInt(temp.c_str(), value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool IniFile::Get(const char* sectionName, const char* key, u32* value, u32 defaultValue)
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(sectionName, key, &temp, 0);
|
||||
|
||||
if (retval && TryParseUInt(temp.c_str(), value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool IniFile::Get(const char* sectionName, const char* key, bool* value, bool defaultValue)
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(sectionName, key, &temp, 0);
|
||||
|
||||
if (retval && TryParseBool(temp.c_str(), value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// TODO: Keep this code below?
|
||||
/*
|
||||
int main()
|
||||
{
|
||||
IniFile ini;
|
||||
ini.Load("my.ini");
|
||||
ini.Set("Hej", "A", "amaskdfl");
|
||||
ini.Set("Mossa", "A", "amaskdfl");
|
||||
ini.Set("Aissa", "A", "amaskdfl");
|
||||
//ini.Read("my.ini");
|
||||
std::string x;
|
||||
ini.Get("Hej", "B", &x, "boo");
|
||||
ini.DeleteKey("Mossa", "A");
|
||||
ini.DeleteSection("Mossa");
|
||||
ini.SortSections();
|
||||
ini.Save("my.ini");
|
||||
//UpdateVars(ini);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
Reference in New Issue
Block a user