-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter.cpp
505 lines (431 loc) · 11.5 KB
/
parameter.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#include "parameter.h"
#include "valuelist.h"
#include "pcre8.h"
#include "recap8.h"
#include "re8map.h"
#include "token8.h"
#include <sstream>
#include <ostream>
#include <cstdint>
#include "ucode8.h"
#include "keytable.h"
/*
Undefined = 0, // Variable is not set
Null = 1, // Null will allow any type
False = 2, // Boolean false
True = 3, // Boolean true
Numeric = 4, // Integer type
Float = 5, // Floating point type
String = 6, // A string obviously
Array = 7, // An array of things
Object = 8, // An object
Resource = 9, // A resource
Reference = 10, // Reference to another value (can be any type!)
Constant = 11, // A constant value
ConstantAST = 12, // I think an Abstract Syntax tree, not quite sure
*/
//! temporary stack storage
union SerUnion {
//! As real number
double dval;
//! As Integer
int64_t ival;
};
const std::string CPunk::datetime_classname = "DateTime";
using namespace pun;
void
pun::unserialize(Php::Value& val, std::istream& ins)
{
char check;
std::string sval;
SerUnion cval;
ins >> check;
switch(check) {
case 'B':
ins >> check;
if (check == '1') {
val = Php::Value(true);
}
else {
val = Php::Value(false);
}
break;
case 'S':
pun::unserialize_str(sval, ins);
val = std::move(sval);
break;
case 'F':
ins.read( ( char*) &cval.dval, sizeof(double));
val = cval.dval;
break;
case 'I':
ins.read( ( char*) &cval.ival, sizeof(int64_t));
val = cval.ival;
break;
case 'D':
case 'A':
case 'O':
pun::unserialize_str(sval, ins);
val = Php::call("unserialize", sval);
break;
case 'K': {
auto ktp = new pun::KeyTable();
ktp->fn_unserialize(ins);
val = ktp->fn_object();
}
break;
case 'V': {
auto vlp = new pun::ValueList();
vlp->fn_unserialize(ins);
val = vlp->fn_object();
}
break;
case 'N':
val = Php::Value();
break;
}
}
Pype
pun::getPype(Php::Value& val) {
auto t = pun::getPype(val.type());
if (t == pun::tObject) {
if (val.instanceOf(CPunk::keytable_classname)) {
return pun::tKeyTable;
}
else if (val.instanceOf(CPunk::valuelist_classname)) {
return pun::tValueList;
}
else if (val.instanceOf(CPunk::datetime_classname)) {
return pun::tDateTime;
}
}
return t;
}
pun::Pype pun::getPype(Php::Type t) {
switch(t) {
case Php::Type::String:
return pun::tString;
case Php::Type::Numeric:
return pun::tInteger;
case Php::Type::Float:
return pun::tFloat;
case Php::Type::Array:
return pun::tArray;
case Php::Type::Object:
return pun::tObject;
case Php::Type::False:
case Php::Type::True:
return pun::tBool;
case Php::Type::Null:
return pun::tNull;
case Php::Type::Resource:
return pun::tResource;
case Php::Type::Reference:
return pun::tReference;
case Php::Type::Undefined:
default:
return pun::tUndefined;
}
}
const char* pun::getPypeId(Pype t)
{
switch(t) {
case pun::tString:
return "string";
case pun::tInteger:
return "integer";
case pun::tFloat:
return "float";
case pun::tKeyTable:
return "table";
case pun::tValueList:
return "list";
case pun::tDateTime:
return "datetime";
case pun::tArray:
return "array";
case pun::tObject:
return "object";
case pun::tBool:
return "boolean";
case pun::tNull:
return "null";
case pun::tResource:
return "resource";
case pun::tReference:
return "reference";
case pun::tUndefined:
default:
return "undefined";
}
}
const char* pun::getTypeName(Php::Type ptype)
{
switch(ptype) {
case Php::Type::String:
return "string";
case Php::Type::Numeric:
return "integer";
case Php::Type::Float:
return "float";
case Php::Type::Array:
return "array";
case Php::Type::Object:
return "object";
case Php::Type::False:
case Php::Type::True:
return "boolean";
case Php::Type::Null:
return "null";
case Php::Type::Resource:
return "resource";
case Php::Type::Reference:
return "reference";
case Php::Type::Undefined:
default:
return "undefined";
}
}
KeyTable* pun::castKeyTable(Php::Value& val) {
if (!val.isObject())
return nullptr;
Php::Base* base = val.implementation();
return dynamic_cast<KeyTable*>(base);
}
std::string
pun::hexUniStr8(const svx::string_view& hexval)
{
char buf[24];
const char* data = hexval.data();
size_t slen = hexval.size();
for(size_t i = 0; i < slen; i++) {
buf[i] = *(data+i);
}
buf[slen] = '\0';
char32_t val = (char32_t) strtol(buf, 0, 16);
EncodeUTF8 ec8;
//Php::out << val << " is " << ec8.result << std::endl;
return std::string(ec8.result, ec8.encode(val));
}
void pun::hexUniStr8(
const svx::string_view& hexval,
std::ostream& os)
{
os << hexUniStr8(hexval);
}
void pun::need_Value(Php::Parameters& param, unsigned int offset)
{
if (param.size() < offset) {
throw Php::Exception(missingParameter("Value", offset));
}
}
std::string
pun::missingParameter(const char* shouldBe, unsigned int offset)
{
std::string result;
result += "Parameter ";
result += std::to_string(offset+1) + " must be " + shouldBe;
return result;
}
std::string
pun::invalidCharacter(char32_t unc8)
{
std::stringstream ss;
ss << "Control code in stream: chr(" << (std::uint_least32_t ) unc8 << ")";
return ss.str();
}
Recap8*
pun::option_Recap8(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset >= ct) {
return nullptr;
}
const Php::Value& object = params[offset];
if (!object.instanceOf(Recap8::PHP_NAME)) {
throw Php::Exception("Parameter 1 should be Recap8 object");
}
Recap8 *obj = (Recap8 *)object.implementation();
return obj;
}
Re8map*
pun::check_Re8map(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset < ct) {
const Php::Value& object = params[offset];
if (object.isObject() && object.instanceOf(Re8map::PHP_NAME)) {
Re8map *obj = (Re8map *)object.implementation();
return obj;
}
}
throw Php::Exception(pun::missingParameter("Re8map object",offset));
}
Pcre8*
pun::check_Pcre8(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset < ct) {
const Php::Value& object = params[offset];
if (object.isObject() && object.instanceOf(Pcre8::PHP_NAME)) {
Pcre8 *obj = (Pcre8 *)object.implementation();
return obj;
}
}
throw Php::Exception(pun::missingParameter("Pcre8 object", offset));
}
bool pun::check_String(Php::Parameters& params,unsigned int offset)
{
auto ct = params.size();
if (offset >= ct) {
throw Php::Exception(pun::missingParameter("String", offset));
}
return params[offset].isString();
}
bool pun::option_Array(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset >= ct) {
return false;
}
return params[offset].isArray();
}
bool pun::option_Int(Php::Parameters& params,unsigned int offset)
{
auto ct = params.size();
return (offset < ct);
}
int
pun::check_Int(Php::Parameters& params,unsigned int offset)
{
auto ct = params.size();
if (ct < 1) {
throw Php::Exception(pun::missingParameter("Integer", offset));
}
return params[0];
}
int
pun::check_IntString(Php::Parameters& params)
{
auto ct = params.size();
if (ct < 2) {
throw Php::Exception(pun::missingParameter("String",2));
}
return params[0];
}
Token8*
pun::check_Token8(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset < ct) {
const Php::Value& object = params[offset];
if (object.isObject() && object.instanceOf(Token8::PHP_NAME)) {
Token8 *obj = (Token8 *)object.implementation();
return obj;
}
}
throw Php::Exception(pun::missingParameter("Token8 object", offset));
}
KeyTable*
pun::check_KeyTable(Php::Parameters& params, unsigned int offset)
{
auto ct = params.size();
if (offset < ct) {
KeyTable *obj = castKeyTable(params[offset]);
if (obj)
return obj;
}
throw Php::Exception(pun::missingParameter("KeyTable object", offset));
}
void pun::serialize_valueList(Php::Base* base, std::ostream& out)
{
pun::ValueList* vlist = ( pun::ValueList* ) base;
vlist->fn_serialize(out);
}
void pun::serialize_keyTable(Php::Base* base, std::ostream& out)
{
pun::KeyTable* ktab = ( pun::KeyTable* ) base;
ktab->fn_serialize(out);
}
void
pun::serialize_str(const char* s, size_t slen, std::ostream& out) {
out.write((const char*) &slen, sizeof(slen));
out.write(s, slen);
}
void
pun::unserialize_str(std::string& cval, std::istream& ins)
{
size_t slen;
ins.read(( char*) &slen, sizeof(slen));
//Php::out << "unser. str len " << slen << std::endl;
cval.assign(slen,'0');
ins.read(( char*) cval.data(), slen);
//Php::out << "unser. str " << cval << std::endl;
}
void
pun::serialize(Php::Value& val, std::ostream& out)
{
auto ptype = pun::getPype(val);
SerUnion cval;
std::string sval;
Php::Value result;
switch(ptype) {
case pun::tBool:
// All our types are distinguishable on capital letter,
// so
out << 'B' << (val.boolValue() ? '1' : '0');
break;
case pun::tString:
out << 'S';
pun::serialize_str(val, val.size(), out);
break;
case pun::tFloat:
out << 'F';
cval.dval = val.floatValue();
out.write((const char*) &cval.dval, sizeof(double));
break;
case pun::tInteger:
out << 'I';
cval.ival = val.numericValue();
out.write((const char*) &cval.ival, sizeof(cval.ival));
//Php::out << "Write tInteger" << std::endl;
break;
case pun::tDateTime:
out << 'D';
result = Php::call("serialize", val);
pun::serialize_str(result, result.size(), out);
break;
case pun::tValueList:
out << 'V';
pun::serialize_valueList(val.implementation(),out);
break;
case pun::tKeyTable:
out << 'K';
pun::serialize_keyTable(val.implementation(),out);
break;
case pun::tArray:
out << 'A';
result = Php::call("serialize", val);
pun::serialize_str(result, result.size(), out);
break;
case pun::tNull:
out << 'N'; // stands for itself
break;
case pun::tObject:
default:
out << 'O';
result = Php::call("serialize", val);
pun::serialize_str(result, result.size(), out);
break;
}
}
Php::Array
pun::to_array(const ValueMap &vmap)
{
Php::Array result;
auto zit = vmap.end();
for( auto ait = vmap.begin(); ait != zit; ait++ ) {
result[ait->first] = ait->second;
}
return result;
}