-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtfparser.cpp
executable file
·164 lines (143 loc) · 3.82 KB
/
rtfparser.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
/*Copyright 2001
*
* rtfparser.cpp
*
* Author: Gregory Ford [email protected]
* RTF token parser based on:
* rtf2html by Dmitry Porapov <[email protected]>,
* based on earlier work of Chuck Shotton.
* distributed under BSD-style license
* RTF token lists and hashing algorithm based on
* RTF Tools, Release 1.10
* 6 April 1994
* Paul DuBois [email protected]
*
* Copying permitted under GNU licence (see COPYING)
*/
// rtfparser.cpp: implementation of the RtfParser class.
//
//////////////////////////////////////////////////////////////////////
#ifdef WIN32
// name decoration causes STL classes to have names > 256 characters
// which is more than VC++ debugger can handle, to avoid ugly warning
// messages disable warning C4786
#pragma warning( disable : 4786 )
#endif
#include <string>
#include <vector>
#include <stack>
using namespace std ;
#include <stdio.h>
#include "linktracker.h"
#include "charsource.h" //
#include "token.h"
#include "destination.h"
#include "tag.h"
#include "stylemap.h"
#include "tokeniser.h"
#include "charoutput.h"
#include "graphicstate.h"
#include "bodydestination.h"
#include "ignoredestination.h"
#include "stylesheetdestination.h"
#include "infodestination.h"
#include "rtfparser.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
RtfParser::RtfParser()
{
}
RtfParser::~RtfParser()
{
if ( body ) delete body;
if ( ignore ) delete ignore;
if ( styleSheet ) delete styleSheet ;
if ( field ) delete field ;
if ( info ) delete info ;
}
void RtfParser::parseDocument(CharSource * source, CharOutput * out)
{
int newDestination = 0;
currDestination = body;
stack<Destination *> destinationStack;
Tokeniser tokeniser(source);
while ( !tokeniser.isFinished() ) {
Token *token = tokeniser.nextToken();
if ( token != NULL) {
if ( token->destination ) {
Destination * newDest;
newDest = SetDestination( ((ControlToken *)token)->rtfMinor ) ;
if (newDest != currDestination ) {
destinationStack.push( currDestination );
currDestination = newDest;
}
}
newDestination = currDestination->handleToken( token );
if ( newDestination ) {
unSetDestination( currDestination );
if ( destinationStack.size() <1 ) {
currDestination = body;
} else {
currDestination = destinationStack.top();
destinationStack.pop();
}
currDestination->handleToken( token );
}
delete token;
}
}
body->popAllTags();
FootnoteDest::outputAll();
}
Destination * RtfParser::SetDestination(int minorCode)
{
switch ( minorCode ) {
case rtfStyleSheet:
return styleSheet;
case rtfInfo:
case rtfITitle:
case rtfISubject:
case rtfIAuthor:
case rtfIKeywords:
return info;
case rtfParNumText:
case rtfBody:
return body;
// return field; // disabled til I cn get fields sussed
case rtfFootnote:
return new FootnoteDest();
case rtfField:
case rtfFieldResult:
case rtfFieldInst:
case rtfCustomDestination:
case rtfFontTbl:
default:
return new IgnoreDestination();
}
}
RtfParser::RtfParser(CharOutput *out)
{
body = new BodyDestination( out );
body->setStyleMap( &styleMap );
body->setLinkTracker( &linkTracker );
ignore = new IgnoreDestination();
styleSheet = new StyleSheetDestination();
styleSheet->setStyleMap( &styleMap );
field = new FieldDest( out, &linkTracker);
FootnoteDest::setOutput( out );
info = new InfoDestination();
currDestination = body;
}
void RtfParser::unSetDestination(Destination *dest)
{
if (
dest != body &&
dest != styleSheet &&
dest != info &&
dest != field
)
{
delete dest;
}
}