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
|
#include <orcus/stream.hpp>
#include <orcus/json_parser.hpp>
#include <orcus/json_parser_thread.hpp>
#include <orcus/string_pool.hpp>
#include <vector>
#include <iostream>
#include <stdio.h>
#include <string>
#include <sys/time.h>
#define SIMULATE_PROCESSING_OVERHEAD 0
using namespace std;
using namespace orcus;
namespace {
class stack_printer
{
public:
explicit stack_printer(const char* msg) :
m_msg(msg)
{
fprintf(stdout, "%s: --begin\n", m_msg.c_str());
m_start_time = get_time();
}
~stack_printer()
{
double end_time = get_time();
fprintf(stdout, "%s: --end (duration: %g sec)\n", m_msg.c_str(), (end_time-m_start_time));
}
private:
double get_time() const
{
timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec / 1000000.0;
}
::std::string m_msg;
double m_start_time;
};
}
class handler
{
string_pool m_pool;
json::parse_tokens_t m_tokens;
std::vector<double> m_results;
void do_work()
{
#if SIMULATE_PROCESSING_OVERHEAD
double f = m_results.empty() ? 0.0 : m_results.back();
for (size_t i = 0; i < 1000; ++i)
f += 0.1;
m_results.push_back(f);
#endif
}
public:
void begin_parse()
{
m_tokens.emplace_back(json::parse_token_t::begin_parse);
do_work();
}
void end_parse()
{
m_tokens.emplace_back(json::parse_token_t::end_parse);
do_work();
}
void begin_array()
{
m_tokens.emplace_back(json::parse_token_t::begin_array);
do_work();
}
void end_array()
{
m_tokens.emplace_back(json::parse_token_t::end_array);
do_work();
}
void begin_object()
{
m_tokens.emplace_back(json::parse_token_t::begin_object);
do_work();
}
void object_key(std::string_view s, bool transient)
{
if (transient)
s = m_pool.intern(s).first;
m_tokens.emplace_back(json::parse_token_t::object_key, s);
do_work();
}
void end_object()
{
m_tokens.emplace_back(json::parse_token_t::end_object);
do_work();
}
void boolean_true()
{
m_tokens.emplace_back(json::parse_token_t::boolean_true);
do_work();
}
void boolean_false()
{
m_tokens.emplace_back(json::parse_token_t::boolean_false);
do_work();
}
void null()
{
m_tokens.emplace_back(json::parse_token_t::null);
do_work();
}
void string(std::string_view s, bool transient)
{
if (transient)
s = m_pool.intern(s).first;
m_tokens.emplace_back(json::parse_token_t::string, s);
do_work();
}
void number(double val)
{
m_tokens.emplace_back(val);
do_work();
}
size_t token_size() const
{
return m_tokens.size();
}
double work_value() const
{
return m_results.back();
}
};
int main(int argc, char** argv) try
{
if (argc < 2)
return EXIT_FAILURE;
const char* filepath = argv[1];
orcus::file_content content(filepath);
cout << "file: " << filepath << endl;
handler hdl;
{
stack_printer __stack_printer__("parsing");
orcus::json_parser<handler> parser(content.str(), hdl);
parser.parse();
}
cout << "parsed token count: " << hdl.token_size() << endl;
#if SIMULATE_PROCESSING_OVERHEAD
cout << "work value: " << hdl.work_value() << endl;
#endif
return EXIT_SUCCESS;
}
catch (const std::exception& e)
{
cerr << e.what() << endl;
return EXIT_FAILURE;
}
|