diff options
| author | wuesk <wsk@tuta.com> | 2026-09-22 13:30:39 +0200 |
|---|---|---|
| committer | wuesk <wsk@tuta.com> | 2026-09-22 13:30:39 +0200 |
| commit | 72fafe4553e075c040734e31164e50c4cdc7f319 (patch) | |
| tree | 4a58557786185e993c11a5c4c27894333ab84842 /src/guestbook.cpp | |
Diffstat (limited to 'src/guestbook.cpp')
| -rw-r--r-- | src/guestbook.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/guestbook.cpp b/src/guestbook.cpp new file mode 100644 index 0000000..0b58ddf --- /dev/null +++ b/src/guestbook.cpp @@ -0,0 +1,78 @@ +#include "guestbook.hpp" +#include "json.hpp" + +#include <filesystem> +#include <fstream> +#include <iostream> + +using json = nlohmann::json; + +namespace { + const std::filesystem::path guestbookFilePath = "data/guestbook.json"; +} + +std::vector<GuestbookEntry> loadGuestbook() +{ + std::ifstream file(guestbookFilePath); + + if (!file.is_open()) + { + return {}; + } + + if (file.peek() == std::ifstream::traits_type::eof()) + { + return {}; + } + + // An existing but empty file is not valid JSON. Treat it like a new + // guestbook so a partially initialized data directory cannot crash the + // server during startup. + json data = json::parse(file, nullptr, false); + + if (data.is_discarded() || !data.is_array()) + { + std::cerr << "Warning: ignoring invalid guestbook data in " + << guestbookFilePath << '\n'; + return {}; + } + + std::vector<GuestbookEntry> entries; + + for (const auto& item : data) + { + GuestbookEntry entry; + entry.name = item.at("name").get<std::string>(); + entry.message = item.at("message").get<std::string>(); + + entries.push_back(entry); + } + + return entries; +} + +bool saveGuestbook(const std::vector<GuestbookEntry>& entries) +{ + std::filesystem::create_directories(guestbookFilePath.parent_path()); + + json data = json::array(); + + for (const GuestbookEntry& entry : entries) + { + data.push_back({ + {"name", entry.name}, + {"message", entry.message} + }); + } + + std::ofstream file(guestbookFilePath); + + if (!file.is_open()) + { + return false; + } + + file << data.dump(4); + + return true; +} |
