2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
#ifndef _INIFILE_H_
|
|
|
|
#define _INIFILE_H_
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
#include "CommonTypes.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <map>
|
2008-12-08 04:46:09 +00:00
|
|
|
#include <string>
|
2010-06-03 04:55:39 +00:00
|
|
|
#include <sstream>
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
// some things that include IniFile.h rely on this being here
|
2008-12-08 04:46:09 +00:00
|
|
|
#include "StringUtil.h"
|
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
class IniFile;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
class Section : public std::map<std::string, std::string>
|
2008-12-08 04:46:09 +00:00
|
|
|
{
|
2010-06-03 04:55:39 +00:00
|
|
|
friend class IniFile;
|
|
|
|
|
2008-12-08 04:46:09 +00:00
|
|
|
public:
|
2010-06-03 04:55:39 +00:00
|
|
|
Section() : m_use_lines(false) {}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
bool Exists(const std::string& key) const;
|
|
|
|
void Delete(const std::string& key);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
void SetLines(const std::vector<std::string>& lines);
|
|
|
|
void GetLines(std::vector<std::string>& lines);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
bool Get(const std::string& key, std::string* const val, const std::string& def = "") const;
|
|
|
|
void Set(const std::string& key, const std::string& val, const std::string& def = "");
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
template <typename V>
|
|
|
|
void Set(const std::string& key, const V val)
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
ss << val;
|
|
|
|
operator[](key) = ss.str();
|
|
|
|
}
|
2009-03-20 11:51:22 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
// if val doesn't match def, set the key's value to val
|
|
|
|
// otherwise delete that key
|
|
|
|
//
|
|
|
|
// this removed a lot of redundant code in the game-properties stuff
|
|
|
|
template <typename V, typename D>
|
|
|
|
void Set(const std::string& key, const V val, const D def)
|
|
|
|
{
|
|
|
|
if (val != def)
|
|
|
|
Set(key, val);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iterator f = find(key);
|
|
|
|
if (f != end())
|
|
|
|
erase(f);
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
template <typename V>
|
|
|
|
bool Get(const std::string& key, V* const val) const
|
|
|
|
{
|
|
|
|
const const_iterator f = find(key);
|
|
|
|
if (f != end())
|
|
|
|
{
|
|
|
|
std::istringstream ss(f->second);
|
|
|
|
ss >> *val;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
template <typename V, typename D>
|
|
|
|
bool Get(const std::string& key, V* const val, const D def) const
|
|
|
|
{
|
|
|
|
if (Get(key, val))
|
|
|
|
return true;
|
|
|
|
*val = def;
|
|
|
|
return false;
|
|
|
|
}
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
protected:
|
|
|
|
void Save(std::ostream& file) const;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
std::vector<std::string> m_lines;
|
2008-12-08 04:46:09 +00:00
|
|
|
|
|
|
|
private:
|
2010-06-03 04:55:39 +00:00
|
|
|
bool m_use_lines;
|
|
|
|
};
|
|
|
|
|
|
|
|
class IniFile : public std::map<std::string, Section>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Clean();
|
|
|
|
bool Exists(const std::string& section) const;
|
|
|
|
void Delete(const std::string& section);
|
|
|
|
|
|
|
|
bool Save(const char filename[]) const;
|
|
|
|
bool Load(const char filename[]);
|
|
|
|
|
|
|
|
bool Save(const std::string& filename) const;
|
|
|
|
bool Load(const std::string& filename);
|
2008-12-08 04:46:09 +00:00
|
|
|
|
2010-06-03 04:55:39 +00:00
|
|
|
void Save(std::ostream& file) const;
|
|
|
|
void Load(std::istream& file);
|
2008-12-08 04:46:09 +00:00
|
|
|
};
|
|
|
|
|
2009-03-28 08:57:34 +00:00
|
|
|
#endif // _INIFILE_H_
|