~ppsspp/ppsspp/ppsspp_1.3.0

« back to all changes in this revision

Viewing changes to ext/native/ext/vjson/main.cpp

  • Committer: Sérgio Benjamim
  • Date: 2017-01-02 00:12:05 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20170102001205-cxbta9za203nmjwm
1.3.0 source (from ppsspp_1.3.0-r160.p5.l1762.a165.t83~56~ubuntu16.04.1.tar.xz).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <vector>
 
2
#include <stdio.h>
 
3
#include "json.h"
 
4
 
 
5
void populate_sources(const char *filter, std::vector<std::vector<char> > &sources)
 
6
{
 
7
        char filename[256];
 
8
        for (int i = 1; i < 64; ++i)
 
9
        {
 
10
                sprintf(filename, filter, i);
 
11
                FILE *fp = fopen(filename, "rb");
 
12
                if (fp)
 
13
                {
 
14
                        fseek(fp, 0, SEEK_END);
 
15
                        int size = ftell(fp);
 
16
                        fseek(fp, 0, SEEK_SET);
 
17
                        std::vector<char> buffer(size + 1);
 
18
                        fread (&buffer[0], 1, size, fp);
 
19
                        fclose(fp);
 
20
                        sources.push_back(buffer);
 
21
                }
 
22
                else
 
23
                {
 
24
                        break;
 
25
                }
 
26
        }
 
27
 
 
28
        printf("Loaded %d json files\n", sources.size());
 
29
}
 
30
 
 
31
#define IDENT(n) for (int i = 0; i < n; ++i) printf("    ")
 
32
 
 
33
void print(json_value *value, int ident = 0)
 
34
{
 
35
        IDENT(ident);
 
36
        if (value->name) printf("\"%s\" = ", value->name);
 
37
        switch(value->type)
 
38
        {
 
39
        case JSON_NULL:
 
40
                printf("null\n");
 
41
                break;
 
42
        case JSON_OBJECT:
 
43
        case JSON_ARRAY:
 
44
                printf(value->type == JSON_OBJECT ? "{\n" : "[\n");
 
45
                for (json_value *it = value->first_child; it; it = it->next_sibling)
 
46
                {
 
47
                        print(it, ident + 1);
 
48
                }
 
49
                IDENT(ident);
 
50
                printf(value->type == JSON_OBJECT ? "}\n" : "]\n");
 
51
                break;
 
52
        case JSON_STRING:
 
53
                printf("\"%s\"\n", value->string_value);
 
54
                break;
 
55
        case JSON_INT:
 
56
                printf("%d\n", value->int_value);
 
57
                break;
 
58
        case JSON_FLOAT:
 
59
                printf("%f\n", value->float_value);
 
60
                break;
 
61
        case JSON_BOOL:
 
62
                printf(value->int_value ? "true\n" : "false\n");
 
63
                break;
 
64
        }
 
65
}
 
66
 
 
67
bool parse(char *source)
 
68
{
 
69
        char *errorPos = 0;
 
70
        char *errorDesc = 0;
 
71
        int errorLine = 0;
 
72
        block_allocator allocator(1 << 10);
 
73
        
 
74
        json_value *root = json_parse(source, &errorPos, &errorDesc, &errorLine, &allocator);
 
75
        if (root)
 
76
        {
 
77
                print(root);
 
78
                return true;
 
79
        }
 
80
 
 
81
        printf("Error at line %d: %s\n%s\n\n", errorLine, errorDesc, errorPos);
 
82
        return false;
 
83
}
 
84
 
 
85
int main(int argc, char **argv)
 
86
{
 
87
        // Fail
 
88
        printf("===FAIL===\n\n");
 
89
        std::vector<std::vector<char> > sources;
 
90
        populate_sources("test/fail%d.json", sources);
 
91
        int passed = 0;
 
92
        for (size_t i = 0; i < sources.size(); ++i)
 
93
        {
 
94
                printf("Parsing %d\n", i + 1);
 
95
                if (parse(&sources[i][0]))
 
96
                {
 
97
                        ++passed;
 
98
                }
 
99
        }
 
100
        printf("Passed %d from %d tests\n", passed, sources.size());
 
101
 
 
102
        // Pass
 
103
        sources.clear();
 
104
        printf("\n===PASS===\n\n");
 
105
        populate_sources("test/pass%d.json", sources);
 
106
        passed = 0;
 
107
        for (size_t i = 0; i < sources.size(); ++i)
 
108
        {
 
109
                printf("Parsing %d\n", i + 1);
 
110
                if (parse(&sources[i][0]))
 
111
                {
 
112
                        ++passed;
 
113
                }
 
114
        }
 
115
        printf("Passed %d from %d tests\n", passed, sources.size());
 
116
 
 
117
        return 0;
 
118
}