summaryrefslogtreecommitdiffstats
path: root/src/arkollon/rcparser.cpp
blob: 6a7850b81f42dc788d428d3efe4375cd2b250d1d (plain)
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
/***************************************************************************
 *   Copyright (C) 2004 by David Sansome                                   *
 *   [email protected]                                                   *
 *                                                                         *
 *   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 <tqregexp.h>
#include <tqfile.h>
#include <tqtextstream.h>

#include "rcparser.h"

RcParser::RcParser()
{
}


RcParser::~RcParser()
{
}

void RcParser::addSearchDir(TQString dir)
{
	dirs.append(dir);
}

bool RcParser::openFile(TQString name)
{
	// Check if it exists
	fileName = "";
	for ( TQStringList::Iterator it = dirs.begin(); it != dirs.end(); ++it )
	{
		if (TQFile::exists((*it) + "/" + name))
		{
			fileName = (*it) + "/" + name;
			break;
		}
	}
	
	if (fileName.isEmpty())
		return false;
	
	// Clear the current data
	sections.clear();
	
	// Read the file's contents
	TQFile file(fileName);
	file.open(IO_ReadOnly);
	TQTextStream stream(&file);
	
	TQRegExp sectionRegExp("^\\[([^\\]]*)\\]$");
	TQRegExp pairRegExp("^([^=\\s]*)([=\\s]*)(.*)$");
	currentSection = "RcParserDefaultSection";
	
	while (!stream.atEnd())
	{
		TQString 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)
		{
			TQString key = pairRegExp.cap(1);
			TQString 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(TQString section)
{
	currentSection = section;
}

TQStringList RcParser::sectionList()
{
	return sections.keys();
}

TQString RcParser::readString(TQString key, TQString def)
{
	TQString ret = sections[currentSection][key];
	if (ret.isEmpty())
		return def;
	return ret;
}

int RcParser::readInt(TQString key, int def)
{
	bool ok;
	int ret = sections[currentSection][key].toInt(&ok);
	if (!ok)
		return def;
	return ret;
}

bool RcParser::readBool(TQString 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;
}

TQStringList RcParser::readList(TQString key)
{
	return TQStringList::split(",", sections[currentSection][key]);
}