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 146
|
/***************************************************************************
* Copyright (C) 2004 by David Sansome *
* me@davidsansome.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "rcparser.h"
#include <qregexp.h>
#include <qfile.h>
#include <qtextstream.h>
RcParser::RcParser()
{
}
RcParser::~RcParser()
{
}
void RcParser::addSearchDir(QString dir)
{
dirs.append(dir);
}
bool RcParser::openFile(QString name)
{
// Check if it exists
fileName = "";
for ( QStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it )
{
if (QFile::exists((*it) + "/" + name))
{
fileName = (*it) + "/" + name;
break;
}
}
if (fileName.isEmpty())
return false;
// Clear the current data
sections.clear();
// Read the file's contents
QFile file(fileName);
file.open(IO_ReadOnly);
QTextStream stream(&file);
QRegExp sectionRegExp("^\\[([^\\]]*)\\]$");
QRegExp pairRegExp("^([^=\\s]*)([=\\s]*)(.*)$");
currentSection = "RcParserDefaultSection";
while (!stream.atEnd())
{
QString line = stream.readLine();
if (line.left(1) == "#") // Comment
continue;
line = line.stripWhiteSpace();
if (sectionRegExp.search(line) != -1)
{
currentSection = sectionRegExp.cap(1);
//printf("Found section \"%s\"\n", currentSection.latin1());
continue;
}
if (pairRegExp.search(line) != -1)
{
QString key = pairRegExp.cap(1);
QString value = pairRegExp.cap(3);
sections[currentSection][key] = value;
//printf("Found pair \"%s\" = \"%s\"\n", key.latin1(), value.latin1());
continue;
}
// Parse error, ignore the line
}
currentSection = "RcParserDefaultSection";
return true;
}
void RcParser::setSection(QString section)
{
currentSection = section;
}
QStringList RcParser::sectionList()
{
return sections.keys();
}
QString RcParser::readString(QString key, QString def)
{
QString ret = sections[currentSection][key];
if (ret.isEmpty())
return def;
return ret;
}
int RcParser::readInt(QString key, int def)
{
bool ok;
int ret = sections[currentSection][key].toInt(&ok);
if (!ok)
return def;
return ret;
}
bool RcParser::readBool(QString key, bool def)
{
bool ret = def;
if (sections[currentSection][key].lower() == "true")
ret = true;
if (sections[currentSection][key].lower() == "false")
ret = false;
if (sections[currentSection][key] == "1")
ret = true;
if (sections[currentSection][key] == "0")
ret = false;
return ret;
}
QStringList RcParser::readList(QString key)
{
return QStringList::split(",", sections[currentSection][key]);
}
|