-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeytable.cpp
471 lines (407 loc) · 10.8 KB
/
keytable.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "keytable.h"
#include "valuelist.h"
#include "parameter.h"
#include <ostream>
#include <sstream>
using namespace pun;
const char* KeyTable::PHP_NAME = "Pun\\KeyTable";
const std::string CPunk::keytable_classname(KeyTable::PHP_NAME);
KeyTable* KeyTable::get_KeyTable(Php::Value& v)
{
if (v.instanceOf(CPunk::keytable_classname))
{
return (KeyTable*) v.implementation();
}
return nullptr;
}
void KeyTable::setup_ext(Php::Extension& ext /* , Php::Interface& if1 , Php::Interface& if2*/) {
Php::Class<KeyTable> keytab(KeyTable::PHP_NAME);
/* keytab.implements(if1);*/
/* keytab.implements(if2);*/
//keytab.extends(tbase);
keytab.method<&KeyTable::setKV> ("set");
keytab.method<&KeyTable::get> ("get");
keytab.method<&KeyTable::path> ("path");
keytab.method<&KeyTable::unsetK> ("unsetKey");
keytab.method<&KeyTable::hasK> ("exists");
keytab.method<&KeyTable::clear> ("clear");
keytab.method<&KeyTable::merge> ("merge", {
Php::ByVal("store" )
});
//keytab.method<&KeyTable::merge> ("merge");
keytab.method<&KeyTable::size> ("size");
keytab.method<&KeyTable::__construct> ("__construct");
keytab.method<&KeyTable::toArray> ("toArray");
keytab.method<&KeyTable::setTag> ("setTag",
{ Php::ByVal("tag") }
);
keytab.method<&KeyTable::getTag> ("getTag");
keytab.method<&KeyTable::replaceVars> ("replaceVars");
ext.add(std::move(keytab));
}
void KeyTable::__construct(Php::Parameters& param) {
if (param.size() > 0) {
Php::Value& v = param[0];
if (v.isArray()) {
fn_merge(v);
}
else if (v.isObject())
{
auto obj = KeyTable::get_KeyTable(v);
fn_merge(obj);
}
}
}
long KeyTable::count()
{
return _store.size();
}
bool KeyTable::offsetExists(const Php::Value & key)
{
return _store.find(key.stringValue()) != _store.end();
}
void KeyTable::offsetSet(const Php::Value &key, const Php::Value &value)
{
_store[key.stringValue()] = value;
}
Php::Value KeyTable::offsetGet(const Php::Value &key)
{
return _store[key.stringValue()];
}
void KeyTable::offsetUnset(const Php::Value &key)
{
_store.erase(key.stringValue());
}
/*
Php::Iterator *KeyTable::getIterator()
{
// construct a new map iterator on the heap
// the (PHP-CPP library will delete it when ready)
return new KT_Iterator(this);
}
*/
void KeyTable::fn_setKV(std::string& key, Php::Value& val)
{
_store[key] = val;
}
void KeyTable::setKV(Php::Parameters& params)
{
if (params.size() < 2) {
throw Php::Exception("KeyTable->setKV(key, value) missing a parameter ");
}
Php::Value& key = params[0];
_store[key.stringValue()] = params[1];
}
Php::Value KeyTable::fn_getV(std::string& key){
return _store[key];
}
/**
Each key except the last must find another KeyTable object
*/
Php::Value KeyTable::path(Php::Parameters& param)
{
bool check = param.size() >= 1;
if (check) {
Php::Value& v = param[0];
if (v.isString()) {
std::string target((const char*) v, v.size());
StringList keys = pun::explode(".", target);
auto ct = keys.size();
auto kit = keys.begin();
auto table = this;
if (ct == 0) {
return Php::Value();
}
while (ct > 1) {
auto fit = table->_store.find(*kit);
if (fit == table->_store.end())
return Php::Value();
table = castKeyTable( fit->second );
if (table == nullptr) {
return Php::Value();
}
ct--;
kit++;
}
// one key left, return whatever it is
auto fit = table->_store.find(*kit);
return (fit == table->_store.end() ? Php::Value() : fit->second);
}
}
throw Php::Exception("No path argument");
}
Php::Value KeyTable::get(Php::Parameters& param)
{
auto ct = param.size();
if (ct < 1) {
throw Php::Exception("KeyTable->get(key, alternate) missing key");
}
auto fit = _store.find(param[0].stringValue());
if (fit != _store.end())
return fit->second;
if (ct < 2) {
throw Php::Exception("KeyTable->get(key, alternate) missing alternate ");
}
return param[1];
}
// Remove value accessed by key
void KeyTable::unsetK(Php::Parameters& params)
{
if (params.size() < 1) {
throw Php::Exception("KeyTable->unsetV(key) missing parameter ");
}
_store.erase(params[0].stringValue());
}
// Return keys as Php Array
Php::Value KeyTable::getKeys()
{
Php::Value result;
int idx = 0;
for(auto ait = _store.begin(); ait != _store.end(); ait++)
{
result[ idx ] = ait->first;
idx++;
}
return result;
}
Php::Value KeyTable::hasK(Php::Parameters& params) {
pun::check_String(params,0);
return (_store.find(params[0].stringValue()) != _store.end());
}
Php::Value KeyTable::getTag() const
{
return _tag;
}
void KeyTable::setTag(Php::Parameters& param)
{
if ((param.size()< 1)) {
throw Php::Exception("setTag: Php Value expected");
}
_tag = param[0];
}
bool KeyTable::fn_hasK(std::string& key) const
{
return (_store.find(key) != _store.end());
}
/*
Php::Value KeyTable::count() const
{
return Php::Value(_store.size());
}
*/
// Return the _store as Array
Php::Value KeyTable::toArray()
{
Php::Array result;
for(auto ait = _store.begin(); ait != _store.end(); ait++)
{
Php::Value& val = ait->second;
if (val.isObject()) {
if (val.instanceOf(KeyTable::PHP_NAME)) {
KeyTable* kt = (KeyTable*) val.implementation();
result[ait->first] = kt->toArray();
continue;
}
else if (val.instanceOf(ValueList::PHP_NAME)) {
ValueList* vlist = (ValueList*) val.implementation();
result[ait->first] = vlist->toArray();
continue;
}
}
result[ ait->first ] = ait->second;
}
return result;
}
Php::Value KeyTable::__toString() {
std::stringstream ss;
ss << "KeyTable [ tag " << _tag << " size " << _store.size() << "]";
return ss.str();
}
Php::Value
KeyTable::fn_object()
{
return Php::Object(PHP_NAME, this);
}
void KeyTable::fn_unserialize(std::istream& ins)
{
size_t keyct;
char check;
std::string key;
ins.read( (char*) &keyct, sizeof(keyct));
//Php::out << "fn_unserialize keyct is " << keyct << std::endl;
ins >> check;
//Php::out << "fn_unserialize check " << check << std::endl;
for(size_t i = 0; i < keyct; i++) {
pun::unserialize_str(key, ins);
//Php::out << "fn_unserialize key " << key << std::endl;
Php::Value val;
pun::unserialize(val, ins);
_store[key] = val;
}
pun::unserialize(_tag, ins);
ins >> check;
//Php::out << "fn_unserialize check2 " << check << std::endl;
}
void KeyTable::fn_serialize(std::ostream& out)
{
auto keyct = _store.size();
out.write((const char*) &keyct,sizeof(keyct));
//Php::out << "KT fn_serialize " << std::endl;
out << '{';
for(auto ait = _store.begin(); ait != _store.end(); ait++)
{
const std::string& key = ait->first;
//Php::out << "fn_serialize key" << key << std::endl;
pun::serialize_str(key.data(), key.size(), out);
pun::serialize(ait->second, out);
}
pun::serialize(_tag, out);
out << '}';
}
std::string
KeyTable::serialize()
{
std::stringstream out;
//Php::out << "KT serialize " << std::endl;
out << 'K';
fn_serialize(out);
return out.str();
}
void KeyTable::unserialize(const char *input, size_t size)
{
std::string buffer(input,size);
//Php::out << "KT unserialize " << std::endl;
std::istringstream ins(buffer);
char check;
ins >> check;
//Php::out << "Check " << check << std::endl;
fn_unserialize(ins);
}
Php::Value KeyTable::__get(const Php::Value &name) const
{
auto fit = _store.find(name.stringValue());
if (fit != _store.end()) {
return fit->second;
}
else
return Php::Value();
}
void KeyTable::__set(const Php::Value &name, const Php::Value &value)
{
_store[name.stringValue()] = value;
}
bool KeyTable::__isset(const Php::Value &name) const
{
return _store.find(name.stringValue()) != _store.end();
}
void KeyTable::__unset(const Php::Value &name)
{
_store.erase(name.stringValue());
}
void
KeyTable::throw_mergeFail(const std::string& key) {
throw Php::Exception("KeyTable merge fail : key clash on " + key);
}
// if merge fails at some point,
// throw exception, leaving merge with incomplete state.
void
KeyTable::fn_merge(KeyTable* other)
{
auto zit = other->end();
for(auto ait = other->begin(); ait != zit; ait++)
{
const std::string& key = ait->first;
auto hereNow = _store.find(key);
if (hereNow != _store.end()) {
// key clashes only allowed for matching tables
KeyTable* kt = pun::castKeyTable(hereNow->second);
if (kt) {
KeyTable* okt = pun::castKeyTable(ait->second);
if (okt) {
kt->fn_merge(okt);
continue;
}
}
throw_mergeFail(key);
}
else {
_store[key] = ait->second;
}
}
}
// only works for another KeyTable, and for KeyTable, ValueList members
// complicated by TOML that ValueList and KeyTable can't be merged together,
// if occupy same key.
Php::Value
KeyTable::merge(Php::Parameters& param) {
bool ok = true;
if (param.size() > 0) {
Php::Value& v = param[0];
if (v.isArray()) {
fn_merge(v);
}
else if (v.isObject()) {
auto obj = KeyTable::get_KeyTable(v);
if (obj != nullptr) {
fn_merge(obj);
}
else {
ok = false;
}
}
}
else {
ok = false;
}
if (!ok) {
throw Php::Exception("KeyTable merge needs array or KeyTable as argument");
}
return fn_object();
}
void KeyTable::fn_merge(const Php::Value& fromArray)
{
for( auto &iter : fromArray) {
const Php::Value& kval = iter.first;
std::string key = kval.stringValue();
if (key.size() > 0) {
// empty keys not allowed.
const Php::Value& val = iter.second;
if (val.isArray()) {
KeyTable* subTable = new KeyTable();
Php::Value table(Php::Object(KeyTable::PHP_NAME, subTable));
subTable->fn_merge(val);
_store[key] = table;
}
else {
_store[key] = val;
}
}
}
}
Php::Value
KeyTable::intf_merge(Php::Value& obj)
{
KeyTable* other = castKeyTable(obj);
fn_merge(other);
return fn_object();
}
void KeyTable::replaceVars(Php::Parameters& param)
{
bool check = (param.size() > 0);
KeyTable* searchKT = nullptr;
Php::Value searchArray;
if (check) {
Php::Value& v = param[0];
if (v.isArray()) {
searchArray = v;
}
else if (v.isObject()) {
searchKT = KeyTable::get_KeyTable(v);
check = (searchKT != nullptr);
}
}
if (!check) {
throw Php::Exception("replaceVars needs a string lookup array or object");
}
replaceVar_ValueMap(_store,param[0]);
}