Commit 3e581e6ccace01bc874cab29b5db9af1718db184
1 parent
07aa03dc
CSV whitespace fixes
Showing
1 changed file
with
101 additions
and
102 deletions
openbr/plugins/gallery/csv.cpp
| @@ -23,94 +23,94 @@ namespace br | @@ -23,94 +23,94 @@ namespace br | ||
| 23 | 23 | ||
| 24 | struct CSVHeader | 24 | struct CSVHeader |
| 25 | { | 25 | { |
| 26 | - QList<int> indices; | 26 | + QList<int> indices; |
| 27 | 27 | ||
| 28 | - CSVHeader() | ||
| 29 | - {} | 28 | + CSVHeader() |
| 29 | + {} | ||
| 30 | 30 | ||
| 31 | - CSVHeader(const QString &key) | 31 | + CSVHeader(const QString &key) |
| 32 | : key(key) | 32 | : key(key) |
| 33 | - {} | ||
| 34 | - | ||
| 35 | - QString key; | ||
| 36 | - QStringList subKeys; | 33 | + {} |
| 34 | + | ||
| 35 | + QString key; | ||
| 36 | + QStringList subKeys; | ||
| 37 | }; | 37 | }; |
| 38 | - | 38 | + |
| 39 | class CSVHeaderList : public QList<CSVHeader> | 39 | class CSVHeaderList : public QList<CSVHeader> |
| 40 | { | 40 | { |
| 41 | public: | 41 | public: |
| 42 | - CSVHeaderList() | ||
| 43 | - {} | ||
| 44 | - | ||
| 45 | - CSVHeaderList(const QList<CSVHeader> &headers) | ||
| 46 | - { | ||
| 47 | - foreach (const CSVHeader &header, headers) | ||
| 48 | - append(header); | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - CSVHeaderList(const QStringList &keys) | ||
| 52 | - { | ||
| 53 | - foreach (const QString &key, keys) | ||
| 54 | - append(CSVHeader(key)); | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - void sort() | ||
| 58 | - { | ||
| 59 | - typedef QPair<QString, int> IndexPair; | ||
| 60 | - QList<IndexPair> sortedKeys = Common::Sort(keys()); | ||
| 61 | - | ||
| 62 | - CSVHeaderList sortedList; | ||
| 63 | - foreach (const IndexPair sortedKey, sortedKeys) | ||
| 64 | - sortedList.append((*this)[sortedKey.second]); | ||
| 65 | - *this = sortedList; | ||
| 66 | - } | ||
| 67 | - | ||
| 68 | - QStringList keys() const | ||
| 69 | - { | ||
| 70 | - QStringList keys; | ||
| 71 | - for (int i=0; i<this->size(); i++) | ||
| 72 | - keys.append((*this)[i].key); | ||
| 73 | - return keys; | ||
| 74 | - } | 42 | + CSVHeaderList() |
| 43 | + {} | ||
| 44 | + | ||
| 45 | + CSVHeaderList(const QList<CSVHeader> &headers) | ||
| 46 | + { | ||
| 47 | + foreach (const CSVHeader &header, headers) | ||
| 48 | + append(header); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + CSVHeaderList(const QStringList &keys) | ||
| 52 | + { | ||
| 53 | + foreach (const QString &key, keys) | ||
| 54 | + append(CSVHeader(key)); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + void sort() | ||
| 58 | + { | ||
| 59 | + typedef QPair<QString, int> IndexPair; | ||
| 60 | + QList<IndexPair> sortedKeys = Common::Sort(keys()); | ||
| 61 | + | ||
| 62 | + CSVHeaderList sortedList; | ||
| 63 | + foreach (const IndexPair sortedKey, sortedKeys) | ||
| 64 | + sortedList.append((*this)[sortedKey.second]); | ||
| 65 | + *this = sortedList; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + QStringList keys() const | ||
| 69 | + { | ||
| 70 | + QStringList keys; | ||
| 71 | + for (int i=0; i<this->size(); i++) | ||
| 72 | + keys.append((*this)[i].key); | ||
| 73 | + return keys; | ||
| 74 | + } | ||
| 75 | 75 | ||
| 76 | static CSVHeaderList fromHeaders(const QStringList &headers) | 76 | static CSVHeaderList fromHeaders(const QStringList &headers) |
| 77 | - { | ||
| 78 | - CSVHeaderList csvHeaders; | ||
| 79 | - QStringList processedKeys; | ||
| 80 | - | ||
| 81 | - for (int i=0; i<headers.size(); i++) { | ||
| 82 | - CSVHeader header; | ||
| 83 | - if (headers[i].contains("_")) { | ||
| 84 | - const QStringList subKeys = headers[i].split("_"); | ||
| 85 | - header.key = subKeys.first(); | ||
| 86 | - | ||
| 87 | - if (processedKeys.contains(header.key)) | ||
| 88 | - continue; | ||
| 89 | - else | ||
| 90 | - processedKeys.append(header.key); | ||
| 91 | - | ||
| 92 | - header.subKeys.append(subKeys.last()); | ||
| 93 | - header.indices.append(i); | ||
| 94 | - | ||
| 95 | - // Look for other subheaders with the same key | ||
| 96 | - for (int j=i+1; j<headers.size(); j++) | ||
| 97 | - if (headers[j].contains("_")) { | ||
| 98 | - const QStringList subKeys = headers[j].split("_"); | ||
| 99 | - if (subKeys.first() == header.key && !header.subKeys.contains(subKeys.last()) /* Check for ill-formed csvs */) { | ||
| 100 | - header.indices.append(j); | ||
| 101 | - header.subKeys.append(subKeys.last()); | ||
| 102 | - } | ||
| 103 | - } | ||
| 104 | - } else { | ||
| 105 | - header.key = headers[i]; | ||
| 106 | - header.indices.append(i); | ||
| 107 | - } | ||
| 108 | - csvHeaders.append(header); | 77 | + { |
| 78 | + CSVHeaderList csvHeaders; | ||
| 79 | + QStringList processedKeys; | ||
| 80 | + | ||
| 81 | + for (int i=0; i<headers.size(); i++) { | ||
| 82 | + CSVHeader header; | ||
| 83 | + if (headers[i].contains("_")) { | ||
| 84 | + const QStringList subKeys = headers[i].split("_"); | ||
| 85 | + header.key = subKeys.first(); | ||
| 86 | + | ||
| 87 | + if (processedKeys.contains(header.key)) | ||
| 88 | + continue; | ||
| 89 | + else | ||
| 90 | + processedKeys.append(header.key); | ||
| 91 | + | ||
| 92 | + header.subKeys.append(subKeys.last()); | ||
| 93 | + header.indices.append(i); | ||
| 94 | + | ||
| 95 | + // Look for other subheaders with the same key | ||
| 96 | + for (int j=i+1; j<headers.size(); j++) | ||
| 97 | + if (headers[j].contains("_")) { | ||
| 98 | + const QStringList subKeys = headers[j].split("_"); | ||
| 99 | + if (subKeys.first() == header.key && !header.subKeys.contains(subKeys.last()) /* Check for ill-formed csvs */) { | ||
| 100 | + header.indices.append(j); | ||
| 101 | + header.subKeys.append(subKeys.last()); | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + } else { | ||
| 105 | + header.key = headers[i]; | ||
| 106 | + header.indices.append(i); | ||
| 107 | + } | ||
| 108 | + csvHeaders.append(header); | ||
| 109 | + } | ||
| 110 | + return csvHeaders; | ||
| 109 | } | 111 | } |
| 110 | - return csvHeaders; | ||
| 111 | - } | ||
| 112 | }; | 112 | }; |
| 113 | - | 113 | + |
| 114 | /*! | 114 | /*! |
| 115 | * \ingroup galleries | 115 | * \ingroup galleries |
| 116 | * \brief Treats each line as a file. | 116 | * \brief Treats each line as a file. |
| @@ -131,7 +131,7 @@ class csvGallery : public FileGallery | @@ -131,7 +131,7 @@ class csvGallery : public FileGallery | ||
| 131 | 131 | ||
| 132 | FileList files; | 132 | FileList files; |
| 133 | CSVHeaderList headers; | 133 | CSVHeaderList headers; |
| 134 | - | 134 | + |
| 135 | ~csvGallery() | 135 | ~csvGallery() |
| 136 | { | 136 | { |
| 137 | f.close(); | 137 | f.close(); |
| @@ -187,7 +187,7 @@ class csvGallery : public FileGallery | @@ -187,7 +187,7 @@ class csvGallery : public FileGallery | ||
| 187 | } | 187 | } |
| 188 | } | 188 | } |
| 189 | } | 189 | } |
| 190 | - | 190 | + |
| 191 | TemplateList readBlock(bool *done) | 191 | TemplateList readBlock(bool *done) |
| 192 | { | 192 | { |
| 193 | readOpen(); | 193 | readOpen(); |
| @@ -206,38 +206,37 @@ class csvGallery : public FileGallery | @@ -206,38 +206,37 @@ class csvGallery : public FileGallery | ||
| 206 | headers = CSVHeaderList::fromHeaders(line.split(regexp).mid(1)); | 206 | headers = CSVHeaderList::fromHeaders(line.split(regexp).mid(1)); |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | - if (combineFiles) { | ||
| 210 | - *done = true; | ||
| 211 | - QMap<QString, File> combinedFiles; | ||
| 212 | - | ||
| 213 | - while (!f.atEnd()) { | 209 | + if (combineFiles) { |
| 210 | + *done = true; | ||
| 211 | + QMap<QString, File> combinedFiles; | ||
| 212 | + | ||
| 213 | + while (!f.atEnd()) { | ||
| 214 | QVariantList values; | 214 | QVariantList values; |
| 215 | foreach (const QString &value, QtUtils::parse(f.readLine(), ',')) | 215 | foreach (const QString &value, QtUtils::parse(f.readLine(), ',')) |
| 216 | values.append(QtUtils::fromString(value)); | 216 | values.append(QtUtils::fromString(value)); |
| 217 | 217 | ||
| 218 | - const QString name = values.first().toString(); | ||
| 219 | - File &in = combinedFiles[name]; | ||
| 220 | - in.name = name; | ||
| 221 | - setValuesFromHeaders(in, headers, values.mid(1)); | ||
| 222 | - } | 218 | + const QString name = values.first().toString(); |
| 219 | + File &in = combinedFiles[name]; | ||
| 220 | + in.name = name; | ||
| 221 | + setValuesFromHeaders(in, headers, values.mid(1)); | ||
| 222 | + } | ||
| 223 | 223 | ||
| 224 | - foreach (const File &in, combinedFiles.values()) | ||
| 225 | - templates.append(in); | ||
| 226 | - } else { | ||
| 227 | - for (qint64 i = 0; i < this->readBlockSize && !f.atEnd(); i++) { | 224 | + foreach (const File &in, combinedFiles.values()) |
| 225 | + templates.append(in); | ||
| 226 | + } else { | ||
| 227 | + for (qint64 i = 0; i < this->readBlockSize && !f.atEnd(); i++) { | ||
| 228 | QVariantList values; | 228 | QVariantList values; |
| 229 | foreach (const QString &value, QtUtils::parse(f.readLine(), ',')) | 229 | foreach (const QString &value, QtUtils::parse(f.readLine(), ',')) |
| 230 | values.append(QtUtils::fromString(value)); | 230 | values.append(QtUtils::fromString(value)); |
| 231 | 231 | ||
| 232 | - File in; | ||
| 233 | - in.name = values.first().toString(); | ||
| 234 | - setValuesFromHeaders(in, headers, values.mid(1)); | ||
| 235 | - in.set("progress", f.pos()); | ||
| 236 | - templates.append(in); | ||
| 237 | - } | ||
| 238 | - *done = f.atEnd(); | 232 | + File in; |
| 233 | + in.name = values.first().toString(); | ||
| 234 | + setValuesFromHeaders(in, headers, values.mid(1)); | ||
| 235 | + in.set("progress", f.pos()); | ||
| 236 | + templates.append(in); | ||
| 237 | + } | ||
| 238 | + *done = f.atEnd(); | ||
| 239 | } | 239 | } |
| 240 | - | ||
| 241 | return templates; | 240 | return templates; |
| 242 | } | 241 | } |
| 243 | 242 | ||
| @@ -247,7 +246,7 @@ class csvGallery : public FileGallery | @@ -247,7 +246,7 @@ class csvGallery : public FileGallery | ||
| 247 | writeOpen(); | 246 | writeOpen(); |
| 248 | if (headers.isEmpty()) { | 247 | if (headers.isEmpty()) { |
| 249 | foreach (const QString &key, t.file.localKeys()) | 248 | foreach (const QString &key, t.file.localKeys()) |
| 250 | - headers.append(CSVHeader(key)); | 249 | + headers.append(CSVHeader(key)); |
| 251 | 250 | ||
| 252 | headers.sort(); | 251 | headers.sort(); |
| 253 | const QString header = QString(QStringList(QStringList("File") + headers.keys()).join(",") + "\n"); | 252 | const QString header = QString(QStringList(QStringList("File") + headers.keys()).join(",") + "\n"); |