]> git.gir.st - ttxd.git/blob - src/vtx2ascii-src/fileio.c
update to dvb-t2 (new hardware, software)
[ttxd.git] / src / vtx2ascii-src / fileio.c
1 /*
2 * fileio.c: Read font-bitmaps, write GIF-, PPM-, ASCII- & VTX-files
3 *
4 * $Id: fileio.c,v 1.5 1997/08/14 22:59:42 mb Exp mb $
5 *
6 * Copyright (c) 1995-96 Martin Buck <martin-2.buck@student.uni-ulm.de>
7 * Read COPYING for more information
8 *
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <dirent.h>
16 #include <errno.h>
17 #include <sys/stat.h>
18 #include "vtx_assert.h"
19 #include "vtxdecode.h"
20 #include "vtxtools.h"
21 #include "misc.h"
22 #include "fileio.h"
23
24 #define VTXFONT_EXT ".vtxfont"
25 typedef enum { FP_ARG, FP_ENV, FP_DEFAULT } fp_src_t;
26
27
28 char *vtx_fontpath;
29
30
31 static const int vtx_img_red[8] = { 0, 255, 0, 255, 0, 255, 0, 255 };
32 static const int vtx_img_green[8] = { 0, 0, 255, 255, 0, 0, 255, 255 };
33 static const int vtx_img_blue[8] = { 0, 0, 0, 0, 255, 255, 255, 255 };
34
35 static const vtxpage_t *vtxpage;
36 static const vtxbmfont_t *vtxfont;
37 static int reveal;
38
39
40
41 void
42 export_ascii(FILE *file, const vtxpage_t *page, int show_hidden) {
43 int pos;
44
45 for (pos = 0; pos < VTX_PAGESIZE; pos++) {
46 if (!(page->attrib[pos] & VTX_HIDDEN) || show_hidden) {
47 fputc(vtx2iso_table[page->chr[pos]], file);
48 } else {
49 fputc(' ', file);
50 }
51 if (pos % 40 == 39)
52 fputc('\n', file);
53 }
54 }
55
56
57 void
58 save_vtx(FILE *file, const byte_t *buffer, const vtx_pageinfo_t *info, int virtual) {
59 fputs("VTXV4", file);
60 fputc(info->pagenum & 0xff, file);
61 fputc(info->pagenum / 0x100, file);
62 fputc(info->hour & 0xff, file);
63 fputc(info->minute & 0xff, file);
64 fputc(info->charset & 0xff, file);
65 fputc(info->delete << 7 | info->headline << 6 | info->subtitle << 5 | info->supp_header << 4 |
66 info->update << 3 | info->inter_seq << 2 | info->dis_disp << 1 | info->serial << 0, file);
67 fputc(info->notfound << 7 | info->pblf << 6 | info->hamming << 5 | (!!virtual) << 4 |
68 0 << 3, file);
69 fwrite(buffer, 1, virtual ? VTX_VIRTUALSIZE : VTX_PAGESIZE, file);
70 }
71
72
73 int
74 load_vtx(FILE *file, byte_t *buffer, vtx_pageinfo_t *info, int *virtual) {
75 byte_t tmp_buffer[VTX_VIRTUALSIZE];
76 vtx_pageinfo_t tmp_inf;
77 unsigned char tmpstr[256];
78 struct stat st;
79 int pos, tmpbits, is_virtual = FALSE, is_sevenbit = FALSE;
80
81 memset(tmp_buffer, ' ', sizeof(tmp_buffer));
82 if (fscanf(file, "VTXV%c", tmpstr) != 1) {
83 if (fstat(fileno(file), &st) < 0) {
84 return LOADERR;
85 /* The stupid INtv format doesn't use a signature, so we have to use the file-length instead */
86 } else if (st.st_size != 1008) {
87 return LOADEMAGIC;
88 }
89 memset(&tmp_inf, 0, sizeof(tmp_inf)); /* Read ITV-file */
90 rewind(file);
91 for (pos = 0; pos <= 23; pos++) {
92 fseek(file, 2, SEEK_CUR);
93 fread(tmp_buffer + pos * 40, 40, 1, file);
94 }
95 /* The first 8 bytes in the INtv-format usually contain garbage (or data I don't understand) */
96 memset(tmp_buffer, vtx_mkparity(' '), 8 * sizeof(byte_t));
97 for (pos = 0; pos <= 2; pos++) {
98 tmpstr[pos] = tmp_buffer[8 + pos];
99 vtx_chkparity(&tmpstr[pos]);
100 }
101 tmpstr[3] = '\0';
102 sscanf(tmpstr, "%3x", &tmp_inf.pagenum);
103 if (!vtx_chkpgnum(tmp_inf.pagenum, TRUE)) {
104 tmp_inf.pagenum = 0;
105 }
106 if (virtual) {
107 *virtual = FALSE;
108 }
109 } else {
110 if (tmpstr[0] != '2' && tmpstr[0] != '3' && tmpstr[0] != '4') {
111 return LOADEVERSION;
112 }
113 tmp_inf.pagenum = fgetc(file) + 0x100 * fgetc(file); /* Read VTX-file */
114 tmp_inf.hour = fgetc(file);
115 tmp_inf.minute = fgetc(file);
116 tmp_inf.charset = fgetc(file);
117 tmpbits = fgetc(file);
118 tmp_inf.delete = !!(tmpbits & 0x80);
119 tmp_inf.headline = !!(tmpbits & 0x40);
120 tmp_inf.subtitle = !!(tmpbits & 0x20);
121 tmp_inf.supp_header = !!(tmpbits & 0x10);
122 tmp_inf.update = !!(tmpbits & 8);
123 tmp_inf.inter_seq = !!(tmpbits & 4);
124 tmp_inf.dis_disp = !!(tmpbits & 2);
125 tmp_inf.serial = (tmpbits & 1);
126 tmpbits = fgetc(file);
127 tmp_inf.notfound = !!(tmpbits & 0x80);
128 tmp_inf.pblf = !!(tmpbits & 0x40);
129 tmp_inf.hamming = !!(tmpbits & 0x20);
130 if (tmpstr[0] == '3') {
131 is_virtual = TRUE;
132 } else if (tmpstr[0] == '4') {
133 is_virtual = !!(tmpbits & 0x10);
134 is_sevenbit = !!(tmpbits & 8);
135 }
136 fread(tmp_buffer, is_virtual ? VTX_VIRTUALSIZE : VTX_PAGESIZE, 1, file);
137 if (virtual) {
138 *virtual = is_virtual;
139 }
140 }
141 if (feof(file)) {
142 return LOADECORRUPT;
143 }
144 if (ferror(file)) {
145 return LOADERR;
146 }
147 if (buffer) {
148 /* If file was saved in seven-bit mode, fake parity bit.
149 * FIXME: Should we add parity to the virtual part too?
150 */
151 if (is_sevenbit) {
152 for (pos = 0; pos < VTX_PAGESIZE; pos++) {
153 tmp_buffer[pos] = vtx_mkparity(tmp_buffer[pos]);
154 }
155 }
156 memcpy(buffer, tmp_buffer, is_virtual ? VTX_VIRTUALSIZE : VTX_PAGESIZE);
157 memset(buffer, vtx_mkparity(7), 8);
158 }
159 if (info) {
160 *info = tmp_inf;
161 }
162 return LOADOK;
163 }
Imprint / Impressum