annotate src/gtk/configfile.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
rev   line source
rlm@1 1 // -*- C++ -*-
rlm@1 2 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
rlm@1 3 // Copyright (C) 1999-2003 Forgotten
rlm@1 4 // Copyright (C) 2004 Forgotten and the VBA development team
rlm@1 5
rlm@1 6 // This program is free software; you can redistribute it and/or modify
rlm@1 7 // it under the terms of the GNU General Public License as published by
rlm@1 8 // the Free Software Foundation; either version 2, or(at your option)
rlm@1 9 // any later version.
rlm@1 10 //
rlm@1 11 // This program is distributed in the hope that it will be useful,
rlm@1 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
rlm@1 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
rlm@1 14 // GNU General Public License for more details.
rlm@1 15 //
rlm@1 16 // You should have received a copy of the GNU General Public License
rlm@1 17 // along with this program; if not, write to the Free Software Foundation,
rlm@1 18 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
rlm@1 19
rlm@1 20 #ifndef __VBA_CONFIGFILE_H__
rlm@1 21 #define __VBA_CONFIGFILE_H__
rlm@1 22
rlm@1 23 #include <list>
rlm@1 24 #include <string>
rlm@1 25 #include <sstream>
rlm@1 26 #include <ostream>
rlm@1 27
rlm@1 28 namespace VBA
rlm@1 29 {
rlm@1 30 namespace Config
rlm@1 31 {
rlm@1 32
rlm@1 33 class NotFound
rlm@1 34 {
rlm@1 35 public:
rlm@1 36 virtual ~NotFound() {}
rlm@1 37
rlm@1 38 protected:
rlm@1 39 NotFound() {}
rlm@1 40 };
rlm@1 41
rlm@1 42 class SectionNotFound : public NotFound
rlm@1 43 {
rlm@1 44 public:
rlm@1 45 SectionNotFound(const std::string & _rsName) :
rlm@1 46 m_sName(_rsName)
rlm@1 47 {
rlm@1 48 }
rlm@1 49 virtual ~SectionNotFound() {}
rlm@1 50
rlm@1 51 inline std::string sGetName() const { return m_sName; }
rlm@1 52
rlm@1 53 private:
rlm@1 54 std::string m_sName;
rlm@1 55 };
rlm@1 56
rlm@1 57 class KeyNotFound : public NotFound
rlm@1 58 {
rlm@1 59 public:
rlm@1 60 KeyNotFound(const std::string & _rsSection, const std::string & _rsKey) :
rlm@1 61 m_sSection(_rsSection),
rlm@1 62 m_sKey(_rsKey)
rlm@1 63 {
rlm@1 64 }
rlm@1 65 virtual ~KeyNotFound() {}
rlm@1 66
rlm@1 67 inline std::string sGetSection() const { return m_sSection; }
rlm@1 68 inline std::string sGetKey() const { return m_sKey; }
rlm@1 69
rlm@1 70 private:
rlm@1 71 std::string m_sSection;
rlm@1 72 std::string m_sKey;
rlm@1 73 };
rlm@1 74
rlm@1 75 class Line
rlm@1 76 {
rlm@1 77 public:
rlm@1 78 Line(const std::string & _rsKey, const std::string & _rsValue);
rlm@1 79
rlm@1 80 std::string m_sKey;
rlm@1 81 std::string m_sValue;
rlm@1 82 };
rlm@1 83
rlm@1 84 class Section : private std::list<Line>
rlm@1 85 {
rlm@1 86 public:
rlm@1 87 explicit Section(const std::string & _rsName);
rlm@1 88
rlm@1 89 inline std::string sGetName() const { return m_sName; }
rlm@1 90
rlm@1 91 bool bKeyExists(const std::string & _rsKey);
rlm@1 92 void vSetKey(const std::string & _rsKey, const std::string & _rsValue);
rlm@1 93 std::string sGetKey(const std::string & _rsKey) const;
rlm@1 94 void vRemoveKey(const std::string & _rsKey);
rlm@1 95
rlm@1 96 template<typename T>
rlm@1 97 void vSetKey(const std::string & _rsKey, const T & _rValue);
rlm@1 98
rlm@1 99 template<typename T>
rlm@1 100 T oGetKey(const std::string & _rsKey) const;
rlm@1 101
rlm@1 102 // read only
rlm@1 103 typedef std::list<Line>::const_iterator const_iterator;
rlm@1 104 inline const_iterator begin() const
rlm@1 105 {
rlm@1 106 return std::list<Line>::begin();
rlm@1 107 }
rlm@1 108 inline const_iterator end() const
rlm@1 109 {
rlm@1 110 return std::list<Line>::end();
rlm@1 111 }
rlm@1 112
rlm@1 113 private:
rlm@1 114 inline iterator begin()
rlm@1 115 {
rlm@1 116 return std::list<Line>::begin();
rlm@1 117 }
rlm@1 118 inline iterator end()
rlm@1 119 {
rlm@1 120 return std::list<Line>::end();
rlm@1 121 }
rlm@1 122
rlm@1 123 std::string m_sName;
rlm@1 124 };
rlm@1 125
rlm@1 126 class File : private std::list<Section>
rlm@1 127 {
rlm@1 128 public:
rlm@1 129 File();
rlm@1 130 File(const std::string & _rsFile);
rlm@1 131 virtual ~File();
rlm@1 132
rlm@1 133 bool bSectionExists(const std::string & _rsName);
rlm@1 134 Section * poAddSection(const std::string & _rsName);
rlm@1 135 Section * poGetSection(const std::string & _rsName);
rlm@1 136 void vRemoveSection(const std::string & _rsName);
rlm@1 137 void vLoad(const std::string & _rsFile,
rlm@1 138 bool _bAddSection = true,
rlm@1 139 bool _bAddKey = true);
rlm@1 140 void vSave(const std::string & _rsFile);
rlm@1 141 void vClear();
rlm@1 142
rlm@1 143 // read only
rlm@1 144 typedef std::list<Section>::const_iterator const_iterator;
rlm@1 145 inline const_iterator begin() const
rlm@1 146 {
rlm@1 147 return std::list<Section>::begin();
rlm@1 148 }
rlm@1 149 inline const_iterator end() const
rlm@1 150 {
rlm@1 151 return std::list<Section>::end();
rlm@1 152 }
rlm@1 153
rlm@1 154 private:
rlm@1 155 inline iterator begin()
rlm@1 156 {
rlm@1 157 return std::list<Section>::begin();
rlm@1 158 }
rlm@1 159 inline iterator end()
rlm@1 160 {
rlm@1 161 return std::list<Section>::end();
rlm@1 162 }
rlm@1 163 };
rlm@1 164
rlm@1 165 // debug
rlm@1 166 std::ostream & operator<<(std::ostream & _roOut, const File & _roConfig);
rlm@1 167
rlm@1 168 template<typename T>
rlm@1 169 void Section::vSetKey(const std::string & _rsKey, const T & _rValue)
rlm@1 170 {
rlm@1 171 std::ostringstream oOut;
rlm@1 172 oOut << _rValue;
rlm@1 173 for (iterator it = begin(); it != end(); it++)
rlm@1 174 {
rlm@1 175 if (it->m_sKey == _rsKey)
rlm@1 176 {
rlm@1 177 it->m_sValue = oOut.str();
rlm@1 178 return;
rlm@1 179 }
rlm@1 180 }
rlm@1 181 push_back(Line(_rsKey, oOut.str()));
rlm@1 182 }
rlm@1 183
rlm@1 184 template<typename T>
rlm@1 185 T Section::oGetKey(const std::string & _rsKey) const
rlm@1 186 {
rlm@1 187 T oValue;
rlm@1 188 for (const_iterator it = begin(); it != end(); it++)
rlm@1 189 {
rlm@1 190 if (it->m_sKey == _rsKey)
rlm@1 191 {
rlm@1 192 std::istringstream oIn(it->m_sValue);
rlm@1 193 oIn >> oValue;
rlm@1 194 return oValue;
rlm@1 195 }
rlm@1 196 }
rlm@1 197 throw KeyNotFound(m_sName, _rsKey);
rlm@1 198 }
rlm@1 199
rlm@1 200 } // namespace Config
rlm@1 201 } // namespace VBA
rlm@1 202
rlm@1 203
rlm@1 204 #endif // __VBA_CONFIGFILE_H__