yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
activity_file.cc
Go to the documentation of this file.
1#include "app/activity_file.h"
2
3#include <cstdio>
4#include <filesystem>
5#include <fstream>
6
7#include "absl/strings/str_format.h"
8
9#ifdef YAZE_WITH_JSON
10#include <nlohmann/json.hpp>
11#endif
12
13namespace yaze::app {
14
15namespace fs = std::filesystem;
16
17ActivityFile::ActivityFile(const std::string& path) : path_(path) {
18 // Touch the file to create it
19 std::ofstream file(path_);
20 if (!file) {
21 // Failed to create file - log warning but don't throw
22 // Discovery will simply not work for this instance
23 }
24}
25
27 if (!path_.empty()) {
28 std::remove(path_.c_str());
29 }
30}
31
33 : path_(std::move(other.path_)) {
34 other.path_.clear();
35}
36
38 if (this != &other) {
39 // Clean up existing file
40 if (!path_.empty()) {
41 std::remove(path_.c_str());
42 }
43 path_ = std::move(other.path_);
44 other.path_.clear();
45 }
46 return *this;
47}
48
50 if (path_.empty()) return;
51
52#ifdef YAZE_WITH_JSON
53 nlohmann::json j;
54 j["pid"] = status.pid;
55 j["version"] = status.version;
56 j["socket_path"] = status.socket_path;
57 j["active_rom"] = status.active_rom;
58 j["start_timestamp"] = status.start_timestamp;
59
60 std::ofstream file(path_);
61 if (file) {
62 file << j.dump(2); // Pretty print with 2-space indent
63 }
64#else
65 // Fallback: Simple JSON string formatting without nlohmann
66 std::ofstream file(path_);
67 if (file) {
68 file << absl::StrFormat(
69 "{\n"
70 " \"pid\": %d,\n"
71 " \"version\": \"%s\",\n"
72 " \"socket_path\": \"%s\",\n"
73 " \"active_rom\": \"%s\",\n"
74 " \"start_timestamp\": %d\n"
75 "}\n",
76 status.pid, status.version, status.socket_path, status.active_rom,
77 status.start_timestamp);
78 }
79#endif
80}
81
83 return !path_.empty() && fs::exists(path_);
84}
85
86} // namespace yaze::app
Manages a JSON status file for instance discovery.
ActivityFile & operator=(const ActivityFile &)=delete
bool Exists() const
Check if the status file exists on disk.
ActivityFile(const std::string &path)
Construct an activity file at the specified path.
void Update(const ActivityStatus &status)
Update the status file with new information.
~ActivityFile()
Destructor removes the status file.
Status information for an active YAZE instance.