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 fprintf(out
, "%s", tag
->name
);
120 len
+= strlen(tag
->name
);
122 /* handle individual tags which accept params */
123 if ((MATCH("bl") || MATCH("pb") || MATCH("pv")) && !in_conditional
)
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'};
137 /* tags which maybe have a number after them */
138 while ((*start
>= '0' && *start
<= '9') || *start
== '.')
140 temp
[i
++] = *start
++;
144 fprintf(out
, "(%s)", temp
);
148 else if (MATCH("xl"))
150 char label
= start
[1];
152 int read
= 1+dump_arg(out
, start
+1, 4, false);
155 if (*start
>= '0' && *start
<= '9')
157 images_with_subimages
[image_count
++] = label
;
159 len
+= dump_arg(out
, start
, 1, false);
163 else if (MATCH("xd"))
165 char label
= start
[0];
167 while (i
<image_count
)
169 if (images_with_subimages
[i
] == label
)
174 PUTCH(out
, *start
++); len
++;
176 ((*start
>= 'a' && *start
<= 'z') ||
177 (*start
>= 'A' && *start
<= 'Z')))
179 PUTCH(out
, *start
); len
++;
186 len
+= 1+dump_arg(out
, start
+1, 4, true);
188 else if (MATCH("Fl"))
191 len
+= 1+dump_arg(out
, start
+1, 2, true);
195 fprintf(out
, "%%Cd");
197 else if (MATCH("Cl"))
200 char xalign
= '\0', yalign
= '\0';
202 read
= 1+dump_arg(out
, start
+1, 2, false);
205 switch (tolower(*start
))
227 read
= dump_arg(out
, start
, 1, false);
230 switch (tolower(*start
))
252 read
= dump_arg(out
, start
, 1, false);
259 fprintf(out
, ",%c", xalign
);
267 fprintf(out
, ",%s%c", xalign
?"":"-,", yalign
);
272 else if (MATCH("Vd") || MATCH("VI"))
275 PUTCH(out
, *start
); len
++;
278 else if (MATCH("Vp"))
280 /* NOTE: almost certainly needs work */
282 len
+= 1+dump_arg(out
, start
+1, 3, true);
284 else if (MATCH("Vi"))
289 if ((start
[1] >= 'a' && start
[1] <= 'z') ||
290 (start
[1] >= 'A' && start
[1] <= 'Z'))
292 read
= 1+dump_arg(out
, start
+1, 1, false);
299 len
+= read
+ dump_viewport_tags(out
, start
+read
);
301 else if (MATCH("Vl") || MATCH("Vi"))
305 read
= 1+dump_arg(out
, start
+1, 1, false);
307 len
+= read
+ dump_viewport_tags(out
, start
+read
);
312 len
+= 1+dump_viewport_tags(out
, start
+1);
324 len
+= 1+dump_arg(out
, start
+1, 1, true);
327 else if (MATCH("St") || MATCH("Sx"))
330 len
+= 1+dump_arg(out
, start
+1, 1, true);
336 len
+= 1+dump_arg(out
, start
+1, 5, true);
341 void parse_text(const char* in
, FILE* out
)
343 const char* end
= in
+strlen(in
);
347 while (in
< end
&& *in
)
371 len
= parse_tag(out
, in
, level
>0);
401 while (*in
&& *in
!= '\n')
412 if (find_escape_character(*in
))
421 int main(int argc
, char* argv
[])
423 char buffer
[10*1024], temp
[512];
424 FILE *in
, *out
= stdout
;
425 int filearg
= 1, i
=0;
427 strcmp(argv
[1],"-h") == 0 ||
428 strcmp(argv
[1],"--help") == 0 )
430 printf("Usage: %s [OPTIONS] infile [outfile]\n", argv
[0]);
431 printf("\nOPTIONS:\n");
432 printf("\t-c\tDon't use new viewport colour tags (non-mono displays only)\n");
433 printf("\t-m\tSkin is for a mono display (different viewport tags)\n");
437 while ((argc
> filearg
) && argv
[filearg
][0] == '-')
440 while (argv
[filearg
][i
])
442 switch(argv
[filearg
][i
])
444 case 'c': /* disable new colour tags */
445 use_new_vp_tags
= false;
447 case 'm': /* skin is for a mono display */
448 is_mono_display
= true;
457 printf("Missing input filename\n");
461 in
= fopen(argv
[filearg
], "r");
464 while (fgets(temp
, 512, in
))
465 strcat(buffer
, temp
);
471 out
= fopen(argv
[filearg
], "w");
474 printf("Couldn't open %s\n", argv
[filearg
]);
479 parse_text(buffer
, out
);