File: decode_simple.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 (621 lines) | stat: -rw-r--r-- 20,092 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <stdlib.h>
#include <stdio.h>
#include "wv.h"

/*
how this works,
we seek to the beginning of the text, we loop for a count of charaters that is stored in the fib.

the piecetable divides the text up into various sections, we keep track of our location vs
the next entry in that table, when we reach that location, we seek to the position that
the table tells us to go.

there are special cases for coming to the end of a section, and for the beginning and ends of
pages. for the purposes of headers and footers etc.
*/
void wvDecodeSimple(wvParseStruct *ps)
	{
	PAPX_FKP para_fkp;
	CHPX_FKP char_fkp;
	PAP apap;
    CHP achp;
	U32 piececount=0,i,j=0,spiece;
	U32 beginfc,endfc;
	U32 begincp,endcp;
	U8 chartype;
	U16 eachchar;
	U32 para_fcFirst, para_fcLim=0xffffffff;
	U32 dummy,nextpara_fcLim=0xffffffff;
	U32 char_fcFirst, char_fcLim=0xffffffff;
	U32 section_fcFirst, section_fcLim=0xffffffff;
	U32 comment_cpFirst=0xffffffffL,comment_cpLim=0xffffffffL;
	BTE *btePapx, *bteChpx;
	U32 *posPapx, *posChpx;
	U32 para_intervals, char_intervals, section_intervals,atrd_intervals;
	int para_pendingclose=0,char_pendingclose=0,section_pendingclose=0,comment_pendingclose=0;
	int para_dirty=0,char_dirty=0,section_dirty=0;
	SED *sed;
	SEP sep;
	U32 *posSedx;
	ATRD *atrd,*catrd=NULL;
	U32 *posAtrd;
	STTBF grpXstAtnOwners,SttbfAtnbkmk;
	BKF *bkf;
	U32 *posBKF;
	U32 bkf_intervals;
	BKL *bkl;
	U32 *posBKL;
	U32 bkl_intervals;

	
	/* this mountain of informatio is just to get comments organized*/
	wvGetATRD_PLCF(&atrd,&posAtrd,&atrd_intervals,ps->fib.fcPlcfandRef,ps->fib.lcbPlcfandRef,ps->tablefd);
	wvGetGrpXst(&grpXstAtnOwners,ps->fib.fcGrpXstAtnOwners,ps->fib.lcbGrpXstAtnOwners,ps->tablefd);
	wvTrace(("offset is %x, len is %d\n",ps->fib.fcSttbfAtnbkmk,ps->fib.lcbSttbfAtnbkmk));
	wvGetSTTBF(&SttbfAtnbkmk,ps->fib.fcSttbfAtnbkmk,ps->fib.lcbSttbfAtnbkmk,ps->tablefd);
	wvGetBKF_PLCF(&bkf,&posBKF,&bkf_intervals,ps->fib.fcPlcfAtnbkf,ps->fib.lcbPlcfAtnbkf,ps->tablefd);
	wvGetBKL_PLCF(&bkl,&posBKL,&bkl_intervals,ps->fib.fcPlcfAtnbkl,ps->fib.lcbPlcfAtnbkl,ps->tablefd);

	/*we will need the stylesheet to do anything useful with layout and look*/
	wvGetSTSH(&ps->stsh,ps->fib.fcStshf,ps->fib.lcbStshf,ps->tablefd);

	/* get font list */
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7) )
		wvGetFFN_STTBF6(&ps->fonts, ps->fib.fcSttbfffn, ps->fib.lcbSttbfffn, ps->tablefd);
	else
		wvGetFFN_STTBF(&ps->fonts, ps->fib.fcSttbfffn, ps->fib.lcbSttbfffn, ps->tablefd);
	      
	/*we will need the table of names to answer questions like the name of the doc*/
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7) )
		{
		wvGetSTTBF6(&ps->anSttbfAssoc,ps->fib.fcSttbfAssoc,ps->fib.lcbSttbfAssoc,ps->tablefd);
		wvGetSTTBF6(&ps->Sttbfbkmk,ps->fib.fcSttbfbkmk,ps->fib.lcbSttbfbkmk,ps->tablefd);
		}
	else /*word 97*/
		{
		wvGetSTTBF(&ps->anSttbfAssoc,ps->fib.fcSttbfAssoc,ps->fib.lcbSttbfAssoc,ps->tablefd);
		wvGetSTTBF(&ps->Sttbfbkmk,ps->fib.fcSttbfbkmk,ps->fib.lcbSttbfbkmk,ps->tablefd);
		}

	/*Extract all the list information that we will need to handle lists later on*/
	wvGetLST(&ps->lst,&ps->noofLST,ps->fib.fcPlcfLst,ps->fib.lcbPlcfLst,ps->tablefd);
	wvGetLFO_records(&ps->lfo,&ps->lfolvl,&ps->lvl,&ps->nolfo,&ps->nooflvl,ps->fib.fcPlfLfo,ps->fib.lcbPlfLfo,ps->tablefd);
	/* init the starting list number table */
	if (ps->nolfo) 
		{
		ps->liststartnos = (U32 *)malloc(9 * ps->nolfo * sizeof(U32));
		ps->listnfcs = (U8 *)malloc(9 * ps->nolfo);
		ps->finallvl = (LVL *)malloc(9 * ps->nolfo * sizeof(LVL));
		for (i=0;i<9 * ps->nolfo;i++) 
				{
				ps->liststartnos[i] = 0xffffffffL;
				ps->listnfcs[i] = 0xff;
				wvInitLVL(&(ps->finallvl[i]));
				}
		}
	else 
		{
		ps->liststartnos=NULL;
		ps->listnfcs=NULL;
		ps->finallvl=NULL;
		}
	/*Extract Graphic Information*/
	wvGetFSPA_PLCF(&ps->fspa,&ps->fspapos,&ps->nooffspa,ps->fib.fcPlcspaMom,ps->fib.lcbPlcspaMom,ps->tablefd);
	wvGetFDOA_PLCF(&ps->fdoa,&ps->fdoapos,&ps->nooffdoa,ps->fib.fcPlcdoaMom,ps->fib.lcbPlcdoaMom,ps->tablefd);


	/* 
	despite what some parts of the spec might have you believe you still need to 
	get the piecetable from even simple files, some simple files can have 8bit
	chars in one part, and 16bit chars in another, so you have to watch out for
	that
	*/
	wvGetCLX(wvQuerySupported(&ps->fib,NULL),&ps->clx,ps->fib.fcClx,ps->fib.lcbClx,ps->fib.fExtChar,ps->tablefd);
	if (ps->clx.nopcd == 0) wvBuildCLXForSimple6(&ps->clx,&ps->fib);	/* for word 6 and just in case */

	para_fcFirst = char_fcFirst = section_fcFirst = wvConvertCPToFC(0,&ps->clx);
	
	/*
	we will need the paragraph and character bounds table to make decisions as 
	to where a para/char run begins and ends
	*/
	if ( (wvQuerySupported(&ps->fib,NULL) == WORD6) || (wvQuerySupported(&ps->fib,NULL) == WORD7))
		{
    	wvGetBTE_PLCF6(&btePapx,&posPapx,&para_intervals,ps->fib.fcPlcfbtePapx,ps->fib.lcbPlcfbtePapx,ps->tablefd);
    	wvGetBTE_PLCF6(&bteChpx,&posChpx,&char_intervals,ps->fib.fcPlcfbteChpx,ps->fib.lcbPlcfbteChpx,ps->tablefd);
		}
	else	/* word 97 */
		{
		wvGetBTE_PLCF(&btePapx,&posPapx,&para_intervals,ps->fib.fcPlcfbtePapx,ps->fib.lcbPlcfbtePapx,ps->tablefd);
    	wvGetBTE_PLCF(&bteChpx,&posChpx,&char_intervals,ps->fib.fcPlcfbteChpx,ps->fib.lcbPlcfbteChpx,ps->tablefd);
		}

	wvGetSED_PLCF(&sed,&posSedx,&section_intervals,ps->fib.fcPlcfsed,ps->fib.lcbPlcfsed,ps->tablefd);
	wvTrace(("section_intervals is %d\n",section_intervals));

	/*
	The text of the file starts at fib.fcMin, but we will use the piecetable 
	records rather than this basic seek.
	fseek(ps->mainfd,ps->fib.fcMin,SEEK_SET);
	*/

	/*
	If !fib.fComplex, the document text stream is represented by the text
	beginning at fib.fcMin up to (but not including) fib.fcMac.
	*/

#ifdef DEBUG
	if ( ps->fib.fcMac != wvGetEndFCPiece(ps->clx.nopcd-1,&ps->clx) )
		wvTrace(("fcMac is not the same as the piecetable %x %x!\n",ps->fib.fcMac,wvGetEndFCPiece(ps->clx.nopcd-1,&ps->clx)));
#endif

	wvInitPAPX_FKP(&para_fkp);
	wvInitCHPX_FKP(&char_fkp);
	   
	wvHandleDocument(ps,DOCBEGIN);



	/*for each piece*/
	for (piececount=0;piececount<ps->clx.nopcd;piececount++)
		{
		chartype = wvGetPieceBoundsFC(&beginfc,&endfc,&ps->clx,piececount);
		fseek(ps->mainfd,beginfc,SEEK_SET);
		wvTrace(("SEEK %x\n",beginfc));
		wvGetPieceBoundsCP(&begincp,&endcp,&ps->clx,piececount);

		/*
		text that is not in the same piece is not guaranteed to have the same properties as
		the rest of the exception run, so force a stop and restart of these properties.
		*/
		char_fcLim = beginfc;
		wvTrace(("%d %d %d\n",begincp,endcp,ps->fib.ccpText));	
		for (i=begincp,j=beginfc;(i<endcp /*&& i<ps->fib.ccpText*/);i++,j += wvIncFC(chartype))
			{
			/* character properties */
			if (j == char_fcLim)
				{
				wvHandleElement(ps, CHARPROPEND, (void*)&achp,char_dirty);
				char_pendingclose = 0;
				}

			/* comment ending location */
            if (i == comment_cpLim)
                {
                wvHandleElement(ps,COMMENTEND, (void*)catrd,0);
                comment_pendingclose=0;
                }

			
			/* paragraph properties */
			if (j == para_fcLim)
				{
				wvHandleElement(ps, PARAEND, (void*)&apap,para_dirty);
				para_pendingclose = 0;
				}
			
			if (j == section_fcLim)
				{
				wvHandleElement(ps, SECTIONEND, (void*)&sep,section_dirty);
				section_pendingclose = 0;
				}

			if ((section_fcLim == 0xffffffff) || (section_fcLim == j))
				{
				wvTrace(("j i is %x %d\n",j,i));
				section_dirty = wvGetSimpleSectionBounds(wvQuerySupported(&ps->fib,NULL),ps,&sep,&section_fcFirst,&section_fcLim, i,&ps->clx,sed,&spiece,posSedx, section_intervals, &ps->stsh,ps->mainfd);
				wvTrace(("section begins at %x ends %x\n", section_fcFirst, section_fcLim));
				}

			if (j == section_fcFirst)
				{
				wvHandleElement(ps, SECTIONBEGIN, (void*)&sep,section_dirty);
				section_pendingclose = 1;
				}
			
			if ((para_fcLim == 0xffffffff) || (para_fcLim == j))
				{
				wvReleasePAPX_FKP(&para_fkp);
				wvGetSimpleParaBounds(wvQuerySupported(&ps->fib,NULL),&para_fkp,&para_fcFirst,&para_fcLim,wvConvertCPToFC(i,&ps->clx), btePapx, posPapx, para_intervals, ps->mainfd);
				wvTrace(("Para from %x to %x, j is %x\n",para_fcFirst,para_fcLim,j));

				if (0 == para_pendingclose)
                    {
                    /*
                    if there's no paragraph open, but there should be then I believe that the fcFirst search
                    has failed me, so I set it to now. I need to investigate this further. I believe it occurs
                    when a the last piece ended simultaneously with the last paragraph, and that the algorithm
                    for finding the beginning of a para breaks under that condition. I need more examples to
                    be sure, but it happens is very large complex files so its hard to find
                    */
                    if (j != para_fcFirst)
                        {
                        wvWarning(("There is no paragraph due to open but one should be, plugging the gap.\n"));
                        para_fcFirst = j;
                        }
                    }
				}

			if (j == para_fcFirst)
				{
				para_dirty = wvAssembleSimplePAP(wvQuerySupported(&ps->fib,NULL),&apap, para_fcLim, &para_fkp, &ps->stsh,ps->data);
			
				/* test section */
				wvReleasePAPX_FKP(&para_fkp);
				wvGetSimpleParaBounds(wvQuerySupported(&ps->fib,NULL),&para_fkp,&dummy,&nextpara_fcLim,para_fcLim, btePapx, posPapx, para_intervals, ps->mainfd);
				wvAssembleSimplePAP(wvQuerySupported(&ps->fib,NULL),&ps->nextpap, nextpara_fcLim, &para_fkp, &ps->stsh,ps->data);
				/* end test section */

				if ( (apap.fInTable) && (!apap.fTtp) )
					{
					wvGetFullTableInit(ps,para_intervals,btePapx,posPapx);
					wvGetRowTap(ps,&apap,para_intervals,btePapx,posPapx);
					}
				else if (apap.fInTable == 0)
					ps->intable=0;
				wvHandleElement(ps, PARABEGIN, (void*)&apap,para_dirty);

				char_fcLim = j;
				para_pendingclose = 1;
				}

			if ((comment_cpLim == 0xffffffffL) || (comment_cpLim == i))
                {
                wvTrace(("searching for the next comment begin cp is %d\n",i));
                catrd = wvGetCommentBounds(&comment_cpFirst,&comment_cpLim,i,atrd,posAtrd,atrd_intervals,&SttbfAtnbkmk,
                bkf,posBKF,bkf_intervals,bkl,posBKL,bkl_intervals);
                wvTrace(("begin and end are %d %d\n",comment_cpFirst,comment_cpLim));
                }

            if (i == comment_cpFirst)
                {
                wvHandleElement(ps,COMMENTBEGIN, (void*)catrd,0);
                comment_pendingclose=1;
                }


			if ((char_fcLim == 0xffffffff) || (char_fcLim == j))
				{
				wvTrace(("j i is %x %d\n", j, i));
				wvReleaseCHPX_FKP(&char_fkp);
				wvGetSimpleCharBounds(wvQuerySupported(&ps->fib, NULL), &char_fkp, &char_fcFirst, &char_fcLim, i, &ps->clx, bteChpx, posChpx, char_intervals, ps->mainfd);
				wvTrace(("char begins at %x ends %x, j is %x\n", char_fcFirst, char_fcLim,j));
				if (0 == char_pendingclose)
					{
					/*
                    if there's no character run open, but there should be then I believe that the fcFirst search
                    has failed me, so I set it to now. I need to investigate this further. 
                    */
                    if (j != char_fcFirst)
                        {
                        wvWarning(("There is no character run due to open but one should be, plugging the gap.\n"));
                        char_fcFirst = j;
                        }

					}
				}

			if (j == char_fcFirst)
				{
				wvTrace(("assembling CHP...\n"));
				/* a CHP's base style is in the para style */
				achp.istd = apap.istd;
				char_dirty = wvAssembleSimpleCHP(wvQuerySupported(&ps->fib, NULL),&achp,char_fcLim, &char_fkp, &ps->stsh);
				wvTrace(("CHP assembled.\n"));
				wvTrace(("font is %d\n",achp.ftcAscii));
				wvTrace(("char spec is %d\n",achp.ftcSym));
				wvHandleElement(ps, CHARPROPBEGIN, (void*)&achp,char_dirty);
				wvTrace(("char lid is %x\n",achp.lidDefault));
				char_pendingclose = 1; 
				}

			eachchar = wvGetChar(ps->mainfd, chartype);

			if ((eachchar == 0x07) && (!achp.fSpec))
				ps->endcell=1;

			ps->currentcp = i;
			wvTrace(("char pos is %x %x\n",j,eachchar));
			wvOutputTextChar(eachchar, chartype, ps,&achp);
			}

		if (j == para_fcLim)
            {
            wvHandleElement(ps,PARAEND, (void*)&apap,para_dirty);
            para_pendingclose=0;
            para_fcLim = 0xffffffffL;
            }

        if (i == comment_cpLim)
            {
            wvHandleElement(ps,COMMENTEND, (void*)catrd,0);
            comment_pendingclose=0;
            comment_cpLim = 0xffffffffL;
            }

        if (j == char_fcLim)
            {
            wvHandleElement(ps,CHARPROPEND, (void*)&achp,char_dirty);
            char_pendingclose=0;
            char_fcLim = 0xffffffffL;
            }

		}
	
	if (char_pendingclose)
		{
		wvInitCHP(&achp);
		wvHandleElement(ps, CHARPROPEND, (void*)&achp,char_dirty);
		}

	if (comment_pendingclose)
        wvHandleElement(ps,COMMENTEND, (void*)catrd,0);

	if (para_pendingclose)
		{
		wvInitPAP(&apap);
		wvHandleElement(ps, PARAEND, (void*)&apap,para_dirty);
		}

	if (section_pendingclose)
		wvHandleElement(ps, SECTIONEND, (void*)&sep,section_dirty);

	wvFree(ps->fspa);
	wvFree(ps->fspapos);
	wvFree(ps->fdoa);
	wvFree(ps->fdoapos);

	wvFree(posBKL);
	wvFree(bkl);
	wvFree(posBKF);
	wvFree(bkf);
	wvFree(posAtrd);
	wvFree(atrd);

	wvReleasePAPX_FKP(&para_fkp);
	wvReleaseCHPX_FKP(&char_fkp);
	wvHandleDocument(ps,DOCEND);
	wvFree(posSedx);
	wvFree(sed);

	wvFree(ps->liststartnos);
	wvFree(ps->listnfcs);
	for (i=0;i<9 * ps->nolfo;i++)
		wvReleaseLVL(&(ps->finallvl[i]));
	wvFree(ps->finallvl);

	wvReleaseLST(&ps->lst,ps->noofLST);
	wvReleaseLFO_records(&ps->lfo,&ps->lfolvl,&ps->lvl,ps->nooflvl);
	wvReleaseSTTBF(&ps->anSttbfAssoc);
    wvFree(btePapx);
	wvFree(posPapx);
    wvFree(bteChpx);
	wvFree(posChpx);
#if 0
	/* 
	so what, this is meaningless 
	C. 
	*/
	if (ps->fib.fcMac != ftell(ps->mainfd))
		wvError(("fcMac did not match end of input !\n"));
#endif
	wvReleaseCLX(&ps->clx);
    wvReleaseFFN_STTBF(&ps->fonts);
	wvReleaseSTSH(&ps->stsh);
	wvReleaseSTTBF(&SttbfAtnbkmk);
    wvReleaseSTTBF(&grpXstAtnOwners);
	if (ps->vmerges)
		{
		for(i=0;i<ps->norows;i++)
			wvFree(ps->vmerges[i]);
		wvFree(ps->vmerges);
		}
	wvFree(ps->cellbounds);
	tokenTreeFreeAll();
	}


/*
When a document is recorded in non-complex format, the bounds of the
paragraph that contains a particular character can be found by 

1) calculating the FC coordinate of the character, 

2) searching the bin table to find an FKP page that describes that FC, 

3) fetching that FKP, and 

4) then searching the FKP to find the interval in the rgfc that encloses the character. 

5) The bounds of the interval are the fcFirst and fcLim of the containing paragraph. 

Every character greater than or equal to fcFirst and less than fcLim is part of
the containing paragraph.

*/
int wvGetSimpleParaBounds(version ver,PAPX_FKP *fkp,U32 *fcFirst, U32 *fcLim, U32 currentfc, BTE *bte, U32 *pos,int nobte,FILE *fd)
	{
	BTE entry;
	long currentpos;
	static noerror;

	/*
	currentfc = wvConvertCPToFC(currentcp,clx);
	*/

	wvTrace(("currentfc is %x\n",currentfc));
	if (currentfc==0xffffffffL)
		{
		wvError(("Para Bounds not found !\n"));
		return(1);
		}

	if (0 != wvGetBTE_FromFC(&entry,currentfc, bte,pos,nobte))
		{
		wvError(("BTE not found !\n"));
		return(1);
		}
	currentpos = ftell(fd);
	/*The pagenumber of the FKP is entry.pn */

	wvTrace(("pn is %d\n",entry.pn));
	wvGetPAPX_FKP(ver,fkp,entry.pn,fd);
	wvTrace(("last entry is %x\n",fkp->rgfc[fkp->crun]));
	while (fkp->rgfc[fkp->crun] <= currentfc)
		{
		if ((fkp->rgfc[fkp->crun] == currentfc) && (currentfc == pos[nobte])	)
			break;
		if (!noerror)
			{
			wvError(("Alert, insane repeat \"insane\" paragraph structure, making wild stab in the dark to attempt to continue\n"));
			noerror++;
			}
		wvReleasePAPX_FKP(fkp);
		entry.pn++;
		wvGetPAPX_FKP(ver,fkp,entry.pn,fd);
		}

	fseek(fd,currentpos,SEEK_SET);

	return(wvGetIntervalBounds(fcFirst,fcLim,currentfc,fkp->rgfc,fkp->crun+1));
	}

int wvGetSimpleCharBounds(version ver, CHPX_FKP *fkp, U32 *fcFirst, U32 *fcLim, U32 currentcp, CLX *clx, BTE *bte, U32 *pos, int nobte, FILE *fd)
	{
	U32 currentfc;
	BTE entry;
	long currentpos;
	static char noerror;

	currentfc = wvConvertCPToFC(currentcp, clx);

	if (currentfc==0xffffffffL)
		{
		wvError(("Char Bounds not found !\n"));
		return(1);
		}

	wvTrace(("char fc is %x\n",currentfc));

	if (0 != wvGetBTE_FromFC(&entry, currentfc, bte, pos, nobte))
		{
		wvError(("BTE not found !\n"));
		return(1);
		}
	currentpos = ftell(fd);
	/*The pagenumber of the FKP is entry.pn */

	wvTrace(("pn is %d\n",entry.pn));
	wvGetCHPX_FKP(ver, fkp, entry.pn, fd);

	while (fkp->rgfc[fkp->crun] <= currentfc)
		{
		if ((fkp->rgfc[fkp->crun] == currentfc) && (currentfc == pos[nobte])	)
			break;
		if (!noerror)
			{
			wvError(("Alert, insane repeat \"insane\" character run structure, making wild stab in the dark to attempt to continue\n"));
			noerror++;
			}
		wvReleaseCHPX_FKP(fkp);
		entry.pn++;
		wvGetCHPX_FKP(ver,fkp,entry.pn,fd);
		}

	fseek(fd, currentpos, SEEK_SET);

	return(wvGetIntervalBounds(fcFirst, fcLim, currentfc, fkp->rgfc, fkp->crun+1));
	}

int wvGetIntervalBounds(U32 *fcFirst, U32 *fcLim, U32 currentfc, U32 *rgfc, U32 nopos)
	{
	U32 i=0;
	while(i<nopos-1)
		{
		wvTrace(("searching...%x %x %x\n",currentfc,wvNormFC(rgfc[i],NULL),wvNormFC(rgfc[i+1],NULL)));
		/*
		if ( (wvNormFC(rgfc[i],NULL) >= currentfc) && (currentfc <= wvNormFC(rgfc[i+1],NULL)) )
		*/
		if ( (currentfc >= wvNormFC(rgfc[i],NULL)) && (currentfc < wvNormFC(rgfc[i+1],NULL)) )
			{
			*fcFirst = wvNormFC(rgfc[i],NULL);
			*fcLim = wvNormFC(rgfc[i+1],NULL);
			return(0);
			}
		i++;
		}
	*fcFirst = wvNormFC(rgfc[nopos-2],NULL);
	*fcLim = wvNormFC(rgfc[nopos-1],NULL);
	wvTrace(("I'd rather not see this happen at all :-)\n"));
	return(0);
	}

/*
it is necessary to use the CP of the character to search the
plcfsed for the index i of the largest CP that is less than or equal to the
character's CP. 

plcfsed.rgcp[i] is the CP of the first character of the
section and plcfsed.rgcp[i+1] is the CP of the character following the
section mark that terminates the section (call it cpLim). 

Then retrieve plcfsed.rgsed[i]. The FC in this SED gives the location where the SEPX for
the section is stored. 

Then create a local SEP with default section properties. If the 
sed.fc != 0xFFFFFFFF, then the sprms within the SEPX that is stored at offset 
sed.fc must be applied to the local SEP. The process thus far has created a 
SEP that describes what the section properties of the section at the last 
full save. 
*/
int wvGetSimpleSectionBounds(version ver,wvParseStruct *ps,SEP *sep,U32 *fcFirst,U32 *fcLim, U32 cp, CLX *clx, SED *sed, U32 *spiece,U32 *posSedx, U32 section_intervals, STSH *stsh,FILE *fd)
	{
	U32 i=0;
	int ret=0;
	SEPX sepx;
	long pos = ftell(fd);
	U32 cpTest=0,j,dummy;

	if (section_intervals ==0)
		{
		wvGetPieceBoundsFC(fcFirst,&dummy,&ps->clx,0);
		wvGetPieceBoundsFC(&dummy,fcLim,&ps->clx,ps->clx.nopcd);
		return(0);
		}

	j=section_intervals-1;

	if (cp == 0) j=0;
	while (i<section_intervals)
		{
		wvTrace(("searching for sep %d %d\n",posSedx[i],cp));
		if ( (posSedx[i] <= cp) && (posSedx[i] > cpTest) )
			{
			cpTest = posSedx[i];
			j = i;
			*spiece = wvGetPieceFromCP(cpTest,clx);
			}
		i++;
		}

	wvTrace(("found at %d %d\n",posSedx[j],posSedx[j+1]));
	*fcFirst = wvConvertCPToFC(posSedx[j], clx);
	*fcLim = wvConvertCPToFC(posSedx[j+1], clx);
	wvTrace(("found at %x %x\n",*fcFirst,*fcLim));

	wvInitSEP(sep);

	if (sed[j].fcSepx != 0xffffffffL)
		{
		fseek(fd,wvNormFC(sed[j].fcSepx,NULL),SEEK_SET);
		wvGetSEPX(ver,&sepx,fd);
		if (ver == WORD8)
			ret = wvAddSEPXFromBucket(sep,&sepx,stsh);
		else
			ret = wvAddSEPXFromBucket6(sep,&sepx,stsh);
		wvReleaseSEPX(&sepx);
		}

	fseek(fd,pos,SEEK_SET);
	return(ret);
	}