• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 13260145531

11 Feb 2025 09:41AM UTC coverage: 12.127% (+0.03%) from 12.101%
13260145531

Pull #3751

github

10110111
Restore deleted additional SC files
Pull Request #3751: Switch skycultures to the new format

14613 of 120497 relevant lines covered (12.13%)

18620.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.86
/src/core/StelIniParser.cpp
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., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
18
 */
19

20
#include "StelIniParser.hpp"
21
#include "StelUtils.hpp"
22

23
#include <QSettings>
24
#include <QString>
25
#include <QVariant>
26
#include <QStringList>
27
#include <QIODevice>
28
#include <QRegularExpression>
29

30
bool readStelIniFile(QIODevice &device, QSettings::SettingsMap &map)
×
31
{
32
        // Is this the right conversion?
33
        const QString& data = QString::fromUtf8(device.readAll().constData());
×
34

35
        // Split by a RE which should match any platform's line breaking rules
36
        static const QRegularExpression matchLbr("[\\n\\r]+");
×
37
        #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0))
38
        const QStringList& lines = data.split(matchLbr, Qt::SkipEmptyParts);
×
39
        #else
40
        const QStringList& lines = data.split(matchLbr, QString::SkipEmptyParts);
41
        #endif
42

43
        QString currentSection = "";
×
44
        static const QRegularExpression sectionRe("^\\[(.+)\\]$");
×
45
        static const QRegularExpression keyRe("^([^=]+)\\s*=\\s*(.+)$");
×
46
        // Remove char ";" from list of chars for comments within config data
47
        // Details: https://github.com/Stellarium/stellarium/issues/2571
48
        static const QRegularExpression cleanComment("[#].*$");
×
49
        static const QRegularExpression initialWhiteSpace("^\\s+");
×
50
        static const QRegularExpression appendedWhitespace("\\s+$");
×
51

52
        for(int i=0; i<lines.size(); i++)
×
53
        {
54
                QString l = lines.at(i);
×
55
                l.replace(cleanComment, "");                // clean comments
×
56
                l.replace(initialWhiteSpace, "");        // clean whitespace
×
57
                l.replace(appendedWhitespace, "");
×
58

59
                QRegularExpressionMatch sectionMatch=sectionRe.match(l);
×
60
                QRegularExpressionMatch keyMatch=keyRe.match(l);
×
61
                // If it's a section marker set the section variable
62
                if (sectionMatch.hasMatch())
×
63
                        currentSection = sectionMatch.captured(1);
×
64

65
                // Otherwise only process if it matches an re which looks like: key = value
66
                else if (keyMatch.hasMatch())
×
67
                {
68
                        // Let REs do the work for us extracting the key and value
69
                        // and cleaning them up by removing whitespace
70
                        QString k = keyMatch.captured(1);
×
71
                        QString v = keyMatch.captured(2);
×
72
                        v.replace(initialWhiteSpace, "");
×
73
                        k.replace(appendedWhitespace, "");
×
74

75
                        // keys with no section should have no leading /, so only
76
                        // add it when there is a valid section.
77
                        if (currentSection != "")
×
78
                                k = currentSection + "/" + k;
×
79

80
                        // Set the map item.
81
                        map[k] = QVariant(v);
×
82
                }
×
83
        }
×
84
        return true;
×
85
}
×
86

87
bool writeStelIniFile(QIODevice &device, const QSettings::SettingsMap &map)
1✔
88
{
89
        // QSettings only gives us a binary file handle when writing the
90
        // ini file, so we must handle alternative end of line marks
91
        // ourselves.
92
        const QString stelEndl = StelUtils::getEndLineChar();
1✔
93

94
        int maxKeyWidth = 30;
1✔
95
        static const QRegularExpression reKeyXt("^([^/]+)/(.+)$");  // for extracting keys/values
1✔
96

97
        // first go over map and find longest key length
98
        QSettings::SettingsMap::const_iterator it=map.constBegin();
1✔
99
        while (it!=map.constEnd())
5✔
100
        {
101
                QString key=it.key();
4✔
102
                QRegularExpressionMatch match=reKeyXt.match(key);
4✔
103
                if (match.hasMatch())
4✔
104
                        key = match.captured(2);
×
105
                if (key.size() > maxKeyWidth) maxKeyWidth = key.size();
4✔
106
                it++;
4✔
107
        }
4✔
108

109
        // OK, this time actually write to the file - first non-section values
110
        QString outputLine;
1✔
111
        it=map.constBegin();
1✔
112
        while (it != map.constEnd())
5✔
113
        {
114
                QRegularExpressionMatch match=reKeyXt.match(it.key());
4✔
115
                if (!match.hasMatch())
4✔
116
                {
117
                        // this is for those keys without a section
118
                        outputLine = QString("%1 = %2").arg(it.key(),0-maxKeyWidth).arg(it.value().toString()) + stelEndl;
4✔
119
                        device.write(outputLine.toUtf8());
4✔
120
                }
121
                it++;
4✔
122
        }
4✔
123

124
        // Now those values with sections.
125
        QString currentSection("");
1✔
126
        it=map.constBegin();
1✔
127
        while (it != map.constEnd())
5✔
128
        {
129
                QRegularExpressionMatch match=reKeyXt.match(it.key());
4✔
130
                if (match.hasMatch())
4✔
131
                {
132
                        QString section = match.captured(1); QString sectionKey = match.captured(2);
×
133

134
                        // detect new sections and write section headers in file
135
                        if (section != currentSection)
×
136
                        {
137
                                currentSection = section;
×
138

139
                                outputLine = stelEndl + "[" + currentSection + "]" + stelEndl;
×
140
                                device.write(outputLine.toUtf8());
×
141
                        }
142
                        outputLine = QString("%1 = %2").arg(sectionKey,0-maxKeyWidth).arg(it.value().toString()) + stelEndl;
×
143
                        device.write(outputLine.toUtf8());
×
144
                }
×
145
                it++;
4✔
146
        }
4✔
147
        return true;
1✔
148
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc