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)
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.
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"
24
#include <glibmm/fileutils.h>
25
#include <glibmm/iochannel.h>
33
using Glib::IOChannel;
35
Line::Line(const string & _rsKey, const string & _rsValue) :
41
Section::Section(const string & _rsName) :
46
bool Section::bKeyExists(const string & _rsKey)
48
for (iterator it = begin(); it != end(); it++)
50
if (it->m_sKey == _rsKey)
58
void Section::vSetKey(const string & _rsKey, const string & _rsValue)
60
for (iterator it = begin(); it != end(); it++)
62
if (it->m_sKey == _rsKey)
64
it->m_sValue = _rsValue;
68
push_back(Line(_rsKey, _rsValue));
71
string Section::sGetKey(const string & _rsKey) const
73
for (const_iterator it = begin(); it != end(); it++)
75
if (it->m_sKey == _rsKey)
80
throw KeyNotFound(m_sName, _rsKey);
83
void Section::vRemoveKey(const string & _rsKey)
85
for (iterator it = begin(); it != end(); it++)
87
if (it->m_sKey == _rsKey)
99
File::File(const string & _rsFile)
108
bool File::bSectionExists(const string & _rsName)
110
for (iterator it = begin(); it != end(); it++)
112
if (it->sGetName() == _rsName)
120
Section * File::poAddSection(const string & _rsName)
122
Section * poSection = NULL;
123
for (iterator it = begin(); it != end(); it++)
125
if (it->sGetName() == _rsName)
130
if (poSection == NULL)
132
push_back(Section(_rsName));
138
Section * File::poGetSection(const string & _rsName)
140
for (iterator it = begin(); it != end(); it++)
142
if (it->sGetName() == _rsName)
147
throw SectionNotFound(_rsName);
150
void File::vRemoveSection(const string & _rsName)
152
for (iterator it = begin(); it != end(); it++)
154
if (it->sGetName() == _rsName)
162
void File::vLoad(const string & _rsFile,
166
string sBuffer = Glib::file_get_contents(_rsFile);
167
Section * poSection = NULL;
168
char ** lines = g_strsplit(sBuffer.c_str(), "\n", 0);
173
if (lines[i][0] == '[')
175
if ((tmp = strchr(lines[i], ']')))
180
poSection = poAddSection(&lines[i][1]);
186
poSection = poGetSection(&lines[i][1]);
195
else if (lines[i][0] != '#' && poSection != NULL)
197
if ((tmp = strchr(lines[i], '=')))
201
if (_bAddKey || poSection->bKeyExists(lines[i]))
203
poSection->vSetKey(lines[i], tmp);
212
void File::vSave(const string & _rsFile)
214
Glib::RefPtr<IOChannel> poFile = IOChannel::create_from_file(_rsFile, "w");
215
poFile->set_encoding("");
217
for (const_iterator poSection = begin();
221
string sName = "[" + poSection->sGetName() + "]\n";
222
poFile->write(sName);
224
for (Section::const_iterator poLine = poSection->begin();
225
poLine != poSection->end();
228
string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n";
229
poFile->write(sLine);
240
std::ostream & operator<<(std::ostream & _roOut, const File & _roFile)
242
for (File::const_iterator poSection = _roFile.begin();
243
poSection != _roFile.end();
246
string sName = "[" + poSection->sGetName() + "]\n";
249
for (Section::const_iterator poLine = poSection->begin();
250
poLine != poSection->end();
253
string sLine = poLine->m_sKey + "=" + poLine->m_sValue + "\n";
261
} // namespace Config