]> git.gir.st - ttxd.git/blob - src/vtx2ascii-src/main.c
initial code import
[ttxd.git] / src / vtx2ascii-src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <getopt.h>
7
8 #include "fileio.h"
9
10 unsigned char buffer[65536];
11 vtxpage_t page;
12 int virtual;
13
14 #define OUT_ASCII 1
15 #define OUT_ANSI 2
16 #define OUT_HTML 3
17
18 void
19 export_ansi_ascii(FILE *file, const vtxpage_t *page, int show_hidden)
20 {
21 int pos;
22 int bg,fg,lbg,lfg,hidden;
23
24 lfg = lbg = -1;
25 for (pos = 0; pos < VTX_PAGESIZE; pos++) {
26 fg = page->attrib[pos] & VTX_COLMASK;
27 bg = (page->attrib[pos] & VTX_BGMASK) >> 3;
28 hidden = (page->attrib[pos] & VTX_HIDDEN) && !show_hidden;
29 if (fg != lfg || bg != lbg) {
30 fprintf(file,"\033[%d;%dm",fg+30,bg+40);
31 lfg = fg;
32 lbg = bg;
33 }
34 fputc(hidden ? ' ' : vtx2iso_table[page->chr[pos]], file);
35 if (pos % 40 == 39) {
36 lfg = lbg = -1;
37 fprintf(file,"\033[0m\n");
38 }
39 }
40 }
41
42 void
43 export_html(FILE *file, const vtxpage_t *page, int show_hidden)
44 {
45 static const char *color[] = {
46 "black", "red", "green", "yellow",
47 "blue", "magenta", "cyan", "white"
48 };
49
50 int pos;
51 int bg,fg,lbg,lfg,hidden;
52 int used[64];
53
54 /* scan for used colors */
55 memset(used,0,sizeof(used));
56 for (pos = 0; pos < VTX_PAGESIZE; pos++) {
57 fg = page->attrib[pos] & VTX_COLMASK;
58 bg = (page->attrib[pos] & VTX_BGMASK) >> 3;
59 used[fg*8+bg] = 1;
60 }
61
62 /* print styles */
63 fprintf(file,"<style type=\"text/css\"> <!--\n");
64 for (bg = 0; bg < 8; bg++) {
65 for (fg = 0; fg < 8; fg++) {
66 if (used[fg*8+bg]) {
67 fprintf(file,"font.c%d { color: %s; background-color: %s }\n",
68 fg*8+bg,color[fg],color[bg]);
69 }
70 }
71 }
72 fprintf(file,"//--> </style>\n");
73
74 /* print page */
75 fprintf(file,"<pre>\n");
76 lfg = lbg = -1;
77 for (pos = 0; pos < VTX_PAGESIZE; pos++) {
78 fg = page->attrib[pos] & VTX_COLMASK;
79 bg = (page->attrib[pos] & VTX_BGMASK) >> 3;
80 hidden = (page->attrib[pos] & VTX_HIDDEN) && !show_hidden;
81 if (fg != lfg || bg != lbg) {
82 if (lfg != -1 || lbg != -1)
83 fprintf(file,"</font>");
84 fprintf(file,"<font class=c%d>",fg*8+bg);
85 lfg = fg;
86 lbg = bg;
87 }
88 fputc(hidden ? ' ' : vtx2iso_table[page->chr[pos]], file);
89 if (pos % 40 == 39) {
90 lfg = lbg = -1;
91 fprintf(file,"</font>\n");
92 }
93 }
94 fprintf(file,"</pre>\n");
95 }
96
97 void
98 usage(char *prog)
99 {
100 fprintf(stderr,"usage: %s [ options ] vtx-file\n",prog);
101 fprintf(stderr,
102 "options: \n"
103 " -a dump plain ascii (default)\n"
104 " -c dump colored ascii (using ansi control sequences).\n"
105 " -h dump html\n");
106 exit(1);
107 }
108
109 int
110 main(int argc, char *argv[])
111 {
112 FILE *fp;
113 char *prog;
114 int c,output=OUT_ASCII;
115
116 if (NULL != (prog = strrchr(argv[0],'/')))
117 prog++;
118 else
119 prog = argv[0];
120
121 for (;;) {
122 if (-1 == (c = getopt(argc, argv, "ach")))
123 break;
124 switch (c) {
125 case 'a':
126 output=OUT_ASCII;
127 break;
128 case 'c':
129 output=OUT_ANSI;
130 break;
131 case 'h':
132 output=OUT_HTML;
133 break;
134 default:
135 usage(prog);
136 }
137 }
138 if (optind+1 != argc)
139 usage(prog);
140
141 if (NULL == (fp = fopen(argv[optind],"r"))) {
142 fprintf(stderr,"%s: %s: %s\n",prog,argv[optind],strerror(errno));
143 exit(1);
144 }
145
146 load_vtx(fp, buffer, &page.info, &virtual);
147 decode_page(buffer, &page, 0, 23);
148 switch(output) {
149 case OUT_ASCII:
150 export_ascii(stdout, &page, 0);
151 break;
152 case OUT_ANSI:
153 export_ansi_ascii(stdout, &page, 0);
154 break;
155 case OUT_HTML:
156 export_html(stdout, &page, 0);
157 break;
158 }
159 return 0;
160 }
Imprint / Impressum