]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
add IPv4 multicasting support
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.10 2003-10-10 01:42:59 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.10 2003-10-10 01:42:59 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 #include <unistd.h>
120 //load all the text strings for Descent
121 void load_text()
122 {
123         CFILE  *tfile;
124         CFILE *ifile;
125         int len,i, have_binary = 0;
126         char *tptr;
127         char *filename="descent.tex";
128
129         if ((i=FindArg("-text"))!=0)
130                 filename = Args[i+1];
131
132         if ((tfile = cfopen(filename,"rb")) == NULL) {
133                 filename="descent.txb";
134                 if ((ifile = cfopen(filename, "rb")) == NULL) {
135                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
136                         return;
137                 }
138                 have_binary = 1;
139
140                 len = cfilelength(ifile);
141
142                 MALLOC(text,char,len);
143
144                 atexit(free_text);
145
146                 cfread(text,1,len,ifile);
147
148                 cfclose(ifile);
149
150         } else {
151                 int c;
152                 char * p;
153
154                 len = cfilelength(tfile);
155
156                 MALLOC(text,char,len);
157
158                 atexit(free_text);
159
160                 //fread(text,1,len,tfile);
161                 p = text;
162                 do {
163                         c = cfgetc( tfile );
164                         if ( c != 13 )
165                                 *p++ = c;
166                 } while ( c!=EOF );
167
168                 cfclose(tfile);
169         }
170
171         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
172                 char *p;
173
174                 Text_string[i] = tptr;
175
176                 tptr = strchr(tptr,'\n');
177
178                 if (!tptr)
179                 {
180                         if (i == 644) break;    /* older datafiles */
181
182                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
183                 }
184
185                 if ( tptr ) *tptr++ = 0;
186
187                 if (have_binary)
188                         decode_text_line(Text_string[i]);
189
190                 //scan for special chars (like \n)
191                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
192                         char newchar;
193
194                         if (p[1] == 'n') newchar = '\n';
195                         else if (p[1] == 't') newchar = '\t';
196                         else if (p[1] == '\\') newchar = '\\';
197                         else
198                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
199
200                         p[0] = newchar;
201                         strcpy(p+1,p+2);
202                         p++;
203                 }
204
205         }
206
207
208         if (i == 644)
209         {
210                 if (len == SHAREWARE_TEXTSIZE)
211                 {
212                         Text_string[173] = Text_string[172];
213                         Text_string[172] = Text_string[171];
214                         Text_string[171] = Text_string[170];
215                         Text_string[170] = Text_string[169];
216                         Text_string[169] = "Windows Joystick";
217                 }
218
219                 Text_string[644] = "Z1";
220                 Text_string[645] = "UN";
221                 Text_string[647] = "P1";
222                 Text_string[648] = "R1";
223                 Text_string[649] = "Y1";
224         }
225
226         //Assert(tptr==text+len || tptr==text+len-2);
227
228 }
229
230