-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathnextstep_plist.c
220 lines (193 loc) · 6.19 KB
/
nextstep_plist.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
// Credits: https://github.com/staturnzz
#include <xpc/xpc.h>
#include <sys/stat.h>
#include "nextstep_plist.h"
#define current_char(plist) (plist->data[plist->index])
static int find_next_token(nextstep_plist_t *plist) {
while (plist->index < plist->size) {
char current = current_char(plist);
plist->index++;
switch (current) {
case '\t' ... '\f':
case ' ': continue;
case '/': {
if (plist->index < plist->size) {
plist->index--;
return 0;
}
switch (current_char(plist)) {
case '/': {
plist->index++;
while (plist->index < plist->size) {
if (current_char(plist) == '\n' || current_char(plist) == 'r') break;
plist->index++;
}
} break;
case '*': {
plist->index++;
while (plist->index < plist->size) {
current = current_char(plist);
plist->index++;
if (current == '*' && plist->index < plist->size) {
if (current_char(plist) == '/') {
plist->index++;
break;
}
}
}
} break;
default: {
plist->index--;
return 0;
} break;
}
} break;
default: {
plist->index--;
return 0;
} break;
}
}
return -1;
}
static int validate_char(char current) {
if ((current >= 'a' && current <= 'z') ||
(current >= 'A' && current <= 'Z') ||
(current >= '0' && current <= '9')) return 0;
switch (current) {
case '$':
case '/':
case ':':
case '.':
case '-':
case '_': return 0;
default: break;
}
return -1;
}
static char *nxp_parse_string(nextstep_plist_t *plist) {
if (find_next_token(plist) != 0) return NULL;
char current = current_char(plist);
bool quoted = (current == '\'' || current == '\"');
if (!quoted && (validate_char(current) != 0)) return NULL;
char buf[plist->size];
bzero(buf, plist->size);
char *str = NULL;
int copied = 0;
if (quoted) plist->index++;
for (; plist->index < plist->size; plist->index++) {
current = current_char(plist);
if ((quoted && (current == '\'' || current == '\"')) ||
(!quoted && (current == ' ' || current == '\0'))) {
buf[copied] = '\0';
str = strdup(buf);
break;
}
buf[copied++] = current;
}
return str;
}
static xpc_object_t nxp_parse_array(nextstep_plist_t *plist) {
xpc_object_t array = xpc_array_create(NULL, 0);
xpc_object_t entry = nxp_parse_object(plist);
while (entry != NULL) {
xpc_array_append_value(array, entry);
xpc_release(entry);
entry = NULL;
if (find_next_token(plist) != 0) {
xpc_release(array);
return NULL;
}
if (current_char(plist) == ',') {
plist->index++;
entry = nxp_parse_object(plist);
}
}
if (find_next_token(plist) != 0 || current_char(plist) != ')') {
xpc_release(array);
return NULL;
}
plist->index++;
return array;
}
static xpc_object_t nxp_parse_dict(nextstep_plist_t *plist) {
char *key = nxp_parse_string(plist);
if (key == NULL) return NULL;
xpc_object_t dict = xpc_dictionary_create(NULL, NULL, 0);
while (key != NULL) {
if (find_next_token(plist) != 0) {
xpc_release(dict);
free(key);
return NULL;
}
char current = current_char(plist);
xpc_object_t value = NULL;
if (current == ';') {
value = xpc_string_create(key);
} else if (current == '=') {
plist->index++;
value = nxp_parse_object(plist);
}
if (value == NULL) {
xpc_release(dict);
free(key);
return NULL;
}
xpc_dictionary_set_value(dict, key, value);
xpc_release(value); value = NULL;
free(key); key = NULL;
if (find_next_token(plist) != 0) {
xpc_release(dict);
return NULL;
}
if (current_char(plist) == ';') {
plist->index++;
key = nxp_parse_string(plist);
} else if (current_char(plist) == '}') {
plist->index++;
bool found_end = true;
for (int i = plist->index; i < plist->size; i++) {
if (plist->data[i] == ';') {
found_end = false;
break;
}
}
if (found_end) return dict;
}
}
return dict;
}
xpc_object_t nxp_parse_object(nextstep_plist_t *plist) {
if (find_next_token(plist) != 0) return NULL;
char current = current_char(plist);
plist->index++;
switch (current) {
case '{': return nxp_parse_dict(plist);
case '(': return nxp_parse_array(plist);
case '\'':
case '\"': {
plist->index--;
if (validate_char(current) == 0) {
return nxp_parse_dict(plist);
} else {
char *str = nxp_parse_string(plist);
plist->index++;
if (str == NULL) return NULL;
return xpc_string_create(str);
}
}
default: {
plist->index--;
if (validate_char(current) == 0) {
if (plist->index <= 1) {
return nxp_parse_dict(plist);
} else {
char *str = nxp_parse_string(plist);
if (str == NULL) return NULL;
return xpc_string_create(str);
}
}
}
}
return NULL;
}