File: sttbf.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (286 lines) | stat: -rw-r--r-- 7,043 bytes parent folder | download
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
#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,FILE *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;
		}
	fseek(fd,offset,SEEK_SET);
	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 **)malloc(sizeof(U16 *)*anS->nostrings);
	else
		anS->s8strings = (S8 **)malloc(sizeof(S8 *)*anS->nostrings);

	if (anS->extradatalen)
		{
		anS->extradata = (U8 **)malloc(sizeof(U8 *)*anS->nostrings);
		for (i=0;i<anS->nostrings;i++)
			anS->extradata[i] = (U8 *)malloc(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 *)malloc(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] = getc(fd);
			}
		}
	else
		{
		for (i=0;i<anS->nostrings;i++)
			{
			slen = getc(fd);
			if (slen == 0)
				anS->s8strings[i] = NULL;
			else
				{
				anS->s8strings[i] = (S8 *)malloc(slen+1);
				for (j=0;j<slen;j++)
					anS->s8strings[i][j] = getc(fd);
				anS->s8strings[i][j]=0;
				}
			if (anS->extradatalen)
				for (j=0;j<anS->extradatalen;j++)
					anS->extradata[i][j] = getc(fd);
			}
		}
	}

void wvReleaseSTTBF(STTBF *item)
    {
    int i;

    if (item->s8strings!= NULL)
        {
        for(i=0;i<item->nostrings;i++)
            free(item->s8strings[i]);
        free(item->s8strings);
        }
    if (item->u16strings!= NULL)
        {
        for(i=0;i<item->nostrings;i++)
            free(item->u16strings[i]);
        free(item->u16strings);
        }
    if (item->extradata!= NULL)
        {
        for(i=0;i<item->nostrings;i++)
            free(item->extradata[i]);
        free(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,FILE *fd)
	{
	int i,j;
	U16 slen;

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

	wvTrace(("word 6 sttbf offset is %x,len %d\n",offset,len));
	if (len == 0)
		{
		anS->nostrings=0;
		return;
		}
	fseek(fd,offset,SEEK_SET);
	anS->nostrings = ibstAssocMaxWord6;
	anS->extendedflag = ibstAssocMaxWord6;	/*just for the sake of it*/
	anS->extradatalen = 0;
	anS->s8strings = (S8 **)malloc(sizeof(S8 *)*anS->nostrings);
	if (len != (U32)getc(fd))
		wvTrace(("word 6 sttbf len does not match up correctly, strange\n"));
	for (i=0;i<anS->nostrings;i++)
		{
		slen = getc(fd);
		if (slen == 0)
			anS->s8strings[i] = NULL;
		else
			{
			anS->s8strings[i] = (S8 *)malloc(slen+1);
			for (j=0;j<slen;j++)
				anS->s8strings[i][j] = getc(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,FILE *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;
	fseek(fd,offset,SEEK_SET);

	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 *)malloc(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);
		}

	}