File: wvConvert.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 (267 lines) | stat: -rw-r--r-- 5,778 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
#include <stdlib.h>
#include <stdio.h>
#include "config.h"
#include "wv.h"
/*
Released under GPL, written by Caolan.McNamara@ul.ie.

Copyright (C) 1998,1999 
	Caolan McNamara

Real Life: Caolan McNamara           *  Doing: MSc in HCI
Work: Caolan.McNamara@ul.ie          *  Phone: +353-86-8790257
URL: http://skynet.csn.ul.ie/~caolan *  Sig: an oblique strategy
How would you have done it?
*/

/*
returns 1 for not an ole doc
2 ole but not supported word doc
-1 for an error of some unknown kind
0 on success
*/

int myelehandler(wvParseStruct *ps,wvTag tag, void *props,int dirty);
int mydochandler(wvParseStruct *ps,wvTag tag);
FILE *wvOpenConfig(char *config);

void usage(void)
	{
	printf("Usage: wvConvert [--config config.xml] [--password password] filename.doc\n");
	exit(-1);
	}

int main(int argc,char **argv)
	{
	char *config=NULL;
	char *password=NULL;
	FILE *input;
	int ret;
    state_data myhandle;
	expand_data expandhandle;
	wvParseStruct ps;
	int c,index=0;
	static struct option long_options[] =
        {
        { "config",1,0,'x'},
        { "password",1,0,'p'},
        { 0,0,0,0 }
        };

	wvInitError();

	if (argc < 2) 
		usage();

	while(1)
		{
		c = getopt_long (argc, argv, "x:p:", long_options, &index);
		if (c == -1)
			break;
		switch(c)
			{
			case 'x':
				if (optarg)
					config = optarg;
				else
					wvError(("No config file given to config option"));
				break;
			case 'p':
                if (optarg)
                    password = optarg;
                else
                    wvError(("No password given to password option"));
                break;
			default:
                usage();
                break;
			}
		}
	
	input = fopen(argv[optind],"rb");

	ret = wvInitParser(&ps,input);
	ps.filename = argv[optind];

	if (ret == 4)
        {
        ret = 0;
        if (password == NULL)
            {
            wvError(("Password required, this is an encrypted document\n"));
            return(-1);
            }
        else
            {
            wvSetPassword(password,&ps);
            if (wvDecrypt97(&ps))
                {
                wvError(("Incorrect Password\n"));
                return(-1);
                }
            }
        }
    else if (ret == 7)
        {
        ret=0;
        if (password == NULL)
            {
            wvError(("Password required, this is an encrypted document\n"));
            return(-1);
            }
        else
            {
            wvSetPassword(password,&ps);
            if (wvDecrypt95(&ps))
                {
                wvError(("Incorrect Password\n"));
                return(-1);
                }
            }
        }

	if (ret)
		{
		wvError(("startup error with file %s\n",argv[1]));
		wvOLEFree();
		return(2);
		}
	
	wvSetElementHandler(myelehandler);
	wvSetDocumentHandler(mydochandler);
    
    wvInitStateData(&myhandle);

	myhandle.fp = wvOpenConfig(config);

	if (myhandle.fp== NULL)
		wvError(("config file not found\n"));
	else
    	ret = wvParseConfig(&myhandle);

	if (!ret)
		{
		expandhandle.sd = &myhandle;
		ps.userData = &expandhandle;
		ret = wvText(&ps);
		}

    wvReleaseStateData(&myhandle);
	if (ret == 2)
		return(2);
	else if (ret != 0)
		ret = -1;
	wvOLEFree();
	return(ret);
	}

int myelehandler(wvParseStruct *ps,wvTag tag, void *props, int dirty)
	{
	expand_data *data = (expand_data *)ps->userData;
	data->anSttbfAssoc = &ps->anSttbfAssoc;
	data->lfo = &ps->lfo;
    data->lfolvl = ps->lfolvl;
    data->lvl = ps->lvl;
    data->nolfo = &ps->nolfo;
    data->nooflvl = &ps->nooflvl;
	data->stsh = &ps->stsh;
	data->lst = &ps->lst;
	data->noofLST = &ps->noofLST;
	data->liststartnos = &ps->liststartnos;
	data->listnfcs = &ps->listnfcs;
	data->finallvl = &ps->finallvl;
    data->fib = &ps->fib;
	data->intable = &ps->intable;
	data->cellbounds = &ps->cellbounds;
	data->nocellbounds = &ps->nocellbounds;
	data->endcell = &ps->endcell;
	data->vmerges = &ps->vmerges;
	data->norows = &ps->norows;
	data->nextpap = &ps->nextpap;
	data->charset = wvAutoCharset(ps);
	data->props = props;

	switch (tag)
		{
		case PARABEGIN:	
			wvBeginPara(data);
			break;
		case PARAEND:
			wvEndPara(data);
			wvCopyPAP(&data->lastpap, (PAP*)(data->props));
			break;
		case SECTIONBEGIN:
			wvBeginSection(data);
			break;
		case SECTIONEND:
			wvEndSection(data);
			break;
		case CHARPROPBEGIN:
			wvBeginCharProp(data,NULL);
		    break;
		case CHARPROPEND:
			wvEndCharProp(data);
			break;
		default:
			break;
		}
	return(0);
	}

int mydochandler(wvParseStruct *ps,wvTag tag)
	{
	expand_data *data = (expand_data *)ps->userData;
	data->anSttbfAssoc = &ps->anSttbfAssoc;
	data->lfo = &ps->lfo;
    data->lfolvl = ps->lfolvl;
    data->lvl = ps->lvl;
    data->nolfo = &ps->nolfo;
    data->nooflvl = &ps->nooflvl;
	data->stsh = &ps->stsh;
	data->lst = &ps->lst;
	data->noofLST = &ps->noofLST;
	data->liststartnos = &ps->liststartnos;
	data->listnfcs = &ps->listnfcs;
	data->finallvl = &ps->finallvl;
    data->fib = &ps->fib;
	data->intable = &ps->intable;
	data->cellbounds = &ps->cellbounds;
	data->nocellbounds = &ps->nocellbounds;
	data->endcell = &ps->endcell;
	data->vmerges = &ps->vmerges;
	data->norows = &ps->norows;

	data->charset = wvAutoCharset(ps);

	switch (tag)
		{
		case DOCBEGIN:	
			wvBeginDocument(data);
			break;
		case DOCEND:
			wvEndDocument(data);
			break;
		default:
			break;
		}
	return(0);
	}

FILE *wvOpenConfig(char *config)
    {
    FILE *tmp;
    int i=0;
    if (config == NULL)
        config = "wvConfig.xml";
    else
        i=1;
    tmp = fopen(config,"rb");
    if (tmp == NULL)
        {
        if (i) wvError(("Attempt to open %s failed, using %s\n",config,HTMLCONFIG));
        config = XMLCONFIG;
        tmp = fopen(config,"rb");
        }
    return(tmp);
    }