From e56e769d279744177b9074aad759cd0f6bdc2ecf Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sat, 25 Dec 2021 12:10:57 +0100 Subject: [PATCH] Patch `.bin` filename to `.yml` --- .../src/firmwares/edgetx/edgetxinterface.cpp | 15 +++++++++++++++ companion/src/firmwares/edgetx/edgetxinterface.h | 2 ++ .../src/firmwares/edgetx/yaml_generalsettings.cpp | 2 +- companion/src/storage/categorized.cpp | 8 +++++--- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/companion/src/firmwares/edgetx/edgetxinterface.cpp b/companion/src/firmwares/edgetx/edgetxinterface.cpp index 878757294..d5a90de1f 100644 --- a/companion/src/firmwares/edgetx/edgetxinterface.cpp +++ b/companion/src/firmwares/edgetx/edgetxinterface.cpp @@ -158,3 +158,18 @@ bool writeRadioSettingsToYaml(const GeneralSettings& settings, QByteArray& data) writeYamlToByteArray(node, data); return true; } + +std::string patchFilenameToYaml(const std::string& str) +{ + constexpr const char MODELS_EXT[] = ".bin"; + constexpr const char YAML_EXT[] = ".yml"; + constexpr unsigned bin_len = sizeof(MODELS_EXT) - 1; + + // patch file extension + auto const dot_pos = str.find_last_of('.'); + if ((dot_pos != std::string::npos) && (str.substr(dot_pos) == MODELS_EXT)) { + std::string tmp = str; + return tmp.replace(dot_pos, bin_len, YAML_EXT); + } + return str; +} diff --git a/companion/src/firmwares/edgetx/edgetxinterface.h b/companion/src/firmwares/edgetx/edgetxinterface.h index 60b69119d..4512ccd8d 100644 --- a/companion/src/firmwares/edgetx/edgetxinterface.h +++ b/companion/src/firmwares/edgetx/edgetxinterface.h @@ -50,3 +50,5 @@ bool writeModelsListToYaml(const std::vector& categories, bool writeModelToYaml(const ModelData& model, QByteArray& data); bool writeRadioSettingsToYaml(const GeneralSettings& settings, QByteArray& data); + +std::string patchFilenameToYaml(const std::string& str); diff --git a/companion/src/firmwares/edgetx/yaml_generalsettings.cpp b/companion/src/firmwares/edgetx/yaml_generalsettings.cpp index fe1e96aea..d824414f9 100644 --- a/companion/src/firmwares/edgetx/yaml_generalsettings.cpp +++ b/companion/src/firmwares/edgetx/yaml_generalsettings.cpp @@ -88,7 +88,7 @@ Node convert::encode(const GeneralSettings& rhs) node["calib"] = calib; node["currModel"] = rhs.currModelIndex; - node["currModelFilename"] = rhs.currModelFilename; + node["currModelFilename"] = patchFilenameToYaml(rhs.currModelFilename); node["contrast"] = rhs.contrast; node["vBatWarn"] = rhs.vBatWarn; node["txVoltageCalibration"] = rhs.txVoltageCalibration; diff --git a/companion/src/storage/categorized.cpp b/companion/src/storage/categorized.cpp index 58e7c7ba0..e6aef817a 100644 --- a/companion/src/storage/categorized.cpp +++ b/companion/src/storage/categorized.cpp @@ -316,9 +316,11 @@ bool CategorizedStorageFormat::writeYaml(const RadioData & radioData) QString modelFilename; if (hasCategories) { - modelFilename = QString("MODELS/%1").arg(model.filename); - modelFiles.push_back({std::string(model.filename), - std::string(model.name), model.category}); + std::string ymlFilename = patchFilenameToYaml(model.filename); + modelFilename = + QString("MODELS/%1").arg(QString::fromStdString(ymlFilename)); + modelFiles.push_back( + {ymlFilename, std::string(model.name), model.category}); } else { modelFilename = QString("MODELS/model%1.yml") .arg(model.modelIndex, 2, 10, QLatin1Char('0'));