-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
203 lines (153 loc) · 5.39 KB
/
main.cpp
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
/***************************************************************************
main.cpp - description
-------------------
begin : Tue Oct 28 2003
copyright : (C) 2003 by Andrew Punch
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string.h>
#include "WriterFactory.h"
#include "ReaderFactory.h"
#include "WriterInterface.h"
#include "ReaderInterface.h"
#include "FileFormat.h"
#include "DebugLog.h"
static void ShowHelp(void);
int main(int argc, char *argv[])
{
DoxEngine::FileFormat inFormat = DoxEngine::FORMAT_UNKNOWN, outFormat = DoxEngine::FORMAT_UNKNOWN;
char *inFilename, *outFilename;
int i;
bool isOutfile = false;
/* No debug by default */
DoxEngine::DebugLog log;
if (argc == 1)
{
ShowHelp();
return 1;
}
for (i=1; i<argc; i++)
{
if (!strncmp(argv[i], "--", 2))
{
if (!strcmp(argv[i], "--from-html"))
inFormat = DoxEngine::FORMAT_HTML;
else if (!strcmp(argv[i], "--from-rtf"))
inFormat = DoxEngine::FORMAT_RTF;
else if (!strcmp(argv[i], "--from-text"))
inFormat = DoxEngine::FORMAT_TEXT;
else if (!strcmp(argv[i], "--to-html"))
outFormat = DoxEngine::FORMAT_HTML;
else if (!strcmp(argv[i], "--to-rtf"))
outFormat = DoxEngine::FORMAT_RTF;
else if (!strcmp(argv[i], "--to-text"))
outFormat = DoxEngine::FORMAT_TEXT;
else if (!strcmp(argv[i], "--debug"))
{
#ifdef ENABLE_LOG_DEBUG
log.SetStream(DoxEngine::LOG_DEBUG, std::cerr);
#else
std::cerr << "Compile option ENABLE_DEBUG_LOG required" << std::endl;
#endif
}
else
{
ShowHelp();
return 2;
}
}
else
{
if (isOutfile)
{
outFilename = argv[i];
std::ifstream input(inFilename);
std::ofstream output(outFilename);
DoxEngine::WriterInterface *writer;
DoxEngine::ReadInterface *reader;
#ifdef ENABLE_LOG_DEBUG
std::cerr << "HTML format: " << DoxEngine::FORMAT_HTML << std::endl;
std::cerr << "Input format: " << inFormat << std::endl;
std::cerr << "Output format: " << outFormat << std::endl;
#endif
DoxEngine::WriterFactories &writerFactories = DoxEngine::WriterFactoriesSingleton::GetWriterFactories();
DoxEngine::WriterFactories::iterator writerIterator = writerFactories.find(outFormat);
if (writerIterator == writerFactories.end())
{
std::cerr << "Internal error initialising writer";
break;
}
else
{
// First type is the key (file format)
// Second type is the value (factory instance)
writer = writerIterator->second->Create(output, log);
#ifdef ENABLE_LOG_DEBUG
std::cerr << "Verify out format: "<< writerIterator->first << std::endl;
#endif
}
DoxEngine::ReaderFactories &readerFactories = DoxEngine::ReaderFactoriesSingleton::GetReaderFactories();
DoxEngine::ReaderFactories::iterator readerIterator = readerFactories.find(inFormat);
if (readerIterator == readerFactories.end())
{
std::cerr << "Internal error initialising reader";
break;
}
else
{
// First type is the key (file format)
// Second type is the value (factory instance)
reader = readerIterator->second->Create(input, *writer, log);
#ifdef ENABLE_LOG_DEBUG
std::cerr << "Verify in format: "<< readerIterator->first << std::endl;
#endif
}
printf("Converting %s to %s\n", inFilename, outFilename);
while(reader->processData());
delete reader;
delete writer;
isOutfile = false;
}
else
{
inFilename = argv[i];
isOutfile = true;
}
}
}
return 0;
}
void ShowHelp(void)
{
using namespace std;
cout << "DocFrac Version 3.1.3" << endl;
cout << "Copyright 2004-2006 Andrew Punch" << endl;
cout << "Released under LGPL" << endl;
cout << "Converts between RTF, HTML and text documents" << endl;
cout << endl;
cout << "Usage:" << endl;
cout << "docfrac <from option> <from file> <to option> <to file>..." << endl;
cout << endl;
cout << "From options:" << endl;
cout << "--from-rtf From RTF" << endl;
cout << "--from-html From HTML" << endl;
cout << "--from-text From text" << endl;
cout << endl;
cout << "To options:" << endl;
cout << "--to-rtf To RTF" << endl;
cout << "--to-html To HTML" << endl;
cout << "--to-text To text" << endl;
cout << endl;
cout << "e.g. docfrac --from-rtf resume.rtf --to-html resume.html" << endl;
}