-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.c
More file actions
210 lines (178 loc) · 4.09 KB
/
Copy pathmethods.c
File metadata and controls
210 lines (178 loc) · 4.09 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
#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
#include "stdarg.h"
#include "string.h"
#include "errno.h"
#include "nano/json.h"
/* ------------------------------------------------------------------------ */
jsn_t *json_item(jsn_t *obj, char const *id)
{
if (obj->type != JS_OBJECT)
return errno = ENOTDIR, NULL;
json_foreach(obj, index)
if (!strcmp(id, obj[index].id.string))
return obj + index;
return errno = ENOENT, NULL;
}
/* ------------------------------------------------------------------------ */
jsn_t *json_cell(jsn_t *obj, int index)
{
if (obj->type != JS_ARRAY)
return errno = ENOTDIR, NULL;
json_foreach(obj, i)
if (index == obj[i].id.number)
return obj + i;
return errno = ENOENT, NULL;
}
/* ------------------------------------------------------------------------ */
int json_boolean(jsn_t *node, int absent)
{
if (!node)
return absent;
switch (node->type) {
default:
case JS_UNDEFINED:
case JS_NULL:
return 0;
case JS_BOOLEAN:
case JS_NUMBER:
return node->data.number ? 1 : 0;
#ifdef JSON_FLOATS
case JS_FLOAT:
return (int)round(node->data.floating) ? 1 : 0;
#endif
case JS_STRING:
return node->data.string[0] ? 1 : 0;
case JS_ARRAY:
case JS_OBJECT:
return 1;
}
return absent;
}
/* ------------------------------------------------------------------------ */
jsn_number_t json_number(jsn_t *node, jsn_number_t absent)
{
if (!node)
return absent;
switch (node->type) {
default:
case JS_UNDEFINED:
case JS_NULL:
return 0;
case JS_BOOLEAN:
case JS_NUMBER:
return node->data.number;
#ifdef JSON_FLOATS
case JS_FLOAT:;
double f = round(node->data.floating);
#ifdef JSON_64BITS_INTEGERS
return (jsn_number_t)f;
#else
return (jsn_number_t)(int64_t)f;
#endif
#endif
case JS_STRING:;
jsn_t num;
char *s = node->data.string;
if (!match_number(&s, &num))
return 0;
return json_number(&num, absent);
case JS_ARRAY:
if (node->data.length == 1)
return json_number(node + 1, absent);
case JS_OBJECT:
return 0;
}
return absent;
}
#ifdef JSON_FLOATS
double json_float(jsn_t *node, double absent)
{
if (!node)
return absent;
switch (node->type) {
default:
case JS_UNDEFINED:
case JS_NULL:
return 0.d;
case JS_BOOLEAN:
case JS_NUMBER:
return (double)node->data.number;
case JS_FLOAT:;
return node->data.floating;
case JS_STRING:;
jsn_t num;
char *s = node->data.string;
if (!*s)
return (double)0;
if (!match_number(&s, &num))
return NAN;
return json_float(&num, absent);
case JS_ARRAY:
if (node->data.length == 1)
return json_float(node + 1, absent);
case JS_OBJECT:
return 0.d;
}
return absent;
}
#endif
#ifdef JSON_64BITS_INTEGERS
#define NSB_LENGTH 16
#else
#define NSB_LENGTH 32
#endif
#define NSB_NUM 16 /* should be degree of 2 ( 2,4,8,16,32...) */
/* ------------------------------------------------------------------------ */
static char *get_number_string_buffer()
{
static char bufs[NSB_NUM][NSB_LENGTH/* string length */];
static int i = 0;
return bufs[i++ & (NSB_NUM-1)];
}
#ifdef JSON_FLOATS
/* ------------------------------------------------------------------------ */
char *float2str(char *p, char *e, double f)
{
e = p + snprintf(p, (size_t)(e-p), JSN_FLOAT_FORMAT, f);
while (e[-1] == '0' && (('0' <= e[-2] && e[-2] <= '9') || e[-2] == '.'))
--e;
*e = 0;
return e;
}
#endif
/* ------------------------------------------------------------------------ */
char const *json_string(jsn_t *node, char const *absent)
{
if (!node)
return absent;
switch (node->type) {
default:
case JS_UNDEFINED:
return "undefined";
case JS_NULL:
return "null";
case JS_BOOLEAN:
return node->data.number ? "true" : "false";
case JS_NUMBER: {
char *buf = get_number_string_buffer();
snprintf(buf, NSB_LENGTH, JSN_NUMBER_FORMAT, node->data.number);
return buf;
}
#ifdef JSON_FLOATS
case JS_FLOAT: {
char *buf = get_number_string_buffer();
float2str(buf, buf + NSB_LENGTH, node->data.floating);
return buf;
}
#endif
case JS_STRING:
return node->data.string;
case JS_ARRAY:
return "[object Array]";
case JS_OBJECT:
return "[object Object]";
}
return absent;
}