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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include "icon_tools.h"
#include "plist.h"
#include "values.h"
enum
{
R_NONE = 0,
R_RESOURCES,
R_SECTION,
R_ICON,
R_PLIST,
R_STRING,
};
typedef struct
{
gchar *tag;
gint id;
} tag_map_t;
static tag_map_t tag_map[] =
{
{"resources", R_RESOURCES},
{"section", R_SECTION},
{"icon", R_ICON},
{"plist", R_PLIST},
{"string", R_STRING},
};
#define TAG_MAP_SZ (sizeof(tag_map)/sizeof(tag_map_t))
typedef struct
{
gchar *key;
gchar *value;
GValue *plist;
GQueue *stack;
GQueue *tag_stack;
gboolean closed_top;
} parse_data_t;
static const gchar*
lookup_attr_value(
const gchar *name,
const gchar **attr_names,
const gchar **attr_values)
{
gint ii;
if (attr_names == NULL) return NULL;
for (ii = 0; attr_names[ii] != NULL; ii++)
{
if (strcmp(name, attr_names[ii]) == 0)
return attr_values[ii];
}
return NULL;
}
static GValue*
read_string_from_file(const gchar *filename)
{
gchar *buffer;
size_t size;
GValue *gval;
FILE *fd;
fd = g_fopen(filename, "r");
if (fd == NULL)
return NULL;
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
buffer = g_malloc(size+1);
size = fread(buffer, 1, size, fd);
buffer[size] = 0;
gval = ghb_value_new(G_TYPE_STRING);
g_value_take_string(gval, buffer);
fclose(fd);
return gval;
}
static void
start_element(
GMarkupParseContext *ctx,
const gchar *tag,
const gchar **attr_names,
const gchar **attr_values,
gpointer ud,
GError **error)
{
parse_data_t *pd = (parse_data_t*)ud;
union
{
gint id;
gpointer pid;
} id;
gint ii;
// Check to see if the first element found has been closed
// If so, ignore any junk following it.
if (pd->closed_top)
return;
for (ii = 0; ii < TAG_MAP_SZ; ii++)
{
if (strcmp(tag, tag_map[ii].tag) == 0)
{
id.id = tag_map[ii].id;
break;
}
}
if (ii == TAG_MAP_SZ)
{
g_warning("Unrecognized start tag (%s)", tag);
return;
}
g_queue_push_head(pd->tag_stack, id.pid);
GType gtype = 0;
GValue *gval = NULL;
GValue *current = g_queue_peek_head(pd->stack);
switch (id.id)
{
case R_SECTION:
{
const gchar *name;
name = lookup_attr_value("name", attr_names, attr_values);
if (name && strcmp(name, "icons") == 0)
{
gval = ghb_dict_value_new();
if (pd->key) g_free(pd->key);
pd->key = g_strdup(name);
g_queue_push_head(pd->stack, gval);
}
} break;
case R_ICON:
{
const gchar *filename, *name;
filename = lookup_attr_value("file", attr_names, attr_values);
name = lookup_attr_value("name", attr_names, attr_values);
if (filename && name)
{
ghb_rawdata_t *rd;
guint size;
rd = g_malloc(sizeof(ghb_rawdata_t));
rd->data = icon_file_serialize(filename, &size);
rd->size = size;
gval = ghb_rawdata_value_new(rd);
if (pd->key) g_free(pd->key);
pd->key = g_strdup(name);
}
else
{
g_warning("%s:missing a requried attribute", name);
}
} break;
case R_PLIST:
{
const gchar *filename, *name;
filename = lookup_attr_value("file", attr_names, attr_values);
name = lookup_attr_value("name", attr_names, attr_values);
if (filename && name)
{
gval = ghb_plist_parse_file(filename);
if (pd->key) g_free(pd->key);
pd->key = g_strdup(name);
}
else
{
g_warning("%s:missing a requried attribute", name);
}
} break;
case R_STRING:
{
const gchar *filename, *name;
filename = lookup_attr_value("file", attr_names, attr_values);
name = lookup_attr_value("name", attr_names, attr_values);
if (filename && name)
{
gval = read_string_from_file(filename);
if (pd->key) g_free(pd->key);
pd->key = g_strdup(name);
}
else
{
g_warning("%s:missing a requried attribute", name);
}
} break;
}
// Add the element to the current container
if (gval)
{ // There's an element to add
if (current == NULL)
{
pd->plist = gval;
return;
}
gtype = G_VALUE_TYPE(current);
if (gtype == ghb_array_get_type())
{
ghb_array_append(current, gval);
}
else if (gtype == ghb_dict_get_type())
{
if (pd->key == NULL)
{
g_warning("No key for dictionary item");
ghb_value_free(gval);
}
else
{
ghb_dict_insert(current, g_strdup(pd->key), gval);
}
}
else
{
g_error("Invalid container type. This shouldn't happen");
}
}
}
static void
end_element(
GMarkupParseContext *ctx,
const gchar *name,
gpointer ud,
GError **error)
{
parse_data_t *pd = (parse_data_t*)ud;
gint id;
union
{
gint id;
gpointer pid;
} start_id;
gint ii;
// Check to see if the first element found has been closed
// If so, ignore any junk following it.
if (pd->closed_top)
return;
for (ii = 0; ii < TAG_MAP_SZ; ii++)
{
if (strcmp(name, tag_map[ii].tag) == 0)
{
id = tag_map[ii].id;
break;
}
}
if (ii == TAG_MAP_SZ)
{
g_warning("Unrecognized start tag (%s)", name);
return;
}
start_id.pid = g_queue_pop_head(pd->tag_stack);
if (start_id.id != id)
g_warning("start tag != end tag: (%s %d) %d", name, id, id);
GValue *gval = NULL;
GValue *current = g_queue_peek_head(pd->stack);
GType gtype = 0;
switch (id)
{
case R_SECTION:
{
g_queue_pop_head(pd->stack);
} break;
}
if (gval)
{
// Get the top of the data structure stack and if it's an array
// or dict, add the current element
if (current == NULL)
{
pd->plist = gval;
pd->closed_top = TRUE;
return;
}
gtype = G_VALUE_TYPE(current);
if (gtype == ghb_array_get_type())
{
ghb_array_append(current, gval);
}
else if (gtype == ghb_dict_get_type())
{
if (pd->key == NULL)
{
g_warning("No key for dictionary item");
ghb_value_free(gval);
}
else
{
ghb_dict_insert(current, g_strdup(pd->key), gval);
}
}
else
{
g_error("Invalid container type. This shouldn't happen");
}
}
if (g_queue_is_empty(pd->tag_stack))
pd->closed_top = TRUE;
}
static void
text_data(
GMarkupParseContext *ctx,
const gchar *text,
gsize len,
gpointer ud,
GError **error)
{
parse_data_t *pd = (parse_data_t*)ud;
if (pd->value) g_free(pd->value);
pd->value = g_strdup(text);
}
static void
passthrough(
GMarkupParseContext *ctx,
const gchar *text,
gsize len,
gpointer ud,
GError **error)
{
//parse_data_t *pd = (parse_data_t*)ud;
//g_debug("passthrough %s", text);
}
static void
parse_error(GMarkupParseContext *ctx, GError *error, gpointer ud)
{
g_warning("Resource parse error: %s", error->message);
}
// This is required or the parser crashes
static void
destroy_notify(gpointer data)
{ // Do nothing
//g_debug("destroy parser");
}
GValue*
ghb_resource_parse(const gchar *buf, gssize len)
{
GMarkupParseContext *ctx;
GMarkupParser parser;
parse_data_t pd;
GError *err = NULL;
pd.stack = g_queue_new();
pd.tag_stack = g_queue_new();
pd.key = NULL;
pd.value = NULL;
pd.plist = ghb_dict_value_new();
g_queue_push_head(pd.stack, pd.plist);
pd.closed_top = FALSE;
parser.start_element = start_element;
parser.end_element = end_element;
parser.text = text_data;
parser.passthrough = passthrough;
parser.error = parse_error;
ctx = g_markup_parse_context_new(&parser, 0, &pd, destroy_notify);
g_markup_parse_context_parse(ctx, buf, len, &err);
g_markup_parse_context_end_parse(ctx, &err);
g_markup_parse_context_free(ctx);
g_queue_free(pd.stack);
g_queue_free(pd.tag_stack);
return pd.plist;
}
GValue*
ghb_resource_parse_file(FILE *fd)
{
gchar *buffer;
size_t size;
GValue *gval;
if (fd == NULL)
return NULL;
fseek(fd, 0, SEEK_END);
size = ftell(fd);
fseek(fd, 0, SEEK_SET);
buffer = g_malloc(size+1);
size = fread(buffer, 1, size, fd);
buffer[size] = 0;
gval = ghb_resource_parse(buffer, (gssize)size);
g_free(buffer);
return gval;
}
gint
main(gint argc, gchar *argv[])
{
FILE *file;
GValue *gval;
if (argc < 3)
{
fprintf(stderr, "Usage: <in resource list> <out resource plist>\n");
return 1;
}
g_type_init();
file = g_fopen(argv[1], "r");
if (file == NULL)
return 1;
gval = ghb_resource_parse_file(file);
ghb_plist_write_file(argv[2], gval);
return 0;
}
|