// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #include "sizzle/Params.hpp" #include #include #include namespace sizzle { void Params::fill(const DeviceSpec &spec) { n_ = 0; for (int i = 0; i < spec.paramCount; ++i) { const ParamSpec &p = spec.params[i]; if (p.isEnum()) { int idx = (int)p.def; int n = p.choiceCount(); if (idx < 0) idx = 0; if (idx > n - 1) idx = n - 1; set(p.key, p.choices[idx]); } else { set(p.key, p.def); } } } int Params::find(const char *key) const { for (int i = 0; i < n_; ++i) if (std::strcmp(keys_[i], key) == 0) return i; return -1; } void Params::set(const char *key, const char *value) { int i = find(key); if (i < 0) { if (n_ >= kMax) return; i = n_++; std::snprintf(keys_[i], kKeyLen, "%s", key); } std::snprintf(vals_[i], kValLen, "%s", value); } void Params::remove(const char *key) { const int i = find(key); if (i < 0) return; for (int k = i; k + 1 < n_; ++k) { // keep the insertion order of the rest std::memcpy(keys_[k], keys_[k + 1], kKeyLen); std::memcpy(vals_[k], vals_[k + 1], kValLen); } --n_; keys_[n_][0] = vals_[n_][0] = '\0'; } void Params::set(const char *key, float value) { // Shortest text that reads back as the same float (Kotlin stores toString()). char buf[kValLen]; std::snprintf(buf, sizeof buf, "%.9g", (double)value); set(key, buf); } float Params::f(const char *key, float fallback) const { int i = find(key); if (i < 0) return fallback; const char *s = vals_[i]; while (std::isspace((unsigned char)*s)) ++s; if (!*s) return fallback; char *end = nullptr; float v = std::strtof(s, &end); while (end && std::isspace((unsigned char)*end)) ++end; return (end && *end == '\0') ? v : fallback; } const char *Params::s(const char *key, const char *fallback) const { int i = find(key); return i < 0 ? fallback : vals_[i]; } } // namespace sizzle