]> icculus.org git repositories - btb/d2x.git/blob - main/text.c
changed args_find to FindArg
[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 #ifdef RCS
16 static char rcsid[] = "$Id: text.c,v 1.1.1.1 2001-01-19 03:30:01 bradleyb Exp $";
17 #endif
18
19 #include <conf.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "pstypes.h"
26 #include "cfile.h"
27 #include "u_mem.h"
28 #include "error.h"
29
30 #include "inferno.h"
31 #include "text.h"
32 #include "args.h"
33 #include "compbit.h"
34
35 char *text;
36
37 char *Text_string[N_TEXT_STRINGS];
38
39 void free_text()
40 {
41         d_free(text);
42 }
43
44 // rotates a byte left one bit, preserving the bit falling off the right
45 void
46 encode_rotate_left(char *c)
47 {
48         int found;
49
50         found = 0;
51         if (*c & 0x80)
52                 found = 1;
53         *c = *c << 1;
54         if (found)
55                 *c |= 0x01;
56 }
57
58 //decode and encoded line of text
59 void decode_text_line(char *p)
60 {
61         for (;*p;p++) {
62                 encode_rotate_left(p);
63                 *p = *p ^ BITMAP_TBL_XOR;
64                 encode_rotate_left(p);
65         }
66 }
67
68 //load all the text strings for Descent
69 void load_text()
70 {
71         CFILE  *tfile;
72         CFILE *ifile;
73         int len,i, have_binary = 0;
74         char *tptr;
75         char *filename="descent.tex";
76
77         if ((i=args_find("-text"))!=0)
78                 filename = Args[i+1];
79
80         if ((tfile = cfopen(filename,"rb")) == NULL) {
81                 filename="descent.txb";
82                 if ((ifile = cfopen(filename, "rb")) == NULL)
83                         Error("Cannot open file DESCENT.TEX or DESCENT.TXB\nIs descent2.hog in your current directory?");
84                 have_binary = 1;
85
86                 len = cfilelength(ifile);
87
88                 MALLOC(text,char,len);
89
90                 atexit(free_text);
91
92                 cfread(text,1,len,ifile);
93
94                 cfclose(ifile);
95
96         } else {
97                 int c;
98                 char * p;
99
100                 len = cfilelength(tfile);
101
102                 MALLOC(text,char,len);
103
104                 atexit(free_text);
105
106                 //fread(text,1,len,tfile);
107                 p = text;
108                 do {
109                         c = cfgetc( tfile );
110                         if ( c != 13 )
111                                 *p++ = c;
112                 } while ( c!=EOF );
113
114                 cfclose(tfile);
115         }
116
117         for (i=0,tptr=text;i<N_TEXT_STRINGS;i++) {
118                 char *p;
119
120                 Text_string[i] = tptr;
121
122                 tptr = strchr(tptr,'\n');
123
124                 #ifdef MACINTOSH                        // total hack for mac patch since they don't want to patch data.
125                 if (!tptr && (i == 644) )
126                         break;
127                 #else
128                 if (!tptr)
129                 {
130                         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);
131                         if (i == 644) break;
132                 }
133                 #endif
134
135                 if ( tptr ) *tptr++ = 0;
136
137                 if (have_binary)
138                         decode_text_line(Text_string[i]);
139
140                 //scan for special chars (like \n)
141                 for (p=Text_string[i];(p=strchr(p,'\\'));) {
142                         char newchar;
143
144                         if (p[1] == 'n') newchar = '\n';
145                         else if (p[1] == 't') newchar = '\t';
146                         else if (p[1] == '\\') newchar = '\\';
147                         else
148                                 Error("Unsupported key sequence <\\%c> on line %d of file <%s>",p[1],i+1,filename); 
149
150                         p[0] = newchar;
151                         strcpy(p+1,p+2);
152                         p++;
153                 }
154  
155         }
156
157 //      Assert(tptr==text+len || tptr==text+len-2);
158         
159 }
160
161