-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdoctxt.c
156 lines (128 loc) · 2.92 KB
/
doctxt.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <zip.h>
#include <libxml/parser.h>
#include "util.h"
#define LEN(a) sizeof(a) / sizeof(a[0])
#define TEMPFILE "/tmp/doctxt-temp.txt"
void
writetofile(char *out_file_path, char *data)
{
FILE *out_file;
out_file = fopen(out_file_path, "w");
fprintf(out_file,"%s", data);
fclose(out_file);
return;
}
void
readzip(const char *path, char *filename)
{
zip_t *zf;
zip_file_t *file;
struct zip_stat st;
int err = 0;
int size;
char *data;
if ((zf = zip_open(path, 0, &err)) == NULL) {
die("Unable to extract zip: %s", path);
}
file = zip_fopen(zf, filename, ZIP_FL_UNCHANGED);
if (file == NULL) {
die("File is wrong format");
}
zip_stat(zf, filename, 0, &st);
size = st.size;
data = ecalloc(sizeof(char), size + 10);
zip_fread(file, data, size);
zip_fclose(file);
zip_close(zf);
data[size] = '\0'; /* fixes bug where sometimes file doesnt end in '\0' */
writetofile(TEMPFILE, data);
free(data);
return;
}
void
parsexml(const char *path, FILE *outfile)
{
// xmlDoc *document;
xmlDocPtr document;
xmlNode *root, *node_body, *node_p, *node_r, *node_t;
xmlChar *text;
document = xmlReadFile(path, NULL, 0);
if (document == NULL) {
die("Unable to read xml file");
}
root = xmlDocGetRootElement(document);
if (root == NULL) {
die("No root in xml file");
}
if (!xmlStrEqual(root->name, (const xmlChar *) "document")) {
die("wrong xml format");
}
for (node_body = root->children; node_body; node_body = node_body->next) {
if (xmlStrEqual(node_body->name, (const xmlChar *) "body")) {
for (node_p = node_body->children; node_p; node_p = node_p->next) {
if (xmlStrEqual(node_p->name, (const xmlChar *) "p")) {
for (node_r = node_p->children; node_r; node_r = node_r->next) {
if (xmlStrEqual(node_r->name, (const xmlChar *) "r")) {
for (node_t = node_r->children; node_t; node_t = node_t->next) {
if (xmlStrEqual(node_t->name, (const xmlChar *) "t")) {
text = xmlNodeGetContent(node_t);
fprintf(outfile, "%s", text);
xmlFree(text);
}
}
}
}
fprintf(outfile, "\n");
}
}
}
break;
}
xmlFreeDoc(document);
xmlCleanupParser();
return;
}
void
usage()
{
die("usage: doctxt infile [-o outfile]");
return;
}
int
main(int argc, char *argv[])
{
FILE *outfile = NULL;
char *outfilename = "out.txt";
char *infilename = "";
if (argc < 2) {
usage();
}
infilename = argv[1];
for (int i = 2; i < argc; i++) {
if (!strcmp(argv[i], "-v")) {
puts("doctxt-"VERSION);
return 0;
} else if (!strcmp(argv[i], "-o")) {
if (argc <= i - 1) {
usage();
}
outfilename = argv[i + 1];
i++;
}
else {
usage();
}
}
readzip(infilename, "word/document.xml");
outfile = fopen(outfilename, "wt");
parsexml(TEMPFILE, outfile);
fclose(outfile);
if (remove(TEMPFILE) != 0) {
die("Unable to delete tempfile");
}
return 0;
}