]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
better error message when hog not found
[btb/d2x.git] / main / text.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: text.c,v 1.4 2002-04-19 21:26:30 bradleyb Exp $";
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "pstypes.h"
27 #include "cfile.h"
28 #include "u_mem.h"
29 #include "error.h"
30
31 #include "inferno.h"
32 #include "text.h"
33 #include "args.h"
34 #include "compbit.h"
35
36 char *text;
37
38 char *Text_string[N_TEXT_STRINGS];
39
40 void free_text()
41 {
42         d_free(text);
43 }
44
45 // rotates a byte left one bit, preserving the bit falling off the right
46 void
47 encode_rotate_left(char *c)
48 {
49         int found;
50
51         found = 0;
52         if (*c & 0x80)
53                 found = 1;
54         *c = *c << 1;
55         if (found)
56                 *c |= 0x01;
57 }
58
59 //decode and encoded line of text
60 void decode_text_line(char *p)
61 {
62         for (;*p;p++) {
63                 encode_rotate_left(p);
64                 *p = *p ^ BITMAP_TBL_XOR;
65                 encode_rotate_left(p);
66         }
67 }
68
69 #include <unistd.h>
70 //load all the text strings for Descent
71 void load_text()
72 {
73         CFILE  *tfile;
74         CFILE *ifile;
75         int len,i, have_binary = 0;
76         char *tptr;
77         char *filename="descent.tex";
78
79         if ((i=FindArg("-text"))!=0)
80                 filename = Args[i+1];
81
82         if ((tfile = cfopen(filename,"rb")) == NULL) {
83                 filename="descent.txb";
84                 if ((ifile = cfopen(filename, "rb")) == NULL) {
85                         char cwd[_MAX_PATH];
86                         getcwd(cwd, _MAX_PATH);
87                         Error("Cannot open file DESCENT.TEX or DESCENT.TXB\nIs descent2.hog in %s?", AltHogdir_initialized?AltHogDir:cwd);
88                 }
89                 have_binary = 1;
90
91                 len = cfilelength(ifile);
92
93                 MALLOC(text,char,len);
94
95                 atexit(free_text);
96
97                 cfread(text,1,len,ifile);
98
99                 cfclose(ifile);
100
101         } else {
102                 int c;
103                 char * p;
104
105                 len = cfilelength(tfile);
106
107                 MALLOC(text,char,len);
108
109                 atexit(free_text);
110
111                 //fread(text,1,len,tfile);
112                 p = text;
113                 do {
114                         c = cfgetc( tfile );
115                         if ( c != 13 )
116                                 *p++ = c;
117                 } while ( c!=EOF );
118
119                 cfclose(tfile);
120         }
121
122         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
123                 char *p;
124
125                 Text_string[i] = tptr;
126
127                 tptr = strchr(tptr,'\n');
128
129                 #ifdef MACINTOSH                        // total hack for mac patch since they don't want to patch data.
130                 if (!tptr && (i == 644) )
131                         break;
132                 #else
133                 if (!tptr)
134                 {
135                         Warning("Not enough strings in text file - expecting %d, found %d\nThis probably means you have the wrong version of the descent 2 datafiles. You need version 1.2\n",N_TEXT_STRINGS,i);
136                         if (i == 644) break;
137                 }
138                 #endif
139
140                 if ( tptr ) *tptr++ = 0;
141
142                 if (have_binary)
143                         decode_text_line(Text_string[i]);
144
145                 //scan for special chars (like \n)
146                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
147                         char newchar;
148
149                         if (p[1] == 'n') newchar = '\n';
150                         else if (p[1] == 't') newchar = '\t';
151                         else if (p[1] == '\\') newchar = '\\';
152                         else
153                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename); 
154
155                         p[0] = newchar;
156                         strcpy(p+1,p+2);
157                         p++;
158                 }
159  
160         }
161
162 //      Assert(tptr==text+len || tptr==text+len-2);
163         
164 }
165
166