~ubuntu-branches/ubuntu/breezy/koffice/breezy

« back to all changes in this revision

Viewing changes to filters/kword/rtf/import/rtfimport_tokenizer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2004-05-09 11:33:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040509113300-vfrdadqsvjfuhn3b
Tags: 1:1.3.1-1
* New upstream bugfix release.
* Built against newer imagemagick (closes: #246623).
* Made koffice-libs/kformula recommend/depend on latex-xft-fonts, which
  provides mathematical fonts that the formula editor can use.  Also
  patched the kformula part to make these fonts the default.
* Changed kword menu hint from "WordProcessors" to "Word processors"
  (closes: #246209).
* Spellchecker configuration is now fixed (closes: #221256, #227568).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   This file is part of the KDE project
 
3
   Copyright (C) 2001 Ewald Snel <ewald@rambo.its.tudelft.nl>
 
4
   Copyright (C) 2001 Tomasz Grobelny <grotk@poczta.onet.pl>
 
5
 
 
6
   This library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2 of the License, or (at your option) any later version.
 
10
*/
 
11
 
 
12
#include <kdebug.h>
 
13
 
 
14
#include "rtfimport_tokenizer.h"
 
15
 
 
16
 
 
17
RTFTokenizer::RTFTokenizer()
 
18
{
 
19
    tokenText.resize( 4112 );
 
20
    fileBuffer.resize( 4096 );
 
21
    infile = 0L;
 
22
}
 
23
 
 
24
/**
 
25
 * Open tokenizer from file.
 
26
 * @param in the input file
 
27
 */
 
28
void RTFTokenizer::open( QFile *in )
 
29
{
 
30
    fileBufferPtr = 0L;
 
31
    fileBufferEnd = 0L;
 
32
    infile = in;
 
33
}
 
34
 
 
35
/**
 
36
 * Reads the next token.
 
37
 */
 
38
void RTFTokenizer::next()
 
39
{
 
40
    int ch;
 
41
    value=0;
 
42
    if (!infile)
 
43
        return;
 
44
 
 
45
    do
 
46
    {
 
47
        if (fileBufferPtr == fileBufferEnd)
 
48
        {
 
49
            int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
50
 
 
51
            if (n <= 0)
 
52
            {
 
53
                // Return CloseGroup on EOF
 
54
                ch = '}';
 
55
                break;
 
56
            }
 
57
            fileBufferPtr = (uchar *)fileBuffer.data();
 
58
            fileBufferEnd = (fileBufferPtr + n);
 
59
        }
 
60
        ch = *fileBufferPtr++;
 
61
    }
 
62
    while (ch == '\n' || ch == '\r' && ch != 0);
 
63
 
 
64
    // Skip one byte for prepend '@' to destinations
 
65
    text = (tokenText.data() + 1);
 
66
    hasParam = false;
 
67
 
 
68
    uchar *_text = (uchar *)text;
 
69
 
 
70
    if (ch == '{')
 
71
        type = RTFTokenizer::OpenGroup;
 
72
    else if (ch == '}')
 
73
        type = RTFTokenizer::CloseGroup;
 
74
    else if (ch == '\\')
 
75
    {
 
76
        type = RTFTokenizer::ControlWord;
 
77
 
 
78
        if (fileBufferPtr == fileBufferEnd)
 
79
        {
 
80
            int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
81
 
 
82
            if (n <= 0)
 
83
            {
 
84
                // Return CloseGroup on EOF
 
85
                type = RTFTokenizer::CloseGroup;
 
86
                return;
 
87
            }
 
88
            fileBufferPtr = (uchar *)fileBuffer.data();
 
89
            fileBufferEnd = (fileBufferPtr + n);
 
90
        }
 
91
        ch = *fileBufferPtr++;
 
92
 
 
93
        // Type is either control word or control symbol
 
94
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
 
95
        {
 
96
            int v = 0;
 
97
 
 
98
            // Read alphabetic string (command)
 
99
            while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
 
100
            {
 
101
                *_text++ = ch;
 
102
 
 
103
                if (fileBufferPtr == fileBufferEnd)
 
104
                {
 
105
                    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
106
 
 
107
                    if (n <= 0)
 
108
                    {
 
109
                        ch = ' ';
 
110
                        break;
 
111
                    }
 
112
                    fileBufferPtr = (uchar *)fileBuffer.data();
 
113
                    fileBufferEnd = (fileBufferPtr + n);
 
114
                }
 
115
                ch = *fileBufferPtr++;
 
116
            }
 
117
 
 
118
            // Read numeric parameter (param)
 
119
            bool isneg = (ch == '-');
 
120
 
 
121
            if (isneg)
 
122
            {
 
123
                if (fileBufferPtr == fileBufferEnd)
 
124
                {
 
125
                    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
126
 
 
127
                    if (n <= 0)
 
128
                    {
 
129
                        // Return CloseGroup on EOF
 
130
                        type = RTFTokenizer::CloseGroup;
 
131
                        return;
 
132
                    }
 
133
                    fileBufferPtr = (uchar *)fileBuffer.data();
 
134
                    fileBufferEnd = (fileBufferPtr + n);
 
135
                }
 
136
                ch = *fileBufferPtr++;
 
137
            }
 
138
            while (ch >= '0' && ch <= '9')
 
139
            {
 
140
                v        = (10 * v) + ch - '0';
 
141
                hasParam = true;
 
142
 
 
143
                if (fileBufferPtr == fileBufferEnd)
 
144
                {
 
145
                    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
146
 
 
147
                    if (n <= 0)
 
148
                    {
 
149
                        ch = ' ';
 
150
                        break;
 
151
                    }
 
152
                    fileBufferPtr = (uchar *)fileBuffer.data();
 
153
                    fileBufferEnd = (fileBufferPtr + n);
 
154
                }
 
155
                ch = *fileBufferPtr++;
 
156
            }
 
157
            value = isneg ? -v : v;
 
158
 
 
159
            // If delimiter is a space, it's part of the control word
 
160
            if (ch != ' ')
 
161
            {
 
162
                --fileBufferPtr;
 
163
            }
 
164
 
 
165
            *_text = 0; // Just put an end of string for the test, it can then be over-written again
 
166
            if ( !qstrncmp( tokenText.data()+1, "bin", 4 ) ) // Test the NULL too to avoid catching keywords starting with "bin"
 
167
            {   // We have \bin, so we need to read the bytes
 
168
                kdDebug(30515) << "Token:" << tokenText << endl;
 
169
                if (value > 0)
 
170
                {
 
171
                    kdDebug(30515) << "\\bin" << value << endl;
 
172
                    type = RTFTokenizer::BinaryData;
 
173
                    binaryData.resize(value);
 
174
                    for (int i=0; i<value; i++)
 
175
                    {
 
176
                        if (fileBufferPtr == fileBufferEnd)
 
177
                        {
 
178
                            const int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
179
 
 
180
                            if (n <= 0)
 
181
                            {
 
182
                                kdError(30515) << "\\bin stream hit end of file." << endl;
 
183
                                type = RTFTokenizer::CloseGroup;
 
184
                                break;
 
185
                            }
 
186
                            fileBufferPtr = (uchar *)fileBuffer.data();
 
187
                            fileBufferEnd = (fileBufferPtr + n);
 
188
                        }
 
189
                        binaryData[i]=*fileBufferPtr++;
 
190
                    }
 
191
                }
 
192
                else
 
193
                {
 
194
                    kdError(30515) << "\\bin with negative value skipping" << endl;
 
195
                }
 
196
            }
 
197
 
 
198
        }
 
199
        else if (ch=='\'')
 
200
        {
 
201
            type = RTFTokenizer::ControlWord;
 
202
            *_text++ = ch;
 
203
            if (fileBufferPtr == fileBufferEnd)
 
204
            {
 
205
                int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
206
 
 
207
                if (n <= 0)
 
208
                {
 
209
                    // Return CloseGroup on EOF
 
210
                    type = RTFTokenizer::CloseGroup;
 
211
                    return;
 
212
                }
 
213
                fileBufferPtr = (uchar *)fileBuffer.data();
 
214
                fileBufferEnd = (fileBufferPtr + n);
 
215
            }
 
216
            ch = *fileBufferPtr++;
 
217
            for(int i=0;i<2;i++)
 
218
            {
 
219
                hasParam = true;
 
220
                value<<=4;
 
221
                value=value|((ch + ((ch & 16) ? 0 : 9)) & 0xf);
 
222
 
 
223
                if (fileBufferPtr == fileBufferEnd)
 
224
                {
 
225
                    int n = infile->readBlock( fileBuffer.data(), fileBuffer.size() );
 
226
 
 
227
                    if (n <= 0)
 
228
                    {
 
229
                        ch = ' ';
 
230
                        break;
 
231
                    }
 
232
                    fileBufferPtr = (uchar *)fileBuffer.data();
 
233
                    fileBufferEnd = (fileBufferPtr + n);
 
234
                }
 
235
                ch = *fileBufferPtr++;
 
236
            }
 
237
                --fileBufferPtr;
 
238
            }
 
239
        else
 
240
        {
 
241
            type = RTFTokenizer::ControlWord;
 
242
            *_text++ = ch;
 
243
        }
 
244
    }
 
245
    else
 
246
    {
 
247
        type = RTFTokenizer::PlainText;
 
248
 
 
249
        // Everything until next backslash, opener or closer
 
250
        while ( ch != '\\' && ch != '{' && ch != '}' && ch != '\n' &&
 
251
                ch != '\r' && fileBufferPtr <= fileBufferEnd )
 
252
        {
 
253
            *_text++ = ch;
 
254
            ch = *fileBufferPtr++;
 
255
        }
 
256
 
 
257
        // Give back last char
 
258
        --fileBufferPtr;
 
259
    }
 
260
    *_text++ = 0;
 
261
}