view src/gtk/configfile.h @ 2:3549bbe597ed

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