diff options
Diffstat (limited to 'utils/themeeditor/models/targetdata.cpp')
-rw-r--r-- | utils/themeeditor/models/targetdata.cpp | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/utils/themeeditor/models/targetdata.cpp b/utils/themeeditor/models/targetdata.cpp new file mode 100644 index 0000000000..a20a4cf18f --- /dev/null +++ b/utils/themeeditor/models/targetdata.cpp | |||
@@ -0,0 +1,245 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2010 Robert Bieber | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | #include "targetdata.h" | ||
23 | |||
24 | #include <QStringList> | ||
25 | |||
26 | const QString TargetData::reserved = "{}:#\n"; | ||
27 | |||
28 | TargetData::TargetData(QString file) | ||
29 | { | ||
30 | if(!QFile::exists(file)) | ||
31 | file = ":/targets/targetdb"; | ||
32 | |||
33 | QFile fin(file); | ||
34 | fin.open(QFile::ReadOnly | QFile::Text); | ||
35 | |||
36 | /* Reading the database */ | ||
37 | QString data = QString(fin.readAll()); | ||
38 | |||
39 | fin.close(); | ||
40 | |||
41 | int cursor = 0; | ||
42 | int index = 0; | ||
43 | while(cursor < data.count()) | ||
44 | { | ||
45 | QString id = scanString(data, cursor); | ||
46 | QString name = ""; | ||
47 | QRect size(0, 0, 0, 0); | ||
48 | ScreenDepth depth = None; | ||
49 | QRect rSize(0, 0, 0, 0); | ||
50 | ScreenDepth rDepth = None; | ||
51 | bool fm = false; | ||
52 | |||
53 | if(id == "") | ||
54 | break; | ||
55 | |||
56 | if(!require('{', data, cursor)) | ||
57 | break; | ||
58 | |||
59 | /* Now we have to parse each of the arguments */ | ||
60 | while(cursor < data.count()) | ||
61 | { | ||
62 | QString key = scanString(data, cursor); | ||
63 | if(key == "") | ||
64 | { | ||
65 | break; | ||
66 | } | ||
67 | else | ||
68 | { | ||
69 | if(!require(':', data, cursor)) | ||
70 | break; | ||
71 | |||
72 | if(key.toLower() == "name") | ||
73 | { | ||
74 | name = scanString(data, cursor); | ||
75 | } | ||
76 | else if(key.toLower() == "screen") | ||
77 | { | ||
78 | QString s = scanString(data, cursor); | ||
79 | if(s[0].toLower() != 'n') | ||
80 | { | ||
81 | int subCursor = 0; | ||
82 | int width = scanInt(s, subCursor); | ||
83 | |||
84 | if(!require('x', s, subCursor)) | ||
85 | break; | ||
86 | |||
87 | int height = scanInt(s, subCursor); | ||
88 | |||
89 | if(!require('@', s, subCursor)) | ||
90 | break; | ||
91 | |||
92 | size = QRect(0, 0, width, height); | ||
93 | depth = scanDepth(s, subCursor); | ||
94 | } | ||
95 | } | ||
96 | else if(key.toLower() == "remote") | ||
97 | { | ||
98 | QString s = scanString(data, cursor); | ||
99 | if(s[0].toLower() != 'n') | ||
100 | { | ||
101 | int subCursor = 0; | ||
102 | int width = scanInt(s, subCursor); | ||
103 | |||
104 | if(!require('x', s, subCursor)) | ||
105 | break; | ||
106 | |||
107 | int height = scanInt(s, subCursor); | ||
108 | |||
109 | if(!require('@', s, subCursor)) | ||
110 | break; | ||
111 | |||
112 | rSize = QRect(0, 0, width, height); | ||
113 | rDepth = scanDepth(s, subCursor); | ||
114 | } | ||
115 | } | ||
116 | else if(key.toLower() == "fm") | ||
117 | { | ||
118 | QString s = scanString(data, cursor); | ||
119 | if(s.toLower() == "yes") | ||
120 | fm = true; | ||
121 | } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | /* Checking for the closing '}' and adding the entry */ | ||
126 | if(require('}', data, cursor)) | ||
127 | { | ||
128 | entries.append(Entry(name, size, depth, rSize, rDepth, fm)); | ||
129 | indices.insert(id, index); | ||
130 | index++; | ||
131 | } | ||
132 | else | ||
133 | { | ||
134 | break; | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | |||
139 | TargetData::~TargetData() | ||
140 | { | ||
141 | } | ||
142 | |||
143 | QString TargetData::scanString(QString data, int &index) | ||
144 | { | ||
145 | QString retval; | ||
146 | |||
147 | /* Skipping whitespace and comments */ | ||
148 | while(index < data.count() && (data[index].isSpace() || data[index] == '#')) | ||
149 | { | ||
150 | if(data[index] == '#') | ||
151 | skipComment(data, index); | ||
152 | else | ||
153 | index++; | ||
154 | } | ||
155 | |||
156 | while(index < data.count() && !reserved.contains(data[index])) | ||
157 | { | ||
158 | if(data[index] == '%') | ||
159 | { | ||
160 | retval.append(data[index + 1]); | ||
161 | index += 2; | ||
162 | } | ||
163 | else | ||
164 | { | ||
165 | retval.append(data[index]); | ||
166 | index++; | ||
167 | } | ||
168 | } | ||
169 | |||
170 | return retval.trimmed(); | ||
171 | } | ||
172 | |||
173 | int TargetData::scanInt(QString data, int &index) | ||
174 | { | ||
175 | /* Skipping whitespace and comments */ | ||
176 | while(index < data.count() && (data[index].isSpace() || data[index] == '#')) | ||
177 | { | ||
178 | if(data[index] == '#') | ||
179 | skipComment(data, index); | ||
180 | else | ||
181 | index++; | ||
182 | } | ||
183 | |||
184 | QString number; | ||
185 | while(index < data.count() && data[index].isDigit()) | ||
186 | number.append(data[index++]); | ||
187 | |||
188 | return number.toInt(); | ||
189 | } | ||
190 | |||
191 | TargetData::ScreenDepth TargetData::scanDepth(QString data, int &index) | ||
192 | { | ||
193 | QString depth = scanString(data, index); | ||
194 | |||
195 | if(depth.toLower() == "grey") | ||
196 | return Grey; | ||
197 | else if(depth.toLower() == "rgb") | ||
198 | return RGB; | ||
199 | else if(depth.toLower() == "mono") | ||
200 | return Mono; | ||
201 | else | ||
202 | return None; | ||
203 | } | ||
204 | |||
205 | void TargetData::skipComment(QString data, int &index) | ||
206 | { | ||
207 | if(data[index] != '#') | ||
208 | return; | ||
209 | |||
210 | while(index < data.count() && data[index] != '\n') | ||
211 | index++; | ||
212 | |||
213 | if(index < data.count()) | ||
214 | index++; | ||
215 | } | ||
216 | |||
217 | bool TargetData::require(QChar required, QString data, int &index) | ||
218 | { | ||
219 | /* Skipping whitespace and comments */ | ||
220 | while(index < data.count() && (data[index].isSpace() || data[index] == '#')) | ||
221 | { | ||
222 | if(data[index] == '#') | ||
223 | skipComment(data, index); | ||
224 | else | ||
225 | index++; | ||
226 | } | ||
227 | |||
228 | if(index == data.count()) | ||
229 | { | ||
230 | return false; | ||
231 | } | ||
232 | else | ||
233 | { | ||
234 | if(data[index] == required) | ||
235 | { | ||
236 | index++; | ||
237 | return true; | ||
238 | } | ||
239 | else | ||
240 | { | ||
241 | return false; | ||
242 | } | ||
243 | } | ||
244 | |||
245 | } | ||