diff --git a/asibfm700_config.h b/asibfm700_config.h index 36f11d5..b024d05 100644 --- a/asibfm700_config.h +++ b/asibfm700_config.h @@ -132,7 +132,51 @@ public: fst.close(); - ec = base_t::fromCharRange(buffer); + // remove possible header (see "save" method) + size_t nskip = 5; + auto lines = std::views::split(buffer, DEFAULT_RECORD_DELIMITER); + if (std::ranges::distance(lines.begin(), lines.end()) >= 5) { + bool head_exists = true; + std::vector tl; + for (auto const& l : lines) { // lines must start with COMMENT_SEQ + tl.emplace_back(mcc::utils::trimSpaces(l)); + if (tl.back().size() >= COMMENT_SEQ.size()) { + auto found = std::ranges::search(tl.back(), COMMENT_SEQ); + if (tl.back().begin() != found.begin()) { + head_exists = false; + break; + } + } else { + head_exists = false; + break; + } + } + if (head_exists) { + // the first and last lines must contain only a comment sequence and possible spaces + if (tl[0].size() != COMMENT_SEQ.size() || tl[3].size() != COMMENT_SEQ.size()) { + head_exists = false; + } else { + if (tl[1] != "# ASTROSIB FM-700 MOUNT CONFIGURATION") { + head_exists = false; + } else { + std::regex rx{ + "# \\(created at " + "20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\.[0-9] " + "UTC\\)"}; + + if (!std::regex_match(std::string{tl[2]}, rx)) { + head_exists = false; + } + } + } + } + + if (!head_exists) { // delete the first N lines + nskip = 0; + } + } + + ec = base_t::fromCharRange(buffer, nskip); if (!ec) { // remove possible spaces in filenames @@ -206,6 +250,7 @@ public: ec = std::make_error_code(std::errc::io_error); } else { try { + fst << generateHeader(); fst << buff; } catch (std::ios_base::failure const& ex) { ec = ex.code(); @@ -229,6 +274,30 @@ public: private: std::filesystem::path _lastConfigPath{}; + + static std::string generateHeader() + { + std::string s{ + "#\n" + "# ASTROSIB FM-700 MOUNT CONFIGURATION\n" + "#\n"}; + + std::format_to( + std::back_inserter(s), "# (created at {:%FT%T} UTC)\n#\n", + std::chrono::round>>(std::chrono::system_clock::now())); + + return s; + } + + // regex for buffer started with standard header + inline static const std::regex headerRx{ + "^[ \\n]*" + "# *\\n" + "# *ASTROSIB FM-700 MOUNT CONFIGURATION *\\n" + "# *\\n" + "# *\\(created at 20[0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]\\.[0-9] UTC\\) *\\n" + "# *\\n" + "[\\s\\S]*"}; }; } // namespace asibfm700 \ No newline at end of file