|
4 | 4 | */
|
5 | 5 |
|
6 | 6 | #include "chip_db.h"
|
| 7 | +#include <QFileInfo> |
| 8 | +#include <QStandardPaths> |
| 9 | +#include <QMessageBox> |
| 10 | +#include <QDir> |
| 11 | +#include <QTextStream> |
| 12 | + |
| 13 | +ChipDb::ChipDb() |
| 14 | +{ |
| 15 | +} |
| 16 | + |
| 17 | +ChipDb::~ChipDb() |
| 18 | +{ |
| 19 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 20 | + delete chipInfoVector[i]; |
| 21 | +} |
| 22 | + |
| 23 | +int ChipDb::getParamFromString(const QString &value, uint32_t ¶m) |
| 24 | +{ |
| 25 | + bool ok; |
| 26 | + |
| 27 | + param = value.toUInt(&ok); |
| 28 | + if (!ok) |
| 29 | + return -1; |
| 30 | + |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +int ChipDb::getParamFromString(const QString &value, uint8_t ¶m) |
| 35 | +{ |
| 36 | + uint32_t temp; |
| 37 | + |
| 38 | + if (getParamFromString(value, temp)) |
| 39 | + return -1; |
| 40 | + |
| 41 | + if (temp > UINT8_MAX) |
| 42 | + return -1; |
| 43 | + |
| 44 | + param = temp; |
| 45 | + |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +int ChipDb::getParamFromHexString(const QString &value, uint32_t ¶m) |
| 50 | +{ |
| 51 | + bool ok; |
| 52 | + |
| 53 | + param = value.toUInt(&ok, 16); |
| 54 | + if (!ok) |
| 55 | + return -1; |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +int ChipDb::getStringFromParam(const uint32_t ¶m, QString &value) |
| 61 | +{ |
| 62 | + value = QString("%1").arg(param); |
| 63 | + |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +int ChipDb::getHexStringFromParam(const uint32_t ¶m, QString &value) |
| 68 | +{ |
| 69 | + value = QString("0x%1").arg(param, 0, 16, QLatin1Char('0')); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +int ChipDb::getOptParamFromString(const QString &value, uint32_t ¶m) |
| 75 | +{ |
| 76 | + if (value.trimmed() == paramNotDefSymbol) |
| 77 | + { |
| 78 | + param = paramNotDefValue; |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + return getParamFromString(value, param); |
| 83 | +} |
| 84 | + |
| 85 | +int ChipDb::getOptParamFromHexString(const QString &value, |
| 86 | + uint32_t ¶m) |
| 87 | +{ |
| 88 | + if (value.trimmed() == paramNotDefSymbol) |
| 89 | + { |
| 90 | + param = paramNotDefValue; |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + return getParamFromHexString(value, param); |
| 95 | +} |
| 96 | + |
| 97 | +int ChipDb::getStringFromOptParam(const uint32_t ¶m, QString &value) |
| 98 | +{ |
| 99 | + if (param == paramNotDefValue) |
| 100 | + { |
| 101 | + value = paramNotDefSymbol; |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + return getStringFromParam(param, value); |
| 106 | +} |
| 107 | + |
| 108 | +int ChipDb::getHexStringFromOptParam(const uint32_t ¶m, |
| 109 | + QString &value) |
| 110 | +{ |
| 111 | + if (param == paramNotDefValue) |
| 112 | + { |
| 113 | + value = paramNotDefSymbol; |
| 114 | + return 0; |
| 115 | + } |
| 116 | + |
| 117 | + return getHexStringFromParam(param, value); |
| 118 | +} |
| 119 | + |
| 120 | +bool ChipDb::isParamValid(uint32_t param, uint32_t min, uint32_t max) |
| 121 | +{ |
| 122 | + return param >= min && param <= max; |
| 123 | +} |
| 124 | + |
| 125 | +bool ChipDb::isOptParamValid(uint32_t param, uint32_t min, uint32_t max) |
| 126 | +{ |
| 127 | + return (param == paramNotDefValue) || |
| 128 | + (param >= min && param <= max); |
| 129 | +} |
| 130 | + |
| 131 | +QString ChipDb::findFile(QString fileName) |
| 132 | +{ |
| 133 | + if (!QFileInfo(fileName).exists() && |
| 134 | + (fileName = QStandardPaths::locate(QStandardPaths::ConfigLocation, |
| 135 | + fileName)).isNull()) |
| 136 | + { |
| 137 | + QMessageBox::critical(nullptr, QObject::tr("Error"), |
| 138 | + QObject::tr("Chip DB file %1 was not" |
| 139 | + " found in %2;%3").arg(fileName).arg(QDir::currentPath()). |
| 140 | + arg(QStandardPaths::standardLocations(QStandardPaths:: |
| 141 | + ConfigLocation).join(';'))); |
| 142 | + return QString(); |
| 143 | + } |
| 144 | + |
| 145 | + return fileName; |
| 146 | +} |
| 147 | + |
| 148 | +void ChipDb::readFromCvs(void) |
| 149 | +{ |
| 150 | + QFile dbFile; |
| 151 | + QString fileName = findFile(getDbFileName()); |
| 152 | + |
| 153 | + if (fileName.isNull()) |
| 154 | + return; |
| 155 | + |
| 156 | + dbFile.setFileName(fileName); |
| 157 | + if (!dbFile.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 158 | + { |
| 159 | + QMessageBox::critical(nullptr, QObject::tr("Error"), |
| 160 | + QObject::tr("Failed to open chip DB file: %1, error: %2") |
| 161 | + .arg(fileName).arg(dbFile.errorString())); |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + QTextStream in(&dbFile); |
| 166 | + while (!in.atEnd()) |
| 167 | + { |
| 168 | + QString line = in.readLine(); |
| 169 | + if (line.isEmpty()) |
| 170 | + continue; |
| 171 | + if (*line.data() == '#') |
| 172 | + continue; |
| 173 | + |
| 174 | + ChipInfo *ci = stringToChipInfo(line); |
| 175 | + if (!ci) |
| 176 | + break; |
| 177 | + |
| 178 | + chipInfoVector.append(ci); |
| 179 | + } |
| 180 | + dbFile.close(); |
| 181 | +} |
| 182 | + |
| 183 | +int ChipDb::readCommentsFromCsv(QFile &dbFile, QString &comments) |
| 184 | +{ |
| 185 | + if (!dbFile.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 186 | + { |
| 187 | + QMessageBox::critical(nullptr, QObject::tr("Error"), |
| 188 | + QObject::tr("Failed to open chip DB file: %1, error: %2") |
| 189 | + .arg(dbFile.fileName()).arg(dbFile.errorString())); |
| 190 | + return -1; |
| 191 | + } |
| 192 | + |
| 193 | + QTextStream in(&dbFile); |
| 194 | + while (!in.atEnd()) |
| 195 | + { |
| 196 | + QString l = in.readLine(); |
| 197 | + if (l.isEmpty()) |
| 198 | + continue; |
| 199 | + if (*l.data() == '#') |
| 200 | + { |
| 201 | + comments.append(l); |
| 202 | + comments.append('\n'); |
| 203 | + } |
| 204 | + } |
| 205 | + dbFile.close(); |
| 206 | + |
| 207 | + return 0; |
| 208 | +} |
| 209 | + |
| 210 | +void ChipDb::writeToCvs(void) |
| 211 | +{ |
| 212 | + QString line; |
| 213 | + QFile dbFile; |
| 214 | + QString fileName = findFile(getDbFileName()); |
| 215 | + |
| 216 | + if (fileName.isNull()) |
| 217 | + return; |
| 218 | + |
| 219 | + dbFile.setFileName(fileName); |
| 220 | + |
| 221 | + if (readCommentsFromCsv(dbFile, line)) |
| 222 | + return; |
| 223 | + |
| 224 | + if (!dbFile.open(QIODevice::WriteOnly | QIODevice::Truncate | |
| 225 | + QIODevice::Text)) |
| 226 | + { |
| 227 | + QMessageBox::critical(nullptr, QObject::tr("Error"), |
| 228 | + QObject::tr("Failed to open chip DB file: %1, error: %2") |
| 229 | + .arg(fileName).arg(dbFile.errorString())); |
| 230 | + return; |
| 231 | + } |
| 232 | + |
| 233 | + QTextStream out(&dbFile); |
| 234 | + out << line; |
| 235 | + |
| 236 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 237 | + { |
| 238 | + chipInfoToString(chipInfoVector[i], line); |
| 239 | + out << line << '\n'; |
| 240 | + } |
| 241 | + |
| 242 | + dbFile.close(); |
| 243 | +} |
| 244 | + |
| 245 | +QStringList ChipDb::getNames() |
| 246 | +{ |
| 247 | + QStringList namesList; |
| 248 | + |
| 249 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 250 | + namesList.append(chipInfoVector[i]->getName()); |
| 251 | + |
| 252 | + return namesList; |
| 253 | +} |
| 254 | + |
| 255 | +ChipInfo *ChipDb::chipInfoGetById(int id) |
| 256 | +{ |
| 257 | + if (id >= chipInfoVector.size() || id < 0) |
| 258 | + return nullptr; |
| 259 | + |
| 260 | + return chipInfoVector[id]; |
| 261 | +} |
| 262 | + |
| 263 | +ChipInfo *ChipDb::chipInfoGetByName(QString name) |
| 264 | +{ |
| 265 | + for(int i = 0; i < chipInfoVector.size(); i++) |
| 266 | + { |
| 267 | + if (!chipInfoVector[i]->getName().compare(name)) |
| 268 | + return chipInfoVector[i]; |
| 269 | + } |
| 270 | + |
| 271 | + return nullptr; |
| 272 | +} |
| 273 | + |
| 274 | +uint32_t ChipDb::pageSizeGetById(int id) |
| 275 | +{ |
| 276 | + ChipInfo *info = chipInfoGetById(id); |
| 277 | + |
| 278 | + return info ? info->getPageSize() : 0; |
| 279 | +} |
| 280 | + |
| 281 | +uint32_t ChipDb::pageSizeGetByName(const QString &name) |
| 282 | +{ |
| 283 | + ChipInfo *info = chipInfoGetByName(name); |
| 284 | + |
| 285 | + return info ? info->getPageSize() : 0; |
| 286 | +} |
| 287 | + |
| 288 | +uint32_t ChipDb::extendedPageSizeGetById(int id) |
| 289 | +{ |
| 290 | + ChipInfo *info = chipInfoGetById(id); |
| 291 | + |
| 292 | + if (!info) |
| 293 | + return 0; |
| 294 | + |
| 295 | + return info->getPageSize() + info->getSpareSize(); |
| 296 | +} |
| 297 | + |
| 298 | +uint32_t ChipDb::extendedPageSizeGetByName(const QString &name) |
| 299 | +{ |
| 300 | + ChipInfo *info = chipInfoGetByName(name); |
| 301 | + |
| 302 | + if (!info) |
| 303 | + return 0; |
| 304 | + |
| 305 | + return info->getPageSize() + info->getSpareSize(); |
| 306 | +} |
| 307 | + |
| 308 | +uint32_t ChipDb::totalSizeGetById(int id) |
| 309 | +{ |
| 310 | + ChipInfo *info = chipInfoGetById(id); |
| 311 | + |
| 312 | + return info ? info->getTotalSize() : 0; |
| 313 | +} |
| 314 | + |
| 315 | +uint32_t ChipDb::totalSizeGetByName(const QString &name) |
| 316 | +{ |
| 317 | + ChipInfo *info = chipInfoGetByName(name); |
| 318 | + |
| 319 | + return info ? info->getTotalSize() : 0; |
| 320 | +} |
| 321 | + |
| 322 | +uint32_t ChipDb::extendedTotalSizeGetById(int id) |
| 323 | +{ |
| 324 | + uint32_t totalSize, totalSpare; |
| 325 | + ChipInfo *info = chipInfoGetById(id); |
| 326 | + |
| 327 | + if (!info) |
| 328 | + return 0; |
| 329 | + |
| 330 | + totalSize = info->getTotalSize(); |
| 331 | + totalSpare = info->getSpareSize() * (totalSize / info->getPageSize()); |
| 332 | + |
| 333 | + return totalSize + totalSpare; |
| 334 | +} |
| 335 | + |
| 336 | +uint32_t ChipDb::extendedTotalSizeGetByName(const QString &name) |
| 337 | +{ |
| 338 | + uint32_t totalSize, totalSpare; |
| 339 | + ChipInfo *info = chipInfoGetByName(name); |
| 340 | + |
| 341 | + if (!info) |
| 342 | + return 0; |
| 343 | + |
| 344 | + totalSize = info->getTotalSize(); |
| 345 | + totalSpare = info->getSpareSize() * (totalSize / info->getPageSize()); |
| 346 | + |
| 347 | + return totalSize + totalSpare; |
| 348 | +} |
| 349 | + |
| 350 | +void ChipDb::addChip(ChipInfo *chipInfo) |
| 351 | +{ |
| 352 | + chipInfoVector.append(chipInfo); |
| 353 | +} |
| 354 | + |
| 355 | +void ChipDb::delChip(int index) |
| 356 | +{ |
| 357 | + delete chipInfoVector[index]; |
| 358 | + chipInfoVector.remove(index); |
| 359 | +} |
| 360 | + |
| 361 | +int ChipDb::size() |
| 362 | +{ |
| 363 | + return chipInfoVector.size(); |
| 364 | +} |
| 365 | + |
| 366 | +void ChipDb::commit() |
| 367 | +{ |
| 368 | + writeToCvs(); |
| 369 | +} |
| 370 | + |
| 371 | +void ChipDb::reset() |
| 372 | +{ |
| 373 | + for (int i = 0; i < chipInfoVector.size(); i++) |
| 374 | + delete chipInfoVector[i]; |
| 375 | + chipInfoVector.clear(); |
| 376 | + readFromCvs(); |
| 377 | +} |
| 378 | + |
| 379 | +ChipInfo *ChipDb::getChipInfo(int chipIndex) |
| 380 | +{ |
| 381 | + return chipIndex >= 0 && chipIndex < chipInfoVector.size() ? |
| 382 | + chipInfoVector[chipIndex] : nullptr; |
| 383 | +} |
7 | 384 |
|
8 | 385 | QString ChipDb::getChipName(int chipIndex)
|
9 | 386 | {
|
|
0 commit comments