| 1 | // Copyright (C) 2024 basysKom GmbH, opensource@basyskom.com |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "nodeidgenerator.h" |
| 5 | |
| 6 | #include <util.h> |
| 7 | |
| 8 | #include <QDebug> |
| 9 | #include <QDir> |
| 10 | #include <QFile> |
| 11 | |
| 12 | using namespace Qt::Literals::StringLiterals; |
| 13 | |
| 14 | bool NodeIdGenerator::parseNodeIds(const QString &name, const QString &path) |
| 15 | { |
| 16 | if (m_nodeIds.contains(key: name)) { |
| 17 | qWarning() << "Duplicate NodeId map name:" << name; |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | QList<NodeId> result; |
| 22 | |
| 23 | QFile file(path); |
| 24 | |
| 25 | if (!file.open(flags: QIODevice::ReadOnly | QIODevice::Text)) { |
| 26 | qWarning() << "Failed to open" << path << "for reading" ; |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | QTextStream in(&file); |
| 31 | while (!in.atEnd()) { |
| 32 | const auto line = in.readLine(); |
| 33 | const auto split = line.split(sep: QLatin1Char(',')); |
| 34 | |
| 35 | if (split.length() != 3 || split.at(i: 0).isEmpty() || split.at(i: 1).isEmpty()) { |
| 36 | qWarning() << "Skip line" << line << "in file" << path; |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | result.push_back(t: { .name: split.at(i: 0), .value: split.at(i: 1), .type: split.at(i: 2) }); |
| 41 | } |
| 42 | |
| 43 | if (result.empty()) { |
| 44 | qWarning() << "No NodeIds in file" << path; |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | m_nodeIds[name] = result; |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | bool NodeIdGenerator::hasNodeIds() const |
| 54 | { |
| 55 | return !m_nodeIds.empty(); |
| 56 | } |
| 57 | |
| 58 | bool NodeIdGenerator::(const QString &prefix, const QString &path, const QString &) |
| 59 | { |
| 60 | if (!hasNodeIds()) { |
| 61 | qWarning() << "Unable to generate node id files, no known node ids" ; |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | const auto name = u"%1nodeids.h"_s .arg(a: prefix.toLower()); |
| 66 | |
| 67 | QDir dir(path); |
| 68 | if (!dir.mkpath(dirPath: u"."_s )) { |
| 69 | qWarning() << "Failed to create directory" << path << "for writing" ; |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | QFile file(dir.absoluteFilePath(fileName: name)); |
| 74 | |
| 75 | if (!file.open(flags: QFile::WriteOnly | QFile::Truncate)) { |
| 76 | qWarning() << "Failed to open file for writing:" << path << file.errorString(); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | QTextStream out(&file); |
| 81 | |
| 82 | out << header << Util::lineBreak(n: 2); |
| 83 | |
| 84 | out << "#pragma once" << Util::lineBreak(n: 2); |
| 85 | |
| 86 | for (auto it = m_nodeIds.constBegin(); it != m_nodeIds.constEnd();) { |
| 87 | const auto enumName = u"%1NodeId"_s .arg(a: it.key()); |
| 88 | out << "enum class " << enumName << " {" << Util::lineBreak(); |
| 89 | |
| 90 | out << Util::indent(level: 1) << "Unknown = 0," << Util::lineBreak(); |
| 91 | |
| 92 | for (const auto &id : it.value()) { |
| 93 | out << Util::indent(level: 1) << id.name << " = " << id.value << "," ; |
| 94 | if (!id.type.isEmpty()) |
| 95 | out << " // " << id.type; |
| 96 | out << Util::lineBreak(); |
| 97 | } |
| 98 | |
| 99 | out << "};" << Util::lineBreak(n: ++it == m_nodeIds.constEnd() ? 1 : 2 ); |
| 100 | } |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |