Debian Codegraph @

DebugAdapterClient/clDapSettingsStore.cpp

raw
1 #include "clDapSettingsStore.hpp"
2
3 #include "fileutils.h"
4
5 JSONItem DapEntry::To() const
6 {
7 auto json = JSONItem::createObject();
8 json.addProperty("name", m_name);
9 json.addProperty("command", m_command);
10 json.addProperty("connection_string", m_connection_string);
11 json.addProperty("environment", m_environment);
12 json.addProperty("flags", m_flags);
13 json.addProperty("env_format", (int)m_envFormat);
14 json.addProperty("launch_type", (int)m_launch_type);
15 return json;
16 }
17
18 void DapEntry::From(const JSONItem& json)
19 {
20 m_name = json["name"].toString();
21 m_command = json["command"].toString();
22 m_connection_string = json["connection_string"].toString();
23 m_environment = json["environment"].toString();
24 m_flags = json["flags"].toSize_t(m_flags);
25 m_envFormat = (dap::EnvFormat)json["env_format"].toInt((int)dap::EnvFormat::LIST);
26 m_launch_type = (DapLaunchType)json["launch_type"].toInt((int)m_launch_type);
27 }
28
29 //////////////////////////////////////////////////////////////
30 //////////////////////////////////////////////////////////////
31
32 clDapSettingsStore::clDapSettingsStore() {}
33
34 clDapSettingsStore::~clDapSettingsStore() {}
35
36 void clDapSettingsStore::Clear() { m_entries.clear(); }
37
38 void clDapSettingsStore::Load(const wxFileName& file)
39 {
40 Clear();
41
42 // ensure that we have something to load
43 if(!file.FileExists()) {
44 wxFileName::Mkdir(file.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
45 FileUtils::WriteFileContent(file, "[]");
46 }
47
48 JSON root(file);
49 if(!root.isOk()) {
50 return;
51 }
52
53 JSONItem arr = root.toElement();
54 int count = arr.arraySize();
55 for(int i = 0; i < count; ++i) {
56 DapEntry d;
57 d.From(arr[i]);
58 m_entries.insert({ d.GetName(), d });
59 }
60 }
61
62 void clDapSettingsStore::Save(const wxFileName& path)
63 {
64 if(!path.IsOk()) {
65 return;
66 }
67 JSON root(cJSON_Array);
68 for(const auto& vt : m_entries) {
69 root.toElement().arrayAppend(vt.second.To());
70 }
71
72 root.save(path);
73 }
74
75 bool clDapSettingsStore::Get(const wxString& name, DapEntry* entry) const
76 {
77 if(!entry || m_entries.count(name) == 0) {
78 return false;
79 }
80
81 *entry = m_entries.find(name)->second;
82 return true;
83 }
84
85 bool clDapSettingsStore::Contains(const wxString& name) const
86 {
87 DapEntry entry;
88 return Get(name, &entry);
89 }
90
91 bool clDapSettingsStore::Set(const DapEntry& entry)
92 {
93 m_entries.erase(entry.GetName());
94 return m_entries.insert({ entry.GetName(), entry }).second;
95 }
96
97 bool clDapSettingsStore::Delete(const wxString& name)
98 {
99 if(m_entries.count(name) == 0) {
100 return false;
101 }
102 m_entries.erase(name);
103 return true;
104 }
105
106 void clDapSettingsStore::Set(const std::vector<DapEntry>& entries)
107 {
108 m_entries.clear();
109 Update(entries);
110 }
111
112 void clDapSettingsStore::Update(const std::vector<DapEntry>& entries)
113 {
114 for(const auto& d : entries) {
115 m_entries.erase(d.GetName());
116 m_entries.insert({ d.GetName(), d });
117 }
118 }