1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/*
* rtini.cpp
*
* Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
* and Martin Trautmann (martintrautmann@gmx.de)
*
* This file may be distributed and/or modified under the terms of the
* GNU General Public License version 2 as published by the Free Software
* Foundation.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
* WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
*/
/////////// rtini.cpp //////////////////////
/////////// Implements INI files. //////////
#include "rtini.h"
#include "rtstreams.h"
#include "rtresource.h"
//#include "rtstring.h"
//#include "rtmap.h"
namespace lrt {
IniFile::IniFile() : data(false)
{
}
IniFile::IniFile(const String& filename) : data(false)
{
read(filename);
}
bool IniFile::hasSection(const String& name) const
{
return data.isSet(name);
}
IniSection& IniFile::getSection(const String& name)
{
return data.get(name);
}
const IniSection& IniFile::getSectionConst(const String& name) const
{
return data.get(name);
}
void IniFile::addSection(const String& name, const IniSection& section)
{
data.put(name, section);
}
bool IniFile::removeSection(const String& name)
{
return data.remove(name);
}
IniFile::Iterator IniFile::begin()
{
return data.begin();
}
IniFile::Iterator IniFile::begin() const
{
return data.begin();
}
bool IniFile::read(const String& filename)
{
InputStream* resin = Resource::getResource(filename); // P.I.S. will delete it for us!
if(resin == 0) return false;
ParseInputStream *in = new ParseInputStream(resin, "\n=", ';');
in->setAutoTrim(true);
in->setIgnoreComments(true);
if(in->fail())
{ delete in; return false; }
String curSecName, curOption, curValue;
IniSection curSection;
String curLine;
// skip front where there's nothing interesting
while(!in->eos() && (!(curLine = in->getLine()).startsWith("[")));
if(in->eos()) { delete in; return false; }
// found first section
curSecName = curLine.substring(1, curLine.length() - 1);
while(!in->eos()) {
curLine = in->getLine();
if(curLine.length() == 0) continue; // ignore empty lines
if(curLine.startsWith("[")) { // new section
addSection(curSecName, curSection);
curSection.clear();
curSecName = curLine.substring(1, curLine.length() - 1);
}
else { // option
int eqIndex = curLine.indexOf('=');
if(eqIndex <= 0) continue;
curOption = curLine.substring(0, eqIndex);
curValue = curLine.substring(eqIndex + 1);
curSection[curOption] = curValue;
}
}
// add last section
addSection(curSecName, curSection);
bool ret = !(in->fail());
delete in;
return ret;
}
bool IniFile::write(const String& filename)
{
FileOutputStream *out = new FileOutputStream(filename);
if(out->fail()) { delete out; return false; }
String line;
for(StringMap<IniSection>::Iterator sects = data.begin(); sects.hasElement(); ++sects)
{
line = "\n["; line += (*sects).getKey(); line += "]\n";
out->write(line);
for(IniSection::Iterator opts = (*sects).getValue().begin(); opts.hasElement(); ++opts)
{
line = (*opts).getKey(); line += '='; line += (*opts).getValue(); line += '\n';
out->write(line);
}
}
bool ret = !(out->fail());
delete out;
return ret;
}
} // namespace
|