view src/gtk/configfile.cpp @ 8:08a8e09ca414

add files required by automake
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:54:39 -0600
parents f9f4f1b99eed
children
line wrap: on
line source
1 // VisualBoyAdvance - Nintendo Gameboy/GameboyAdvance (TM) emulator.
2 // Copyright (C) 1999-2003 Forgotten
3 // Copyright (C) 2004 Forgotten and the VBA development team
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2, or(at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "configfile.h"
21 #include <string.h>
22 #include <glib.h>
24 #include <glibmm/fileutils.h>
25 #include <glibmm/iochannel.h>
27 namespace VBA
28 {
29 namespace Config
30 {
32 using std::string;
33 using Glib::IOChannel;
35 Line::Line(const string & _rsKey, const string & _rsValue) :
36 m_sKey(_rsKey),
37 m_sValue(_rsValue)
38 {
39 }
41 Section::Section(const string & _rsName) :
42 m_sName(_rsName)
43 {
44 }
46 bool Section::bKeyExists(const string & _rsKey)
47 {
48 for (iterator it = begin(); it != end(); it++)
49 {
50 if (it->m_sKey == _rsKey)
51 {
52 return true;
53 }
54 }
55 return false;
56 }
58 void Section::vSetKey(const string & _rsKey, const string & _rsValue)
59 {
60 for (iterator it = begin(); it != end(); it++)
61 {
62 if (it->m_sKey == _rsKey)
63 {
64 it->m_sValue = _rsValue;
65 return;
66 }
67 }
68 push_back(Line(_rsKey, _rsValue));
69 }
71 string Section::sGetKey(const string & _rsKey) const
72 {
73 for (const_iterator it = begin(); it != end(); it++)
74 {
75 if (it->m_sKey == _rsKey)
76 {
77 return it->m_sValue;
78 }
79 }
80 throw KeyNotFound(m_sName, _rsKey);
81 }
83 void Section::vRemoveKey(const string & _rsKey)
84 {
85 for (iterator it = begin(); it != end(); it++)
86 {
87 if (it->m_sKey == _rsKey)
88 {
89 erase(it);
90 return;
91 }
92 }
93 }
95 File::File()
96 {
97 }
99 File::File(const string & _rsFile)
100 {
101 vLoad(_rsFile);
102 }
104 File::~File()
105 {
106 }
108 bool File::bSectionExists(const string & _rsName)
109 {
110 for (iterator it = begin(); it != end(); it++)
111 {
112 if (it->sGetName() == _rsName)
113 {
114 return true;
115 }
116 }
117 return false;
118 }
120 Section * File::poAddSection(const string & _rsName)
121 {
122 Section * poSection = NULL;
123 for (iterator it = begin(); it != end(); it++)
124 {
125 if (it->sGetName() == _rsName)
126 {
127 poSection = &(*it);
128 }
129 }
130 if (poSection == NULL)
131 {
132 push_back(Section(_rsName));
133 poSection = &back();
134 }
135 return poSection;
136 }
138 Section * File::poGetSection(const string & _rsName)
139 {
140 for (iterator it = begin(); it != end(); it++)
141 {
142 if (it->sGetName() == _rsName)
143 {
144 return &(*it);
145 }
146 }
147 throw SectionNotFound(_rsName);
148 }
150 void File::vRemoveSection(const string & _rsName)
151 {
152 for (iterator it = begin(); it != end(); it++)
153 {
154 if (it->sGetName() == _rsName)
155 {
156 erase(it);
157 return;
158 }
159 }
160 }
162 void File::vLoad(const string & _rsFile,
163 bool _bAddSection,
164 bool _bAddKey)
165 {
166 string sBuffer = Glib::file_get_contents(_rsFile);
167 Section * poSection = NULL;
168 char ** lines = g_strsplit(sBuffer.c_str(), "\n", 0);
169 char * tmp;
170 int i = 0;
171 while (lines[i])
172 {
173 if (lines[i][0] == '[')
174 {
175 if ((tmp = strchr(lines[i], ']')))
176 {
177 *tmp = '\0';
178 if (_bAddSection)
179 {
180 poSection = poAddSection(&lines[i][1]);
181 }
182 else
183 {
184 try
185 {
186 poSection = poGetSection(&lines[i][1]);
187 }
188 catch (...)
189 {
190 poSection = NULL;
191 }
192 }
193 }
194 }
195 else if (lines[i][0] != '#' && poSection != NULL)
196 {
197 if ((tmp = strchr(lines[i], '=')))
198 {
199 *tmp = '\0';
200 tmp++;
201 if (_bAddKey || poSection->bKeyExists(lines[i]))
202 {
203 poSection->vSetKey(lines[i], tmp);
204 }
205 }
206 }
207 i++;
208 }
209 g_strfreev(lines);
210 }
212 void File::vSave(const string & _rsFile)
213 {
214 Glib::RefPtr<IOChannel> poFile = IOChannel::create_from_file(_rsFile, "w");
215 poFile->set_encoding("");
217 for (const_iterator poSection = begin();
218 poSection != end();
219 poSection++)
220 {
221 string sName = "[" + poSection->sGetName() + "]\n";
222 poFile->write(sName);
224 for (Section::const_iterator poLine = poSection->begin();
225 poLine != poSection->end();
226 poLine++)
227 {
228 string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n";
229 poFile->write(sLine);
230 }
231 poFile->write("\n");
232 }
233 }
235 void File::vClear()
236 {
237 clear();
238 }
240 std::ostream & operator<<(std::ostream & _roOut, const File & _roFile)
241 {
242 for (File::const_iterator poSection = _roFile.begin();
243 poSection != _roFile.end();
244 poSection++)
245 {
246 string sName = "[" + poSection->sGetName() + "]\n";
247 _roOut << sName;
249 for (Section::const_iterator poLine = poSection->begin();
250 poLine != poSection->end();
251 poLine++)
252 {
253 string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n";
254 _roOut << sLine;
255 }
256 _roOut << "\n";
257 }
258 return _roOut;
259 }
261 } // namespace Config
262 } // namespace VBA