Commit 2288768bcce0467cf410d1912d44e8f021242e99
1 parent
3289608e
cleaned up turkGallery implementation
Showing
1 changed file
with
28 additions
and
24 deletions
openbr/plugins/turk.cpp
| @@ -13,41 +13,45 @@ class turkGallery : public Gallery | @@ -13,41 +13,45 @@ class turkGallery : public Gallery | ||
| 13 | { | 13 | { |
| 14 | Q_OBJECT | 14 | Q_OBJECT |
| 15 | 15 | ||
| 16 | + struct Attribute : public QStringList | ||
| 17 | + { | ||
| 18 | + QString name; | ||
| 19 | + Attribute(const QString &str = QString()) | ||
| 20 | + { | ||
| 21 | + const int i = str.indexOf('['); | ||
| 22 | + name = str.mid(0, i); | ||
| 23 | + if (i != -1) | ||
| 24 | + append(str.mid(i+1, str.length()-i-2).split(",")); | ||
| 25 | + } | ||
| 26 | + }; | ||
| 27 | + | ||
| 16 | TemplateList readBlock(bool *done) | 28 | TemplateList readBlock(bool *done) |
| 17 | { | 29 | { |
| 18 | *done = true; | 30 | *done = true; |
| 19 | TemplateList templates; | 31 | TemplateList templates; |
| 20 | - if (!file.exists()) return templates; | ||
| 21 | - | ||
| 22 | QStringList lines = QtUtils::readLines(file); | 32 | QStringList lines = QtUtils::readLines(file); |
| 23 | - QRegExp regexp(",(?!(?:\\w+,?)+\\])"); | ||
| 24 | - | ||
| 25 | - QStringList headers; | ||
| 26 | - | ||
| 27 | - if (!lines.isEmpty()) headers = lines.takeFirst().split(regexp); | 33 | + if (lines.empty()) |
| 34 | + qFatal(".turk Gallery missing header."); | ||
| 35 | + QList<Attribute> types; | ||
| 36 | + foreach (const QString &header, parse(lines.takeFirst())) | ||
| 37 | + types.append(header); | ||
| 28 | 38 | ||
| 29 | foreach (const QString &line, lines) { | 39 | foreach (const QString &line, lines) { |
| 30 | - QStringList words = line.split(regexp); | ||
| 31 | - if (words.size() != headers.size()) continue; | ||
| 32 | - File f; | ||
| 33 | - f.name = words[0]; | ||
| 34 | - f.set("Label", words[0].mid(0,5)); | 40 | + const QStringList words = parse(line); |
| 41 | + if (words.size() != types.size()) | ||
| 42 | + qFatal(".turk Gallery incorrect column count."); | ||
| 35 | 43 | ||
| 44 | + File f(words[0], words[0].mid(0,5)); | ||
| 36 | for (int i=1; i<words.size(); i++) { | 45 | for (int i=1; i<words.size(); i++) { |
| 37 | - QStringList categories = headers[i].split('['); | ||
| 38 | - categories.last().chop(1); // Remove trailing bracket | ||
| 39 | - QStringList types = categories.last().split(','); | ||
| 40 | - | ||
| 41 | - QStringList ratings = words[i].split(','); | ||
| 42 | - ratings.first() = ratings.first().mid(1); // Remove first bracket | ||
| 43 | - ratings.last().chop(1); // Remove trailing bracket | ||
| 44 | - | ||
| 45 | - if (types.size() != ratings.size()) continue; | 46 | + Attribute &type = types[i]; |
| 47 | + Attribute rating(words[i]); | ||
| 48 | + if (type.size() != rating.size()) | ||
| 49 | + qFatal(".turk Gallery incorrect ratings count."); | ||
| 46 | 50 | ||
| 47 | QMap<QString,QVariant> categoryMap; | 51 | QMap<QString,QVariant> categoryMap; |
| 48 | - for (int j=0; j<types.size(); j++) categoryMap.insert(types[j],ratings[j]); | ||
| 49 | - | ||
| 50 | - f.set(categories[0], categoryMap); | 52 | + for (int j=0; j<type.size(); j++) |
| 53 | + categoryMap.insert(type[j], rating[j]); | ||
| 54 | + f.set(type.name, categoryMap); | ||
| 51 | } | 55 | } |
| 52 | templates.append(f); | 56 | templates.append(f); |
| 53 | } | 57 | } |