3
* Copyright (C) 2008 Matthew Gates
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
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
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
#include "StelIniParser.hpp"
25
#include <QStringList>
29
bool readStelIniFile(QIODevice &device, QSettings::SettingsMap &map)
31
// Is this the right conversion?
32
QString data = QString::fromUtf8(device.readAll().data());
34
// Split by a RE which should match any platform's line breaking rules
35
QStringList lines = data.split(QRegExp("[\\n\\r]+"), QString::SkipEmptyParts);
37
QString currentSection = "";
38
QRegExp sectionRe("^\\[(.+)\\]$");
39
QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
41
for(int i=0; i<lines.size(); i++)
43
QString l = lines.at(i);
44
l.replace(QRegExp("#.*$"), ""); // clean comments
45
l.replace(QRegExp("^\\s+"), ""); // clean whitespace
46
l.replace(QRegExp("\\s+$"), "");
48
// If it's a section marker set the section variable
49
if(sectionRe.exactMatch(l))
50
currentSection = sectionRe.cap(1);
51
// Otherwise only process if it macthes an re which looks like: key = value
52
else if (keyRe.exactMatch(l))
54
// Let REs do the work for us exatracting the key and value
55
// and cleaning them up by removing whitespace
56
QString k = keyRe.cap(1); QString v = keyRe.cap(2);
57
v.replace(QRegExp("^\\s+"), ""); k.replace(QRegExp("\\s+$"), "");
59
// keys with no section should have no leading /, so only
60
// add it when there is a valid section.
61
if (currentSection != "")
62
k = currentSection + "/" + k;
72
bool writeStelIniFile(QIODevice &device, const QSettings::SettingsMap &map)
75
QRegExp reKeyXt("^(.+)/(.+)$"); // for extracting keys/values
77
// first go over map and find longest key length
78
for(int i=0; i<map.keys().size(); i++)
80
QString k = map.keys().at(i);
82
if (reKeyXt.exactMatch(k))
84
if (key.size() > maxKeyWidth) maxKeyWidth = key.size();
87
// OK, this time actually write to the file - first non-section values
89
for(int i=0; i<map.keys().size(); i++)
91
QString k = map.keys().at(i);
92
if (!reKeyXt.exactMatch(k))
94
// this is for those keys without a section
95
outputLine = QString("%1").arg(k,0-maxKeyWidth) + " = " + map[k].toString() + stelEndl;
96
device.write(outputLine.toUtf8());
100
// Now those values with sections.
101
QString currentSection("");
102
for(int i=0; i<map.keys().size(); i++)
104
QString k = map.keys().at(i);
105
if (reKeyXt.exactMatch(k))
107
QString sec = reKeyXt.cap(1); QString key = reKeyXt.cap(2);
109
// detect new sections and write section headers in file
110
if (sec != currentSection)
112
currentSection = sec;
114
outputLine = stelEndl + "[" + currentSection + "]" + stelEndl;
115
device.write(outputLine.toUtf8());
117
outputLine = QString("%1").arg(key,0-maxKeyWidth) + " = " + map[k].toString() + stelEndl;
118
device.write(outputLine.toUtf8());