rlm@1: // -*- C++ -*- rlm@1: // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator. rlm@1: // Copyright (C) 1999-2003 Forgotten rlm@1: // Copyright (C) 2004 Forgotten and the VBA development team rlm@1: rlm@1: // This program is free software; you can redistribute it and/or modify rlm@1: // it under the terms of the GNU General Public License as published by rlm@1: // the Free Software Foundation; either version 2, or(at your option) rlm@1: // any later version. rlm@1: // rlm@1: // This program is distributed in the hope that it will be useful, rlm@1: // but WITHOUT ANY WARRANTY; without even the implied warranty of rlm@1: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the rlm@1: // GNU General Public License for more details. rlm@1: // rlm@1: // You should have received a copy of the GNU General Public License rlm@1: // along with this program; if not, write to the Free Software Foundation, rlm@1: // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. rlm@1: rlm@1: #ifndef __VBA_CONFIGFILE_H__ rlm@1: #define __VBA_CONFIGFILE_H__ rlm@1: rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: #include rlm@1: rlm@1: namespace VBA rlm@1: { rlm@1: namespace Config rlm@1: { rlm@1: rlm@1: class NotFound rlm@1: { rlm@1: public: rlm@1: virtual ~NotFound() {} rlm@1: rlm@1: protected: rlm@1: NotFound() {} rlm@1: }; rlm@1: rlm@1: class SectionNotFound : public NotFound rlm@1: { rlm@1: public: rlm@1: SectionNotFound(const std::string & _rsName) : rlm@1: m_sName(_rsName) rlm@1: { rlm@1: } rlm@1: virtual ~SectionNotFound() {} rlm@1: rlm@1: inline std::string sGetName() const { return m_sName; } rlm@1: rlm@1: private: rlm@1: std::string m_sName; rlm@1: }; rlm@1: rlm@1: class KeyNotFound : public NotFound rlm@1: { rlm@1: public: rlm@1: KeyNotFound(const std::string & _rsSection, const std::string & _rsKey) : rlm@1: m_sSection(_rsSection), rlm@1: m_sKey(_rsKey) rlm@1: { rlm@1: } rlm@1: virtual ~KeyNotFound() {} rlm@1: rlm@1: inline std::string sGetSection() const { return m_sSection; } rlm@1: inline std::string sGetKey() const { return m_sKey; } rlm@1: rlm@1: private: rlm@1: std::string m_sSection; rlm@1: std::string m_sKey; rlm@1: }; rlm@1: rlm@1: class Line rlm@1: { rlm@1: public: rlm@1: Line(const std::string & _rsKey, const std::string & _rsValue); rlm@1: rlm@1: std::string m_sKey; rlm@1: std::string m_sValue; rlm@1: }; rlm@1: rlm@1: class Section : private std::list rlm@1: { rlm@1: public: rlm@1: explicit Section(const std::string & _rsName); rlm@1: rlm@1: inline std::string sGetName() const { return m_sName; } rlm@1: rlm@1: bool bKeyExists(const std::string & _rsKey); rlm@1: void vSetKey(const std::string & _rsKey, const std::string & _rsValue); rlm@1: std::string sGetKey(const std::string & _rsKey) const; rlm@1: void vRemoveKey(const std::string & _rsKey); rlm@1: rlm@1: template rlm@1: void vSetKey(const std::string & _rsKey, const T & _rValue); rlm@1: rlm@1: template rlm@1: T oGetKey(const std::string & _rsKey) const; rlm@1: rlm@1: // read only rlm@1: typedef std::list::const_iterator const_iterator; rlm@1: inline const_iterator begin() const rlm@1: { rlm@1: return std::list::begin(); rlm@1: } rlm@1: inline const_iterator end() const rlm@1: { rlm@1: return std::list::end(); rlm@1: } rlm@1: rlm@1: private: rlm@1: inline iterator begin() rlm@1: { rlm@1: return std::list::begin(); rlm@1: } rlm@1: inline iterator end() rlm@1: { rlm@1: return std::list::end(); rlm@1: } rlm@1: rlm@1: std::string m_sName; rlm@1: }; rlm@1: rlm@1: class File : private std::list
rlm@1: { rlm@1: public: rlm@1: File(); rlm@1: File(const std::string & _rsFile); rlm@1: virtual ~File(); rlm@1: rlm@1: bool bSectionExists(const std::string & _rsName); rlm@1: Section * poAddSection(const std::string & _rsName); rlm@1: Section * poGetSection(const std::string & _rsName); rlm@1: void vRemoveSection(const std::string & _rsName); rlm@1: void vLoad(const std::string & _rsFile, rlm@1: bool _bAddSection = true, rlm@1: bool _bAddKey = true); rlm@1: void vSave(const std::string & _rsFile); rlm@1: void vClear(); rlm@1: rlm@1: // read only rlm@1: typedef std::list
::const_iterator const_iterator; rlm@1: inline const_iterator begin() const rlm@1: { rlm@1: return std::list
::begin(); rlm@1: } rlm@1: inline const_iterator end() const rlm@1: { rlm@1: return std::list
::end(); rlm@1: } rlm@1: rlm@1: private: rlm@1: inline iterator begin() rlm@1: { rlm@1: return std::list
::begin(); rlm@1: } rlm@1: inline iterator end() rlm@1: { rlm@1: return std::list
::end(); rlm@1: } rlm@1: }; rlm@1: rlm@1: // debug rlm@1: std::ostream & operator<<(std::ostream & _roOut, const File & _roConfig); rlm@1: rlm@1: template rlm@1: void Section::vSetKey(const std::string & _rsKey, const T & _rValue) rlm@1: { rlm@1: std::ostringstream oOut; rlm@1: oOut << _rValue; rlm@1: for (iterator it = begin(); it != end(); it++) rlm@1: { rlm@1: if (it->m_sKey == _rsKey) rlm@1: { rlm@1: it->m_sValue = oOut.str(); rlm@1: return; rlm@1: } rlm@1: } rlm@1: push_back(Line(_rsKey, oOut.str())); rlm@1: } rlm@1: rlm@1: template rlm@1: T Section::oGetKey(const std::string & _rsKey) const rlm@1: { rlm@1: T oValue; rlm@1: for (const_iterator it = begin(); it != end(); it++) rlm@1: { rlm@1: if (it->m_sKey == _rsKey) rlm@1: { rlm@1: std::istringstream oIn(it->m_sValue); rlm@1: oIn >> oValue; rlm@1: return oValue; rlm@1: } rlm@1: } rlm@1: throw KeyNotFound(m_sName, _rsKey); rlm@1: } rlm@1: rlm@1: } // namespace Config rlm@1: } // namespace VBA rlm@1: rlm@1: rlm@1: #endif // __VBA_CONFIGFILE_H__