]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
fix bugfix of 2004-05-15
[btb/d2x.git] / main / text.c
1 /* $Id: text.c,v 1.13 2004-08-28 23:17:45 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.13 2004-08-28 23:17:45 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 #include "compbit.h"
41
42 #define SHAREWARE_TEXTSIZE  14677
43
44 char *text;
45
46 char *Text_string[N_TEXT_STRINGS];
47
48 void free_text()
49 {
50         d_free(text);
51 }
52
53 // rotates a byte left one bit, preserving the bit falling off the right
54 void
55 encode_rotate_left(char *c)
56 {
57         int found;
58
59         found = 0;
60         if (*c & 0x80)
61                 found = 1;
62         *c = *c << 1;
63         if (found)
64                 *c |= 0x01;
65 }
66
67 //decode and encoded line of text
68 void decode_text_line(char *p)
69 {
70         for (;*p;p++) {
71                 encode_rotate_left(p);
72                 *p = *p ^ BITMAP_TBL_XOR;
73                 encode_rotate_left(p);
74         }
75 }
76
77 #if !defined(_MSC_VER) && !defined(macintosh)
78 #include <unistd.h>
79 #endif
80 //load all the text strings for Descent
81 void load_text()
82 {
83         CFILE  *tfile;
84         CFILE *ifile;
85         int len,i, have_binary = 0;
86         char *tptr;
87         char *filename="descent.tex";
88
89         if ((i=FindArg("-text"))!=0)
90                 filename = Args[i+1];
91
92         if ((tfile = cfopen(filename,"rb")) == NULL) {
93                 filename="descent.txb";
94                 if ((ifile = cfopen(filename, "rb")) == NULL) {
95                         Warning("Cannot open file DESCENT.TEX or DESCENT.TXB");
96                         return;
97                 }
98                 have_binary = 1;
99
100                 len = cfilelength(ifile);
101
102                 MALLOC(text,char,len);
103
104                 atexit(free_text);
105
106                 cfread(text,1,len,ifile);
107
108                 cfclose(ifile);
109
110         } else {
111                 int c;
112                 char * p;
113
114                 len = cfilelength(tfile);
115
116                 MALLOC(text,char,len);
117
118                 atexit(free_text);
119
120                 //fread(text,1,len,tfile);
121                 p = text;
122                 do {
123                         c = cfgetc( tfile );
124                         if ( c != 13 )
125                                 *p++ = c;
126                 } while ( c!=EOF );
127
128                 cfclose(tfile);
129         }
130
131         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
132                 char *p;
133
134                 Text_string[i] = tptr;
135
136                 tptr = strchr(tptr,'\n');
137
138                 if (!tptr)
139                 {
140                         if (i == 644) break;    /* older datafiles */
141
142                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
143                 }
144
145                 if ( tptr ) *tptr++ = 0;
146
147                 if (have_binary)
148                         decode_text_line(Text_string[i]);
149
150                 //scan for special chars (like \n)
151                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
152                         char newchar;
153
154                         if (p[1] == 'n') newchar = '\n';
155                         else if (p[1] == 't') newchar = '\t';
156                         else if (p[1] == '\\') newchar = '\\';
157                         else
158                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
159
160                         p[0] = newchar;
161                         strcpy(p+1,p+2);
162                         p++;
163                 }
164
165         }
166
167
168         if (i == 644)
169         {
170                 if (len == SHAREWARE_TEXTSIZE)
171                 {
172                         Text_string[173] = Text_string[172];
173                         Text_string[172] = Text_string[171];
174                         Text_string[171] = Text_string[170];
175                         Text_string[170] = Text_string[169];
176                         Text_string[169] = "Windows Joystick";
177                 }
178
179                 Text_string[644] = "Z1";
180                 Text_string[645] = "UN";
181                 Text_string[647] = "P1";
182                 Text_string[648] = "R1";
183                 Text_string[649] = "Y1";
184         }
185
186         //Assert(tptr==text+len || tptr==text+len-2);
187
188 }
189
190