mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-05-01 16:58:35 +00:00
Moved InputPluginCommon into the Plugins directory. I don't think it made sense in Core. Some minor fixes to the new wiimote plugin also.
git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@5362 8ced0084-cf51-0410-be5f-012b33b47a6e
This commit is contained in:
parent
c2ac9be9b2
commit
ba89d91872
17 changed files with 49 additions and 41 deletions
142
Source/Plugins/InputPluginCommon/Src/IniFile.cpp
Normal file
142
Source/Plugins/InputPluginCommon/Src/IniFile.cpp
Normal file
|
@ -0,0 +1,142 @@
|
|||
|
||||
#include "IniFile.h"
|
||||
|
||||
//
|
||||
// TrimChars
|
||||
//
|
||||
// trim whitespace, or any, chars from both ends
|
||||
//
|
||||
template <typename S>
|
||||
std::string TrimChars( const std::string& str, const S space )
|
||||
{
|
||||
const size_t start = str.find_first_not_of( space );
|
||||
|
||||
if ( str.npos == start )
|
||||
return "";
|
||||
|
||||
return str.substr( start, str.find_last_not_of( space ) - start + 1 );
|
||||
}
|
||||
|
||||
//
|
||||
// IniSection :: Set
|
||||
//
|
||||
// set key's value if it doesn't match the default
|
||||
// otherwise remove the key from the section if it exists
|
||||
//
|
||||
void IniSection::Set( const std::string& key, const std::string& val, const std::string& def )
|
||||
{
|
||||
if ( val != def )
|
||||
operator[](key) = val;
|
||||
else
|
||||
{
|
||||
iterator f = find(key);
|
||||
if ( f != end() )
|
||||
erase( f );
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// IniSection :: Get
|
||||
//
|
||||
// return a key's value if it exists
|
||||
// otherwise return the default
|
||||
//
|
||||
std::string IniSection::Get( const std::string& key, const std::string& def )
|
||||
{
|
||||
const const_iterator f = find(key);
|
||||
if ( f != end() )
|
||||
if ( false == f->second.empty() )
|
||||
return f->second;
|
||||
return def;
|
||||
}
|
||||
|
||||
//
|
||||
// IniFile :: Save
|
||||
//
|
||||
// save a file
|
||||
//
|
||||
void IniFile::Save( std::ostream& file )
|
||||
{
|
||||
const_iterator i = begin(),
|
||||
e = end();
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
// skip a line at new sections
|
||||
file << "\n[" << i->first << "]\n";
|
||||
Section::const_iterator si = i->second.begin(),
|
||||
se = i->second.end();
|
||||
for ( ; si != se; ++si )
|
||||
file << si->first << " = " << si->second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// IniFile :: Load
|
||||
//
|
||||
// load a file
|
||||
//
|
||||
void IniFile::Load( std::istream& file )
|
||||
{
|
||||
const char* const space = "\t\r ";
|
||||
std::string line;
|
||||
// start off with an empty section
|
||||
Section* section = &(*this)[""];
|
||||
while ( std::getline( file, line ).good() ) // read a line
|
||||
{
|
||||
line = TrimChars(line,space);
|
||||
if ( line.size() )
|
||||
{
|
||||
switch ( line[0] )
|
||||
{
|
||||
// comment
|
||||
case '#' :
|
||||
case ';' :
|
||||
break;
|
||||
// section
|
||||
case '[' :
|
||||
// kinda odd trimming
|
||||
section = &(*this)[ TrimChars(line,"][\t\r ") ];
|
||||
break;
|
||||
// key/value
|
||||
default :
|
||||
{
|
||||
std::istringstream ss(line);
|
||||
std::string key; std::getline( ss, key, '=' );
|
||||
std::string val; std::getline( ss, val );
|
||||
(*section)[ TrimChars(key,space) ] = TrimChars(val,space);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Clean();
|
||||
}
|
||||
|
||||
//
|
||||
// IniFile :: Clean
|
||||
//
|
||||
// remove empty key/values and sections
|
||||
// after trying to access ini sections/values, they are automatically allocated
|
||||
// this deletes the empty stuff
|
||||
//
|
||||
void IniFile::Clean()
|
||||
{
|
||||
iterator i = begin(),
|
||||
e = end();
|
||||
for ( ; i != e; )
|
||||
{
|
||||
Section::iterator si = i->second.begin(),
|
||||
se = i->second.end();
|
||||
for ( ; si != se; )
|
||||
{
|
||||
if ( si->second.empty() )
|
||||
i->second.erase( si++ );
|
||||
else
|
||||
++si;
|
||||
}
|
||||
if ( i->second.empty() )
|
||||
erase( i++ );
|
||||
else
|
||||
++i;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue