]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
SHAREWARE fixes
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.9 2003-06-16 06:57:34 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Code for localizable text
18  *
19  * Old Log:
20  * Revision 1.1  1995/05/16  15:31:44  allender
21  * Initial revision
22  *
23  * Revision 2.0  1995/02/27  11:33:09  john
24  * New version 2.0, which has no anonymous unions, builds with
25  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
26  *
27  * Revision 1.11  1994/12/14  12:53:23  matt
28  * Improved error handling
29  *
30  * Revision 1.10  1994/12/09  18:36:44  john
31  * Added code to make text read from hogfile.
32  *
33  * Revision 1.9  1994/12/08  20:56:34  john
34  * More cfile stuff.
35  *
36  * Revision 1.8  1994/12/08  17:20:06  yuan
37  * Cfiling stuff.
38  *
39  * Revision 1.7  1994/12/05  15:10:36  allender
40  * support encoded descent.tex file (descent.txb)
41  *
42  * Revision 1.6  1994/12/01  14:18:34  matt
43  * Now support backslash chars in descent.tex file
44  *
45  * Revision 1.5  1994/10/27  00:13:10  john
46  * Took out cfile.
47  *
48  * Revision 1.4  1994/07/11  15:33:49  matt
49  * Put in command-line switch to load different text files
50  *
51  * Revision 1.3  1994/07/10  09:56:25  yuan
52  * #include <stdio.h> added for FILE type.
53  *
54  * Revision 1.2  1994/07/09  22:48:14  matt
55  * Added localizable text
56  *
57  * Revision 1.1  1994/07/09  21:30:46  matt
58  * Initial revision
59  *
60  *
61  */
62
63 #ifdef HAVE_CONFIG_H
64 #include <conf.h>
65 #endif
66
67 #ifdef RCS
68 static char rcsid[] = "$Id: text.c,v 1.9 2003-06-16 06:57:34 btb Exp $";
69 #endif
70
71 #include <stdlib.h>
72 #include <string.h>
73
74 #include "pstypes.h"
75 #include "cfile.h"
76 #include "u_mem.h"
77 #include "error.h"
78
79 #include "inferno.h"
80 #include "text.h"
81 #include "args.h"
82 #include "compbit.h"
83
84 char *text;
85
86 char *Text_string[N_TEXT_STRINGS];
87
88 void free_text()
89 {
90         d_free(text);
91 }
92
93 // rotates a byte left one bit, preserving the bit falling off the right
94 void
95 encode_rotate_left(char *c)
96 {
97         int found;
98
99         found = 0;
100         if (*c & 0x80)
101                 found = 1;
102         *c = *c << 1;
103         if (found)
104                 *c |= 0x01;
105 }
106
107 //decode and encoded line of text
108 void decode_text_line(char *p)
109 {
110         for (;*p;p++) {
111                 encode_rotate_left(p);
112                 *p = *p ^ BITMAP_TBL_XOR;
113                 encode_rotate_left(p);
114         }
115 }
116
117 #include <unistd.h>
118 //load all the text strings for Descent
119 void load_text()
120 {
121         CFILE  *tfile;
122         CFILE *ifile;
123         int len,i, have_binary = 0;
124         char *tptr;
125         char *filename="descent.tex";
126
127         if ((i=FindArg("-text"))!=0)
128                 filename = Args[i+1];
129
130         if ((tfile = cfopen(filename,"rb")) == NULL) {
131                 filename="descent.txb";
132                 if ((ifile = cfopen(filename, "rb")) == NULL) {
133                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
134                         return;
135                 }
136                 have_binary = 1;
137
138                 len = cfilelength(ifile);
139
140                 MALLOC(text,char,len);
141
142                 atexit(free_text);
143
144                 cfread(text,1,len,ifile);
145
146                 cfclose(ifile);
147
148         } else {
149                 int c;
150                 char * p;
151
152                 len = cfilelength(tfile);
153
154                 MALLOC(text,char,len);
155
156                 atexit(free_text);
157
158                 //fread(text,1,len,tfile);
159                 p = text;
160                 do {
161                         c = cfgetc( tfile );
162                         if ( c != 13 )
163                                 *p++ = c;
164                 } while ( c!=EOF );
165
166                 cfclose(tfile);
167         }
168
169         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
170                 char *p;
171
172                 Text_string[i] = tptr;
173
174                 tptr = strchr(tptr,'\n');
175
176 #ifdef MACINTOSH    // total hack for mac patch since they don't want to patch data.
177                 if (!tptr && (i == 644) )
178                         break;
179 #else
180                 if (!tptr)
181                 {
182                         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);
183                         if (i == 644) break;
184                 }
185 #endif
186
187                 if ( tptr ) *tptr++ = 0;
188
189                 if (have_binary)
190                         decode_text_line(Text_string[i]);
191
192                 //scan for special chars (like \n)
193                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
194                         char newchar;
195
196                         if (p[1] == 'n') newchar = '\n';
197                         else if (p[1] == 't') newchar = '\t';
198                         else if (p[1] == '\\') newchar = '\\';
199                         else
200                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
201
202                         p[0] = newchar;
203                         strcpy(p+1,p+2);
204                         p++;
205                 }
206
207         }
208
209         //Assert(tptr==text+len || tptr==text+len-2);
210
211 }
212
213