// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #include "io/PresetIo.hpp" #include #include #include #include #include "io/KotlinText.hpp" #include "sizzle/Specs.hpp" namespace sizzle::io { using namespace model; std::string PresetIo::exportText(const ToolboxSlot &slot) { if (slot.isEmpty()) return std::string(HEADER) + "\nTYPE=EMPTY"; const DeviceSpec *spec = findDevice(slot.type.c_str()); std::string o; auto line = [&](const std::string &s) { o += s; o += '\n'; }; line(HEADER); line("TYPE=" + slot.type); line("NAME=" + slot.name); // Params in the type's declared order, then any extra keys the slot holds. const int n = spec ? spec->paramCount : 0; for (int i = 0; i < n; ++i) { const ParamSpec &p = spec->params[i]; line(std::string(p.key) + "=" + slot.s(p.key, kotlinFloatToString(p.def).c_str())); } for (int i = 0; i < slot.values.size(); ++i) { const char *key = slot.values.keyAt(i); bool declared = false; for (int k = 0; k < n && !declared; ++k) declared = std::strcmp(spec->params[k].key, key) == 0; if (!declared) line(std::string(key) + "=" + slot.values.valueAt(i)); } return o; } bool PresetIo::importText(ToolboxSlot &slot, const std::string &text) { std::vector> map; // a LinkedHashMap std::unordered_map index; std::string type; bool hasType = false; for (const std::string &raw : kt::lines(text)) { const std::string line = kt::trim(raw); if (line.empty() || line == HEADER) continue; const size_t eq = line.find('='); if (eq == std::string::npos) continue; std::string key = line.substr(0, eq), value = line.substr(eq + 1); if (key == "TYPE") { hasType = value.find('\0') == std::string::npos && findDevice(value.c_str()) != nullptr; type = hasType ? value : std::string(); } else if (key == "NAME") { slot.name = value; } else { auto it = index.find(key); if (it != index.end()) map[it->second].second = std::move(value); else { index.emplace(key, map.size()); map.emplace_back(std::move(key), std::move(value)); } } } if (!hasType) return false; slot.fill(type); // reset to defaults for the type (and its display name) ... if (kt::trim(slot.name).empty()) slot.name = findDevice(type.c_str())->displayName; for (const auto &kv : map) slot.set(kv.first.c_str(), kv.second.c_str()); // ... then the saved values return true; } } // namespace sizzle::io