aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Foundation/src/Var.cpp
blob: 03deaef8548c6ce0fb063bf38ae81d0a7d591ecf (plain) (blame)
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
//
// Var.cpp
//
// Library: Foundation
// Package: Core
// Module:  Var
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Dynamic/Var.h"
#include "Poco/Dynamic/Struct.h"
#include <algorithm>
#include <cctype>
#include <vector>
#include <list>
#include <deque>


namespace Poco {
namespace Dynamic {


Var::Var()
#ifdef POCO_NO_SOO
	: _pHolder(0)
#endif
{
}


Var::Var(const char* pVal)
#ifdef POCO_NO_SOO 
	: _pHolder(new VarHolderImpl<std::string>(pVal))
{
}
#else
{
	construct(std::string(pVal));
}
#endif


Var::Var(const Var& other)
#ifdef POCO_NO_SOO
	: _pHolder(other._pHolder ? other._pHolder->clone() : 0)
{
}
#else
{
	if ((this != &other) && !other.isEmpty())
			construct(other);
}
#endif


Var::~Var()
{
	destruct();
}


Var& Var::operator = (const Var& rhs)
{
#ifdef POCO_NO_SOO
	Var tmp(rhs);
	swap(tmp);
#else
	if ((this != &rhs) && !rhs.isEmpty())
		construct(rhs);
	else if ((this != &rhs) && rhs.isEmpty())
		_placeholder.erase();
#endif
	return *this;
}


const Var Var::operator + (const Var& other) const
{
	if (isInteger())
	{
		if (isSigned())
			return add<Poco::Int64>(other);
		else
			return add<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return add<double>(other);
	else if (isString())
		return add<std::string>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


Var& Var::operator += (const Var& other)
{
	if (isInteger())
	{
		if (isSigned())
			return *this = add<Poco::Int64>(other);
		else
			return *this = add<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return *this = add<double>(other);
	else if (isString())
		return *this = add<std::string>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


const Var Var::operator - (const Var& other) const
{
	if (isInteger())
	{
		if (isSigned())
			return subtract<Poco::Int64>(other);
		else
			return subtract<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return subtract<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


Var& Var::operator -= (const Var& other)
{
	if (isInteger())
	{
		if (isSigned())
			return *this = subtract<Poco::Int64>(other);
		else
			return *this = subtract<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return *this = subtract<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


const Var Var::operator * (const Var& other) const
{
	if (isInteger())
	{
		if (isSigned())
			return multiply<Poco::Int64>(other);
		else
			return multiply<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return multiply<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


Var& Var::operator *= (const Var& other)
{
	if (isInteger())
	{
		if (isSigned())
			return *this = multiply<Poco::Int64>(other);
		else
			return *this = multiply<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return *this = multiply<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


const Var Var::operator / (const Var& other) const
{
	if (isInteger())
	{
		if (isSigned())
			return divide<Poco::Int64>(other);
		else
			return divide<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return divide<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


Var& Var::operator /= (const Var& other)
{
	if (isInteger())
	{
		if (isSigned())
			return *this = divide<Poco::Int64>(other);
		else
			return *this = divide<Poco::UInt64>(other);
	}
	else if (isNumeric())
		return *this = divide<double>(other);
	else
		throw InvalidArgumentException("Invalid operation for this data type.");
}


Var& Var::operator ++ ()
{
	if (!isInteger())
		throw InvalidArgumentException("Invalid operation for this data type.");

	return *this = *this + 1;
}


const Var Var::operator ++ (int)
{
	if (!isInteger())
		throw InvalidArgumentException("Invalid operation for this data type.");

	Var tmp(*this);
	*this += 1;
	return tmp;
}


Var& Var::operator -- ()
{
	if (!isInteger())
		throw InvalidArgumentException("Invalid operation for this data type.");

	return *this = *this - 1;
}


const Var Var::operator -- (int)
{
	if (!isInteger())
		throw InvalidArgumentException("Invalid operation for this data type.");

	Var tmp(*this);
	*this -= 1;
	return tmp;
}


bool Var::operator == (const Var& other) const
{
	if (isEmpty() != other.isEmpty()) return false;
	if (isEmpty() && other.isEmpty()) return true;
	return convert<std::string>() == other.convert<std::string>();
}


bool Var::operator == (const char* other) const
{
	if (isEmpty()) return false;
	return convert<std::string>() == other;
}


bool Var::operator != (const Var& other) const
{
	if (isEmpty() && other.isEmpty()) return false;
	else if (isEmpty() || other.isEmpty()) return true;

	return convert<std::string>() != other.convert<std::string>();
}


bool Var::operator != (const char* other) const
{
	if (isEmpty()) return true;
	return convert<std::string>() != other;
}


bool Var::operator < (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<std::string>() < other.convert<std::string>();
}


bool Var::operator <= (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<std::string>() <= other.convert<std::string>();
}


bool Var::operator > (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<std::string>() > other.convert<std::string>();
}


bool Var::operator >= (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<std::string>() >= other.convert<std::string>();
}


bool Var::operator || (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<bool>() || other.convert<bool>();
}


bool Var::operator && (const Var& other) const
{
	if (isEmpty() || other.isEmpty()) return false;
	return convert<bool>() && other.convert<bool>();
}


void Var::empty()
{
#ifdef POCO_NO_SOO
	delete _pHolder;
	_pHolder = 0;
#else
	if (_placeholder.isLocal()) this->~Var();
	else delete content();
	_placeholder.erase();
#endif
}


void Var::clear()
{
#ifdef POCO_NO_SOO
	delete _pHolder;
	_pHolder = 0;
#else
	if (_placeholder.isLocal()) this->~Var();
	else delete content();
	_placeholder.erase();
#endif
}


Var& Var::getAt(std::size_t n)
{
	if (isVector())
		return holderImpl<std::vector<Var>,
			InvalidAccessException>("Not a vector.")->operator[](n);
	else if (isList())
		return holderImpl<std::list<Var>,
			InvalidAccessException>("Not a list.")->operator[](n);
	else if (isDeque())
		return holderImpl<std::deque<Var>,
			InvalidAccessException>("Not a deque.")->operator[](n);
	else if (isStruct())
		return structIndexOperator(holderImpl<Struct<int>,
			InvalidAccessException>("Not a struct."), static_cast<int>(n));
	else if (!isString() && !isEmpty() && (n == 0))
		return *this;
	
	throw RangeException("Index out of bounds.");
}


char& Var::at(std::size_t n)
{
	if (isString())
	{
		return holderImpl<std::string,
			InvalidAccessException>("Not a string.")->operator[](n);
	}

	throw InvalidAccessException("Not a string.");
}


Var& Var::getAt(const std::string& name)
{
	return holderImpl<DynamicStruct,
		InvalidAccessException>("Not a struct.")->operator[](name);
}


Var Var::parse(const std::string& val)
{
	std::string::size_type t = 0;
	return parse(val, t);
}


Var Var::parse(const std::string& val, std::string::size_type& pos)
{
	// { -> an Object==DynamicStruct
	// [ -> an array
	// '/" -> a string (strip '/")
	// other: also treat as string
	skipWhiteSpace(val, pos);
	if (pos < val.size())
	{
		switch (val[pos])
		{
		case '{':
			return parseObject(val, pos);
		case '[':
			return parseArray(val, pos);
		case '"':
			return parseJSONString(val, pos);
		default:
			{
				std::string str = parseString(val, pos);
				if (str == "false")
					return false;

				if (str == "true")
					return true;

				bool isNumber = false;
				bool isSigned = false;
				int separators = 0;
				int frac = 0;
				int index = 0;
				size_t size = str.size();
				for (size_t i = 0; i < size ; ++i)
				{
					int ch = str[i];
					if ((ch == '-' || ch == '+') && index == 0)
					{
						if (ch == '-')
							isSigned = true;
					}
					else if (Ascii::isDigit(ch))
					{
						isNumber |= true;
					}
					else if (ch == '.' || ch == ',')
					{
						frac = ch;
						++separators;
						if (separators > 1)
							return str;
					}
					else
						return str;

					++index;
				}

				if (frac && isNumber)
				{
					const double number = NumberParser::parseFloat(str, frac);
					return Var(number);
				}
				else if (frac == 0 && isNumber && isSigned)
				{
					const Poco::Int64 number = NumberParser::parse64(str);
					return number;
				}
				else if (frac == 0 && isNumber && !isSigned)
				{
					const Poco::UInt64 number = NumberParser::parseUnsigned64(str);
					return number;
				}

				return str;
			}
		}
	}
	std::string empty;
	return empty;
}


Var Var::parseObject(const std::string& val, std::string::size_type& pos)
{
	poco_assert_dbg (pos < val.size() && val[pos] == '{');
	++pos;
	skipWhiteSpace(val, pos);
	DynamicStruct aStruct;
	while (val[pos] != '}' && pos < val.size())
	{
		std::string key = parseString(val, pos);
		skipWhiteSpace(val, pos);
		if (val[pos] != ':')
			throw DataFormatException("Incorrect object, must contain: key : value pairs"); 
		++pos; // skip past :
		Var value = parse(val, pos);
		aStruct.insert(key, value);
		skipWhiteSpace(val, pos);
		if (val[pos] == ',')
		{
			++pos;
			skipWhiteSpace(val, pos);
		}
	}
	if (val[pos] != '}')
		throw DataFormatException("Unterminated object"); 
	++pos;
	return aStruct;
}


Var Var::parseArray(const std::string& val, std::string::size_type& pos)
{
	poco_assert_dbg (pos < val.size() && val[pos] == '[');
	++pos;
	skipWhiteSpace(val, pos);
	std::vector<Var> result;
	while (val[pos] != ']' && pos < val.size())
	{
		result.push_back(parse(val, pos));
		skipWhiteSpace(val, pos);
		if (val[pos] == ',')
		{
			++pos;
			skipWhiteSpace(val, pos);
		}
	}
	if (val[pos] != ']')
		throw DataFormatException("Unterminated array"); 
	++pos;
	return result;
}


std::string Var::parseString(const std::string& val, std::string::size_type& pos)
{
	poco_assert_dbg (pos < val.size());
	if (val[pos] == '"')
	{
		return parseJSONString(val, pos);
	}
	else
	{
		std::string result;
		while (pos < val.size() 
			&& !Poco::Ascii::isSpace(val[pos]) 
			&& val[pos] != ','
			&& val[pos] != ']'
			&& val[pos] != '}')
		{
			result += val[pos++];
		}
		return result;
	}
}


std::string Var::parseJSONString(const std::string& val, std::string::size_type& pos)
{
	poco_assert_dbg (pos < val.size() && val[pos] == '"');
	++pos;
	std::string result;
	bool done = false;
	while (pos < val.size() && !done)
	{
		switch (val[pos])
		{
		case '"':
			done = true;
			++pos;
			break;
		case '\\':
			if (pos < val.size())
			{
				++pos;
				switch (val[pos])
				{
				case 'b':
					result += '\b';
					break; 
				case 'f':
					result += '\f';
					break; 
				case 'n':
					result += '\n';
					break; 
				case 'r':
					result += '\r';
					break; 
				case 't':
					result += '\t';
					break; 
				default:
					result += val[pos];
					break;
				}
				break;
			}
			else
			{
				result += val[pos];
			}
			++pos;
			break;
		default:
			result += val[pos++];
			break;
		}
	}
	if (!done) throw Poco::DataFormatException("unterminated JSON string");
	return result;
}


void Var::skipWhiteSpace(const std::string& val, std::string::size_type& pos)
{
	poco_assert_dbg (pos < val.size());
	while (std::isspace(val[pos]) && pos < val.size())
		++pos;
}


std::string Var::toString(const Var& any)
{
	std::string res;
	Impl::appendJSONValue(res, any);
	return res;
}


Var& Var::structIndexOperator(VarHolderImpl<Struct<int> >* pStr, int n) const
{
	return pStr->operator[](n);
}


} } // namespace Poco::Dynamic