File: sttbf.c

package info (click to toggle)
wv 1.2.9-9
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,284 kB
  • sloc: ansic: 31,044; sh: 11,660; xml: 1,677; makefile: 20
file content (344 lines) | stat: -rw-r--r-- 8,992 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/* wvWare
 * Copyright (C) Caolan McNamara, Dom Lachowicz, and others
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "wv.h"

/*
STTBF (STring TaBle stored in File)

Word has many tables of strings that are stored as Pascal type strings.
STTBFs consist of an optional short containing 0xFFFF, indicating that the
strings are extended character strings, a short indicating how many strings
are included in the string table, another short indicating the size in bytes
of the extra data stored with each string and each string followed by the
extra data. Non-extended charater Pascal strings begin with a single byte
length count which describes how many characters follow the length byte in
the string. If pst is a pointer to an array of characters storing a Pascal
style string then the length of the string is *pst+1. In an STTBF Pascal
style strings are concatenated one after another until the length of the
STTBF recorded in the FIB is exhausted. Extra data associated with a string
may also be stored in an sttbf. When extra data is stored for an STTBF, it
is written at the end of each string. For example: The extra data for an
STTBF consists of a short. If the string "Cat" were stored, the actual entry
in the string table would consist of a length byte containing 3 (3 for
"Cat") followed by the bytes 'C' 'a' 't', followed by the 2 bytes containing
the short. Extended character strings are stored just the same, except they
have a double byte length count and each extended character occupies two
bytes.
*/


void
wvGetSTTBF (STTBF * anS, U32 offset, U32 len, wvStream * fd)
{
    int i, j;
    U16 slen;

    anS->s8strings = NULL;
    anS->u16strings = NULL;
    anS->extradata = NULL;

    wvTrace (("sttbf offset is %x,len %d\n", offset, len));
    if (len == 0)
      {
	  anS->nostrings = 0;
	  return;
      }
    wvStream_goto (fd, offset);
    anS->extendedflag = read_16ubit (fd);
    if (anS->extendedflag != 0xFFFF)
      {
	  /*old U8 strings */
	  anS->nostrings = anS->extendedflag;
      }
    else
      {
	  /*U16 chars */
	  anS->nostrings = read_16ubit (fd);
      }
    anS->extradatalen = read_16ubit (fd);

    if (anS->extendedflag == 0xFFFF)
	anS->u16strings = (U16 **) wvMalloc (sizeof (U16 *) * anS->nostrings);
    else
	anS->s8strings = (S8 **) wvMalloc (sizeof (S8 *) * anS->nostrings);

    if (anS->extradatalen)
      {
	  anS->extradata = (U8 **) wvMalloc (sizeof (U8 *) * anS->nostrings);
	  for (i = 0; i < anS->nostrings; i++)
	      anS->extradata[i] = (U8 *) wvMalloc (anS->extradatalen);
      }

    if (anS->extendedflag == 0xFFFF)
      {
	  for (i = 0; i < anS->nostrings; i++)
	    {
		slen = read_16ubit (fd);
		if (slen == 0)
		    anS->u16strings[i] = NULL;
		else
		  {
		      anS->u16strings[i] =
			  (U16 *) wvMalloc (sizeof (U16) * (slen + 1));
		      for (j = 0; j < slen; j++)
			  anS->u16strings[i][j] = read_16ubit (fd);
		      anS->u16strings[i][j] = 0;
		  }
		if (anS->extradatalen)
		    for (j = 0; j < anS->extradatalen; j++)
			anS->extradata[i][j] = read_8ubit (fd);
	    }
      }
    else
      {
	  for (i = 0; i < anS->nostrings; i++)
	    {
		slen = read_8ubit (fd);
		if (slen == 0)
		    anS->s8strings[i] = NULL;
		else
		  {
		      anS->s8strings[i] = (S8 *) wvMalloc (slen + 1);
		      for (j = 0; j < slen; j++)
			  anS->s8strings[i][j] = read_8ubit (fd);
		      anS->s8strings[i][j] = 0;
		  }
		if (anS->extradatalen)
		    for (j = 0; j < anS->extradatalen; j++)
			anS->extradata[i][j] = read_8ubit (fd);
	    }
      }
}

void
wvReleaseSTTBF (STTBF * item)
{
    int i;

    if (item->s8strings != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	      wvFree (item->s8strings[i]);
	  wvFree (item->s8strings);
      }
    if (item->u16strings != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	      wvFree (item->u16strings[i]);
	  wvFree (item->u16strings);
      }
    if (item->extradata != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	      wvFree (item->extradata[i]);
	  wvFree (item->extradata);
      }
}


void
wvListSTTBF (STTBF * item)
{
    int i, j;
    U16 *letter;

    if (item->s8strings != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	      fprintf (stderr, "string is %s\n", item->s8strings[i]);
      }
    else if (item->u16strings != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	    {
		fprintf (stderr, "string is ");
		letter = item->u16strings[i];
		while ((letter != NULL) && (*letter != 0))
		    fprintf (stderr, "%c", *letter++);
		fprintf (stderr, "\n");
	    }
      }

    if (item->extradata != NULL)
      {
	  for (i = 0; i < item->nostrings; i++)
	      for (j = 0; j < item->extradatalen; j++)
		  fprintf (stderr, " %x ", item->extradata[i][j]);
	  fprintf (stderr, "\n");
      }
}


void
wvPrintTitle (wvParseStruct * ps, STTBF * item)
{
    int i = 0;
    CHP achp;
    wvInitCHP (&achp);

    if ((item) && (item->nostrings >= 3))
      {
	  if (item->extendedflag == 0xFFFF)
	    {
		if (item->u16strings[ibstAssocTitle] != NULL)
		  {
		      while (item->u16strings[ibstAssocTitle][i])
			{
			    wvTrace (
				     ("title char is %c\n",
				      (item->u16strings[ibstAssocTitle][i])));
			    wvOutputTextChar (item->u16strings[ibstAssocTitle]
					      [i++], 0, ps, &achp);
			}
		      return;
		  }
	    }
	  else
	    {
		if (item->s8strings[ibstAssocTitle] != NULL)
		  {
		      while (item->s8strings[ibstAssocTitle][i])
			  wvOutputTextChar (item->s8strings[ibstAssocTitle]
					    [i++], 1, ps, &achp);
		      return;
		  }
	    }
      }
    printf ("Untitled");
}


void
wvGetSTTBF6 (STTBF * anS, U32 offset, U32 len, wvStream * fd)
{
    int i, j;
    U16 slen;
    U32 table_len = 0;

    anS->s8strings = NULL;
    anS->u16strings = NULL;
    anS->extradata = NULL;
    anS->nostrings = 0;

    wvTrace (("word 6 sttbf offset is %x,len %d\n", offset, len));
    if (len == 0)
      {
	  anS->nostrings = 0;
	  return;
      }
    wvStream_goto (fd, offset);
    /* this is very bad, since there can be many more strings in a table than
       17 !!! We have to do two passes over the string table
    anS->nostrings = ibstAssocMaxWord6; */
#ifdef DEBUG
    /* the table len is a 16 bit, not 8-bit value */
    if (len != (U32) read_16ubit (fd))
	wvTrace (("word 6 sttbf len does not match up correctly, strange\n"));
#else
    /* whether debugging or not, have to get over the table length value !!! */
    read_16ubit (fd);
#endif

    while(table_len < len)
    {
       slen = read_8ubit (fd);
       table_len++;
       if(slen > 0)
       {
           anS->nostrings++;
           for(i = 0; i < slen; i++)
               read_8ubit(fd);
           table_len += slen;
       }
    }

    anS->extendedflag = ibstAssocMaxWord6;  /*just for the sake of it */
    anS->extradatalen = 0;
    anS->s8strings = (S8 **) wvMalloc (sizeof (S8 *) * anS->nostrings);

    /* now go back to the strings table */
    wvStream_goto (fd, offset + 2);

    for (i = 0; i < anS->nostrings; i++)
      {
	  slen = read_8ubit (fd);
	  if (slen == 0)
	      anS->s8strings[i] = NULL;
	  else
	    {
		anS->s8strings[i] = (S8 *) wvMalloc (slen + 1);
		for (j = 0; j < slen; j++)
		    anS->s8strings[i][j] = read_8ubit (fd);
		anS->s8strings[i][j] = 0;
	    }
      }
}


U16 *
UssrStrBegin (STTBF * sttbf, int no)
{
    if (no >= sttbf->nostrings)
	return (NULL);

    return (sttbf->u16strings[no] + 11);
}


void
wvGetGrpXst (STTBF * anS, U32 offset, U32 len, wvStream * fd)
{
    U16 slen, i;
    U32 pos = 0;

    anS->extendedflag = 1;
    anS->nostrings = 0;
    anS->extradatalen = 0;
    anS->s8strings = NULL;
    anS->u16strings = NULL;
    anS->extradata = NULL;
    if (len == 0)
	return;
    wvStream_goto (fd, offset);

    while (pos < len)
      {
	  slen = read_16ubit (fd);
	  pos += 2;
	  anS->nostrings++;
	  anS->u16strings =
	      (U16 **) realloc (anS->u16strings,
				sizeof (U16 *) * anS->nostrings);
	  anS->u16strings[anS->nostrings - 1] =
	      (U16 *) wvMalloc (sizeof (U16) * (slen + 1));
	  for (i = 0; i < slen; i++)
	      anS->u16strings[anS->nostrings - 1][i] = read_16ubit (fd);
	  anS->u16strings[anS->nostrings - 1][i] = 0;
	  pos += (i * 2);
      }

}