-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathserialize.cpp
79 lines (64 loc) · 1.83 KB
/
serialize.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "config.h"
#include "serialize.hpp"
#include <cereal/archives/json.hpp>
#include <cereal/types/set.hpp>
#include <cereal/types/string.hpp>
#include <phosphor-logging/lg2.hpp>
#include <filesystem>
#include <fstream>
// Register class version with Cereal
CEREAL_CLASS_VERSION(phosphor::led::Serialize, CLASS_VERSION)
namespace phosphor
{
namespace led
{
namespace fs = std::filesystem;
bool Serialize::getGroupSavedState(const std::string& objPath) const
{
return savedGroups.contains(objPath);
}
void Serialize::storeGroups(const std::string& group, bool asserted)
{
// If the name of asserted group does not exist in the archive and the
// Asserted property is true, it is inserted into archive.
// If the name of asserted group exist in the archive and the Asserted
// property is false, entry is removed from the archive.
auto iter = savedGroups.find(group);
if (iter != savedGroups.end() && asserted == false)
{
savedGroups.erase(iter);
}
if (iter == savedGroups.end() && asserted)
{
savedGroups.emplace(group);
}
auto dir = path.parent_path();
if (!fs::exists(dir))
{
fs::create_directories(dir);
}
std::ofstream os(path.c_str(), std::ios::binary);
cereal::JSONOutputArchive oarchive(os);
oarchive(savedGroups);
}
void Serialize::restoreGroups()
{
if (!fs::exists(path))
{
lg2::info("File does not exist, FILE_PATH = {PATH}", "PATH", path);
return;
}
try
{
std::ifstream is(path.c_str(), std::ios::in | std::ios::binary);
cereal::JSONInputArchive iarchive(is);
iarchive(savedGroups);
}
catch (const cereal::Exception& e)
{
lg2::error("Failed to restore groups, ERROR = {ERROR}", "ERROR", e);
fs::remove(path);
}
}
} // namespace led
} // namespace phosphor