This commit is contained in:
2026-05-20 17:57:50 +03:00
parent ed0fefbc75
commit 85d2bbd615

View File

@@ -132,7 +132,51 @@ public:
fst.close(); 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<std::string_view> 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) { if (!ec) {
// remove possible spaces in filenames // remove possible spaces in filenames
@@ -206,6 +250,7 @@ public:
ec = std::make_error_code(std::errc::io_error); ec = std::make_error_code(std::errc::io_error);
} else { } else {
try { try {
fst << generateHeader();
fst << buff; fst << buff;
} catch (std::ios_base::failure const& ex) { } catch (std::ios_base::failure const& ex) {
ec = ex.code(); ec = ex.code();
@@ -229,6 +274,30 @@ public:
private: private:
std::filesystem::path _lastConfigPath{}; 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::duration<int64_t, std::ratio<1, 10>>>(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 } // namespace asibfm700