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);
193 else if (MATCH("Cl"))
196 char xalign
= '\0', yalign
= '\0';
198 read
= 1+dump_arg(out
, start
+1, 2, false);
201 switch (tolower(*start
))
223 read
= dump_arg(out
, start
, 1, false);
226 switch (tolower(*start
))
248 read
= dump_arg(out
, start
, 1, false);
255 fprintf(out
, ",%c", xalign
);
263 fprintf(out
, ",%s%c", xalign
?"":"-,", yalign
);
268 else if (MATCH("Vd") || MATCH("VI"))
271 PUTCH(out
, *start
); len
++;
274 else if (MATCH("Vp"))
276 /* NOTE: almost certainly needs work */
278 len
+= 1+dump_arg(out
, start
+1, 3, true);
280 else if (MATCH("Vl") || MATCH("Vi"))
284 read
= 1+dump_arg(out
, start
+1, 1, false);
286 len
+= read
+ dump_viewport_tags(out
, start
+read
);
291 len
+= 1+dump_viewport_tags(out
, start
+1);
303 len
+= 1+dump_arg(out
, start
+1, 1, true);
306 else if (MATCH("St") || MATCH("Sx"))
309 len
+= 1+dump_arg(out
, start
+1, 1, true);
315 len
+= 1+dump_arg(out
, start
+1, 5, true);
320 void parse_text(const char* in
, FILE* out
)
322 const char* end
= in
+strlen(in
);
326 while (in
< end
&& *in
)
350 len
= parse_tag(out
, in
, level
>0);
380 while (*in
&& *in
!= '\n')
387 if (find_escape_character(*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;
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");
412 while ((argc
> filearg
) && argv
[filearg
][0] == '-')
415 while (argv
[filearg
][i
])
417 switch(argv
[filearg
][i
])
419 case 'c': /* disable new colour tags */
420 use_new_vp_tags
= false;
422 case 'm': /* skin is for a mono display */
423 is_mono_display
= true;
432 printf("Missing input filename\n");
436 in
= fopen(argv
[filearg
], "r");
439 while (fgets(temp
, 512, in
))
440 strcat(buffer
, temp
);
446 out
= fopen(argv
[filearg
], "w");
449 printf("Couldn't open %s\n", argv
[filearg
]);
454 parse_text(buffer
, out
);