-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTimXmlRpc.cpp
1748 lines (1443 loc) · 42.1 KB
/
TimXmlRpc.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
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
XmlRpc C++ client for Windows
-----------------------------
Created by Dr Tim Cooper, [email protected], March 2009.
This lets you talk to web sites using XmlRpc from C++ on Windows. It differs
from similar libraries as follows:
- works on Windows
- supports HTTPS (SSL)
- uses wininet to manage HTTP/HTTPS, so it's really very minimal
- much faster than XmlRpc++ which suffers from STL performance problems.
This project consists of 2 files: "TimXmlRpc.h" && "TimXmlRpc.cpp".
Parts of this project have been taken from Chris Morley's "XmlRpc++" project,
in particular the API. Chris's contribution is acknowledged by marking his
work with the token: "/*ChrisMorley/". Thanks, Chris!
*/
#include <windows.h>
#include <wininet.h>
#include <cmath>
#include <iterator>
#include <limits>
#include "TimXmlRpc.h"
static const char VALUE_TAG[] = "<value>";
static const char VALUE_ETAG[] = "</value>";
static const char BOOLEAN_TAG[] = "<boolean>";
static const char BOOLEAN_ETAG[] = "</boolean>";
static const char DOUBLE_TAG[] = "<double>";
static const char DOUBLE_ETAG[] = "</double>";
static const char INT_TAG[] = "<int>";
static const char INT_ETAG[] = "</int>";
static const char I4_TAG[] = "<i4>";
static const char I4_ETAG[] = "</i4>";
static const char STRING_TAG[] = "<string>";
static const char STRING_ETAG[] = "</string>";
static const char DATETIME_TAG[] = "<dateTime.iso8601>";
static const char DATETIME_ETAG[] = "</dateTime.iso8601>";
static const char BASE64_TAG[] = "<base64>";
static const char BASE64_ETAG[] = "</base64>";
static const char ARRAY_TAG[] = "<array>";
static const char DATA_TAG[] = "<data>";
static const char DATA_ETAG[] = "</data>";
static const char ARRAY_ETAG[] = "</array>";
static const char STRUCT_TAG[] = "<struct>";
static const char MEMBER_TAG[] = "<member>";
static const char NAME_TAG[] = "<name>";
static const char NAME_ETAG[] = "</name>";
static const char MEMBER_ETAG[] = "</member>";
static const char STRUCT_ETAG[] = "</struct>";
//---------------------------------- Misc: -------------------------------
static bool strieq(const char* a, const char* b)
{
return _stricmp(a, b) == 0;
}
static bool strbegins(const char* bigstr, const char* smallstr, bool casesensitive)
{
const char* smalls = smallstr;
const char* bigs = bigstr;
while (*smalls) {
if (casesensitive) {
if (*bigs != *smalls)
return false;
else bigs++, smalls++;
}
else {
if (toupper(*bigs) != toupper(*smalls))
return false;
else bigs++, smalls++;
}
}
return true;
}
//---------------------------------- ValueArray: -------------------------------
void XmlRpcValue::ValueArray::resize(int n)
{
if (n >= _allocated) {
// Optimise growing of the array:
int power2 = n - 1;
power2 |= power2 >> 1;
power2 |= power2 >> 2;
power2 |= power2 >> 4;
power2 |= power2 >> 8;
power2 |= power2 >> 16;
power2++;
// Set the size:
A = (XmlRpcValue*)realloc(A, power2 * sizeof(XmlRpcValue));
memset(A + _allocated, 0, (power2 - _allocated) * sizeof(XmlRpcValue));
_allocated = power2;
}
_size = n;
}
bool XmlRpcValue::ValueArray::operator==(ValueArray &other)
{
if (_size != other._size)
return false;
for (int i=0; i < _size; i++) {
if (A[i] != other.A[i])
return false;
}
return true;
}
XmlRpcValue::ValueArray::~ValueArray()
{
for (int i=0; i < _size; i++)
A[i].invalidate();
free(A);
}
//---------------------------------- base64.h: -------------------------------
/* <Chris Morley> */
static const
int _base64Chars[]= {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9',
'+','/' };
#define _0000_0011 0x03
#define _1111_1100 0xFC
#define _1111_0000 0xF0
#define _0011_0000 0x30
#define _0011_1100 0x3C
#define _0000_1111 0x0F
#define _1100_0000 0xC0
#define _0011_1111 0x3F
#define _EQUAL_CHAR (-1)
#define _UNKNOWN_CHAR (-2)
#define _IOS_FAILBIT std::ios_base::failbit
#define _IOS_EOFBIT std::ios_base::eofbit
#define _IOS_BADBIT std::ios_base::badbit
#define _IOS_GOODBIT std::ios_base::goodbit
// TEMPLATE CLASS base64_put
class base64
{
public:
typedef unsigned char byte_t;
typedef char char_type;
typedef std::char_traits<char> traits_type;
// base64 requires max line length <= 72 characters
// you can fill end of line
// it may be crlf, crlfsp, noline or other class like it
struct crlf
{
template<class _OI>
_OI operator()(_OI _To) const{
*_To = std::char_traits<char>::to_char_type('\r'); ++_To;
*_To = std::char_traits<char>::to_char_type('\n'); ++_To;
return (_To);
}
};
struct crlfsp
{
template<class _OI>
_OI operator()(_OI _To) const{
*_To = std::char_traits<char>::to_char_type('\r'); ++_To;
*_To = std::char_traits<char>::to_char_type('\n'); ++_To;
*_To = std::char_traits<char>::to_char_type(' '); ++_To;
return (_To);
}
};
struct noline
{
template<class _OI>
_OI operator()(_OI _To) const{
return (_To);
}
};
struct three2four
{
void zero()
{
_data[0] = 0;
_data[1] = 0;
_data[2] = 0;
}
byte_t get_0() const
{
return _data[0];
}
byte_t get_1() const
{
return _data[1];
}
byte_t get_2() const
{
return _data[2];
}
void set_0(byte_t _ch)
{
_data[0] = _ch;
}
void set_1(byte_t _ch)
{
_data[1] = _ch;
}
void set_2(byte_t _ch)
{
_data[2] = _ch;
}
// 0000 0000 1111 1111 2222 2222
// xxxx xxxx xxxx xxxx xxxx xxxx
// 0000 0011 1111 2222 2233 3333
int b64_0() const {return (_data[0] & _1111_1100) >> 2;}
int b64_1() const {return ((_data[0] & _0000_0011) << 4) + ((_data[1] & _1111_0000)>>4);}
int b64_2() const {return ((_data[1] & _0000_1111) << 2) + ((_data[2] & _1100_0000)>>6);}
int b64_3() const {return (_data[2] & _0011_1111);}
void b64_0(int _ch) {_data[0] = ((_ch & _0011_1111) << 2) | (_0000_0011 & _data[0]);}
void b64_1(int _ch) {
_data[0] = ((_ch & _0011_0000) >> 4) | (_1111_1100 & _data[0]);
_data[1] = ((_ch & _0000_1111) << 4) | (_0000_1111 & _data[1]); }
void b64_2(int _ch) {
_data[1] = ((_ch & _0011_1100) >> 2) | (_1111_0000 & _data[1]);
_data[2] = ((_ch & _0000_0011) << 6) | (_0011_1111 & _data[2]); }
void b64_3(int _ch){
_data[2] = (_ch & _0011_1111) | (_1100_0000 & _data[2]);}
private:
byte_t _data[3];
};
template<class _II, class _OI, class _State, class _Endline>
_II put(_II _First, _II _Last, _OI _To, _State&, _Endline) const
{
three2four _3to4;
int line_octets = 0;
while(_First != _Last)
{
_3to4.zero();
//
_3to4.set_0(*_First);
_First++;
if(_First == _Last)
{
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
*_To = std::char_traits<char>::to_char_type('='); ++_To;
*_To = std::char_traits<char>::to_char_type('='); ++_To;
goto __end;
}
_3to4.set_1(*_First);
_First++;
if(_First == _Last)
{
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
*_To = std::char_traits<char>::to_char_type('='); ++_To;
goto __end;
}
_3to4.set_2(*_First);
_First++;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_0()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_1()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_2()]); ++_To;
*_To = std::char_traits<char>::to_char_type(_base64Chars[_3to4.b64_3()]); ++_To;
if(line_octets == 17) // base64
{
//_To = _Endl(_To);
*_To = '\n'; ++_To;
line_octets = 0;
}
else
++line_octets;
}
__end: ;
return (_First);
}
template<class _II, class _OI, class _State>
_II get(_II _First, _II _Last, _OI _To, _State& _St) const
{
three2four _3to4;
int _Char;
while(_First != _Last)
{
// Take octet
_3to4.zero();
// -- 0 --
// Search next valid char...
while((_Char = _getCharType(*_First)) < 0 && _Char == _UNKNOWN_CHAR)
{
if(++_First == _Last)
{
_St |= _IOS_FAILBIT|_IOS_EOFBIT; return _First; // unexpected EOF
}
}
if(_Char == _EQUAL_CHAR){
// Error! First character in octet can't be '='
_St |= _IOS_FAILBIT;
return _First;
}
else
_3to4.b64_0(_Char);
// -- 1 --
// Search next valid char...
while(++_First != _Last)
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
break;
if(_First == _Last) {
_St |= _IOS_FAILBIT|_IOS_EOFBIT; // unexpected EOF
return _First;
}
if(_Char == _EQUAL_CHAR){
// Error! Second character in octet can't be '='
_St |= _IOS_FAILBIT;
return _First;
}
else
_3to4.b64_1(_Char);
// -- 2 --
// Search next valid char...
while(++_First != _Last)
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
break;
if(_First == _Last) {
// Error! Unexpected EOF. Must be '=' or base64 character
_St |= _IOS_FAILBIT|_IOS_EOFBIT;
return _First;
}
if(_Char == _EQUAL_CHAR){
// OK!
_3to4.b64_2(0);
_3to4.b64_3(0);
// chek for EOF
if(++_First == _Last)
{
// Error! Unexpected EOF. Must be '='. Ignore it.
//_St |= _IOS_BADBIT|_IOS_EOFBIT;
_St |= _IOS_EOFBIT;
}
else
if(_getCharType(*_First) != _EQUAL_CHAR)
{
// Error! Must be '='. Ignore it.
//_St |= _IOS_BADBIT;
}
else
++_First; // Skip '='
// write 1 byte to output
*_To = (byte_t) _3to4.get_0();
return _First;
}
else
_3to4.b64_2(_Char);
// -- 3 --
// Search next valid char...
while(++_First != _Last)
if((_Char = _getCharType(*_First)) != _UNKNOWN_CHAR)
break;
if(_First == _Last) {
// Unexpected EOF. It's error. But ignore it.
//_St |= _IOS_FAILBIT|_IOS_EOFBIT;
_St |= _IOS_EOFBIT;
return _First;
}
if(_Char == _EQUAL_CHAR)
{
// OK!
_3to4.b64_3(0);
// write to output 2 bytes
*_To = (byte_t) _3to4.get_0();
*_To = (byte_t) _3to4.get_1();
++_First; // set position to next character
return _First;
}
else
_3to4.b64_3(_Char);
// write to output 3 bytes
*_To = (byte_t) _3to4.get_0();
*_To = (byte_t) _3to4.get_1();
*_To = (byte_t) _3to4.get_2();
++_First;
} // while(_First != _Last)
return (_First);
}
protected:
int _getCharType(int _Ch) const
{
if(_base64Chars[62] == _Ch)
return 62;
if(_base64Chars[63] == _Ch)
return 63;
if((_base64Chars[0] <= _Ch) && (_base64Chars[25] >= _Ch))
return _Ch - _base64Chars[0];
if((_base64Chars[26] <= _Ch) && (_base64Chars[51] >= _Ch))
return _Ch - _base64Chars[26] + 26;
if((_base64Chars[52] <= _Ch) && (_base64Chars[61] >= _Ch))
return _Ch - _base64Chars[52] + 52;
if(_Ch == std::char_traits<char>::to_int_type('='))
return _EQUAL_CHAR;
return _UNKNOWN_CHAR;
}
};
/*-----------------------------------------------------------------------*/
void XmlRpcValue::invalidate()
{
switch (_type) {
case TypeString: free(u.asString); break;
case TypeDateTime: delete u.asTime; break;
case TypeBase64: delete u.asBinary; break;
case TypeArray: delete u.asArray; break;
case TypeStruct: delete u.asStruct; break;
default: break;
}
_type = TypeInvalid;
u.asBinary = 0;
}
// Type checking
void XmlRpcValue::assertTypeOrInvalid(Type t)
{
if (_type == TypeInvalid || _type == TypeNil) {
_type = t;
switch (_type) { // Ensure there is a valid value for the type
case TypeString: u.asString = _strdup(""); break;
case TypeDateTime: u.asTime = new struct tm(); break;
case TypeBase64: u.asBinary = new BinaryData(); break;
case TypeArray: u.asArray = new ValueArray(); break;
case TypeStruct: u.asStruct = new ValueStruct(); break;
default: u.asBinary = 0; break;
}
}
else if (_type != t)
throw XmlRpcException("type error");
}
void XmlRpcValue::assertArray(int size) const
{
if (_type != TypeArray)
throw XmlRpcException("type error: expected an array");
else if (int(u.asArray->size()) < size)
throw XmlRpcException("range error: array index too large");
}
void XmlRpcValue::assertArray(int size)
{
if (_type == TypeInvalid) {
_type = TypeArray;
u.asArray = new ValueArray(size);
}
else if (_type == TypeArray) {
if (int(u.asArray->size()) < size) {
u.asArray->resize(size);
}
}
else
throw XmlRpcException("type error: expected an array");
}
void XmlRpcValue::assertStruct()
{
if (_type == TypeInvalid) {
_type = TypeStruct;
u.asStruct = new ValueStruct();
} else if (_type != TypeStruct)
throw XmlRpcException("type error: expected a struct");
}
XmlRpcValue::ValueArray::ValueArray(ValueArray &other)
{
A = NULL;
_size = _allocated = 0;
resize(other._size);
for (int i=0 ; i < _size; i++) {
A[i] = other.A[i];
}
}
/* This copy constructor does a deep copy. It's equivalent to: "XmlRpcValue(XmlRpcValue&orig);".
Tim> In my applications, I manage to avoid using copy constructors, && if you're interested
in performance, you should also consider whether you can avoid copy constructors, e.g. by
passing values by reference.
Thanks to Eric Schneider for identifying a fault with a previous version of this copy constructor.
*/
XmlRpcValue& XmlRpcValue::operator=(XmlRpcValue const& rhs)
{
if (this != &rhs) {
invalidate();
_type = rhs._type;
switch (_type) {
case TypeBoolean: u.asBool = rhs.u.asBool; break;
case TypeInt: u.asInt = rhs.u.asInt; break;
case TypeDouble: u.asDouble = rhs.u.asDouble; break;
case TypeDateTime: u.asTime = new struct tm(*rhs.u.asTime); break;
case TypeString: u.asString = _strdup(rhs.u.asString); break;
case TypeBase64: u.asBinary = new BinaryData(*rhs.u.asBinary); break;
case TypeArray: u.asArray = new ValueArray(*rhs.u.asArray); break;
case TypeStruct: u.asStruct = new ValueStruct(*rhs.u.asStruct); break;
default: u.asBinary = 0; break;
}
}
return *this;
}
/* </Chris Morley> */
// Map something like: "T2-D&T1" to "T2-D&T1"
// Returns a 'strdup()' version of the input string.
static char* xmlDecode(const char* s, const char* end)
{
char* dest = (char*)malloc(end - s + 1);
char* d = dest;
while (s < end) {
if (*s != '&')
*d++ = *s++;
else if (strbegins(s, "&", true))
*d++ = '&', s += 5;
else if (strbegins(s, """, true))
*d++ = '\"', s += 6;
else if (strbegins(s, "'", true)/*! standard*/ || strbegins(s, "'", true))
*d++ = '\'', s += 6;
else if (strbegins(s, "<", true))
*d++ = '<', s += 4;
else if (strbegins(s, ">", true))
*d++ = '>', s += 4;
else if (strbegins(s, "&#", true)) {
s += 2;
*d++ = (char)atoi(s);
while (*s >= '0' && *s <= '9')
s++;
if (*s == ';')
s++;
}
else
*d++ = *s++; // assert(false);
}
*d = '\0';
return dest;
}
// Replace raw text with xml-encoded entities.
static std::string xmlEncode(const char* s)
{
std::ostringstream ostr;
while (*s) {
if (*s == '&')
ostr << "&";
else if (*s == '<')
ostr << "<";
else if (*s == '>')
ostr << ">";
else if (*s == '"')
ostr << """;
else if (*s == '\'')
ostr << "'";// Would David prefer: "'" ?
else if (*s < ' ' || *s >= 127)
ostr << "&#" << int((unsigned char)*s) << ';';
else ostr << *s;
s++;
}
return ostr.str();
}
/* <Chris Morley> */
// Predicate for tm equality
static bool tmEq(struct tm const& t1, struct tm const& t2)
{
return t1.tm_sec == t2.tm_sec && t1.tm_min == t2.tm_min &&
t1.tm_hour == t2.tm_hour && t1.tm_mday == t2.tm_mday &&
t1.tm_mon == t2.tm_mon && t1.tm_year == t2.tm_year;
}
bool XmlRpcValue::operator==(XmlRpcValue const& other) const
{
if (_type != other._type)
return false;
switch (_type) {
case TypeBoolean: return ( !u.asBool && !other.u.asBool) ||
( u.asBool && other.u.asBool);
case TypeInt: return u.asInt == other.u.asInt;
case TypeDouble: return std::abs(u.asDouble - other.u.asDouble) < std::numeric_limits<double>::epsilon();
case TypeDateTime: return tmEq(*u.asTime, *other.u.asTime);
case TypeString: return *u.asString == *other.u.asString;
case TypeBase64: return *u.asBinary == *other.u.asBinary;
case TypeArray: return *u.asArray == *other.u.asArray;
// The map<>::operator== requires the definition of value< for kcc
case TypeStruct: //return *u.asStruct == *other.u.asStruct;
{
if (u.asStruct->size() != other.u.asStruct->size())
return false;
ValueStruct::const_iterator it1=u.asStruct->begin();
ValueStruct::const_iterator it2=other.u.asStruct->begin();
while (it1 != u.asStruct->end()) {
const XmlRpcValue& v1 = it1->second;
const XmlRpcValue& v2 = it2->second;
if ( ! (v1 == v2))
return false;
it1++;
it2++;
}
return true;
}
default: break;
}
return true; // Both invalid values ...
}
// Works for strings, binary data, arrays, && structs.
int XmlRpcValue::size() const
{
switch (_type) {
case TypeString: return int(strlen(u.asString));
case TypeBase64: return int(u.asBinary->size());
case TypeArray: return int(u.asArray->size());
case TypeStruct: return int(u.asStruct->size());
default: break;
}
throw XmlRpcException("type error");
}
// Checks for existence of struct member
bool XmlRpcValue::hasMember(const std::string& name) const
{
return _type == TypeStruct && u.asStruct->find(name) != u.asStruct->end();
}
/* </Chris Morley> */
static void SkipWhiteSpace(const char* &s)
{
while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
s++;
}
static char* GobbleTag(const char* &s, char dest[128])
// E.g. given: "< string / >", return "<string/>"
{
*dest = '\0';
SkipWhiteSpace(s);
if (*s != '<')
return dest;
char* d = dest;
*d++ = *s++;
SkipWhiteSpace(s);
while (*s && *s != '>') {
if (*s == ' ' && (d[-1] == '<' || d[-1] == '/' || s[1] == ' ' || s[1] == '/' || s[1] == '>'))
s++;
else *d++ = *s++;
}
*d++ = '>';
*d = '\0';
if (*s == '>')
s++;
return dest;
}
static void GobbleExpectedTag(const char* &s, const char* etag)
{
char tag[128];
GobbleTag(s, tag);
if (! strieq(tag, etag))
throw XmlRpcException(std::string("Expecting tag: ") + etag);
}
// Set the value from xml. The chars at *offset into valueXml
// should be the start of a <value> tag. Destroys any existing value.
void XmlRpcValue::fromXml(const char* &s)
{
char tag[128];
invalidate();
// Gobble the <value> tag:
GobbleTag(s, tag);
if (strieq(tag, "<value>"))
; // good
else if (strieq(tag, "<value/>")) {
// Jeff Rasmussen claims that <value/> is valid XmlRpc. I think he's correct.
_type = TypeString;
u.asString = _strdup("");
return;
}
else
throw XmlRpcException(std::string("Expecting tag: <value>"));
// Gobble the type tag:
GobbleTag(s, tag);
if (*tag == '\0') {
// If no type is indicated, the type is string.
stringFromXml(s);
}
else if (strieq(tag, BOOLEAN_TAG)) {
boolFromXml(s);
GobbleExpectedTag(s, BOOLEAN_ETAG);
}
else if (strieq(tag, I4_TAG)) {
intFromXml(s);
GobbleExpectedTag(s, I4_ETAG);
}
else if (strieq(tag, INT_TAG)) {
intFromXml(s);
GobbleExpectedTag(s, INT_ETAG);
}
else if (strieq(tag, DOUBLE_TAG)) {
doubleFromXml(s);
GobbleExpectedTag(s, DOUBLE_ETAG);
}
else if (strieq(tag, STRING_TAG)) {
stringFromXml(s);
GobbleExpectedTag(s, STRING_ETAG);
}
else if (strieq(tag, DATETIME_TAG)) {
timeFromXml(s);
GobbleExpectedTag(s, DATETIME_ETAG);
}
else if (strieq(tag, BASE64_TAG)) {
binaryFromXml(s);
GobbleExpectedTag(s, BASE64_ETAG);
}
else if (strieq(tag, ARRAY_TAG)) {
arrayFromXml(s);
GobbleExpectedTag(s, ARRAY_ETAG);
}
else if (strieq(tag, STRUCT_TAG)) {
structFromXml(s);
GobbleExpectedTag(s, STRUCT_ETAG);
}
else if (strieq(tag, "<string/>")) {
_type = TypeString;
u.asString = _strdup("");
}
else if (strieq(tag, "<nil/>")) {
_type = TypeNil;
}
else if (strieq(tag, "<struct/>")) {
_type = TypeStruct;
u.asStruct = new ValueStruct;
}
else if (strieq(tag, VALUE_ETAG)) {
// "If no type is indicated, the type is string."
_type = TypeString;
u.asString = _strdup("");
return; // don't gobble VALUE_ETAG because we already did
}
else {
throw XmlRpcException(std::string("Unknown type tag: ") + tag);
}
GobbleExpectedTag(s, VALUE_ETAG);
}
// Encode the Value in xml
void XmlRpcValue::toXml(std::ostringstream &ostr) const
{
switch (_type) {
case TypeBoolean: return boolToXml(ostr);
case TypeInt: return intToXml(ostr);
case TypeDouble: return doubleToXml(ostr);
case TypeString: return stringToXml(ostr);
case TypeDateTime: return timeToXml(ostr);
case TypeBase64: return binaryToXml(ostr);
case TypeArray: return arrayToXml(ostr);
case TypeStruct: return structToXml(ostr);
case TypeNil: return nilToXml(ostr);
case TypeInvalid: throw XmlRpcException("Undefined XmlRpc value");
default: break;
}
}
void XmlRpcValue::boolFromXml(const char* &s)
{
if (*s == '0' && s[1] == '<') {
_type = TypeBoolean;
u.asBool = false;
s++;
}
else if (*s == '1' && s[1] == '<') {
_type = TypeBoolean;
u.asBool = true;
s++;
}
else throw XmlRpcException("bad BOOL");
}
void XmlRpcValue::boolToXml(std::ostringstream &ostr) const
{
ostr << VALUE_TAG << BOOLEAN_TAG << (u.asBool ? "1" : "0") << BOOLEAN_ETAG << VALUE_ETAG;
}
void XmlRpcValue::intFromXml(const char* &s)
{
char* valueEnd;
long ivalue = strtol(s, &valueEnd, 10);
if (valueEnd == s)
throw XmlRpcException("Bad double");
_type = TypeInt;
u.asInt = int(ivalue);
s = valueEnd;
}
void XmlRpcValue::intToXml(std::ostringstream &ostr) const
{
ostr << VALUE_TAG << I4_TAG << u.asInt << I4_ETAG << VALUE_ETAG;
}
void XmlRpcValue::doubleFromXml(const char* &s)
{
char* valueEnd;
double dvalue = strtod(s, &valueEnd);
if (valueEnd == s)
throw XmlRpcException("Bad double");
_type = TypeDouble;
u.asDouble = dvalue;
s = valueEnd;
}
void XmlRpcValue::doubleToXml(std::ostringstream &ostr) const
{
ostr << VALUE_TAG << DOUBLE_TAG << u.asDouble << DOUBLE_ETAG << VALUE_ETAG;
// This will use the default ostream::precision() to display the double. To display
// values with greater accuracy, call e.g. 'ostr.precision(12)' at the top level.
}
void XmlRpcValue::stringFromXml(const char* &s)
{
const char* valueEnd = strchr(s, '<');
if (valueEnd == NULL)
throw XmlRpcException("Bad string");
_type = TypeString;
u.asString = xmlDecode(s, valueEnd);
s = valueEnd;
}
void XmlRpcValue::stringToXml(std::ostringstream &ostr) const
{
if (u.asString == NULL || *u.asString == '\0')
ostr << VALUE_TAG << "<string/>" << VALUE_ETAG;
else ostr << VALUE_TAG << xmlEncode(u.asString) << VALUE_ETAG;
// The 'STRING_TAG' is optional.
}
void XmlRpcValue::nilToXml(std::ostringstream &ostr) const
{
ostr << VALUE_TAG << "<nil/>" << VALUE_ETAG;
}
void XmlRpcValue::timeFromXml(const char* &s)
{
const char* valueEnd = strchr(s, '<');
if (valueEnd == NULL)
throw XmlRpcException("Bad time value");
struct tm t;
if (sscanf_s(s, "%4d%2d%2dT%2d:%2d:%2d", &t.tm_year,&t.tm_mon,&t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec) != 6)
throw XmlRpcException("Bad time value");
t.tm_wday = t.tm_yday = t.tm_isdst = -1;
t.tm_mon -= 1;
_type = TypeDateTime;
u.asTime = new struct tm(t);
s = valueEnd;
}
void XmlRpcValue::timeToXml(std::ostringstream &ostr) const
{
struct tm* t = u.asTime;
char buf[30];