]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
better fix for model data freeing bug
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.15 2004-10-23 17:42:13 schaffner 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.15 2004-10-23 17:42:13 schaffner 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 #if !defined(_MSC_VER) && !defined(macintosh)
79 #include <unistd.h>
80 #endif
81 //load all the text strings for Descent
82 void load_text()
83 {
84         CFILE  *tfile;
85         CFILE *ifile;
86         int len,i, have_binary = 0;
87         char *tptr;
88         char *filename="descent.tex";
89
90         if ((i=FindArg("-text"))!=0)
91                 filename = Args[i+1];
92
93         if ((tfile = cfopen(filename,"rb")) == NULL) {
94                 filename="descent.txb";
95                 if ((ifile = cfopen(filename, "rb")) == NULL) {
96                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
97                         return;
98                 }
99                 have_binary = 1;
100
101                 len = cfilelength(ifile);
102
103                 MALLOC(text,char,len);
104
105                 atexit(free_text);
106
107                 cfread(text,1,len,ifile);
108
109                 cfclose(ifile);
110
111         } else {
112                 int c;
113                 char * p;
114
115                 len = cfilelength(tfile);
116
117                 MALLOC(text,char,len);
118
119                 atexit(free_text);
120
121                 //fread(text,1,len,tfile);
122                 p = text;
123                 do {
124                         c = cfgetc( tfile );
125                         if ( c != 13 )
126                                 *p++ = c;
127                 } while ( c!=EOF );
128
129                 cfclose(tfile);
130         }
131
132         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
133                 char *p;
134
135                 Text_string[i] = tptr;
136
137                 tptr = strchr(tptr,'\n');
138
139                 if (!tptr)
140                 {
141                         if (i == 644) break;    /* older datafiles */
142
143                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
144                 }
145
146                 if ( tptr ) *tptr++ = 0;
147
148                 if (have_binary)
149                         decode_text_line(Text_string[i]);
150
151                 //scan for special chars (like \n)
152                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
153                         char newchar;
154
155                         if (p[1] == 'n') newchar = '\n';
156                         else if (p[1] == 't') newchar = '\t';
157                         else if (p[1] == '\\') newchar = '\\';
158                         else
159                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
160
161                         p[0] = newchar;
162                         strcpy(p+1,p+2);
163                         p++;
164                 }
165
166         }
167
168
169         if (i == 644)
170         {
171                 if (len == SHAREWARE_TEXTSIZE)
172                 {
173                         Text_string[173] = Text_string[172];
174                         Text_string[172] = Text_string[171];
175                         Text_string[171] = Text_string[170];
176                         Text_string[170] = Text_string[169];
177                         Text_string[169] = "Windows Joystick";
178                 }
179
180                 Text_string[644] = "Z1";
181                 Text_string[645] = "UN";
182                 Text_string[646] = "P1";
183                 Text_string[647] = "R1";
184                 Text_string[648] = "Y1";
185         }
186
187         //Assert(tptr==text+len || tptr==text+len-2);
188
189 }
190
191