~ubuntu-branches/debian/jessie/stellarium/jessie

« back to all changes in this revision

Viewing changes to src/stelutils/StelIniParser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cédric Delfosse
  • Date: 2009-03-13 20:07:22 UTC
  • mfrom: (1.1.8 upstream)
  • mto: (11.1.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 7.
  • Revision ID: james.westby@ubuntu.com-20090313200722-gbgujsmzsa8a02ty
Import upstream version 0.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Stellarium
3
 
 * Copyright (C) 2008 Matthew Gates
4
 
 * 
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.
9
 
 * 
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.
14
 
 * 
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.
18
 
 */
19
 
 
20
 
#include "StelIniParser.hpp"
21
 
 
22
 
#include <QSettings>
23
 
#include <QString>
24
 
#include <QVariant>
25
 
#include <QStringList>
26
 
#include <QIODevice>
27
 
#include <QRegExp>
28
 
 
29
 
bool readStelIniFile(QIODevice &device, QSettings::SettingsMap &map)
30
 
{
31
 
        // Is this the right conversion?
32
 
        QString data = QString::fromUtf8(device.readAll().data());
33
 
 
34
 
        // Split by a RE which should match any platform's line breaking rules
35
 
        QStringList lines = data.split(QRegExp("[\\n\\r]+"), QString::SkipEmptyParts);
36
 
 
37
 
        QString currentSection = "";
38
 
        QRegExp sectionRe("^\\[(.+)\\]$");
39
 
        QRegExp keyRe("^([^=]+)\\s*=\\s*(.+)$");
40
 
 
41
 
        for(int i=0; i<lines.size(); i++)
42
 
        {
43
 
                QString l = lines.at(i);
44
 
                l.replace(QRegExp("#.*$"), "");         // clean comments
45
 
                l.replace(QRegExp("^\\s+"), "");        // clean whitespace
46
 
                l.replace(QRegExp("\\s+$"), "");
47
 
 
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))
53
 
                {
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+$"), "");
58
 
 
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;
63
 
 
64
 
                        // Set the map item.
65
 
                        map[k] = QVariant(v);
66
 
                }
67
 
        }
68
 
        
69
 
        return true;
70
 
}
71
 
 
72
 
bool writeStelIniFile(QIODevice &device, const QSettings::SettingsMap &map)
73
 
{
74
 
        int maxKeyWidth = 30;
75
 
        QRegExp reKeyXt("^(.+)/(.+)$");  // for extracting keys/values
76
 
 
77
 
        // first go over map and find longest key length
78
 
        for(int i=0; i<map.keys().size(); i++)
79
 
        {
80
 
                QString k = map.keys().at(i);
81
 
                QString key = k;
82
 
                if (reKeyXt.exactMatch(k))
83
 
                        key = reKeyXt.cap(2);
84
 
                if (key.size() > maxKeyWidth) maxKeyWidth = key.size();
85
 
        }
86
 
 
87
 
        // OK, this time actually write to the file - first non-section values
88
 
        QString outputLine;
89
 
        for(int i=0; i<map.keys().size(); i++)
90
 
        {
91
 
                QString k = map.keys().at(i);
92
 
                if (!reKeyXt.exactMatch(k))
93
 
                {
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());
97
 
                }
98
 
        }
99
 
        
100
 
        // Now those values with sections.
101
 
        QString currentSection("");
102
 
        for(int i=0; i<map.keys().size(); i++)
103
 
        {
104
 
                QString k = map.keys().at(i);
105
 
                if (reKeyXt.exactMatch(k))
106
 
                {
107
 
                        QString sec = reKeyXt.cap(1); QString key = reKeyXt.cap(2);
108
 
 
109
 
                        // detect new sections and write section headers in file
110
 
                        if (sec != currentSection)
111
 
                        {
112
 
                                currentSection = sec;
113
 
                                
114
 
                                outputLine = stelEndl + "[" + currentSection + "]" + stelEndl;
115
 
                                device.write(outputLine.toUtf8());
116
 
                        }
117
 
                        outputLine = QString("%1").arg(key,0-maxKeyWidth) + " = " + map[k].toString() + stelEndl;
118
 
                        device.write(outputLine.toUtf8());
119
 
                }
120
 
        }
121
 
        return true;
122
 
}
123