-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosrm_c_json_renderer.cpp
More file actions
237 lines (219 loc) · 5.43 KB
/
osrm_c_json_renderer.cpp
File metadata and controls
237 lines (219 loc) · 5.43 KB
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
#include "osrm_c.h"
#include <stack>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
// copied from osrm-backend
std::string escape_JSON(const std::string &input)
{
// escape and skip reallocations if possible
std::string output;
output.reserve(input.size() + 4); // +4 assumes two backslashes on avg
for (const char letter : input)
{
switch (letter)
{
case '\\':
output += "\\\\";
break;
case '"':
output += "\\\"";
break;
case '/':
output += "\\/";
break;
case '\b':
output += "\\b";
break;
case '\f':
output += "\\f";
break;
case '\n':
output += "\\n";
break;
case '\r':
output += "\\r";
break;
case '\t':
output += "\\t";
break;
default:
output.append(1, letter);
break;
}
}
return output;
}
extern "C" {
enum Status {
ERROR,
VALUE, // waiting any value
OBJECT, // at clean state of object
OBJECT_HALF, // object with only key filled
OBJECT_CONTINUE, // object with some pairs
ARRAY, // at clean state of array
ARRAY_CONTINUE, // array with some elements
};
struct osrm_json_renderer_t {
explicit osrm_json_renderer_t() {
stack_.push(VALUE);
}
std::ostringstream stream_;
std::stack<Status> stack_;
};
unsigned char ValueSwitch(osrm_json_renderer_t* json_state) {
// used for anything that's capable to be a value
if (json_state->stack_.empty()) {
json_state->stack_.push(ERROR);
return false;
}
switch (json_state->stack_.top()) {
case VALUE:
json_state->stack_.pop();
break;
case OBJECT_HALF:
json_state->stream_ << ':';
json_state->stack_.pop();
json_state->stack_.push(OBJECT_CONTINUE);
break;
case ARRAY:
json_state->stack_.pop();
json_state->stack_.push(ARRAY_CONTINUE);
break;
case ARRAY_CONTINUE:
json_state->stream_ << ',';
break;
case OBJECT_CONTINUE:
case OBJECT:
json_state->stack_.push(ERROR);
case ERROR:
return false;
}
return true;
}
void push_object(void* state) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (ValueSwitch(json_state)) {
json_state->stream_ << '{';
json_state->stack_.push(OBJECT);
}
}
void push_array(void* state) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (ValueSwitch(json_state)) {
json_state->stream_ << '[';
json_state->stack_.push(ARRAY);
}
}
void pop(void* state) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (json_state->stack_.empty()) {
json_state->stack_.push(ERROR);
return;
}
switch (json_state->stack_.top()) {
case OBJECT:
case OBJECT_CONTINUE:
json_state->stream_ << '}';
break;
case ARRAY:
case ARRAY_CONTINUE:
json_state->stream_ << ']';
break;
case VALUE:
case OBJECT_HALF:
json_state->stack_.push(ERROR);
case ERROR:
return;
}
json_state->stack_.pop();
}
void append_string(void* state, const char* data, size_t size) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (json_state->stack_.empty()) {
json_state->stack_.push(ERROR);
return;
}
switch (json_state->stack_.top()) {
case VALUE:
json_state->stack_.pop();
break;
case OBJECT_CONTINUE:
json_state->stream_ << ',';
case OBJECT:
json_state->stack_.pop();
json_state->stack_.push(OBJECT_HALF);
break;
case OBJECT_HALF:
json_state->stream_ << ':';
json_state->stack_.pop();
json_state->stack_.push(OBJECT_CONTINUE);
break;
case ARRAY:
json_state->stack_.pop();
json_state->stack_.push(ARRAY_CONTINUE);
break;
case ARRAY_CONTINUE:
json_state->stream_ << ',';
break;
case ERROR:
return;
}
const std::string value(data, size);
json_state->stream_ << '\"';
json_state->stream_ << escape_JSON(value);
json_state->stream_ << '\"';
}
void append_number(void* state, double value) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (ValueSwitch(json_state)) {
json_state->stream_.precision(10);
json_state->stream_ << value;
}
}
void append_bool(void* state, unsigned char value) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (ValueSwitch(json_state)) {
json_state->stream_ << (value?"true":"false");
}
}
void append_null(void* state) {
osrm_json_renderer_t* json_state = (osrm_json_renderer_t*)state;
if (ValueSwitch(json_state)) {
json_state->stream_ << "null";
}
}
osrm_json_renderer_t* osrm_json_renderer_create() {
return new osrm_json_renderer_t;
}
void osrm_json_renderer_destroy(osrm_json_renderer_t* renderer) {
delete renderer;
}
size_t osrm_json_renderer_harvest(
osrm_json_renderer_t* renderer, char** dataptr) {
assert(dataptr != NULL);
assert(*dataptr == NULL);
size_t size = 0;
if (renderer->stack_.empty()) {
std::string value = renderer->stream_.str();
size = value.size();
*dataptr = (char*)malloc(size);
memcpy(*dataptr, value.data(), size);
}
return size;
}
osrm_json_handler_t* osrm_json_renderer_create_handler(
osrm_json_renderer_t* renderer) {
return osrm_json_handler_create(
(void*)renderer,
&push_object,
&push_array,
&pop,
&append_string,
&append_number,
&append_bool,
&append_null);
}
}