Bump version numbers for 3.13
[maemo-rb.git] / utils / skinupdater / skinupdater.c
blobfe7edbb81babf5a797b229958e6119b4fce622fa
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 if (!strcmp(tag->name, "C"))
121 fprintf(out, "Cd");
122 return 1;
125 fprintf(out, "%s", tag->name);
126 len += strlen(tag->name);
127 start += len;
128 /* handle individual tags which accept params */
129 if ((MATCH("bl") || MATCH("pb") || MATCH("pv")) && !in_conditional)
131 if (*start == '|')
133 int i=0;
134 char filename[128];
135 len++; start++;
136 PUTCH(out, '(');
137 /* |file|x|y|width|height| -> (x,y,width,height,file) */
138 while (start[i] != '|')
140 filename[i] = start[i];
141 i++;
143 filename[i] = '\0';
144 len +=i+1;
145 start += i+1;
146 /* TODO: need to verify that we are actually using the long form... */
147 len += dump_arg(out, start, 4, false);
148 if (i>0)
150 fprintf(out, ",%s", filename);
152 PUTCH(out, ')');
155 else if (MATCH("d") || MATCH("D") || MATCH("mv") || MATCH("pS") || MATCH("pE") || MATCH("t") || MATCH("Tl"))
157 char temp[8] = {'\0'};
158 int i = 0;
159 /* tags which maybe have a number after them */
160 while ((*start >= '0' && *start <= '9') || *start == '.')
162 temp[i++] = *start++;
164 if (i!= 0)
166 fprintf(out, "(%s)", temp);
167 len += i;
170 else if (MATCH("xl"))
172 char label = start[1];
173 PUTCH(out, '(');
174 int read = 1+dump_arg(out, start+1, 4, false);
175 len += read;
176 start += read;
177 if (*start>= '0' && *start <= '9')
179 images_with_subimages[image_count++] = label;
180 PUTCH(out, ',');
181 len += dump_arg(out, start, 1, false);
183 PUTCH(out, ')');
185 else if (MATCH("xd"))
187 char label = start[0];
188 int i=0;
189 while (i<image_count)
191 if (images_with_subimages[i] == label)
192 break;
193 i++;
195 PUTCH(out, '(');
196 PUTCH(out, *start++); len++;
197 if (i<image_count &&
198 ((*start >= 'a' && *start <= 'z') ||
199 (*start >= 'A' && *start <= 'Z')))
201 PUTCH(out, *start); len++;
203 PUTCH(out, ')');
205 else if (MATCH("x"))
207 PUTCH(out, '(');
208 len += 1+dump_arg(out, start+1, 4, true);
210 else if (MATCH("Fl"))
212 PUTCH(out, '(');
213 len += 1+dump_arg(out, start+1, 2, true);
215 else if (MATCH("Cl"))
217 int read;
218 char xalign = '\0', yalign = '\0';
219 PUTCH(out, '(');
220 read = 1+dump_arg(out, start+1, 2, false);
221 len += read;
222 start += read;
223 switch (tolower(*start))
225 case 'l':
226 case 'c':
227 case 'r':
228 case '+':
229 case '-':
230 xalign = *start;
231 len++;
232 start++;
233 break;
234 case 'd':
235 case 'D':
236 case 'i':
237 case 'I':
238 case 's':
239 case 'S':
240 len++;
241 start++;
242 break;
244 PUTCH(out,',');
245 read = dump_arg(out, start, 1, false);
246 len += read;
247 start += read;
248 switch (tolower(*start))
250 case 't':
251 case 'c':
252 case 'b':
253 case '+':
254 case '-':
255 yalign = *start;
256 len++;
257 start++;
258 break;
259 case 'd':
260 case 'D':
261 case 'i':
262 case 'I':
263 case 's':
264 case 'S':
265 len++;
266 start++;
267 break;
269 PUTCH(out,',');
270 read = dump_arg(out, start, 1, false);
271 if (xalign)
273 if (xalign == '-')
274 xalign = 'l';
275 if (xalign == '+')
276 xalign = 'r';
277 fprintf(out, ",%c", xalign);
279 if (yalign)
281 if (yalign == '-')
282 yalign = 't';
283 if (yalign == '+')
284 yalign = 'b';
285 fprintf(out, ",%s%c", xalign?"":"-,", yalign);
287 PUTCH(out, ')');
288 len += read;
290 else if (MATCH("Vd") || MATCH("VI"))
292 PUTCH(out, '(');
293 PUTCH(out, *start); len++;
294 PUTCH(out, ')');
296 else if (MATCH("Vp"))
298 int read;
299 /* NOTE: almost certainly needs work */
300 PUTCH(out, '(');
301 read = 1+dump_arg(out, start+1, 1, false);
302 PUTCH(out, ',');
303 while (start[read] != '|')
305 PUTCH(out, start[read++]);
307 PUTCH(out, ',');
308 read++;
309 while (start[read] != '|')
311 PUTCH(out, start[read++]);
313 PUTCH(out, ')');
314 read++;
315 len += read;
317 else if (MATCH("Vi"))
319 int read = 1;
321 PUTCH(out, '(');
322 if ((start[1] >= 'a' && start[1] <= 'z') ||
323 (start[1] >= 'A' && start[1] <= 'Z'))
325 read = 1+dump_arg(out, start+1, 1, false);
327 else
329 PUTCH(out, '-');
331 PUTCH(out, ',');
332 len += read + dump_viewport_tags(out, start+read);
334 else if (MATCH("Vl") || MATCH("Vi"))
336 int read;
337 PUTCH(out, '(');
338 read = 1+dump_arg(out, start+1, 1, false);
339 PUTCH(out, ',');
340 len += read + dump_viewport_tags(out, start+read);
342 else if (MATCH("V"))
344 PUTCH(out, '(');
345 len += 1+dump_viewport_tags(out, start+1);
347 else if (MATCH("X"))
349 if (*start == 'd')
351 fprintf(out, "(d)");
352 len ++;
354 else
356 PUTCH(out, '(');
357 len += 1+dump_arg(out, start+1, 1, true);
360 else if (MATCH("St") || MATCH("Sx"))
362 PUTCH(out, '(');
363 len += 1+dump_arg(out, start+1, 1, true);
366 else if (MATCH("T"))
368 PUTCH(out, '(');
369 len += 1+dump_arg(out, start+1, 5, true);
371 return len;
374 void parse_text(const char* in, FILE* out)
376 const char* end = in+strlen(in);
377 int level = 0;
378 int len;
379 top:
380 while (in < end && *in)
382 if (*in == '%')
384 PUTCH(out, *in++);
385 switch(*in)
388 case '%':
389 case '<':
390 case '|':
391 case '>':
392 case ';':
393 case '#':
394 case ')':
395 case '(':
396 case ',':
397 PUTCH(out, *in++);
398 goto top;
399 break;
400 case '?':
401 if (in[1] == 'C' && in[2] == '<')
403 fprintf(out, "?C");
404 in += 2;
405 goto top;
407 else
409 PUTCH(out, *in++);
411 break;
413 len = parse_tag(out, in, level>0);
414 if (len < 0)
416 PUTCH(out, *in++);
418 else
420 in += len;
423 else if (*in == '<')
425 level++;
426 PUTCH(out, *in++);
428 else if (*in == '>')
430 level--;
431 PUTCH(out, *in++);
433 else if (*in == '|')
435 if (level == 0)
437 PUTCH(out, '%');
439 PUTCH(out, *in++);
441 else if (*in == '#')
443 while (*in && *in != '\n')
445 PUTCH(out, *in++);
448 else if (*in == ';')
450 PUTCH(out, *in++);
452 else
454 if (find_escape_character(*in))
456 PUTCH(out, '%');
458 PUTCH(out, *in++);
463 int main(int argc, char* argv[])
465 char buffer[10*1024], temp[512];
466 FILE *in, *out = stdout;
467 int filearg = 1, i=0;
468 if( (argc < 2) ||
469 strcmp(argv[1],"-h") == 0 ||
470 strcmp(argv[1],"--help") == 0 )
472 printf("Usage: %s [OPTIONS] infile [outfile]\n", argv[0]);
473 printf("\nOPTIONS:\n");
474 printf("\t-c\tDon't use new viewport colour tags (non-mono displays only)\n");
475 printf("\t-m\tSkin is for a mono display (different viewport tags)\n");
476 return 0;
479 while ((argc > filearg) && argv[filearg][0] == '-')
481 i=1;
482 while (argv[filearg][i])
484 switch(argv[filearg][i])
486 case 'c': /* disable new colour tags */
487 use_new_vp_tags = false;
488 break;
489 case 'm': /* skin is for a mono display */
490 is_mono_display = true;
491 break;
493 i++;
495 filearg++;
497 if (argc == filearg)
499 printf("Missing input filename\n");
500 return 1;
503 in = fopen(argv[filearg], "r");
504 if (!in)
505 return 1;
506 while (fgets(temp, 512, in))
507 strcat(buffer, temp);
508 fclose(in);
509 filearg++;
511 if (argc > filearg)
513 out = fopen(argv[filearg], "w");
514 if (!out)
516 printf("Couldn't open %s\n", argv[filearg]);
517 return 1;
521 parse_text(buffer, out);
522 if (out != stdout)
523 fclose(out);
524 return 0;