ptypes

ptypes Commit Details


Date:2013-05-12 16:17:00 (11 years 7 months ago)
Author:Natalie Adams
Branch:default
Commit:3005c9d543da
Parents: 209d6fa3805c
Message:remaning ptparray and adding pjson

Changes:
Csrc/ptparray.cpp → src/ptparray.cxx
Asrc/pjson.cxx
Minclude/ptypes.h (2 diffs)

File differences

include/ptypes.h
10531053
10541054
10551055
1056
1057
1058
1059
10561060
10571061
10581062
......
11291133
11301134
11311135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
11321196
11331197
11341198
virtual ~evariant();
};
/*
Added by Nathan Adams - the following is not apart of the original ptypes
*/
enum {
ARR_ASSOCIATIVE,
ARR_LIST,
inline bool islist(const tparray& t) { return t.tag == ARR_LIST; }
inline bool isnull(const tparray& t) { return t.tag == ARR_NULL; }
//http://stackoverflow.com/a/7936901/195722
template<typename T> class ByRef {
public:
ByRef() {
}
ByRef(const T value) : mValue(value) {
}
operator T&() const {
return((T&)mValue);
}
private:
T mValue;
};
class json
{
protected:
string ParseString(const string & json, int & index, bool & success);
large ParseNumber(const string & json, int & index, bool & success);
#ifndef USE_VARIANT
tparray * ParseObject(const string & json, int & index, bool & success);
tparray * ParseArray(const string & json, int & index, bool & success);
#else
variant ParseObject(const string & json, int & index, bool & success);
variant ParseArray(const string & json, int & index, bool & success);
#endif
int GetLastIndexOfNumber(const string & json, int index);
variant ParseValue(const string & json, int & index, bool & success);
int LookAhead(const string & json, int index);
void EatWhiteSpace(const string & json, int & index);
int NextToken(const string & json, int & index);
bool SerializeValue(const variant & value, string & builder);
bool SerializeString(const string & aString, string & builder);
bool SerializeObject(const tparray & anObject, string & builder);
bool SerializeArray(const tparray & anArray, string & builder);
bool SerializeNumber(large number, string & builder);
public:
enum Token { TOKEN_NONE,
TOKEN_CURLY_OPEN,
TOKEN_CURLY_CLOSE,
TOKEN_SQUARED_OPEN,
TOKEN_SQUARED_CLOSE,
TOKEN_COLON,
TOKEN_COMMA,
TOKEN_STRING,
TOKEN_NUMBER,
TOKEN_TRUE,
TOKEN_FALSE,
TOKEN_NULL };
variant JsonDecode(const string & json, bool & success = ByRef<bool>(true));
string JsonEncode(const variant & v);
};
// -------------------------------------------------------------------- //
// --- pre-2.0 compatibility declarations ----------------------------- //
// -------------------------------------------------------------------- //
src/ptparray.cxx
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
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include "ptypes.h"
#include "pstreams.h"
PTYPES_BEGIN
bool ptdecl anext(const tparray& a, int& i, variant& val)
{
//if (a._vals.size() == i)
//if (a._vals.get_count() == i)
if (alength(a._vals) == i)
{
return false;
}
else
{
val = a._vals[(large)i];
i++;
return true;
}
}
bool ptdecl anext(const tparray& a, int& i, variant& val, variant & key)
{
//if (a._keys.size() == i)
//if (a._keys.get_count() == i)
if (alength(a._keys) == i)
{
return false;
}
else
{
val = a._vals[(large)i];
key = a._keys[(large)i];
i++;
return true;
}
}
void ptdecl add(tparray & t, const variant & val)
{
if (t.tag == ARR_NULL || t.tag == ARR_LIST)
{
t.tag = ARR_LIST;
//pout.put("here1");
//t._vals.push_back(val);
add(t._vals, val);
//t._vals.add(val);
//pout.put("here2");
}
//else drop it
}
void ptdecl add(tparray & t, const variant & key, const variant & val)
{
if (t.tag == ARR_NULL || t.tag == ARR_ASSOCIATIVE)
{
t.tag = ARR_ASSOCIATIVE;
add(t._vals, val);
add(t._keys, key);
//t._vals.add(val);
//t._keys.add(key);
//t._vals.push_back(val);
//t._keys.push_back(key);
}
}
variant ptdecl get(tparray & t, const variant & val)
{
//for(int i = 0; i < t._keys.size(); i++)
//for(int i = 0; i < t._keys.get_count(); i++)
for(int i = 0; i < alength(t._keys); i++)
{
if (t._keys[(large)i] == val)
{
return t._vals[(large)i];
}
}
return NULL;
}
variant ptdecl at(tparray & t, large index)
{
return t._vals[index];
}
variant ptdecl keyat(tparray & t, large index)
{
return t._keys[index];
}
PTYPES_END

Archive Download the corresponding diff file

Branches

Tags

Page rendered in 0.65190s using 14 queries.