]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
remove rcs tags
[btb/d2x.git] / main / text.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code for localizable text
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "pstypes.h"
28 #include "cfile.h"
29 #include "u_mem.h"
30 #include "error.h"
31
32 #include "inferno.h"
33 #include "text.h"
34 #include "args.h"
35
36 #define SHAREWARE_TEXTSIZE  14677
37
38 char *text;
39
40 char *Text_string[N_TEXT_STRINGS];
41
42 void free_text()
43 {
44         d_free(text);
45 }
46
47 // rotates a byte left one bit, preserving the bit falling off the right
48 void
49 encode_rotate_left(char *c)
50 {
51         int found;
52
53         found = 0;
54         if (*c & 0x80)
55                 found = 1;
56         *c = *c << 1;
57         if (found)
58                 *c |= 0x01;
59 }
60
61 #define BITMAP_TBL_XOR 0xD3
62
63 //decode an encoded line of text of bitmaps.tbl
64 void decode_text_line(char *p)
65 {
66         for (;*p;p++) {
67                 encode_rotate_left(p);
68                 *p = *p ^ BITMAP_TBL_XOR;
69                 encode_rotate_left(p);
70         }
71 }
72
73 // decode buffer of text, preserves newlines
74 void decode_text(char *buf, int len)
75 {
76         char *ptr;
77         int i;
78
79         for (i = 0, ptr = buf; i < len; i++, ptr++)
80         {
81                 if (*ptr != '\n')
82                 {
83                         encode_rotate_left(ptr);
84                         *ptr = *ptr  ^ BITMAP_TBL_XOR;
85                         encode_rotate_left(ptr);
86                 }
87         }
88 }
89
90
91 #if !defined(_MSC_VER) && !defined(macintosh)
92 #include <unistd.h>
93 #endif
94 //load all the text strings for Descent
95 void load_text()
96 {
97         CFILE  *tfile;
98         CFILE *ifile;
99         int len,i, have_binary = 0;
100         char *tptr;
101         char *filename="descent.tex";
102
103         if ((i=FindArg("-text"))!=0)
104                 filename = Args[i+1];
105
106         if ((tfile = cfopen(filename,"rb")) == NULL) {
107                 filename="descent.txb";
108                 if ((ifile = cfopen(filename, "rb")) == NULL) {
109                         Error("Cannot open file DESCENT.TEX or DESCENT.TXB");
110                         return;
111                 }
112                 have_binary = 1;
113
114                 len = cfilelength(ifile);
115
116                 MALLOC(text,char,len);
117
118                 atexit(free_text);
119
120                 cfread(text,1,len,ifile);
121
122                 cfclose(ifile);
123
124         } else {
125                 int c;
126                 char * p;
127
128                 len = cfilelength(tfile);
129
130                 MALLOC(text,char,len);
131
132                 atexit(free_text);
133
134                 //fread(text,1,len,tfile);
135                 p = text;
136                 do {
137                         c = cfgetc( tfile );
138                         if ( c != 13 )
139                                 *p++ = c;
140                 } while ( c!=EOF );
141
142                 cfclose(tfile);
143         }
144
145         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
146                 char *p;
147
148                 Text_string[i] = tptr;
149
150                 tptr = strchr(tptr,'\n');
151
152                 if (!tptr)
153                 {
154                         if (i == 644) break;    /* older datafiles */
155
156                         Error("Not enough strings in text file - expecting %d, found %d\n", N_TEXT_STRINGS, i);
157                 }
158
159                 if ( tptr ) *tptr++ = 0;
160
161                 if (have_binary)
162                         decode_text_line(Text_string[i]);
163
164                 //scan for special chars (like \n)
165                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
166                         char newchar, *q;
167
168                         if (p[1] == 'n') newchar = '\n';
169                         else if (p[1] == 't') newchar = '\t';
170                         else if (p[1] == '\\') newchar = '\\';
171                         else
172                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename);
173
174                         p[0] = newchar;
175
176                         //shift rest of string to left
177                         for (q = p+1; *q; q++) {
178                                 *q = *(q+1);
179                         }
180
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