-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuson.cpp
352 lines (324 loc) · 6.21 KB
/
uson.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "uson.h"
int USON::FIND_DATA(std::string obj,std::string src)
{
bool sign=true;
bool *sub=new bool[obj.length()];
for(int i=0;i<obj.length();i++)
sub[i]=false;
int end=(int)((int)(src.length()/obj.length())*((int)obj.length()));
if(end==0)
{
delete[] sub;
return -1;
}
for(int i=0;i<src.length();i++)
{
if(i>=end)
{
delete[] sub;
return -2;
}
for(int j=0;j<obj.length();j++)
{
if(src[i+j]==obj[j])
sub[j]=true;
}
for(int j=0;j<obj.length();j++)
{
sign=(sign && sub[j]);
}
if(sign==true)
{
delete[] sub;
return i+1;
}
else
sign=true;
}
delete[] sub;
return -2;
}
USON::USON_D USON::Decode(std::string item)
{
USON_D ret;
std::string tmp;
int pos_s=FIND_DATA("[-",item)+1;
int pos_e=FIND_DATA("-]",item)-1;
tmp=item.substr(pos_s,pos_e-pos_s);
item=item.substr(FIND_DATA("=",item),item.length());
ret.data_name=tmp;
pos_s=FIND_DATA("[-",item)+1;
pos_e=FIND_DATA("-]",item)-1;
tmp=item.substr(pos_s,pos_e-pos_s);
if(FIND_DATA("-]",tmp)>=0)
tmp=tmp.substr(0,tmp.length()-2);
ret.data_value=tmp;
return ret;
}
USON::~USON()
{
delete[] rom;
return;
}
int USON::ReadFile(std::string file)
{
std::string tmp;
std::fstream fh(file.c_str(),std::ios::in);
if(!fh.is_open())
return -1;//can not open file/file does not exist
getline(fh,tmp);
if(tmp=="[DOCTYPE USON]")
{
getline(fh,tmp);
ram=Decode(tmp);
if(ram.data_name=="DATA_COUNT")
{
data_count=std::stoi(ram.data_value);
rom=new USON_D[std::stoi(ram.data_value)];
}
else
return -2;
for(int i=0;i<std::stoi(ram.data_value);i++)
{
getline(fh,tmp);
if(tmp=="-END-")
return -3;//unexpected end of file
rom[i]=Decode(tmp);
tmp="";
}
getline(fh,tmp);
if(tmp=="-END-")
{
fh.close();
return 0;//success
}
else
return -3;
}
else
return -2;//file format error
}
std::string USON::GetData(std::string name)
{
for(int i=0;i<data_count;i++)
{
if(rom[i].data_name==name)
return rom[i].data_value;
}
return "NO_MATCH_DATA_FOUND";
}
bool USON::ChangeValue(std::string name,std::string new_value)
{
for(int i=0;i<data_count;i++)
{
if(rom[i].data_name==name)
{
rom[i].data_value=new_value;
return true;
}
}
return true;
}
bool USON::ChangeName(std::string oldname,std::string newname)
{
for(int i=0;i<data_count;i++)
{
if(rom[i].data_name==oldname)
{
rom[i].data_name=newname;
return true;
}
}
return false;
}
int USON::WriteToFile(std::string file)
{
std::fstream fw(file.c_str(),std::ios::out);
if(!fw.is_open())
return -1;//can not create file
fw<<"[DOCTYPE USON]"<<std::endl;
fw<<"[-DATA_COUNT-] = [-"<<data_count<<"-]"<<std::endl;
for(int i=0;i<data_count;i++)
{
fw<<"[-"<<rom[i].data_name<<"-] = [-"<<rom[i].data_value<<"-]"<<std::endl;
}
fw<<"-END-"<<std::endl;
fw.close();
return 0;
}
std::string USON::operator[](std::string name)
{
std::string empty="";
for(int i=0;i<data_count;i++)
{
if(rom[i].data_name==name)
return rom[i].data_value;
}
return empty;
}
USON_LIST::~USON_LIST()
{
delete[] rom.data;
return;
}
std::string USON_LIST::add(char a,char b)
{
char str[3];
str[0]=a;
str[1]=b;
str[2]='\0';
return std::string(str);
}
std::string USON_LIST::add(std::string a,char b)
{
char twstr[2];
twstr[0]=b;
twstr[1]='\0';
std::string ret=a+std::string(twstr);
return ret;
}
int USON_LIST::DecodeLists(int cnt,std::string src)
{
rom.count=cnt;
rom.data=new std::string[cnt];
std::string tmp="";
for(int i=0;i<src.length();i++)
{
if(src[i]==',')
{
rom.data[rom.count-cnt]=tmp;
tmp="";
cnt--;
}
else
{
tmp=add(tmp,src[i]);
}
}
rom.data[rom.count-1]=tmp;
return 0;
}
std::string USON_LIST::GetItem(int i)
{
if(i<rom.count)
return rom.data[i];
else
return "";
}
std::string USON_LIST::operator[](int i)
{
std::string empty="";
if(i<rom.count)
return rom.data[i];
else
return empty;
}
DOMAIN::~DOMAIN()
{
for(int i=0;i<domain_count;i++)
{
delete[] rom[i].sub_name;
delete[] rom[i].dns_type;
}
delete[] rom;
return;
}
DOMAIN::DNS_TYPE DOMAIN::stod(std::string src)
{
DNS_TYPE ret;
if(src=="A")
ret=A;
else if(src=="AAAA")
ret=AAAA;
else
ret=_NULL;
return ret;
}
std::string DOMAIN::dtos(DOMAIN::DNS_TYPE src)
{
std::string ret;
if(src==A)
ret="A";
else if(src==AAAA)
ret="AAAA";
else
ret="";
return ret;
}
std::string DOMAIN::CreateQueryString(std::string s,int n)
{
std::string tmp="DOMAIN_";
tmp=tmp+std::to_string(n);
if(s!="")
tmp=tmp+"_";
tmp=tmp+s;
return tmp;
}
int DOMAIN::LoadFile(std::string file)
{
int rf=ReadFile(file);
if(rf!=0)
return rf;
domain_count=std::stoi(GetData("DOMAIN_COUNT"));
rom=new INFO[domain_count];
for(int i=0;i<domain_count;i++)
{
rom[i].domain=GetData(CreateQueryString("",i+1));
rom[i].sub_count=std::stoi(GetData(CreateQueryString("SUB_COUNT",i+1)));
rom[i].sub_name=new std::string[rom[i].sub_count];
rom[i].dns_type=new DNS_TYPE[rom[i].sub_count];
DecodeLists(rom[i].sub_count,GetData(CreateQueryString("SUB_NAME_LIST",i+1)));
for(int j=0;j<rom[i].sub_count;j++)
{
rom[i].sub_name[j]=GetItem(j);
}
DecodeLists(rom[i].sub_count,GetData(CreateQueryString("SUB_NAME_TYPE_LIST",i+1)));
for(int j=0;j<rom[i].sub_count;j++)
{
rom[i].dns_type[j]=stod(GetItem(j));
}
}
return 0;
}
DOMAIN::INFO DOMAIN::GetDomainInfo(std::string domain)
{
INFO empty;
for(int i=0;i<domain_count;i++)
if(rom[i].domain==domain)
return rom[i];
return empty;
}
std::string DOMAIN::GetDNSType(std::string domain,std::string sub_name)
{
for(int i=0;i<domain_count;i++)
if(rom[i].domain==domain)
for(int j=0;j<rom[i].sub_count;j++)
if(rom[i].sub_name[j]==sub_name)
return dtos(rom[i].dns_type[j]);
return "ERROR_NO_DOMAIN_MATCHED";
}
std::string DOMAIN::GetSubName(std::string domain,int index)
{
for(int i=0;i<domain_count;i++)
if(rom[i].domain==domain)
return rom[i].sub_name[index];
return "ERROR_NO_DOMAIN_MATCHED";
}
std::string DOMAIN:: GetDomain(int index)
{
if(index<domain_count)
return rom[index].domain;
else
return "ERROR_OUT_OF_RANGE";
}
int DOMAIN::GetSubCount(std::string domain)
{
for(int i=0;i<domain_count;i++)
if(rom[i].domain==domain)
return rom[i].sub_count;
return 0;
}
int DOMAIN::GetDomainCount()
{
return domain_count;
}