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
|
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include <cJSON.h>
#include "idlist.h"
#include "vector.h"
#include "text.h"
static char *load_data_from_file(const char *filename)
{
int ret = 0;
FILE *file = NULL;
char *data = NULL;
file = fopen(filename, "rb");
if (file == NULL) {
perror(filename);
return NULL;
}
ret |= fseek(file, 0, SEEK_END);
long file_size = ftell(file);
ret |= fseek(file, 0, SEEK_SET);
if (ret == -1 || file_size == -1) {
perror("fseek/ftell");
goto cleanup_close;
}
data = calloc(file_size + 1, sizeof(char));
if (data == NULL) {
perror("calloc");
goto cleanup_close;
}
size_t nbyte = fread(data, sizeof(char), file_size, file);
if (nbyte == 0) {
perror("fread");
goto cleanup_free;
}
goto cleanup_close;
cleanup_free:
free(data);
data = NULL;
cleanup_close:
fclose(file);
return data;
}
static char *texts_cjson_get_string(cJSON *object, const char *key)
{
char *string = NULL;
cJSON *item = cJSON_GetObjectItem(object, key);
if (cJSON_IsString(item)) {
char *value = cJSON_GetStringValue(item);
if (value != NULL) {
size_t size = strlen(value) + 1;
string = calloc(size, sizeof(char));
if (string != NULL)
strncpy(string, value, size);
}
}
return string;
}
static int texts_cjson_get_int(cJSON *object, const char *key)
{
int value = 0;
cJSON *item = cJSON_GetObjectItem(object, key);
if (cJSON_IsNumber(item))
value = item->valueint;
return value;
}
struct idlist *load_texts_from_json(const char *filename)
{
struct cJSON_Hooks hooks = (struct cJSON_Hooks){
.malloc_fn = malloc,
.free_fn = free,
};
cJSON_InitHooks(&hooks);
char *data = load_data_from_file(filename);
if (data == NULL) {
return NULL;
}
struct idlist *texts = NULL;
struct idlist *texts_node = texts;
cJSON *json_data = cJSON_Parse(data);
if (cJSON_IsArray(json_data)) {
size_t array_size = cJSON_GetArraySize(json_data);
for (size_t i = 0; i < array_size; i++) {
cJSON *item = cJSON_GetArrayItem(json_data, i);
if (cJSON_IsObject(item)) {
/* Allocate texts node to be filled */
if (!texts) {
texts_node = texts = list_new();
} else {
texts_node = list_append(texts);
}
texts_node->obj = calloc(1, sizeof(struct text));
struct text *text = texts_node->obj;
/* Fill the node */
text->type = texts_cjson_get_string(item, "type");
text->scene = texts_cjson_get_string(item, "scene");
text->text = texts_cjson_get_string(item, "text");
text->font = texts_cjson_get_string(item, "font");
text->size = texts_cjson_get_int(item, "size");
text->pos.x = texts_cjson_get_int(item, "x");
text->pos.y = texts_cjson_get_int(item, "y");
}
}
}
free(data);
cJSON_Delete(json_data);
return texts;
}
static FILE *openFile(char *filename)
{
FILE *file;
if (!(file = fopen(filename, "rb"))){
printf("ERROR: file \"%s\"cannot be opened\n", filename);
exit(EXIT_FAILURE);
}
return file;
}
struct idlist *load_texts(char *filename)
{
FILE *file = openFile(filename);
yaml_parser_t parser;
yaml_event_t event;
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, file);
struct idlist *texts = NULL;
struct idlist *texts_node = texts;
do {
if (!yaml_parser_parse(&parser, &event)) {
fprintf(stderr, "Parser error %d\n", parser.error);
exit(EXIT_FAILURE);
}
switch (event.type)
{
case YAML_MAPPING_START_EVENT:
if (!texts) {
texts = list_new();
texts_node = texts;
} else
texts_node = list_append(texts);
texts_node->obj = calloc(1, sizeof(struct text));
break;
case YAML_SCALAR_EVENT:
;yaml_event_t ev;
yaml_parser_parse(&parser, &ev);
while (ev.type != YAML_SCALAR_EVENT) {
yaml_event_delete(&ev);
if (!yaml_parser_parse(&parser, &ev)) {
fprintf(stderr, "Parser error %d\n", parser.error);
exit(EXIT_FAILURE);
}
}
struct text *text = texts_node->obj;
if (!strcmp((char *)event.data.scalar.value, "type")) {
text->type = malloc(sizeof(char)
* (strlen((char *)ev.data.scalar.value) + 1));
strcpy(text->type, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "scene")) {
text->scene = malloc(sizeof(char)
* (strlen((char *)ev.data.scalar.value) + 1));
strcpy(text->scene, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "text")) {
text->text = malloc(sizeof(char)
* (strlen((char *)ev.data.scalar.value) + 1));
strcpy(text->text, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "font")) {
text->font = malloc(sizeof(char)
* (strlen((char *)ev.data.scalar.value) + 1));
strcpy(text->font, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "size")) {
text->size = atoi((char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "x")) {
text->pos.x = atoi((char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "y")) {
text->pos.y = atoi((char *)ev.data.scalar.value);
}
yaml_event_delete(&ev);
break;
default:
break;
}
if (event.type != YAML_STREAM_END_EVENT)
yaml_event_delete(&event);
} while (event.type != YAML_STREAM_END_EVENT);
yaml_event_delete(&event);
yaml_parser_delete(&parser);
fclose(file);
return texts;
}
void text_destroy(void *obj)
{
struct text *text = obj;
if (text->type)
free(text->type);
if (text->scene)
free(text->scene);
if (text->text)
free(text->text);
if (text->font)
free(text->font);
free(text);
}
|