File: piecetable.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 (110 lines) | stat: -rw-r--r-- 3,144 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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "wv.h"

extern FILE *erroroutput;

int get_piecetable(FILE *in,U32 **rgfc,U32 **avalrgfc,U16 **sprm,U32 *clxcount)
	{
	U32 lcb;
	int nopieces=0;
	int i;
	U32 aval;

	lcb = read_32ubit(in);
	(*clxcount)+=(lcb+4);
	nopieces = (lcb-4)/12;
	error(erroroutput,"lcb is %ld, theres %d pieces\n",lcb,nopieces);
	
	*rgfc = (U32 *) malloc((nopieces +1) * sizeof(U32));
	*avalrgfc = (U32 *) malloc((nopieces) * sizeof(U32));
	*sprm = (U16 *)  malloc((nopieces) * sizeof(U16));

	if ( ((*rgfc) == NULL) || ((*avalrgfc) == NULL) || ((*sprm) == NULL) )
		{
		fprintf(erroroutput,"aborting due lack to lack of memory\n");
		exit(-1);
		}

	for(i=0;i<nopieces+1;i++)
		{
		(*rgfc)[i] = read_32ubit(in);
		error(erroroutput," array entry is %x\n",(*rgfc)[i]);
		}

	for(i=0;i<nopieces;i++)
		{
		aval = read_16ubit(in);
		(*avalrgfc)[i] = read_32ubit(in);
		if ((*avalrgfc)[i] & 0x40000000UL)
			{
			error(erroroutput,"mpiece: current piece is %d file offsets of pieces are (%x)\n",i,((*avalrgfc)[i]&0xbfffffffUL)/2);
			error(erroroutput,"mpiece: end of pieces is (%x)\n",((*avalrgfc)[i]&0xbfffffffUL)/2+(*rgfc)[i+1]-(*rgfc)[i]);
			}
		else
			{
			error(erroroutput,"mpiece: current piece is %d file offsets of pieces are (%x)\n",i,(*avalrgfc)[i]);
			error(erroroutput,"mpiece: end of pieces is (%x)\n",(*avalrgfc)[i]+(*rgfc)[i+1]-(*rgfc)[i]);
			}
		(*sprm)[i] = read_16ubit(in);
		error(erroroutput,"TOUGH: the sprm referenced here is %d\n",(*sprm)[i]);
		if ((*sprm)[i] & 0x01)
			error(erroroutput,"sprm varient 2\n");
		else
			{
			error(erroroutput,"sprm varient 1, isprm is %x, val is %d\n",((*sprm)[i]&0x00fe)>>1,((*sprm)[i]&0xff00)>>8);
			}
		}
	error(erroroutput,"NOPIECES is %d\n",nopieces);
	return(nopieces);
	}

/*
query_piece takes a piecetable, and a given character position. 
nextpiece is modified to contain the file position of the nextpiece
	in the list
flag_8_16 is modified to be 1 if the piece that querycp is in is one 
	that consists of 8 bit chars, and 0 if the piece is 16 bit chars 
	(the usual). 
the index of the piece that querycp belongs to is returned.
*/
int query_piece_cp(U32 *rgfc,U32* avalrgfc,int nopieces,U32 querycp,U32 *nextpiececp,int *flag_8_16)
	{
	int i=0;

    while(i<nopieces)
        {
        if (rgfc[i+1] > querycp)
            {
			if (nextpiececp != NULL)
            	*nextpiececp = rgfc[i+1];
            if (!(avalrgfc[i] & 0x40000000UL))
				if (flag_8_16 != NULL)
                	*flag_8_16=1;
            break;
            }
        i++;
        }
	return(i);
	}


/*
does the same as query_piece_cp, only it seeks fd to the beginning of the piece that
querycp is in, and returns the position that we've seeked to.
*/
int query_piece_cp_seek(U32 *rgfc,U32* avalrgfc,int nopieces,long int querycp,U32 *nextpiececp,int *flag_8_16,FILE *fd)
	{
	int index = query_piece_cp(rgfc,avalrgfc,nopieces,querycp,nextpiececp,flag_8_16);
	int thisfc;

	if (avalrgfc[index] & 0x40000000UL)
		thisfc = (avalrgfc[index]&0xbfffffffUL)/2;
	else
		thisfc = avalrgfc[index];
	fseek(fd,thisfc,SEEK_SET);

	return(thisfc);
	}