]> icculus.org git repositories - btb/d2x.git/blob - utilities/txb2tex.c
comments/formatting
[btb/d2x.git] / utilities / txb2tex.c
1 /*
2  * Modified by Bradley Bell, 2002, 2003
3  * This program is licensed under the terms of the GPL, version 2 or later
4  */
5
6 #include <stdio.h>
7 #include <string.h>
8
9 int
10 main(int argc, char *argv[])
11 {
12         FILE *file, *outfile;
13         char outfilename[64];
14         char ch;
15         int code;
16
17         if (argc < 2) {
18                 printf("TXB2TEX V1.0 Copyright (c) Bryan Aamot, 1995\n"
19                            "Modified by Bradley Bell, 2002, 2003\n"
20                        "TXB to Text converter for Descent HOG files.\n"
21                        "Converts a *.txb descent hog file to an ascii file.\n"
22                        "Usage: TXB2TEX <txb file name> <text file name>\n"
23                        "Example: TXB2TEX briefing.txb briefing.tex\n");
24                 return 1;
25         }
26         file = fopen(argv[1], "rb");
27         if (!file) {
28                 printf("Can't open txb file (%s)\n", argv[1]);
29                 return 2;
30         }
31
32         if (argc > 2)
33                 strcpy(outfilename, argv[2]);
34         else {
35                 strcpy(outfilename, argv[1]);
36                 strcpy(strrchr(outfilename, '.'), ".tex");
37         }
38
39         outfile = fopen(outfilename, "wb");
40         if (!outfile) {
41                 printf("Can't open file (%s)\n", outfilename);
42                 fclose(file);
43                 return 2;
44         }
45         for (;;) {
46                 code = getc(file);
47                 if (feof(file)) break;
48                 if (code == 0x0a) {
49                         fprintf(outfile, "\x0d\x0a");
50                 } else {
51                         ch = (  ( (code&0x3f) << 2 ) + ( (code&0xc0) >> 6 )  ) ^ 0xa7;
52                         fprintf(outfile, "%c", ch);
53                 }
54         }
55
56         fclose(outfile);
57         fclose(file);
58
59         return 0;
60 }