File: ppthtml.c

package info (click to toggle)
xlhtml 0.5.1-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze, wheezy
  • size: 2,888 kB
  • ctags: 562
  • sloc: ansic: 9,010; sh: 3,094; perl: 436; makefile: 145; csh: 73
file content (430 lines) | stat: -rw-r--r-- 9,811 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
/*
   pptHtml - Format a PowerPoint Presentation into Html
   Copyright 2002 Charles N Wyble <jackshck@thewybles.com>	

   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
 */

#if !(defined( __BORLANDC__ ) || defined( __WIN32__ ))
#include "config.h"			/* Created by ./configure script */
#include "support.h"		/* Needs to be before internal.h */
#include "internal.h"		/* Needs to be before cole */
#include "cole.h"
#else
#include "config.h.in"		/* Created by ./configure script */
#include "support.h"		/* Needs to be before internal.h */
#include "internal.h"		/* Needs to be before cole */
#include "cole.h.in"
#include <dir.h>
#endif


#include <stdio.h>
#include <string.h>	/* for strcpy() */
#include <ctype.h>	/* For isprint */
#include <stdlib.h> /* For exitt() */


#define PRGNAME	"pptHtml"
#if !(defined( __WIN32__ ) || defined( __BORLANDC__ ))
#define PRGVER		VERSION
#else
#define PRGVER		"0.4"
#endif
#define WORK_SIZE	8192
static char FileName[2][32] =			/* The section of the PowerPoint File we read */
{
	"/PowerPoint Document",	/* Power Point 97 & 2000 */
	"/PP40"				/* Everything else ? */
};


/* Function Prototypes */
COLE_LOCATE_ACTION_FUNC dump_file;
static void container_processor(int);
static void atom_processor(int, int, int, unsigned char);
static void print_unicode(unsigned char *, int);
static void print_utf8(unsigned short c);
static void put_utf8(unsigned short c);

/* Global data */
static char filename[128];
static unsigned char working_buffer[WORK_SIZE];
static int buf_idx=0;
static int output_this_container = 0;
static int past_first_slide = 0;
static int last_container = 0;

int main (int argc, char **argv)
{
	int f_ptr = 0;
	COLEFS * cfs;
	COLERRNO colerrno;

	if (argc < 2 || strcmp(argv[1], "--help") == 0)
	{
		fprintf (stderr, PRGNAME " - Outputs Power Point files as Html.\n"
			"Usage: %s <FILE>\n", argv[0]);
		exit (1);
	}
	else
	{
		strncpy(filename, argv[1], 124);
#if defined( __WIN32__ ) || defined( __BORLANDC__ )
		{
			char *ptr = strchr(filename, ':');
			if (ptr)
			{
				int len;
				char new_drive[MAXPATH];
				fnsplit(filename, new_drive, 0, 0, 0);
				if (new_drive[0] >= 'a')
					setdisk(new_drive[0] - 'a');
				else
					setdisk(new_drive[0] - 'A');
				ptr++;	/* Get past the colon */
				len = strlen(ptr);
				memmove(filename, ptr, len);
				filename[len] = 0;
			}
	}
#endif
		cfs = cole_mount (filename, &colerrno);
		if (cfs == NULL)
		{
			cole_perror (PRGNAME, colerrno);
			exit (1);
		}
	}

	while (cole_locate_filename (cfs, FileName[f_ptr], NULL, dump_file, &colerrno))
	{
		if (f_ptr)
		{	/* Two strikes...we're out! */
			cole_perror (PRGNAME, colerrno);
			if (colerrno == COLE_EFILENOTFOUND)
				fprintf(stderr, "Section: PowerPoint Document\n");
			break;
		}
		else	/* Don't do this... */
			f_ptr++;
	}

	if (cole_umount (cfs, &colerrno))
	{
		cole_perror ("travel", colerrno);
		exit (1);
	}

	return 0;
}


void dump_file(COLEDIRENT *cde, void *_info)
{
	unsigned long version=0, instance=0, type=0, length=0, target=0, count=0;
	unsigned char buf[16];
	COLEFILE *cf;
	COLERRNO err;

	cf = cole_fopen_direntry(cde, &err);

/* Ouput Header */
	printf("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n");
	printf("<HTML><HEAD><TITLE>%s", filename);
	printf("</TITLE></HEAD><BODY>\n");

/* Output body */
	while (cole_fread(cf, buf, 1, &err))
	{
		if (count == 0)
		{
			instance = buf[0];
			type = 0;
			length = 0;
			target = 80;	/* ficticious number */
		}
		else if (count == 1)
		{
			instance |= (buf[0]<<8);
			version = instance &0x000F;
			instance = (instance>>4);
		}
		else if (count == 2)
			type = (unsigned)buf[0];
		else if (count == 3)
			type |= (buf[0]<<8)&0x00000FFFL;
		else if (count == 4)
			length = (unsigned)buf[0];
		else if (count == 5)
			length |= (buf[0]<<8);
		else if (count == 6)
			length |= (buf[0]<<16);
		else if (count == 7)
		{
			length |= (buf[0]<<24);
			target = length;
			if (version == 0x0F)
			{	/* Do container level Processing */
				container_processor(type);
				count = -1;
			}
		}
		if (count > 7)
		{	/* Here is where we want to process the data
			   based on the Atom type... */
			atom_processor(type, count-8, target-1, buf[0]);
		}
		if (count == (target+7))
			count = 0;
		else
			count++;
	}

	if (past_first_slide)
		printf("<HR>");

	printf("&nbsp;<br>\n");

	/* Output Credit */
	printf("<hr><FONT SIZE=-1>Created with <a href=\"http://chicago.sf.net/xlhtml\">pptHtml</a></FONT><br>\n" );

	/* Output Tail */
	printf("</BODY></HTML>\n");
	cole_fclose(cf, &err);
}

static void container_processor(int type)
{
	if (type == 0x03EE)
	{
		if (past_first_slide)
			printf("<BR><HR><BR>\n");
		else
			past_first_slide = 1;
	}
	switch (type)
	{
		case 0x000D:
			if (last_container == 0x11)		/* suppress notes info */
				output_this_container = 0;
			else
				output_this_container = 1;
			break;
		case 0x0FF0:
			output_this_container = 1;
			break;
		default:
/*			printf("Cont:%x|\n", type);	*/
			output_this_container = 0;
			break;
	}
	last_container = type;
}

static void atom_processor(int type, int count, int buf_last, unsigned char data)
{
	if ((buf_idx >= WORK_SIZE)||(output_this_container == 0))
		return;

	if (count == 0)
	{
		memset(working_buffer, 0, WORK_SIZE);
		buf_idx = 0;
	}
	
	switch (type)
	{
		case 0x0FA0:	/* Text String in unicode */
			working_buffer[buf_idx++] = data;
			if (count == buf_last)
			{
/*				printf("Atom:%x|\n", type);	*/
/*				working_buffer[buf_idx++] = 0;       */
/*				printf("%s<BR>\n", working_buffer); */
				print_unicode(working_buffer, buf_idx);
				printf("<BR>\n");
			}
			break;
		case 0x0FA8:	/* Text String in ASCII */
			working_buffer[buf_idx++] = data;
			if (count == buf_last)
			{
				int i;
/*				working_buffer[buf_idx++] = 0;	*/
/*				printf("Atom:%x|\n", type);	*/
				for (i=0;i<buf_idx; i++)
				{
/*					printf("%02X &nbsp;", (int)working_buffer[i]); */	/* Debug */
					if (working_buffer[i] == 0x0D)
						printf("<BR>\n");
					else
						putchar(working_buffer[i]);
				}
				printf("<BR>\n");
			}
			break;
		case 0x0FBA:	/* CString - unicode... */
			working_buffer[buf_idx++] = data;
			if (count == buf_last)
			{
/*				working_buffer[buf_idx++] = 0;	*/
/*				printf("%s<BR>\n", working_buffer); */
/*				printf("Atom:%x|\n", type);	*/
				print_unicode(working_buffer, buf_idx);
				printf("<BR>\n");
			}
			break;
		default:
			break;
	}
}

static void print_unicode(unsigned char *ucs, int len)
{
	int i;
	for (i = 0; i < len; i += 2)
		print_utf8(ucs[i] | (ucs[i+1] << 8));
}

static void OutputCharCorrected(unsigned char c)
{
	switch (c)
	{	/* Special char handlers here... */
		case '\r':
			printf("<BR>\n");
			break;
		case 0x3C:
			printf("&lt;");
			break;
		case 0x3E:
			printf("&gt;");
			break;
		case 0x26:
			printf("&amp;");
			break;
		case 0x22:
			printf("&quot;");
			break;
		/* Also need to cover 128-159 since MS uses this area... */
		case 0x80:		/* Euro Symbol */
			printf("&#8364;");
			break;
		case 0x82:		/* baseline single quote */
			printf("&#8218;");
			break;
		case 0x83:		/* florin */
			printf("&#402;");
			break;
		case 0x84:		/* baseline double quote */
			printf("&#8222;");
			break;
		case 0x85:		/* ellipsis */
			printf("&#8230;");
			break;
		case 0x86:		/* dagger */
		    printf("&#8224;");
		    break;
		case 0x87:		/* double dagger */
		    printf("&#8225;");
		    break;
		case 0x88:		/* circumflex accent */
		    printf("&#710;");
		    break;
		case 0x89:		/* permile */
		    printf("&#8240;");
		    break;
		case 0x8A:		/* S Hacek */
		    printf("&#352;");
		    break;
		case 0x8B:		/* left single guillemet */
		    printf("&#8249;");
		    break;
		case 0x8C:		/* OE ligature */
		    printf("&#338;");
		    break;
		case 0x8E:		/*  #LATIN CAPITAL LETTER Z WITH CARON */
			printf("&#381;");
			break;
		case 0x91:		/* left single quote ? */
		    printf("&#8216;");
		    break;
		case 0x92:		/* right single quote ? */
		    printf("&#8217;");
		    break;
		case 0x93:		/* left double quote */
		    printf("&#8220;");
		    break;
		case 0x94:		/* right double quote */
		    printf("&#8221;");
		    break;
		case 0x95:		/* bullet */
		    printf("&#8226;");
		    break;
		case 0x96:		/* endash */
		    printf("&#8211;");
		    break;
		case 0x97:		/* emdash */
		    printf("&#8212;");
		    break;
		case 0x98:		/* tilde accent */
		    printf("&#732;");
		    break;
		case 0x99:		/* trademark ligature */
		    printf("&#8482;");
		    break;
		case 0x9A:		/* s Haceks Hacek */
		    printf("&#353;");
		    break;
		case 0x9B:		/* right single guillemet */
		    printf("&#8250;");
		    break;
		case 0x9C:		/* oe ligature */
		    printf("&#339;");
		    break;
		case 0x9F:		/* Y Dieresis */
		    printf("&#376;");
		    break;
		default:
			putchar(c);
			break;
	}
}

static void print_utf8(unsigned short c)
{
	if (c == 0)
		return;
		
	if (c < 0x80)
		OutputCharCorrected(c);
	else if (c < 0x800)
	{
		putchar(0xC0 | (c >>  6));
		put_utf8(c);
	}
	else
	{
		putchar(0xE0 | (c >> 12));
		put_utf8(c >>  6);
		put_utf8(c);
	}
}

static void put_utf8(unsigned short c)
{
	putchar(0x0080 | ((short)c & 0x003F));
}