aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkJSON.cpp
blob: acdc9a71f0f136b78790198b4d916c5c8b1fc77a (plain)
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
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkJSON.h"
#include "SkString.h"

struct SkJSON::Object::Slot {
    Slot(const char name[], Type type) {
        fNext = NULL;
        
        size_t len = strlen(name);
        char* str = new char[len + 2];
        str[0] = (char)type;
        memcpy(str + 1, name, len + 1);
        fName = str;
        
        // fValue is uninitialized
    }
    ~Slot();
    
    const char* name() const {
        return fName ? &fName[1] : "";
    }
    
    Type type() const {
        return (Type)fName[0];
    }
    
    Slot*   fNext;
    char*   fName;    // fName[0] is the type
    union {
        SkJSON::Object* fObject;
        SkJSON::Array*  fArray;
        char*           fString;
        int32_t         fInt;
        float           fFloat;
        bool            fBool;
        intptr_t        fIntPtr;    // for generic getter
    } fValue;
};

SkJSON::Object::Slot::~Slot() {
    switch (this->type()) {
        case kObject:
            delete fValue.fObject;
            break;
        case kArray:
        case kString:
            delete[] fValue.fString;
            break;
        default:
            break;
    }
    delete[] fName;
}

///////////////////////////////////////////////////////////////////////////////

SkJSON::Object::~Object() {
    Slot* slot = fHead;
    while (slot) {
        Slot* next = slot->fNext;
        delete slot;
        slot = next;
    }
}

SkJSON::Object::Slot* SkJSON::Object::addSlot(Slot* slot) {
    SkASSERT(NULL == slot->fNext);
    if (NULL == fHead) {
        SkASSERT(NULL == fTail);
        fHead = fTail = slot;
    } else {
        SkASSERT(fTail);
        SkASSERT(NULL == fTail->fNext);
        fTail->fNext = slot;
        fTail = slot;
    }
}

void SkJSON::Object::addObject(const char name[], SkJSON::Object* value) {
    Slot* slot = addSlot(new Slot(name, kObject));
    fTail->fValue.fObject = value;
}

void SkJSON::Object::addArray(const char name[], SkJSON::Array* value) {
    Slot* slot = addSlot(new Slot(name, kArray));
    fTail->fValue.fArray = value;
}

void SkJSON::Object::addString(const char name[], const char value[]) {
    Slot* slot = addSlot(new Slot(name, kString));
    size_t len = strlen(value);
    char* str = new char[len + 1];
    memcpy(str, value, len + 1);
    slot->fValue.fString = str;
}

void SkJSON::Object::addInt(const char name[], int32_t value) {
    Slot* slot = addSlot(new Slot(name, kInt));
    fTail->fValue.fInt = value;
}

void SkJSON::Object::addFloat(const char name[], float value) {
    Slot* slot = addSlot(new Slot(name, kFloat));
    fTail->fValue.fFloat = value;
}

void SkJSON::Object::addBool(const char name[], bool value) {
    Slot* slot = addSlot(new Slot(name, kBool));
    fTail->fValue.fBool = value;
}

///////////////////////////////////////////////////////////////////////////////

const SkJSON::Object::Slot* SkJSON::Object::findSlot(const char name[]) const {
    for (const Slot* slot = fHead; slot; slot = slot->fNext) {
        if (!strcmp(slot->name(), name)) {
            return slot;
        }
    }
    return NULL;
}

const SkJSON::Object::Slot* SkJSON::Object::findSlotAndType(const char name[],
                                                        Type t) const {
    const Slot* slot = this->findSlot(name);
    if (slot && (slot->type() != t)) {
        slot = NULL;
    }
    return slot;
}

bool SkJSON::Object::findString(const char name[], SkString* value) const {
    const Slot* slot = this->findSlotAndType(name, kString);
    if (slot) {
        if (value) {
            value->set(slot->fValue.fString);
        }
        return true;
    }
    return false;
}

bool SkJSON::Object::findInt(const char name[], int32_t* value) const {
    const Slot* slot = this->findSlotAndType(name, kInt);
    if (slot) {
        if (value) {
            *value = slot->fValue.fInt;
        }
        return true;
    }
    return false;
}

bool SkJSON::Object::findFloat(const char name[], float* value) const {
    const Slot* slot = this->findSlotAndType(name, kFloat);
    if (slot) {
        if (value) {
            *value = slot->fValue.fFloat;
        }
        return true;
    }
    return false;
}

bool SkJSON::Object::findBool(const char name[], bool* value) const {
    const Slot* slot = this->findSlotAndType(name, kBool);
    if (slot) {
        if (value) {
            *value = slot->fValue.fBool;
        }
        return true;
    }
    return false;
}

bool SkJSON::Object::findObject(const char name[], SkJSON::Object** value) const {
    const Slot* slot = this->findSlotAndType(name, kObject);
    if (slot) {
        if (value) {
            *value = slot->fValue.fObject;
        }
        return true;
    }
    return false;
}

///////////////////////////////////////////////////////////////////////////////

static void tabForLevel(int level) {
    for (int i = 0; i < level; ++i) {
        SkDebugf("    ");
    }
}

void SkJSON::Object::dump() const {
    SkDebugf("{\n");
    this->dumpLevel(0);
    SkDebugf("}\n");
}

void SkJSON::Object::dumpLevel(int level) const {
    for (Slot* slot = fHead; slot; slot = slot->fNext) {
        Type t = slot->type();        
        tabForLevel(level + 1);
        SkDebugf("\"%s\" : ", slot->name());
        switch (slot->type()) {
            case kObject:
                if (slot->fValue.fObject) {
                    SkDebugf("{\n");
                    slot->fValue.fObject->dumpLevel(level + 1);
                    tabForLevel(level + 1);
                    SkDebugf("}");
                } else {
                    SkDebugf("null");
                }
                break;
            case kArray:
                if (slot->fValue.fArray) {
                    SkDebugf("[");
                    slot->fValue.fArray->dumpLevel(level + 1);
                    SkDebugf("]");
                } else {
                    SkDebugf("null");
                }
                break;
            case kString:
                SkDebugf("\"%s\"", slot->fValue.fString);
                break;
            case kInt:
                SkDebugf("%d", slot->fValue.fInt);
                break;
            case kFloat:
                SkDebugf("%g", slot->fValue.fFloat);
                break;
            case kBool:
                SkDebugf("%s", slot->fValue.fBool ? "true" : "false");
                break;
            default:
                SkASSERT(!"how did I get here");
                break;
        }
        if (slot->fNext) {
            SkDebugf(",");
        }
        SkDebugf("\n");
    }
}

void SkJSON::Array::dumpLevel(int level) const {
    if (0 == fCount) {
        return;
    }
    int last = fCount - 1;

    switch (this->type()) {
        case kInt: {
            for (int i = 0; i < last; ++i) {
                SkDebugf(" %d,", fArray.fInts[i]);
            }
            SkDebugf(" %d ", fArray.fInts[last]);
        } break;
        case kFloat: {
            for (int i = 0; i < last; ++i) {
                SkDebugf(" %g,", fArray.fFloats[i]);
            }
            SkDebugf(" %g ", fArray.fFloats[last]);
        } break;
        case kBool: {
            for (int i = 0; i < last; ++i) {
                SkDebugf(" %s,", fArray.fBools[i] ? "true" : "false");
            }
            SkDebugf(" %s ", fArray.fInts[last] ? "true" : "false");
        } break;
        default:
            SkASSERT(!"unsupported array type");
            break;
    }
}

///////////////////////////////////////////////////////////////////////////////

static const uint8_t gBytesPerType[] = {
    sizeof(SkJSON::Object*),
    sizeof(SkJSON::Array*),
    sizeof(char*),
    sizeof(int32_t),
    sizeof(float),
    sizeof(bool)
};

void SkJSON::Array::init(Type type, int count, const void* src) {
    if (count < 0) {
        count = 0;
    }
    size_t size = count * gBytesPerType[type];

    fCount = count;
    fType = type;
    fArray.fVoids = sk_malloc_throw(size);
    if (src) {
        memcpy(fArray.fVoids, src, size);
    }
}

SkJSON::Array::Array(Type type, int count) {
    this->init(type, count, NULL);
}

SkJSON::Array::Array(const int32_t values[], int count) {
    this->init(kInt, count, values);
}
    
SkJSON::Array::Array(const float values[], int count) {
    this->init(kFloat, count, values);
}

SkJSON::Array::Array(const bool values[], int count) {
    this->init(kBool, count, values);
}

SkJSON::Array::Array(const Array& src) {
    this->init(src.type(), src.count(), src.fArray.fVoids);
}

SkJSON::Array::~Array() {
    sk_free(fArray.fVoids);
}