1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
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];
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
)
58 if (find_escape_character(start
[l
]))
69 int dump_viewport_tags(FILE* out
, const char* start
)
74 return dump_arg(out
, start
, 5, true);
78 int arg_count
= use_new_vp_tags
?5:7;
79 len
+= dump_arg(out
, start
, arg_count
, true);
82 if (start
[len
] != '-')
84 fprintf(out
, "%%Vf(");
85 len
+= dump_arg(out
, start
+len
, 1, true);
89 while (start
[len
++] != '|');
91 if (start
[len
] != '-')
93 fprintf(out
, "%%Vb(");
94 len
+= dump_arg(out
, start
+len
, 1, true);
98 while (start
[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
;
109 for(tag
= legal_tags
;
110 tag
->name
[0] && strncmp(start
, tag
->name
, strlen(tag
->name
)) != 0;
114 if (tag
->params
[0] == '\0')
116 fprintf(out
, "%s", tag
->name
);
117 return strlen(tag
->name
);
119 if (!strcmp(tag
->name
, "C"))
125 fprintf(out
, "%s", tag
->name
);
126 len
+= strlen(tag
->name
);
128 /* handle individual tags which accept params */
129 if ((MATCH("bl") || MATCH("pb") || MATCH("pv")) && !in_conditional
)
137 /* |file|x|y|width|height| -> (x,y,width,height,file) */
138 while (start
[i
] != '|')
140 filename
[i
] = start
[i
];
146 /* TODO: need to verify that we are actually using the long form... */
147 len
+= dump_arg(out
, start
, 4, false);
150 fprintf(out
, ",%s", filename
);
155 else if (MATCH("d") || MATCH("D") || MATCH("mv") || MATCH("pS") || MATCH("pE") || MATCH("t") || MATCH("Tl"))
157 char temp
[8] = {'\0'};
159 /* tags which maybe have a number after them */
160 while ((*start
>= '0' && *start
<= '9') || *start
== '.')
162 temp
[i
++] = *start
++;
166 fprintf(out
, "(%s)", temp
);
170 else if (MATCH("xl"))
172 char label
= start
[1];
174 int read
= 1+dump_arg(out
, start
+1, 4, false);
177 if (*start
>= '0' && *start
<= '9')
179 images_with_subimages
[image_count
++] = label
;
181 len
+= dump_arg(out
, start
, 1, false);
185 else if (MATCH("xd"))
187 char label
= start
[0];
189 while (i
<image_count
)
191 if (images_with_subimages
[i
] == label
)
196 PUTCH(out
, *start
++); len
++;
198 ((*start
>= 'a' && *start
<= 'z') ||
199 (*start
>= 'A' && *start
<= 'Z')))
201 PUTCH(out
, *start
); len
++;
208 len
+= 1+dump_arg(out
, start
+1, 4, true);
210 else if (MATCH("Fl"))
213 len
+= 1+dump_arg(out
, start
+1, 2, true);
215 else if (MATCH("Cl"))
218 char xalign
= '\0', yalign
= '\0';
220 read
= 1+dump_arg(out
, start
+1, 2, false);
223 switch (tolower(*start
))
245 read
= dump_arg(out
, start
, 1, false);
248 switch (tolower(*start
))
270 read
= dump_arg(out
, start
, 1, false);
277 fprintf(out
, ",%c", xalign
);
285 fprintf(out
, ",%s%c", xalign
?"":"-,", yalign
);
290 else if (MATCH("Vd") || MATCH("VI"))
293 PUTCH(out
, *start
); len
++;
296 else if (MATCH("Vp"))
299 /* NOTE: almost certainly needs work */
301 read
= 1+dump_arg(out
, start
+1, 1, false);
303 while (start
[read
] != '|')
305 PUTCH(out
, start
[read
++]);
309 while (start
[read
] != '|')
311 PUTCH(out
, start
[read
++]);
317 else if (MATCH("Vi"))
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);
332 len
+= read
+ dump_viewport_tags(out
, start
+read
);
334 else if (MATCH("Vl") || MATCH("Vi"))
338 read
= 1+dump_arg(out
, start
+1, 1, false);
340 len
+= read
+ dump_viewport_tags(out
, start
+read
);
345 len
+= 1+dump_viewport_tags(out
, start
+1);
357 len
+= 1+dump_arg(out
, start
+1, 1, true);
360 else if (MATCH("St") || MATCH("Sx"))
363 len
+= 1+dump_arg(out
, start
+1, 1, true);
369 len
+= 1+dump_arg(out
, start
+1, 5, true);
374 void parse_text(const char* in
, FILE* out
)
376 const char* end
= in
+strlen(in
);
380 while (in
< end
&& *in
)
401 if (in
[1] == 'C' && in
[2] == '<')
413 len
= parse_tag(out
, in
, level
>0);
443 while (*in
&& *in
!= '\n')
454 if (find_escape_character(*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;
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");
479 while ((argc
> filearg
) && argv
[filearg
][0] == '-')
482 while (argv
[filearg
][i
])
484 switch(argv
[filearg
][i
])
486 case 'c': /* disable new colour tags */
487 use_new_vp_tags
= false;
489 case 'm': /* skin is for a mono display */
490 is_mono_display
= true;
499 printf("Missing input filename\n");
503 in
= fopen(argv
[filearg
], "r");
506 while (fgets(temp
, 512, in
))
507 strcat(buffer
, temp
);
513 out
= fopen(argv
[filearg
], "w");
516 printf("Couldn't open %s\n", argv
[filearg
]);
521 parse_text(buffer
, out
);