-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonState.cc
More file actions
201 lines (171 loc) · 4.1 KB
/
JsonState.cc
File metadata and controls
201 lines (171 loc) · 4.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
///////////////////////////////////////////////////////////
//
// SuperDiskIndex
//
////////////////////
//
//
//
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "Global.h"
#include "Helpers.h"
#include "Types.h"
#include "JsonState.h"
#include <string>
#include <vector>
#include <map>
#include <memory>
#include <deque>
#include <stdarg.h>
///////////////////////////////////////////////////////////
typedef std::map<std::string, std::string> kvmap_t;
//typedef std::deque<std::string> json_queue_t;
///////////////////////////////////////////////////////////
std::string indent(int lvl)
{
char const *sym=" ";
std::string tmp;
for (int i=0; i<lvl; i++) tmp+=sym;
return tmp;
}
///////////////////////////////////////////////////////////
class SCtx
{
public:
SCtx() {}
~SCtx() {}
void CreateFromString(std::string const &s)
{
col.clear();
size_t pos = 0;
size_t len = 0;
while (len<s.size())
{
len = s.find('.', pos)-pos;
//printf("%d:%d(%s)\n", pos, len, s.substr(pos, len).c_str());
col.push_back(s.substr(pos, len));
pos+=len+1;
}
}
std::string Get(u32 level)
{
if (level>=col.size()) return "";
return col[level];
}
bool Compare(SCtx const &other)
{
if (col.size()!=other.col.size()) return false;
for (u32 i=0; i<col.size(); i++) if (col[i].compare(other.col[i])!=0) return false;
return true;
}
operator std::string()
{
std::string tmp;
for (u32 i=0; i<col.size(); i++) { tmp+="/"; tmp+=col[i]; }
return tmp;
}
SCtx GetPath()
{
SCtx gen;
if (col.size()==0) return gen;
for (u32 i=0; i<col.size()-1; i++)
{
gen.col.push_back(col[i]);
}
return gen;
}
std::string GetElement()
{
if (col.size()==0) return "";
return col.back();
}
int Size() { return col.size(); }
void Pop() { if (!col.empty()) col.pop_back(); }
void Push(std::string s) { col.push_back(s); }
protected:
std::deque<std::string> col;
};
///////////////////////////////////////////////////////////
struct JSStorage
{
kvmap_t kvmap;
};
///////////////////////////////////////////////////////////
JsonState::JsonState()
{
Storage = new JSStorage();
}
JsonState::~JsonState()
{
delete Storage;
}
void JsonState::Test()
{
}
void JsonState::Set(char const *key, char const *valfmt, ...)
{
va_list args;
va_start(args, valfmt);
size_t size = vsnprintf( nullptr, 0, valfmt, args) + 1; // Extra space for '\0'
if( size <= 0 ) { clog(0, "ERR setting json dict value for '%s'.\n", key); return; }
std::unique_ptr<char[]> buf( new char[ size ] );
va_start(args, valfmt);
vsnprintf( buf.get(), size, valfmt, args );
Storage->kvmap[key] = std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}
char const *JsonState::Get(char const *key)
{
return Storage->kvmap[key].c_str();
}
void JsonState::Dump()
{
for (kvmap_t::const_iterator it = Storage->kvmap.begin(); it!=Storage->kvmap.end(); it++)
{
printf("# JSON | '%s' -> '%s'.\n", it->first.c_str(), it->second.c_str());
}
}
void JsonState::WriteToFile(char const *fn)
{
int fd = open(fn, O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE);
if (fd>=0)
{
int il=0;
SCtx curctx,ectx;
for (kvmap_t::const_iterator it = Storage->kvmap.begin(); it!=Storage->kvmap.end(); it++)
{
ectx.CreateFromString(it->first);
//printf("%s || %s || %s\n", ((std::string)ectx).c_str(), ((std::string)ectx.GetPath()).c_str(), ((std::string)ectx.GetElement()).c_str());
if (!ectx.GetPath().Compare(curctx))
{
for (int i=0; i<curctx.Size(); i++)
{
if ((ectx.Get(i).compare(curctx.Get(i))!=0) && (curctx.Size()>=(ectx.Size()-1)))
{
il--;
dprintf(fd, "%s}\n", indent(il).c_str());
curctx.Pop();
}
}
for (int i=0; i<ectx.Size()-1; i++)
{
if (ectx.Get(i).compare(curctx.Get(i))!=0)
{
dprintf(fd, "%s\"%s\": {\n", indent(il).c_str(), ectx.Get(i).c_str());
il++;
}
}
curctx=ectx.GetPath();
}
dprintf(fd, "%s\"%s\": \"%s\",\n", indent(il).c_str(), ectx.GetElement().c_str(), it->second.c_str());
}
while (curctx.Size()>0)
{
il--;
dprintf(fd, "%s}\n", indent(il).c_str());
curctx.Pop();
}
}
close(fd);
}