]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
revert to Error if text file not found.
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.17 2005-03-20 04:05:32 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  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #ifdef RCS
26 static char rcsid[] = "$Id: text.c,v 1.17 2005-03-20 04:05:32 btb Exp $";
27 #endif
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "pstypes.h"
33 #include "cfile.h"
34 #include "u_mem.h"
35 #include "error.h"
36
37 #include "inferno.h"
38 #include "text.h"
39 #include "args.h"
40
41 #define SHAREWARE_TEXTSIZE  14677
42
43 char *text;
44
45 char *Text_string[N_TEXT_STRINGS];
46
47 void free_text()
48 {
49         d_free(text);
50 }
51
52 // rotates a byte left one bit, preserving the bit falling off the right
53 void
54 encode_rotate_left(char *c)
55 {
56         int found;
57
58         found = 0;
59         if (*c & 0x80)
60                 found = 1;
61         *c = *c << 1;
62         if (found)
63                 *c |= 0x01;
64 }
65
66 #define BITMAP_TBL_XOR 0xD3
67
68 //decode an encoded line of text of bitmaps.tbl
69 void decode_text_line(char *p)
70 {
71         for (;*p;p++) {
72                 encode_rotate_left(p);
73                 *p = *p ^ BITMAP_TBL_XOR;
74                 encode_rotate_left(p);
75         }
76 }
77
78 // decode buffer of text, preserves newlines
79 void decode_text(char *buf, int len)
80 {
81         char *ptr;
82         int i;
83
84         for (i = 0, ptr = buf; i < len; i++, ptr++)
85         {
86                 if (*ptr != '\n')
87                 {
88                         encode_rotate_left(ptr);
89                         *ptr = *ptr  ^ BITMAP_TBL_XOR;
90                         encode_rotate_left(ptr);
91                 }
92         }
93 }
94
95
96 #if !defined(_MSC_VER) && !defined(macintosh)
97 #include <unistd.h>
98 #endif
99 //load all the text strings for Descent
100 void load_text()
101 {
102         CFILE  *tfile;
103         CFILE *ifile;
104         int len,i, have_binary = 0;
105         char *tptr;
106         char *filename="descent.tex";
107
108         if ((i=FindArg("-text"))!=0)
109                 filename = Args[i+1];
110
111         if ((tfile = cfopen(filename,"rb")) == NULL) {
112                 filename="descent.txb";
113                 if ((ifile = cfopen(filename, "rb")) == NULL) {
114                         Error("Cannot open file DESCENT.TEX or DESCENT.TXB");
115                         return;
116                 }
117                 have_binary = 1;
118
119                 len = cfilelength(ifile);
120
121                 MALLOC(text,char,len);
122
123                 atexit(free_text);
124
125                 cfread(text,1,len,ifile);
126
127                 cfclose(ifile);
128
129         } else {
130                 int c;
131                 char * p;
132
133                 len = cfilelength(tfile);
134
135                 MALLOC(text,char,len);
136
137                 atexit(free_text);
138
139                 //fread(text,1,len,tfile);
140                 p = text;
141                 do {
142                         c = cfgetc( tfile );
143                         if ( c != 13 )
144                                 *p++ = c;
145                 } while ( c!=EOF );
146
147                 cfclose(tfile);
148         }
149
150         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
151                 char *p;
152
153                 Text_string[i] = tptr;
154
155                 tptr = strchr(tptr,'\n');
156
157                 if (!tptr)
158                 {
159                         if (i == 644) break;    /* older datafiles */
160
161                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
162                 }
163
164                 if ( tptr ) *tptr++ = 0;
165
166                 if (have_binary)
167                         decode_text_line(Text_string[i]);
168
169                 //scan for special chars (like \n)
170                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
171                         char newchar;
172
173                         if (p[1] == 'n') newchar = '\n';
174                         else if (p[1] == 't') newchar = '\t';
175                         else if (p[1] == '\\') newchar = '\\';
176                         else
177                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
178
179                         p[0] = newchar;
180                         strcpy(p+1,p+2);
181                         p++;
182                 }
183
184         }
185
186
187         if (i == 644)
188         {
189                 if (len == SHAREWARE_TEXTSIZE)
190                 {
191                         Text_string[173] = Text_string[172];
192                         Text_string[172] = Text_string[171];
193                         Text_string[171] = Text_string[170];
194                         Text_string[170] = Text_string[169];
195                         Text_string[169] = "Windows Joystick";
196                 }
197
198                 Text_string[644] = "Z1";
199                 Text_string[645] = "UN";
200                 Text_string[646] = "P1";
201                 Text_string[647] = "R1";
202                 Text_string[648] = "Y1";
203         }
204
205         //Assert(tptr==text+len || tptr==text+len-2);
206
207 }
208
209