File: decode_simple.c

package info (click to toggle)
wv 1.2.4-2
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,952 kB
  • ctags: 3,905
  • sloc: ansic: 30,943; sh: 10,225; xml: 1,677; makefile: 53
file content (878 lines) | stat: -rw-r--r-- 25,931 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
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/* 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 "wv.h"
#include "wvinternal.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, subdocument whichdoc)
{
    PAPX_FKP para_fkp;
    CHPX_FKP char_fkp;
    PAP apap;
    CHP achp;
    U32 piececount = 0, i, j = 0, spiece;
    U32 beginfc, endfc;
	U32 stream_size;
    U32 begincp, endcp;
    int ichartype;
    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;
    FTXBXS *ftxbx;
    U32 *txbxTxt;
    U32 txbxTxt_intervals;
    BKD *bkd;
    U32 *posBKD;
    U32 bkd_intervals;
    wvVersion ver;

    external_wvReleasePAPX_FKP ();
    external_wvReleaseCHPX_FKP ();

    ver = wvQuerySupported (&ps->fib, NULL);


    /* 
       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 (ver, &ps->clx, ps->fib.fcClx, (U32) ps->fib.lcbClx,
	      (U8) ps->fib.fExtChar, ps->tablefd);
    /* for word 6 and just in case */
    if (ps->clx.nopcd == 0)
	wvBuildCLXForSimple6 (&ps->clx, &ps->fib);

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

    /*dop */
    wvGetDOP (ver, &ps->dop, ps->fib.fcDop, ps->fib.lcbDop, ps->tablefd);
    wvTrace (("tabstops are every %d twips\n", ps->dop.dxaTab));

    /*textbox information */
    wvGetFTXBXS_PLCF (&ftxbx, &txbxTxt, &txbxTxt_intervals,
		      ps->fib.fcPlcftxbxTxt, ps->fib.lcbPlcftxbxTxt,
		      ps->tablefd);
    wvGetBKD_PLCF (&bkd, &posBKD, &bkd_intervals, ps->fib.fcPlcftxbxBkd,
		   ps->fib.lcbPlcftxbxBkd, ps->tablefd);


    /* 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->fib.fcPlcfAtnbkf, ps->fib.lcbPlcfAtnbkf,
           ps->tablefd);


    /* get font list */
    if ((ver == WORD6)
	|| (ver == 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 ((ver == WORD6)
	|| (ver == 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 *) wvMalloc (9 * ps->nolfo * sizeof (U32));
	  ps->listnfcs = (U8 *) wvMalloc (9 * ps->nolfo);
	  ps->finallvl = (LVL *) wvMalloc (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);



    /*
       we will need the paragraph and character bounds table to make decisions as 
       to where a para/char run begins and ends
     */
    if ((ver == WORD6)
	|| (ver == 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.
     */

    if ((ver == WORD2) && !ps->fib.fComplex)
      {
	  wvHandleDocument (ps, DOCBEGIN);
	  wvStream_goto (ps->mainfd, ps->fib.fcMin);
	  for (i = ps->fib.fcMin; i < ps->fib.fcMac; i++)
	    {
		eachchar = wvGetChar (ps->mainfd, 1);
		(*(ps->charhandler)) (ps, eachchar, 1, ps->fib.lid);
		/* (*(ps->scharhandler))(ps,eachchar,&achp;  no go */
		/* wvOutputTextChar(eachchar, 1, ps, &achp); no go */
		/* Formatting still lacking. This is just a start. */
	    }
	  wvHandleDocument (ps, DOCEND);
	  wvReleaseSTTBF (&ps->anSttbfAssoc);
	  wvReleaseSTTBF (&ps->Sttbfbkmk);
	  wvFree (posChpx);
	  wvFree (bteChpx);
	  wvFree (btePapx);
	  wvReleaseCLX (&ps->clx);
	  wvReleaseSTSH (&ps->stsh);
	  return;
      }


#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);

    if(wvHandleDocument (ps, DOCBEGIN))
		goto  finish_processing;


	/*get stream size for bounds checking*/
	stream_size = wvStream_size(ps->mainfd);

    /*for each piece */
    for (piececount = 0; piececount < ps->clx.nopcd; piececount++)
      {
	  ichartype =
	      wvGetPieceBoundsFC (&beginfc, &endfc, &ps->clx, piececount);
	  if(ichartype==-1)
		  break;
	  chartype = (U8) ichartype;
	  /*lvm007@aha.ru fix antiloop: check stream size */
	  if(beginfc>stream_size || endfc>stream_size){
		  wvError (
		   ("Piece Bounds out of range!, its a disaster\n"));
		  continue;
	  }

	  wvStream_goto (ps->mainfd, beginfc);

	  wvTrace (("SEEK %x\n", beginfc));

	  /*lvm007@aha.ru fix antiloop fix*/
	  if(wvGetPieceBoundsCP (&begincp, &endcp, &ps->clx, piececount)==-1)
		  break;

	  /*
	     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 (ver, 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 (ver, &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 (ver, &apap, para_fcLim, &para_fkp, ps);

		      /* test section */
		      wvReleasePAPX_FKP (&para_fkp);
		      wvGetSimpleParaBounds (ver, &para_fkp,
					     &dummy, &nextpara_fcLim,
					     para_fcLim, btePapx, posPapx,
					     para_intervals, ps->mainfd);
		      wvAssembleSimplePAP (ver, &ps->nextpap, nextpara_fcLim, &para_fkp, ps);
		      /* 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 (ver, &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;
			      }

			}
		      else{
  			/* lvm007@aha.ru fix: if currentfc>fcFirst but CHARPROP's changed look examples/charprops.doc*/
			if(char_fcFirst< j)
				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 (ver, &achp, &apap,
					       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;
	    }

      }

 finish_processing:
    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);

    internal_wvReleasePAPX_FKP (&para_fkp);
    internal_wvReleaseCHPX_FKP (&char_fkp);
    wvHandleDocument (ps, DOCEND);
    wvFree (posSedx);
    wvFree (sed);
    wvFree(bkd);
    wvFree(posBKD);
    wvFree(ftxbx);
    wvFree(txbxTxt); 
#if defined(WIN32)
   wvReleasePAPX_FKP (&para_fkp);
   wvReleaseCHPX_FKP (&char_fkp);   

   external_wvReleasePAPX_FKP ();
   external_wvReleaseCHPX_FKP();
#endif


    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);
    wvReleaseSTTBF (&ps->Sttbfbkmk);
    wvFree (btePapx);
    wvFree (posPapx);
    wvFree (bteChpx);
    wvFree (posChpx);
    wvFree (bkd);
    wvFree (posBKD);
    wvFree (txbxTxt);
    wvFree (ftxbx);
#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);
	wvOLEFree(ps);
    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 (wvVersion ver, PAPX_FKP * fkp, U32 * fcFirst,
		       U32 * fcLim, U32 currentfc, BTE * bte, U32 * pos,
		       int nobte, wvStream * fd)
{
    BTE entry;
    long currentpos;

    /*
       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 = wvStream_tell (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;
	  
	  /* Bad things man... */
	  wvError (("Alert, insane repeat \"insane\" paragraph structure,"
		    "making wild stab in the dark to attempt to continue\n"));
	  wvReleasePAPX_FKP (fkp);
	  entry.pn++;
	  wvGetPAPX_FKP (ver, fkp, entry.pn, fd);
      }

    wvStream_goto (fd, currentpos);

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

int
wvGetSimpleCharBounds (wvVersion ver, CHPX_FKP * fkp, U32 * fcFirst,
		       U32 * fcLim, U32 currentcp, CLX * clx, BTE * bte,
		       U32 * pos, int nobte, wvStream * fd)
{
    U32 currentfc;
    BTE entry;
    long currentpos;

    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 = wvStream_tell (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;

	  /* Bad things man... */
	  wvError (("Alert, insane repeat \"insane\" character run structure,"
		    "making wild stab in the dark to attempt to continue\n"));
	  wvReleaseCHPX_FKP (fkp);
	  entry.pn++;
	  wvGetCHPX_FKP (ver, fkp, entry.pn, fd);
      }

    wvStream_goto (fd, currentpos);

    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 (wvVersion ver, wvParseStruct * ps, SEP * sep,
			  U32 * fcFirst, U32 * fcLim, U32 cp, CLX * clx,
			  SED * sed, U32 * spiece, U32 * posSedx,
			  U32 section_intervals, STSH * stsh, wvStream * fd)
{
    U32 i = 0;
    int ret = 0;
    SEPX sepx;
    long pos = wvStream_tell (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)
      {
	  wvStream_goto (fd, wvNormFC (sed[j].fcSepx, NULL));
	  wvGetSEPX (ver, &sepx, fd);
	  if (ver == WORD8)
	      ret = wvAddSEPXFromBucket (sep, &sepx, stsh);
	  else
	      ret = wvAddSEPXFromBucket6 (sep, &sepx, stsh);
	  wvReleaseSEPX (&sepx);
      }

    wvStream_goto (fd, pos);
    return (ret);
}

U32
wvGetBeginFC (wvParseStruct * ps, subdocument whichdoc)
{
    U32 para_fcFirst = 0x400;
    switch (whichdoc)
      {
      case Dmain:
      default:
	  para_fcFirst = wvConvertCPToFC (0, &ps->clx);
	  break;
      case Dfootnote:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText, &ps->clx);
	  break;
      case Dheader:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText + ps->fib.ccpFtn,
					  &ps->clx);
	  break;
      case Dannotation:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText + ps->fib.ccpFtn +
					  ps->fib.ccpHdr, &ps->clx);
	  break;
      case Dendnote:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText + ps->fib.ccpFtn +
					  ps->fib.ccpHdr + ps->fib.ccpAtn,
					  &ps->clx);
	  break;
      case Dtextbox:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText + ps->fib.ccpFtn +
					  ps->fib.ccpHdr + ps->fib.ccpAtn +
					  ps->fib.ccpEdn, &ps->clx);
	  break;
      case Dheader_textbox:
	  para_fcFirst = wvConvertCPToFC (ps->fib.ccpText + ps->fib.ccpFtn +
					  ps->fib.ccpHdr + ps->fib.ccpAtn +
					  ps->fib.ccpEdn + ps->fib.ccpTxbx,
					  &ps->clx);
	  break;
      }
    return (para_fcFirst);
}