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