change the %Cl tag to be cleaner. it it now %Cl(x, y, width, height [, xalign][,...
[kugel-rb.git] / utils / skinupdater / skinupdater.c
blob6577b77e6aaab549f81a12bd809524bcb1afd552
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: tag_table.c 26346 2010-05-28 02:30:27Z jdgordon $
10 * Copyright (C) 2010 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include "tag_table.h"
29 #define PUTCH(out, c) fprintf(out, "%c", c)
30 extern struct tag_info legal_tags[];
32 char images_with_subimages[100];
33 int image_count = 0;
35 /** Command line setting **/
36 bool is_mono_display = false;
37 bool use_new_vp_tags = true;
40 /* dump "count" args to output replacing '|' with ',' except after the last count.
41 * return the amount of chars read. (start+return will be after the last | )
43 int dump_arg(FILE* out, const char* start, int count, bool close)
45 int l = 0;
46 while (count)
48 if (start[l] == '|')
50 if (count > 1)
52 PUTCH(out, ',');
53 } else if (close) {
54 PUTCH(out, ')');
56 count--;
57 } else {
58 if (find_escape_character(start[l]))
60 PUTCH(out, '%');
62 PUTCH(out, start[l]);
64 l++;
66 return l;
69 int dump_viewport_tags(FILE* out, const char* start)
71 int len = 0;
72 if (is_mono_display)
74 return dump_arg(out, start, 5, true);
76 else
78 int arg_count = use_new_vp_tags?5:7;
79 len += dump_arg(out, start, arg_count, true);
80 if (!use_new_vp_tags)
81 return len;
82 if (start[len] != '-')
84 fprintf(out, "%%Vf(");
85 len += dump_arg(out, start+len, 1, true);
87 else
89 while (start[len++] != '|');
91 if (start[len] != '-')
93 fprintf(out, "%%Vb(");
94 len += dump_arg(out, start+len, 1, true);
96 else
98 while (start[len++] != '|');
101 return len;
104 #define MATCH(s) (!strcmp(tag->name, s))
105 int parse_tag(FILE* out, const char* start, bool in_conditional)
107 struct tag_info *tag;
108 int len = 0;
109 for(tag = legal_tags;
110 tag->name[0] && strncmp(start, tag->name, strlen(tag->name)) != 0;
111 tag++) ;
112 if (!tag->name[0])
113 return -1;
114 if (tag->params[0] == '\0')
116 fprintf(out, "%s", tag->name);
117 return strlen(tag->name);
119 fprintf(out, "%s", tag->name);
120 len += strlen(tag->name);
121 start += len;
122 /* handle individual tags which accept params */
123 if ((MATCH("bl") || MATCH("pb") || MATCH("pv")) && !in_conditional)
125 if (*start == '|')
127 len++; start++;
128 PUTCH(out, '(');
129 /* TODO: need to verify that we are actually using the long form... */
130 len += dump_arg(out, start, 5, true);
133 else if (MATCH("d") || MATCH("D") || MATCH("mv") || MATCH("pS") || MATCH("pE") || MATCH("t") || MATCH("Tl"))
135 char temp[8] = {'\0'};
136 int i = 0;
137 /* tags which maybe have a number after them */
138 while ((*start >= '0' && *start <= '9') || *start == '.')
140 temp[i++] = *start++;
142 if (i!= 0)
144 fprintf(out, "(%s)", temp);
145 len += i;
148 else if (MATCH("xl"))
150 char label = start[1];
151 PUTCH(out, '(');
152 int read = 1+dump_arg(out, start+1, 4, false);
153 len += read;
154 start += read;
155 if (*start>= '0' && *start <= '9')
157 images_with_subimages[image_count++] = label;
158 PUTCH(out, ',');
159 len += dump_arg(out, start, 1, false);
161 PUTCH(out, ')');
163 else if (MATCH("xd"))
165 char label = start[0];
166 int i=0;
167 while (i<image_count)
169 if (images_with_subimages[i] == label)
170 break;
171 i++;
173 PUTCH(out, '(');
174 PUTCH(out, *start++); len++;
175 if (i<image_count &&
176 ((*start >= 'a' && *start <= 'z') ||
177 (*start >= 'A' && *start <= 'Z')))
179 PUTCH(out, *start); len++;
181 PUTCH(out, ')');
183 else if (MATCH("x"))
185 PUTCH(out, '(');
186 len += 1+dump_arg(out, start+1, 4, true);
188 else if (MATCH("Fl"))
190 PUTCH(out, '(');
191 len += 1+dump_arg(out, start+1, 2, true);
193 else if (MATCH("Cl"))
195 int read;
196 char xalign = '\0', yalign = '\0';
197 PUTCH(out, '(');
198 read = 1+dump_arg(out, start+1, 2, false);
199 len += read;
200 start += read;
201 switch (tolower(*start))
203 case 'l':
204 case 'c':
205 case 'r':
206 case '+':
207 case '-':
208 xalign = *start;
209 len++;
210 start++;
211 break;
212 case 'd':
213 case 'D':
214 case 'i':
215 case 'I':
216 case 's':
217 case 'S':
218 len++;
219 start++;
220 break;
222 PUTCH(out,',');
223 read = dump_arg(out, start, 1, false);
224 len += read;
225 start += read;
226 switch (tolower(*start))
228 case 't':
229 case 'c':
230 case 'b':
231 case '+':
232 case '-':
233 yalign = *start;
234 len++;
235 start++;
236 break;
237 case 'd':
238 case 'D':
239 case 'i':
240 case 'I':
241 case 's':
242 case 'S':
243 len++;
244 start++;
245 break;
247 PUTCH(out,',');
248 read = dump_arg(out, start, 1, false);
249 if (xalign)
251 if (xalign == '-')
252 xalign = 'l';
253 if (xalign == '+')
254 xalign = 'r';
255 fprintf(out, ",%c", xalign);
257 if (yalign)
259 if (yalign == '-')
260 yalign = 't';
261 if (yalign == '+')
262 yalign = 'b';
263 fprintf(out, ",%s%c", xalign?"":"-,", yalign);
265 PUTCH(out, ')');
266 len += read;
268 else if (MATCH("Vd") || MATCH("VI"))
270 PUTCH(out, '(');
271 PUTCH(out, *start); len++;
272 PUTCH(out, ')');
274 else if (MATCH("Vp"))
276 /* NOTE: almost certainly needs work */
277 PUTCH(out, '(');
278 len += 1+dump_arg(out, start+1, 3, true);
280 else if (MATCH("Vl") || MATCH("Vi"))
282 int read;
283 PUTCH(out, '(');
284 read = 1+dump_arg(out, start+1, 1, false);
285 PUTCH(out, ',');
286 len += read + dump_viewport_tags(out, start+read);
288 else if (MATCH("V"))
290 PUTCH(out, '(');
291 len += 1+dump_viewport_tags(out, start+1);
293 else if (MATCH("X"))
295 if (*start+1 == 'd')
297 fprintf(out, "(d)");
298 len ++;
300 else
302 PUTCH(out, '(');
303 len += 1+dump_arg(out, start+1, 1, true);
306 else if (MATCH("St") || MATCH("Sx"))
308 PUTCH(out, '(');
309 len += 1+dump_arg(out, start+1, 1, true);
312 else if (MATCH("T"))
314 PUTCH(out, '(');
315 len += 1+dump_arg(out, start+1, 5, true);
317 return len;
320 void parse_text(const char* in, FILE* out)
322 const char* end = in+strlen(in);
323 int level = 0;
324 int len;
325 top:
326 while (in < end && *in)
328 if (*in == '%')
330 PUTCH(out, *in++);
331 switch(*in)
334 case '%':
335 case '<':
336 case '|':
337 case '>':
338 case ';':
339 case '#':
340 case ')':
341 case '(':
342 case ',':
343 PUTCH(out, *in++);
344 goto top;
345 break;
346 case '?':
347 PUTCH(out, *in++);
348 break;
350 len = parse_tag(out, in, level>0);
351 if (len < 0)
353 PUTCH(out, *in++);
355 else
357 in += len;
360 else if (*in == '<')
362 level++;
363 PUTCH(out, *in++);
365 else if (*in == '>')
367 level--;
368 PUTCH(out, *in++);
370 else if (*in == '|')
372 if (level == 0)
374 PUTCH(out, '%');
376 PUTCH(out, *in++);
378 else if (*in == '#')
380 while (*in && *in != '\n')
382 PUTCH(out, *in++);
385 else
387 if (find_escape_character(*in))
389 PUTCH(out, '%');
391 PUTCH(out, *in++);
396 int main(int argc, char* argv[])
398 char buffer[10*1024], temp[512];
399 FILE *in, *out = stdout;
400 int filearg = 1, i=0;
401 if( (argc < 2) ||
402 strcmp(argv[1],"-h") == 0 ||
403 strcmp(argv[1],"--help") == 0 )
405 printf("Usage: %s [OPTIONS] infile [outfile]\n", argv[0]);
406 printf("\nOPTIONS:\n");
407 printf("\t-c\tDon't use new viewport colour tags (non-mono displays only)\n");
408 printf("\t-m\tSkin is for a mono display (different viewport tags)\n");
409 return 0;
412 while ((argc > filearg) && argv[filearg][0] == '-')
414 i=1;
415 while (argv[filearg][i])
417 switch(argv[filearg][i])
419 case 'c': /* disable new colour tags */
420 use_new_vp_tags = false;
421 break;
422 case 'm': /* skin is for a mono display */
423 is_mono_display = true;
424 break;
426 i++;
428 filearg++;
430 if (argc == filearg)
432 printf("Missing input filename\n");
433 return 1;
436 in = fopen(argv[filearg], "r");
437 if (!in)
438 return 1;
439 while (fgets(temp, 512, in))
440 strcat(buffer, temp);
441 fclose(in);
442 filearg++;
444 if (argc > filearg)
446 out = fopen(argv[filearg], "w");
447 if (!out)
449 printf("Couldn't open %s\n", argv[filearg]);
450 return 1;
454 parse_text(buffer, out);
455 if (out != stdout)
456 fclose(out);
457 return 0;