]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
ac632150efc05935a4745225701a13f7972b4241
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.8 2003-04-07 23:36: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.8 2003-04-07 23:36:34 btb Exp $";
69 #endif
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <string.h>
74
75 #include "pstypes.h"
76 #include "cfile.h"
77 #include "u_mem.h"
78 #include "error.h"
79
80 #include "inferno.h"
81 #include "text.h"
82 #include "args.h"
83 #include "compbit.h"
84
85 char *text;
86
87 char *Text_string[N_TEXT_STRINGS];
88
89 void free_text()
90 {
91         d_free(text);
92 }
93
94 // rotates a byte left one bit, preserving the bit falling off the right
95 void
96 encode_rotate_left(char *c)
97 {
98         int found;
99
100         found = 0;
101         if (*c & 0x80)
102                 found = 1;
103         *c = *c << 1;
104         if (found)
105                 *c |= 0x01;
106 }
107
108 //decode and encoded line of text
109 void decode_text_line(char *p)
110 {
111         for (;*p;p++) {
112                 encode_rotate_left(p);
113                 *p = *p ^ BITMAP_TBL_XOR;
114                 encode_rotate_left(p);
115         }
116 }
117
118 #include <unistd.h>
119 //load all the text strings for Descent
120 void load_text()
121 {
122         CFILE  *tfile;
123         CFILE *ifile;
124         int len,i, have_binary = 0;
125         char *tptr;
126         char *filename="descent.tex";
127
128         if ((i=FindArg("-text"))!=0)
129                 filename = Args[i+1];
130
131         if ((tfile = cfopen(filename,"rb")) == NULL) {
132                 filename="descent.txb";
133                 if ((ifile = cfopen(filename, "rb")) == NULL) {
134                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
135                         return;
136                 }
137                 have_binary = 1;
138
139                 len = cfilelength(ifile);
140
141                 MALLOC(text,char,len);
142
143                 atexit(free_text);
144
145                 cfread(text,1,len,ifile);
146
147                 cfclose(ifile);
148
149         } else {
150                 int c;
151                 char * p;
152
153                 len = cfilelength(tfile);
154
155                 MALLOC(text,char,len);
156
157                 atexit(free_text);
158
159                 //fread(text,1,len,tfile);
160                 p = text;
161                 do {
162                         c = cfgetc( tfile );
163                         if ( c != 13 )
164                                 *p++ = c;
165                 } while ( c!=EOF );
166
167                 cfclose(tfile);
168         }
169
170         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
171                 char *p;
172
173                 Text_string[i] = tptr;
174
175                 tptr = strchr(tptr,'\n');
176
177 #ifdef MACINTOSH    // total hack for mac patch since they don't want to patch data.
178                 if (!tptr && (i == 644) )
179                         break;
180 #else
181                 if (!tptr)
182                 {
183                         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);
184                         if (i == 644) break;
185                 }
186 #endif
187
188                 if ( tptr ) *tptr++ = 0;
189
190                 if (have_binary)
191                         decode_text_line(Text_string[i]);
192
193                 //scan for special chars (like \n)
194                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
195                         char newchar;
196
197                         if (p[1] == 'n') newchar = '\n';
198                         else if (p[1] == 't') newchar = '\t';
199                         else if (p[1] == '\\') newchar = '\\';
200                         else
201                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
202
203                         p[0] = newchar;
204                         strcpy(p+1,p+2);
205                         p++;
206                 }
207
208         }
209
210         //Assert(tptr==text+len || tptr==text+len-2);
211
212 }
213
214