]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
Visual C, PocketPC fixes
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.11 2003-11-26 12:26:33 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.11 2003-11-26 12:26:33 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 #define SHAREWARE_TEXTSIZE  14677
85
86 char *text;
87
88 char *Text_string[N_TEXT_STRINGS];
89
90 void free_text()
91 {
92         d_free(text);
93 }
94
95 // rotates a byte left one bit, preserving the bit falling off the right
96 void
97 encode_rotate_left(char *c)
98 {
99         int found;
100
101         found = 0;
102         if (*c & 0x80)
103                 found = 1;
104         *c = *c << 1;
105         if (found)
106                 *c |= 0x01;
107 }
108
109 //decode and encoded line of text
110 void decode_text_line(char *p)
111 {
112         for (;*p;p++) {
113                 encode_rotate_left(p);
114                 *p = *p ^ BITMAP_TBL_XOR;
115                 encode_rotate_left(p);
116         }
117 }
118
119 #ifndef _MSC_VER
120 #include <unistd.h>
121 #endif
122 //load all the text strings for Descent
123 void load_text()
124 {
125         CFILE  *tfile;
126         CFILE *ifile;
127         int len,i, have_binary = 0;
128         char *tptr;
129         char *filename="descent.tex";
130
131         if ((i=FindArg("-text"))!=0)
132                 filename = Args[i+1];
133
134         if ((tfile = cfopen(filename,"rb")) == NULL) {
135                 filename="descent.txb";
136                 if ((ifile = cfopen(filename, "rb")) == NULL) {
137                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
138                         return;
139                 }
140                 have_binary = 1;
141
142                 len = cfilelength(ifile);
143
144                 MALLOC(text,char,len);
145
146                 atexit(free_text);
147
148                 cfread(text,1,len,ifile);
149
150                 cfclose(ifile);
151
152         } else {
153                 int c;
154                 char * p;
155
156                 len = cfilelength(tfile);
157
158                 MALLOC(text,char,len);
159
160                 atexit(free_text);
161
162                 //fread(text,1,len,tfile);
163                 p = text;
164                 do {
165                         c = cfgetc( tfile );
166                         if ( c != 13 )
167                                 *p++ = c;
168                 } while ( c!=EOF );
169
170                 cfclose(tfile);
171         }
172
173         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
174                 char *p;
175
176                 Text_string[i] = tptr;
177
178                 tptr = strchr(tptr,'\n');
179
180                 if (!tptr)
181                 {
182                         if (i == 644) break;    /* older datafiles */
183
184                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
185                 }
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
210         if (i == 644)
211         {
212                 if (len == SHAREWARE_TEXTSIZE)
213                 {
214                         Text_string[173] = Text_string[172];
215                         Text_string[172] = Text_string[171];
216                         Text_string[171] = Text_string[170];
217                         Text_string[170] = Text_string[169];
218                         Text_string[169] = "Windows Joystick";
219                 }
220
221                 Text_string[644] = "Z1";
222                 Text_string[645] = "UN";
223                 Text_string[647] = "P1";
224                 Text_string[648] = "R1";
225                 Text_string[649] = "Y1";
226         }
227
228         //Assert(tptr==text+len || tptr==text+len-2);
229
230 }
231
232