summaryrefslogtreecommitdiff
path: root/src/rtfx.cpp
blob: 4d1bbd6cd9c0d808f8775a361fe60d0f430fe3da (plain)
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

#include "usuals.h"
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

#include "rtfreader.h"
#include "rtfanalyser.h"

int usage()
{
	fprintf(stderr, "usage: rtfm <inrtf> <outxml>\n");
	return 2;
}

int main(int argc, char* argv[])
{
	if(argc < 3)
		return usage();

	try
	{
		FILE* file = fopen(argv[1], "rb");

		if(!file)
		{
			fprintf(stderr, "rtfm: couldn't open file: %s: %s\n", argv[1], strerror(errno));
			return 1;
		}

		RtfParserOptions options;

		RtfParser handler(options);
		RtfReader rtf;
		rtf.setHandler(&handler);
		bool ret = rtf.parse(file);
		fclose(file);

		if(!ret)
		{
			fprintf(stderr, "rtfm: rtf parse failed: %s\n", rtf.getParseErrors().c_str());
			return 1;
		}


		DOM::Document doc = handler.getDocument();
		string xml = doc.serialize();

		FILE* out = fopen(argv[2], "wb");
		if(!out)
		{
			fprintf(stderr, "rtfm: couldn't open file: %s: %s\n", argv[2], strerror(errno));
			return 1;
		}

		fwrite(xml.c_str(), 1, xml.length(), out);
		fclose(out);
		return 0;

	}
	catch(DOM::DOMException& e)
	{
		fprintf(stderr, "rtfm: xml dom error: %s\n", e.getMessage());
	}

	return 1;
}