forked from xyb3rt/sxiv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.c
360 lines (320 loc) · 8.53 KB
/
palette.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
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
/* sxiv: palette.c
* Copyright (c) 2012 Devon Smith <decasm at gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/* http://www.vim.org/scripts/script.php?script_id=1218 */
/* vim nerd commenter */
#include <stdio.h>
#include <string.h>
#include <yaml.h>
#include "palette.h"
#include "util.h"
#define PALETTE_C_LOG 0
#define SUBMAP "*SUBMAP*"
void handle_scalar_node(yaml_document_t *, yaml_node_t *);
void handle_mapping_node(yaml_document_t *, yaml_node_t *);
void handle_sequence_node(yaml_document_t *, yaml_node_t *);
void handle_node(yaml_document_t *, yaml_node_t *);
char * event_type_to_string(yaml_event_type_t type) {
switch(type) {
case YAML_NO_EVENT:
return "No Event";
break;
case YAML_STREAM_START_EVENT:
return "Stream Start Event";
break;
case YAML_STREAM_END_EVENT:
return "Stream End Event";
break;
case YAML_DOCUMENT_START_EVENT:
return "Document Start Event";
break;
case YAML_DOCUMENT_END_EVENT:
return "Document End Event";
break;
case YAML_ALIAS_EVENT:
return "Alias Event";
break;
case YAML_SCALAR_EVENT:
return "Scalar Event";
break;
case YAML_SEQUENCE_START_EVENT:
return "Sequence Start Event";
break;
case YAML_SEQUENCE_END_EVENT:
return "Sequence End Event";
break;
case YAML_MAPPING_START_EVENT:
return "Mapping Start Event";
break;
case YAML_MAPPING_END_EVENT:
return "Mapping End Event";
break;
}
return "Unknown Event";
}
palette_t * new_palette() {
palette_t * p = malloc(sizeof(palette_t));
p->index = -1;
p->label = NULL;
p->next = NULL;
p->tags = NULL;
return p;
}
keytagmap_t * new_keytagmap() {
keytagmap_t * k = malloc(sizeof(keytagmap_t));
k->key = 0;
k->tag = NULL;
k->next = NULL;
return k;
}
void display_palette(palette_t * palette) {
keytagmap_t * ktm;
while ( palette ) {
fprintf(stderr, "Palette: label:(%s) index(%d)\n", palette->label, palette->index);
ktm = palette->tags;
while ( ktm ) {
fprintf(stderr, " %c: %s\n", ktm->key, ktm->tag);
ktm = ktm->next;
}
palette = palette->next;
}
}
mapping_t * new_mapping() {
mapping_t * m = malloc(sizeof(mapping_t));
m->key = NULL;
m->value = NULL;
m->next = NULL;
m->down = NULL;
return m;
}
void display_map(mapping_t * m) {
if ( m->key && m->value ) {
fprintf(stderr, " %s = %s\n", m->key, m->value);
}
else if ( m->key ) {
fprintf(stderr, " %s = \n", m->key);
}
if ( m->down ) {
display_map(m->down);
}
if ( m->next ) {
display_map(m->next);
}
}
/* Experimental parsing. Uses potentially customizable handler routines.
* More work than is needed at the moment.
*/
int parse_palette_event_map(yaml_parser_t * parser) {
yaml_event_t event;
char * scalar;
mapping_t * root = NULL, * mapping = NULL, * parent = NULL;
root = new_mapping();
do {
yaml_parser_parse(parser, &event);
switch(event.type) {
case YAML_NO_EVENT:
case YAML_ALIAS_EVENT:
case YAML_STREAM_START_EVENT:
case YAML_STREAM_END_EVENT:
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_END_EVENT:
case YAML_SEQUENCE_START_EVENT:
case YAML_SEQUENCE_END_EVENT:
/* ignore all these events */
break;
case YAML_MAPPING_START_EVENT:
if ( ! mapping ) {
root->down = mapping = new_mapping();
}
else if ( mapping->key && ! mapping->value ) {
mapping->value = s_strdup(SUBMAP);
parent = mapping;
mapping->down = new_mapping();
mapping = mapping->down;
}
break;
case YAML_MAPPING_END_EVENT:
mapping = parent;
break;
case YAML_SCALAR_EVENT:
scalar = (char *)event.data.scalar.value;
if ( mapping ) {
if ( ! mapping->key ) {
mapping->key = s_strdup(scalar);
}
else if ( ! mapping->value ) {
mapping->value = s_strdup(scalar);
}
else {
/* Key and Value set on current mapping */
mapping->next = new_mapping();
mapping = mapping->next;
mapping->key = s_strdup(scalar);
}
}
break;
}
if(event.type != YAML_STREAM_END_EVENT) {
yaml_event_delete(&event);
}
} while(event.type != YAML_STREAM_END_EVENT);
display_map(root);
return 0;
}
typedef void (*yaml_map_handler)(mapping2_t *);
void handle_map_start(mapping2_t * map) {
fprintf(stderr, "OK OK\n");
}
void handle_map_end(mapping2_t * map) {
fprintf(stderr, "OK OK\n");
}
/* Experimental parsing. Uses potentially customizable handler routines.
* More work than is needed at the moment.
*/
int parse_palette_funcs(yaml_parser_t * parser) {
yaml_event_t event;
char * scalar;
mapping2_t * map = NULL;
yaml_map_handler map_start = handle_map_start;
yaml_map_handler map_end = handle_map_end;
do {
yaml_parser_parse(parser, &event);
switch(event.type) {
case YAML_NO_EVENT:
case YAML_ALIAS_EVENT:
case YAML_STREAM_START_EVENT:
case YAML_STREAM_END_EVENT:
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_END_EVENT:
case YAML_SEQUENCE_START_EVENT:
case YAML_SEQUENCE_END_EVENT:
/* ignore all these events */
break;
case YAML_MAPPING_START_EVENT:
(*map_start)(map);
break;
case YAML_MAPPING_END_EVENT:
(*map_end)(map);
break;
case YAML_SCALAR_EVENT:
scalar = (char *)event.data.scalar.value;
break;
}
if(event.type != YAML_STREAM_END_EVENT) {
yaml_event_delete(&event);
}
} while(event.type != YAML_STREAM_END_EVENT);
return 0;
}
palette_t * parse_palette(yaml_parser_t * parser) {
yaml_event_t event;
char * scalar;
palette_t * palette = NULL, * root_palette = NULL;
keytagmap_t * ktm =NULL;
char * longest_tag = NULL;
int longest_tag_length = 0;
do {
yaml_parser_parse(parser, &event);
switch(event.type) {
case YAML_NO_EVENT:
case YAML_ALIAS_EVENT:
case YAML_STREAM_START_EVENT:
case YAML_STREAM_END_EVENT:
case YAML_DOCUMENT_START_EVENT:
case YAML_DOCUMENT_END_EVENT:
case YAML_SEQUENCE_START_EVENT:
case YAML_SEQUENCE_END_EVENT:
/* ignore all these events */
break;
case YAML_MAPPING_START_EVENT:
if ( palette && ! palette->tags ) {
palette->tags = ktm = new_keytagmap();
}
else {
}
break;
case YAML_MAPPING_END_EVENT:
ktm = NULL;
break;
case YAML_SCALAR_EVENT:
scalar = (char *)event.data.scalar.value;
if ( ! ktm ) {
if ( ! palette ) {
palette = root_palette = new_palette();
}
else {
palette->next = new_palette();
palette = palette->next;
}
if ( strlen(scalar) == 1 && palette->index == -1 ) {
sscanf(scalar, "%d", &(palette->index));
}
}
else {
if ( strcmp(scalar, "label") == 0 ) {
palette->label = s_strdup(scalar);
}
else if ( palette && palette->label && strcmp(palette->label, "label") == 0 ) {
palette->label = s_strdup(scalar);
}
else if ( ! ktm->key ) {
sscanf(scalar, "%c", &(ktm->key));
}
else if ( ! ktm->tag ) {
ktm->tag = s_strdup(scalar);
if (strlen(ktm->tag) > longest_tag_length) {
longest_tag = ktm->tag;
longest_tag_length = strlen(longest_tag);
}
}
else { /* The next key for the current ktm */
ktm->next = new_keytagmap();
ktm = ktm->next;
sscanf(scalar, "%c", &(ktm->key));
}
}
break;
}
if(event.type != YAML_STREAM_END_EVENT) {
yaml_event_delete(&event);
}
} while(event.type != YAML_STREAM_END_EVENT);
return root_palette;
}
palette_t * load_palettes(char * filename) {
yaml_parser_t parser;
FILE *input;
palette_t * palette;
input = fopen(filename, "rb");
/* Create the Parser object. */
yaml_parser_initialize(&parser);
/* Set a file input. */
yaml_parser_set_input_file(&parser, input);
/*if ( ! parse_palette_funcs(&parser) )*/
/*if ( ! parse_palette(&parser) )(*/
palette = parse_palette(&parser);
if ( NULL == palette )
goto error;
/* Destroy the Parser object. */
yaml_parser_delete(&parser);
return palette;
/* On error. */
error:
/* Destroy the Parser object. */
yaml_parser_delete(&parser);
return NULL;
}