Thu, 29 Mar 2018 12:09:06 +0300
Branch close
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | * LDForge: LDraw parts authoring CAD |
1072 | 3 | * Copyright (C) 2013 - 2017 Teemu Piippo |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | * This program is free software: you can redistribute it and/or modify |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | * it under the terms of the GNU General Public License as published by |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | * the Free Software Foundation, either version 3 of the License, or |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | * (at your option) any later version. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | * This program is distributed in the hope that it will be useful, |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | * GNU General Public License for more details. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | * |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | * You should have received a copy of the GNU General Public License |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | */ |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
998 | 19 | #include <QMessageBox> |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | #include "colors.h" |
1012 | 21 | #include "ldpaths.h" |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
23 | ColorData* LDColor::colorData = nullptr; |
1153 | 24 | const LDColor LDColor::nullColor = -1; |
946 | 25 | |
1044 | 26 | /* |
27 | * Initializes the color information module. | |
28 | */ | |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
29 | void LDColor::initColors() |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | { |
998 | 31 | static ColorData colors; |
1114
ffd49a28f49e
Moved some global constants into appropriate namespaces.
Teemu Piippo <teemu@hecknology.net>
parents:
1086
diff
changeset
|
32 | LDColor::colorData = &colors; |
946 | 33 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
1044 | 35 | /* |
1153 | 36 | * Default-constructs an LDColor to 0(black). |
1044 | 37 | */ |
38 | LDColor::LDColor() : | |
1153 | 39 | m_index {0} {} |
1044 | 40 | |
41 | /* | |
42 | * Constructs an LDColor by index. | |
43 | */ | |
1153 | 44 | LDColor::LDColor(qint32 index) : |
45 | m_index {index} {} | |
1044 | 46 | |
47 | /* | |
1174
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
48 | * Constructs a direct color. |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
49 | */ |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
50 | LDColor::LDColor(QColor color, bool transparent) : |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
51 | m_index {directColorIndex(color, transparent)} {} |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
52 | |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
53 | /* |
1044 | 54 | * Returns whether or not the color is valid. |
55 | */ | |
946 | 56 | bool LDColor::isValid() const |
57 | { | |
998 | 58 | if (isLDConfigColor() and data().name.isEmpty()) |
946 | 59 | return false; // Unknown LDConfig color |
1044 | 60 | else |
61 | return m_index != -1; | |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
62 | } |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
1044 | 64 | /* |
65 | * Returns whether or not this color is defined in LDConfig.ldr. | |
66 | * This is false for e.g. direct colors. | |
67 | */ | |
946 | 68 | bool LDColor::isLDConfigColor() const |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
69 | { |
1044 | 70 | return colorData->contains(index()); |
998 | 71 | } |
72 | ||
1044 | 73 | /* |
74 | * Returns the ColorData entry for this color. | |
75 | */ | |
998 | 76 | const ColorData::Entry& LDColor::data() const |
77 | { | |
1044 | 78 | return colorData->get(index()); |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
79 | } |
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
80 | |
1044 | 81 | /* |
82 | * Returns the name of this color. | |
83 | */ | |
946 | 84 | QString LDColor::name() const |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
85 | { |
946 | 86 | if (isDirect()) |
1153 | 87 | return "0x" + QString::number(index(), 16).toUpper(); |
946 | 88 | else if (isLDConfigColor()) |
998 | 89 | return data().name; |
946 | 90 | else if (index() == -1) |
91 | return "null color"; | |
92 | else | |
1044 | 93 | return "unknown"; |
946 | 94 | } |
95 | ||
1044 | 96 | /* |
97 | * Returns the hexadecimal code of this color. | |
98 | */ | |
946 | 99 | QString LDColor::hexcode() const |
100 | { | |
101 | return faceColor().name(); | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | |
1044 | 104 | /* |
105 | * Returns the color used for surfaces. | |
106 | */ | |
946 | 107 | QColor LDColor::faceColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | { |
946 | 109 | if (isDirect()) |
110 | { | |
1044 | 111 | // Direct color -- compute from the index. |
946 | 112 | QColor color; |
1044 | 113 | color.setRed((index() & 0x0FF0000) >> 16); |
114 | color.setGreen((index() & 0x000FF00) >> 8); | |
115 | color.setBlue(index() & 0x00000FF); | |
946 | 116 | |
117 | if (index() >= 0x3000000) | |
1044 | 118 | color.setAlpha(128); |
946 | 119 | |
120 | return color; | |
121 | } | |
122 | else if (isLDConfigColor()) | |
123 | { | |
998 | 124 | return data().faceColor; |
946 | 125 | } |
126 | else | |
127 | { | |
128 | return Qt::black; | |
129 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | |
1044 | 132 | /* |
133 | * Returns the color used for edge lines. | |
134 | */ | |
946 | 135 | QColor LDColor::edgeColor() const |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | { |
946 | 137 | if (isDirect()) |
1044 | 138 | return luma(faceColor()) < 48 ? Qt::white : Qt::black; |
946 | 139 | else if (isLDConfigColor()) |
998 | 140 | return data().edgeColor; |
946 | 141 | else |
142 | return Qt::black; | |
143 | } | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | |
1044 | 145 | /* |
146 | * Returns the index number of this color. | |
147 | */ | |
946 | 148 | qint32 LDColor::index() const |
149 | { | |
150 | return m_index; | |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | } |
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | |
1044 | 153 | /* |
154 | * Returns a string containing the preferred representation of the index. | |
155 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
156 | QString LDColor::indexString() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
157 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
158 | if (isDirect()) |
1044 | 159 | { |
160 | // Use hexadecimal notation for direct colors. | |
161 | return "0x" + QString::number(index(), 16).toUpper(); | |
162 | } | |
163 | else | |
164 | { | |
165 | return QString::number(index()); | |
166 | } | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
167 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
168 | |
1044 | 169 | /* |
170 | * Returns whether or not this color is a direct color. | |
171 | * Direct colors are picked by RGB value and are not defined in LDConfig.ldr. | |
172 | */ | |
806
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
173 | bool LDColor::isDirect() const |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
174 | { |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
175 | return index() >= 0x02000000; |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
176 | } |
4240f47aa2d4
- moved most of LDColorData API into LDColor
Santeri Piippo <crimsondusk64@gmail.com>
parents:
797
diff
changeset
|
177 | |
1044 | 178 | /* |
1174
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
179 | * Returns the LDraw color index for a direct color. |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
180 | */ |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
181 | qint32 LDColor::directColorIndex(QColor color, bool transparent) |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
182 | { |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
183 | qint32 index = transparent ? 0x03000000 : 0x02000000; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
184 | index |= color.red() << 16; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
185 | index |= color.green() << 8; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
186 | index |= color.blue(); |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
187 | return index; |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
188 | } |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
189 | |
91696a2e022c
Cleanup ColorSelector, moved direct color composition to a new LDColor constructor.
Teemu Piippo <teemu@hecknology.net>
parents:
1159
diff
changeset
|
190 | /* |
1044 | 191 | * LDColors are hashed by their index. |
192 | */ | |
1031
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
193 | uint qHash(LDColor color) |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
194 | { |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
195 | return color.index(); |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
196 | } |
55c0d3beea0d
removed removeDuplicates in favor of QSet, and the unused ObjectList class
Teemu Piippo <crimsondusk64@gmail.com>
parents:
1014
diff
changeset
|
197 | |
1044 | 198 | /* |
199 | * Calculates the luma-value for the given color. | |
200 | * c.f. https://en.wikipedia.org/wiki/Luma_(video) | |
201 | */ | |
1153 | 202 | int luma(const QColor& color) |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
203 | { |
1153 | 204 | return static_cast<int>(round(0.2126 * color.red() + 0.7152 * color.green() + 0.0722 * color.blue())); |
655
b376645315ab
- renamed files to camelCase
Santeri Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
205 | } |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
206 | |
1044 | 207 | /* |
208 | * Constructs the color data array. | |
209 | */ | |
998 | 210 | ColorData::ColorData() |
795
195fa1fff9c3
- changed all color usage to use LDColor classes instead of color indices. Added support for direct colors.
Santeri Piippo <crimsondusk64@gmail.com>
parents:
706
diff
changeset
|
211 | { |
998 | 212 | // Initialize main and edge colors, they're special like that. |
1044 | 213 | m_data[MainColor].faceColor = "#AAAAAA"; |
998 | 214 | m_data[MainColor].edgeColor = Qt::black; |
215 | m_data[MainColor].name = "Main color"; | |
1153 | 216 | m_data[EdgeColor].faceColor = Qt::black; |
217 | m_data[EdgeColor].edgeColor = Qt::black; | |
998 | 218 | m_data[EdgeColor].name = "Edge color"; |
1044 | 219 | |
220 | // Load the rest from LDConfig.ldr. | |
998 | 221 | loadFromLdconfig(); |
222 | } | |
223 | ||
1044 | 224 | /* |
225 | * ColorData :: contains | |
226 | * | |
227 | * Returns whether or not the given color index is present in the array. | |
228 | */ | |
229 | bool ColorData::contains(int code) const | |
946 | 230 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
231 | return code >= 0 and code < countof(m_data); |
998 | 232 | } |
233 | ||
1044 | 234 | /* |
235 | * Returns an entry in the color array. | |
236 | */ | |
237 | const ColorData::Entry& ColorData::get(int code) const | |
998 | 238 | { |
1044 | 239 | if (not contains(code)) |
240 | throw std::runtime_error {"Attempted to get non-existant color information"}; | |
946 | 241 | |
998 | 242 | return m_data[code]; |
243 | } | |
244 | ||
1044 | 245 | /* |
246 | * Loads color information from LDConfig.ldr. | |
247 | */ | |
998 | 248 | void ColorData::loadFromLdconfig() |
249 | { | |
1012 | 250 | QString path = LDPaths::ldConfigPath(); |
1044 | 251 | QFile file {path}; |
998 | 252 | |
1153 | 253 | if (not file.open(QIODevice::ReadOnly)) |
946 | 254 | { |
1044 | 255 | QMessageBox::critical(nullptr, "Error", "Unable to open LDConfig.ldr for parsing: " + file.errorString()); |
946 | 256 | return; |
257 | } | |
258 | ||
998 | 259 | // TODO: maybe LDConfig can be loaded as a Document? Or would that be overkill? |
1044 | 260 | while (not file.atEnd()) |
946 | 261 | { |
1153 | 262 | QString line = QString::fromUtf8(file.readLine()); |
946 | 263 | |
264 | if (line.isEmpty() or line[0] != '0') | |
265 | continue; // empty or illogical | |
266 | ||
1044 | 267 | line.remove('\r'); |
268 | line.remove('\n'); | |
946 | 269 | |
270 | // Parse the line | |
1044 | 271 | LDConfigParser parser = {line}; |
998 | 272 | QString name; |
273 | QString facename; | |
274 | QString edgename; | |
275 | QString codestring; | |
946 | 276 | |
277 | // Check 0 !COLOUR, parse the name | |
1044 | 278 | if (not parser.compareToken(0, "0") or not parser.compareToken(1, "!COLOUR") or not parser.getToken(name, 2)) |
946 | 279 | continue; |
280 | ||
281 | // Replace underscores in the name with spaces for readability | |
1044 | 282 | name.replace("_", " "); |
946 | 283 | |
1153 | 284 | if (not parser.parseTag("CODE", codestring)) |
946 | 285 | continue; |
286 | ||
287 | bool ok; | |
1044 | 288 | int code = codestring.toShort(&ok); |
946 | 289 | |
1044 | 290 | if (not ok or not contains(code)) |
946 | 291 | continue; |
292 | ||
1044 | 293 | if (not parser.parseTag("VALUE", facename) or not parser.parseTag("EDGE", edgename)) |
946 | 294 | continue; |
295 | ||
296 | // Ensure that our colors are correct | |
1044 | 297 | QColor faceColor = {facename}; |
298 | QColor edgeColor = {edgename}; | |
946 | 299 | |
300 | if (not faceColor.isValid() or not edgeColor.isValid()) | |
301 | continue; | |
302 | ||
1044 | 303 | // Fill in the entry now. |
998 | 304 | Entry& entry = m_data[code]; |
946 | 305 | entry.name = name; |
306 | entry.faceColor = faceColor; | |
307 | entry.edgeColor = edgeColor; | |
998 | 308 | |
1044 | 309 | // If the alpha tag is present, fill in that too. |
310 | if (parser.parseTag("ALPHA", codestring)) | |
311 | entry.faceColor.setAlpha(qBound(0, codestring.toInt(), 255)); | |
946 | 312 | } |
313 | } | |
314 | ||
1044 | 315 | /* |
316 | * Constructs the LDConfig.ldr parser. | |
317 | */ | |
318 | LDConfigParser::LDConfigParser(QString inputText) | |
946 | 319 | { |
1153 | 320 | m_tokens = inputText.split(' ', QString::SkipEmptyParts); |
946 | 321 | } |
322 | ||
1044 | 323 | /* |
324 | * Returns whether or not there is a token at the given position. | |
325 | * If there is, fills in the value parameter with it. | |
326 | */ | |
327 | bool LDConfigParser::getToken(QString& tokenText, int position) | |
946 | 328 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
329 | if (position >= countof(m_tokens)) |
1044 | 330 | { |
946 | 331 | return false; |
1044 | 332 | } |
333 | else | |
334 | { | |
335 | tokenText = m_tokens[position]; | |
336 | return true; | |
337 | } | |
946 | 338 | } |
339 | ||
1044 | 340 | /* |
341 | * Attempts to find the provided token in the parsed LDConfig.ldr line. | |
342 | * If found, fills in the first parameter with the position of the token. | |
343 | * | |
344 | * The args parameter specifies how many arguments (i.e. following tokens) the token needs to have. | |
345 | */ | |
346 | bool LDConfigParser::findToken(int& tokenPosition, QString needle, int args) | |
946 | 347 | { |
1065
c8ecddbd99e9
Actually, let's call it countof(). Makes more sense.
Teemu Piippo <teemu@hecknology.net>
parents:
1063
diff
changeset
|
348 | for (int i = 0; i < (countof(m_tokens) - args); ++i) |
946 | 349 | { |
350 | if (m_tokens[i] == needle) | |
351 | { | |
1044 | 352 | tokenPosition = i; |
946 | 353 | return true; |
354 | } | |
355 | } | |
356 | ||
357 | return false; | |
358 | } | |
359 | ||
1044 | 360 | /* |
361 | * Returns whether or not the token at the given position has the given text value. | |
362 | */ | |
1153 | 363 | bool LDConfigParser::compareToken(int position, QString text) |
946 | 364 | { |
1044 | 365 | QString token; |
1153 | 366 | return getToken(token, position) and (token == text); |
946 | 367 | } |
368 | ||
1044 | 369 | /* |
370 | * Finds an attribute in the line, and fills in its value. | |
371 | * For instance, if the line contains "ALPHA 128", this function can find the "128" for "ALPHA". | |
372 | * Returns whether or not the attribute was found. | |
373 | */ | |
1153 | 374 | bool LDConfigParser::parseTag(QString key, QString& value) |
946 | 375 | { |
1044 | 376 | int position; |
946 | 377 | |
378 | // Try find the token and get its position | |
1153 | 379 | if (not findToken(position, key, 1)) |
1044 | 380 | { |
946 | 381 | return false; |
1044 | 382 | } |
383 | else | |
384 | { | |
385 | // Get the token after it and store it in. | |
1153 | 386 | return getToken(value, position + 1); |
1044 | 387 | } |
388 | } |