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
|
#include <stdio.h>
#include <stdlib.h>
#include "../libspectre/spectre-utils.h"
#include "../libspectre/ps.h"
static char *outputdir;
#define BUFFER_SIZE 32768
static void
write_section (FILE *fd, const char *section, long begin, long end)
{
FILE *dst;
char *filename;
static char buf[BUFFER_SIZE];
unsigned int read;
size_t left = end - begin;
filename = _spectre_strdup_printf ("%s/%s.txt", outputdir, section);
dst = fopen (filename, "w");
if (!dst) {
printf ("Skipping section %s, could not open dest file %s\n",
section, filename);
free (filename);
return;
}
free (filename);
fseek (fd, begin, SEEK_SET);
while (left > 0) {
size_t to_read = BUFFER_SIZE;
if (left < to_read)
to_read = left;
read = fread (buf, sizeof (char), to_read, fd);
fwrite (buf, sizeof (char), read, dst);
left -= read;
}
fclose (dst);
}
int main (int argc, char **argv)
{
FILE *fd;
struct document *doc;
unsigned int i;
outputdir = argv[2];
fd = fopen (argv[1], "r");
if (!fd) {
printf ("Error opening file %s\n", argv[1]);
return 1;
}
doc = psscan (fd, argv[1], SCANSTYLE_NORMAL);
if (!doc) {
printf ("Error parsing document\n");
fclose (fd);
return 1;
}
write_section (fd, "prolog", doc->beginprolog, doc->endprolog);
write_section (fd, "setup", doc->beginsetup, doc->endsetup);
for (i = 0; i < doc->numpages; i++) {
char *pagename;
pagename = _spectre_strdup_printf ("page-%d", i);
write_section (fd, pagename, doc->pages[i].begin, doc->pages[i].end);
free (pagename);
}
write_section (fd, "trailer", doc->begintrailer, doc->endtrailer);
fclose (fd);
psdocdestroy (doc);
return 0;
}
|