-
Notifications
You must be signed in to change notification settings - Fork 17
/
json_write.c
253 lines (222 loc) · 8.04 KB
/
json_write.c
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
/***************************************************************************
* Original Diku Mud copyright (C) 1990, 1991 by Sebastian Hammer, *
* Michael Seifert, Hans Henrik Strfeldt, Tom Madsen, and Katja Nyboe. *
* *
* Merc Diku Mud improvments copyright (C) 1992, 1993 by Michael *
* Chastain, Michael Quan, and Mitchell Tse. *
* *
* In order to use any part of this Merc Diku Mud, you must comply with *
* both the original Diku license in 'license.doc' as well the Merc *
* license in 'license.txt'. In particular, you may not remove either of *
* these copyright notices. *
* *
* Much time and thought has gone into this software and you are *
* benefitting. We hope that you share your changes too. What goes *
* around, comes around. *
***************************************************************************/
/***************************************************************************
* ROM 2.4 is copyright 1993-1998 Russ Taylor *
* ROM has been brought to you by the ROM consortium *
* Russ Taylor ([email protected]) *
* Gabrielle Taylor ([email protected]) *
* Brian Moore ([email protected]) *
* By using this code, you have agreed to follow the terms of the *
* ROM license, in the file Rom24/doc/rom.license *
***************************************************************************/
#include "json_write.h"
#include "json.h"
#include "utils.h"
#include <sys/stat.h>
#include <string.h>
static int json_indent_level = 0;
static int json_nest_level = 0;
static int json_indented = 0;
#define INDENT_SIZE 2
static void json_print_indent (FILE *fp) {
static int last_indent = 0;
static char space_buf[256];
int i;
if (json_indented)
return;
while (last_indent < json_indent_level) {
int pos = last_indent * INDENT_SIZE;
for (i = 0; i < INDENT_SIZE; i++)
space_buf[pos++] = ' ';
space_buf[pos] = '\0';
last_indent++;
}
if (last_indent > json_indent_level) {
space_buf[json_indent_level * INDENT_SIZE] = '\0';
last_indent = json_indent_level;
}
fwrite (space_buf, sizeof (char), json_indent_level * INDENT_SIZE, fp);
json_indented = 1;
}
static void json_next_line (FILE *fp) {
fputc ('\n', fp);
json_indented = 0;
}
const char *json_escaped_string (const char *value, int newline_pos) {
static char buf[MAX_STRING_LENGTH * 4];
char *pos = buf;
*pos = '\0';
while (*value != '\0') {
switch (*value) {
case '\b': strcat (pos, "\\b"); pos += 2; break;
case '\f': strcat (pos, "\\f"); pos += 2; break;
case '\t': strcat (pos, "\\t"); pos += 2; break;
case '"': strcat (pos, "\\\""); pos += 2; break;
case '\\': strcat (pos, "\\\\"); pos += 2; break;
case '\n': {
#ifdef BASEMUD_WRITE_EXTENDED_JSON
const char *next_ch = value + 1;
while (*next_ch == '\r' && *next_ch != '\0')
next_ch++;
pos += sprintf (pos, "\n%*c", newline_pos + 1, '|');
#else
strcat (pos, "\\n");
pos += 2;
#endif
break;
}
case '\r':
break;
default:
*(pos++) = *value;
*pos = '\0';
break;
}
++value;
}
return buf;
}
void json_print_real (JSON_T *json, FILE *fp, int new_line) {
int newline_pos;
json_print_indent (fp);
newline_pos = INDENT_SIZE * json_indent_level;
if (json_nest_level > 0 && json->parent->type != JSON_ARRAY) {
const char *json_name = json->name
? json_escaped_string (json->name, newline_pos) : "NULL";
fputc ('"', fp);
fputs (json_name, fp);
fputs ("\": ", fp);
newline_pos += 4 + strlen (json_name);
}
switch (json->type) {
case JSON_DICE:
case JSON_STRING: {
char *value = (char *) json->value;
fputc ('"', fp);
fputs (json_escaped_string (value, newline_pos), fp);
fputc ('"', fp);
break;
}
case JSON_NUMBER: {
json_num *value = (json_num *) json->value;
fprintf (fp, "%g", *value);
break;
}
case JSON_INTEGER: {
json_int *value = (json_int *) json->value;
fprintf (fp, "%ld", *value);
break;
}
case JSON_BOOLEAN: {
bool *value = (bool *) json->value;
fputs (*value ? "true" : "false", fp);
break;
}
case JSON_NULL:
fputs ("null", fp);
break;
case JSON_OBJECT:
case JSON_ARRAY: {
JSON_T *j;
char pleft = (json->type == JSON_OBJECT) ? '{' : '[';
char pright = (json->type == JSON_OBJECT) ? '}' : ']';
if (json->first_child == NULL) {
fputc (pleft, fp);
fputc (pright, fp);
}
else if (json->child_count == 1) {
fputc (pleft, fp);
json_nest_level++;
json_print_real (json->first_child, fp, 0);
json_nest_level--;
fputc (pright, fp);
}
else {
fputc (pleft, fp);
json_next_line (fp);
json_indent_level++;
json_nest_level++;
for (j = json->first_child; j != NULL; j = j->next)
json_print (j, fp);
json_nest_level--;
json_indent_level--;
json_print_indent (fp);
fputc (pright, fp);
}
break;
}
default:
bugf ("json_print(): Unhandled type %d", json->type);
fputs ("BAD-TYPE", fp);
break;
}
if (json->next)
fputc (',', fp);
#define IS_SIMPLE_TYPE(x) \
((x)->type == JSON_NULL || (x)->type == JSON_NUMBER || \
(x)->type == JSON_INTEGER || ( \
(x)->type == JSON_STRING && strlen((char *) (x)->value) <= 15))
if (new_line) {
if (json->parent && json->parent->type == JSON_ARRAY && json->next &&
IS_SIMPLE_TYPE(json) && IS_SIMPLE_TYPE(json->next))
fputc (' ', fp);
else
json_next_line (fp);
}
#undef IS_SIMPLE_TYPE
}
void json_print (JSON_T *json, FILE *fp) {
json_print_real (json, fp, 1);
}
void json_fwrite (JSON_T *json, const char *filename) {
FILE *fp = fopen (filename, "w");
BAIL_IF_BUGF (fp == NULL,
"json_fwrite(): Couldn't open '%s' for writing", filename);
json_print (json, fp);
fclose (fp);
}
int json_mkdir (const char *dir) {
const char *last_slash, *end;
if (dir == NULL || dir[0] == '\0')
return 0;
last_slash = strrchr (dir, '/');
end = last_slash ? (last_slash + 1) : dir;
if (!strcmp (end, ".") || !strcmp (end, ".."))
return 0;
#if defined(__MINGW32__)
return mkdir (dir) == 0;
#else
return mkdir (dir, 0777) == 0;
#endif
}
int json_mkdir_to (const char *filename) {
const char *pos, *next;
char buf[256];
int len = 0, made = 0;
buf[0] = '\0';
pos = filename;
while (pos != NULL && *pos != '\0') {
next = strchr (pos, '/');
if (!next)
break;
len += snprintf (buf + len, sizeof (buf) - len, "%s%.*s",
(buf[0] == '\0') ? "" : "/", (int) (next - pos), pos);
json_mkdir (buf);
pos = next + 1;
}
return made;
}