~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to src/tohtml.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Albin Tonnerre
  • Date: 2007-05-29 13:13:36 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529131336-85ygaddivvmkd3xc
Tags: 1.3.21pre22-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules: call dh_iconcache
  - Remove g++ build dependency
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*****
 
2
*
 
3
* TOra - An Oracle Toolkit for DBA's and developers
 
4
* Copyright (C) 2003-2005 Quest Software, Inc
 
5
* Portions Copyright (C) 2005 Other Contributors
 
6
 
7
* This program is free software; you can redistribute it and/or
 
8
* modify it under the terms of the GNU General Public License
 
9
* as published by the Free Software Foundation;  only version 2 of
 
10
* the License is valid for this program.
 
11
 
12
* This program is distributed in the hope that it will be useful,
 
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
* GNU General Public License for more details.
 
16
 
17
* You should have received a copy of the GNU General Public License
 
18
* along with this program; if not, write to the Free Software
 
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
20
*
 
21
*      As a special exception, you have permission to link this program
 
22
*      with the Oracle Client libraries and distribute executables, as long
 
23
*      as you follow the requirements of the GNU GPL in regard to all of the
 
24
*      software in the executable aside from Oracle client libraries.
 
25
*
 
26
*      Specifically you are not permitted to link this program with the
 
27
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 
28
*      And you are not permitted to distribute binaries compiled against
 
29
*      these libraries without written consent from Quest Software, Inc.
 
30
*      Observe that this does not disallow linking to the Qt Free Edition.
 
31
*
 
32
*      You may link this product with any GPL'd Qt library such as Qt/Free
 
33
*
 
34
* All trademarks belong to their respective owners.
 
35
*
 
36
*****/
 
37
 
 
38
#include "toconf.h"
 
39
 
 
40
 
 
41
#include "tomain.h"
 
42
#include "tohtml.h"
 
43
 
 
44
#include <ctype.h>
 
45
#include <string.h>
 
46
 
 
47
#include <qapplication.h>
 
48
#include <qregexp.h>
 
49
 
 
50
toHtml::toHtml(const QCString &data)
 
51
{
 
52
    Length = strlen(data);
 
53
    Data = new char[Length + 1];
 
54
    strcpy(Data, data);
 
55
    Position = 0;
 
56
    LastChar = 0;
 
57
}
 
58
 
 
59
toHtml::~toHtml()
 
60
{
 
61
    delete[] Data;
 
62
}
 
63
 
 
64
void toHtml::skipSpace(void)
 
65
{
 
66
    if (Position >= Length)
 
67
        return ;
 
68
    char c = LastChar;
 
69
    if (!c)
 
70
        c = Data[Position];
 
71
    if (isspace(c))
 
72
    {
 
73
        Position++;
 
74
        LastChar = 0;
 
75
        while (Position < Length && isspace(Data[Position]))
 
76
            Position++;
 
77
    }
 
78
}
 
79
 
 
80
bool toHtml::eof(void)
 
81
{
 
82
    if (Position > Length)
 
83
        throw qApp->translate("toHtml", "Invalidly went beyond end of file");
 
84
    return Position == Length;
 
85
}
 
86
 
 
87
void toHtml::nextToken(void)
 
88
{
 
89
    if (eof())
 
90
        throw qApp->translate("toHtml", "Reading HTML after eof");
 
91
    QualifierNum = 0;
 
92
    char c = LastChar;
 
93
    if (!c)
 
94
        c = Data[Position];
 
95
    if (c == '<')
 
96
    {
 
97
        IsTag = true;
 
98
        Position++;
 
99
        LastChar = 0;
 
100
        skipSpace();
 
101
        if (Position >= Length)
 
102
            throw qApp->translate("toHtml", "Lone < at end");
 
103
        if (Data[Position] != '/')
 
104
        {
 
105
            Open = true;
 
106
        }
 
107
        else
 
108
        {
 
109
            Open = false;
 
110
            Position++;
 
111
        }
 
112
        skipSpace();
 
113
        {
 
114
            size_t start = Position;
 
115
            while (Position < Length && !isspace(Data[Position]) && Data[Position] != '>')
 
116
            {
 
117
                Data[Position] = tolower(Data[Position]);
 
118
                Position++;
 
119
            }
 
120
            Tag = mid(start, Position - start);
 
121
        }
 
122
        for (;;)
 
123
        {
 
124
            skipSpace();
 
125
            if (Position >= Length)
 
126
                throw qApp->translate("toHtml", "Unended tag at end");
 
127
 
 
128
            c = LastChar;
 
129
            if (!c)
 
130
                c = Data[Position];
 
131
            if (c == '>')
 
132
            {
 
133
                LastChar = 0;
 
134
                Position++;
 
135
                break;
 
136
            }
 
137
 
 
138
            // Must always be an empty char here, so LastChar not needed to be checked.
 
139
 
 
140
            {
 
141
                size_t start = Position;
 
142
 
 
143
                while (Position < Length &&
 
144
                        !isspace(Data[Position]) &&
 
145
                        Data[Position] != '=' &&
 
146
                        Data[Position] != '>')
 
147
                {
 
148
                    Data[Position] = tolower(Data[Position]);
 
149
                    Position++;
 
150
                }
 
151
                Qualifiers[QualifierNum].Name = mid(start, Position - start);
 
152
            }
 
153
            skipSpace();
 
154
            if (Position >= Length)
 
155
                throw qApp->translate("toHtml", "Unended tag qualifier at end");
 
156
            c = LastChar;
 
157
            if (!c)
 
158
                c = Data[Position];
 
159
            if (c == '=')
 
160
            {
 
161
                LastChar = 0;
 
162
                Position++;
 
163
                skipSpace();
 
164
                if (Position >= Length)
 
165
                    throw qApp->translate("toHtml", "Unended tag qualifier data at end");
 
166
                c = Data[Position];
 
167
                if (c == '\'' || c == '\"')
 
168
                {
 
169
                    Position++;
 
170
                    size_t start = Position;
 
171
                    while (Data[Position] != c)
 
172
                    {
 
173
                        Position++;
 
174
                        if (Position >= Length)
 
175
                            throw qApp->translate("toHtml", "Unended quoted string at end");
 
176
                    }
 
177
                    Qualifiers[QualifierNum].Value = mid(start, Position - start);
 
178
                    Position++;
 
179
                    LastChar = 0;
 
180
                }
 
181
                else
 
182
                {
 
183
                    size_t start = Position;
 
184
                    while (!isspace(Data[Position]) && Data[Position] != '>')
 
185
                    {
 
186
                        Position++;
 
187
                        if (Position >= Length)
 
188
                            throw qApp->translate("toHtml", "Unended qualifier data at end");
 
189
                    }
 
190
                    Qualifiers[QualifierNum].Value = mid(start, Position - start);
 
191
                }
 
192
            }
 
193
            QualifierNum++;
 
194
            if (QualifierNum >= TO_HTML_MAX_QUAL)
 
195
                throw qApp->translate("toHtml", "Exceded qualifier max in toHtml");
 
196
        }
 
197
    }
 
198
    else
 
199
    {
 
200
        IsTag = false;
 
201
        size_t start = Position;
 
202
        Position++;
 
203
        LastChar = 0;
 
204
        while (Position < Length)
 
205
        {
 
206
            if (Data[Position] == '<')
 
207
                break;
 
208
            Position++;
 
209
        }
 
210
        Text = mid(start, Position - start);
 
211
    }
 
212
}
 
213
 
 
214
const char *toHtml::value(const QCString &q)
 
215
{
 
216
    for (int i = 0;i < QualifierNum;i++)
 
217
    {
 
218
        if (q == Qualifiers[i].Name)
 
219
            return Qualifiers[i].Value;
 
220
    }
 
221
    return NULL;
 
222
}
 
223
 
 
224
QCString toHtml::text()
 
225
{
 
226
    QCString ret;
 
227
    for (const char *cur = Text;*cur;cur++)
 
228
    {
 
229
        char c = *cur;
 
230
        if (c == '&')
 
231
        {
 
232
            const char *start = cur + 1;
 
233
            while (*cur && *cur != ';')
 
234
                cur++;
 
235
            QCString tmp(start, cur - start);
 
236
            if (tmp[0] == '#')
 
237
            {
 
238
                tmp = tmp.right(tmp.length() - 1);
 
239
                ret += char(tmp.toInt());
 
240
            }
 
241
            else if (tmp == "auml")
 
242
                ret += "�";
 
243
            // The rest of the & codes...
 
244
        }
 
245
        else
 
246
            ret += c;
 
247
    }
 
248
    return ret;
 
249
}
 
250
 
 
251
const char *toHtml::mid(size_t start, size_t size)
 
252
{
 
253
    if (size == 0)
 
254
        return "";
 
255
    if (start >= Length)
 
256
        throw qApp->translate("toHtml", "Tried to access string out of bounds in mid (start=%1)").arg(start);
 
257
    if (size > Length)
 
258
        throw qApp->translate("toHtml", "Tried to access string out of bounds in mid (size=%1)").arg(size);
 
259
    if (start + size > Length)
 
260
        throw qApp->translate("toHtml", "Tried to access string out of bounds in mid (total=%1+%2>%3)").
 
261
        arg(start).
 
262
        arg(size).
 
263
        arg(Length);
 
264
 
 
265
    LastChar = Data[start + size];
 
266
    Data[start + size] = 0;
 
267
    return Data + start;
 
268
}
 
269
 
 
270
bool toHtml::search(const QCString &all, const QString &str)
 
271
{
 
272
    QCString data(str.lower().latin1());
 
273
    enum {
 
274
        beginning,
 
275
        inTag,
 
276
        inString,
 
277
        inWord
 
278
    } lastState = beginning, state = beginning;
 
279
    unsigned int pos = 0;
 
280
    char endString = 0;
 
281
    for (size_t i = 0;i < all.length();i++)
 
282
    {
 
283
        char c = tolower(all.at(i));
 
284
        if (c == '\'' || c == '\"')
 
285
        {
 
286
            endString = c;
 
287
            state = inString;
 
288
        }
 
289
        else if (c == '<')
 
290
        {
 
291
            state = inTag;
 
292
        }
 
293
        else
 
294
        {
 
295
            switch (state)
 
296
            {
 
297
            case inString:
 
298
                if (c == endString)
 
299
                    state = lastState;
 
300
                break;
 
301
            case beginning:
 
302
                if (data.at(pos) != c)
 
303
                {
 
304
                    pos = 0;
 
305
                    state = inWord;
 
306
                }
 
307
                else
 
308
                {
 
309
                    pos++;
 
310
                    if (pos >= data.length())
 
311
                    {
 
312
                        if (i + 1 >= all.length() || !isalnum(all.at(i + 1)))
 
313
                            return true;
 
314
                        pos = 0;
 
315
                    }
 
316
                    break;
 
317
                }
 
318
                // Intentionally no break here
 
319
            case inWord:
 
320
                if (!isalnum(c))
 
321
                    state = beginning;
 
322
                break;
 
323
            case inTag:
 
324
                if (c == '>')
 
325
                    state = beginning;
 
326
                break;
 
327
            }
 
328
        }
 
329
    }
 
330
    return false;
 
331
}
 
332
 
 
333
QString toHtml::escape(const QString &html)
 
334
{
 
335
    QString ret = html;
 
336
 
 
337
    static QRegExp amp(QString::fromLatin1("\\&"));
 
338
    static QRegExp lt(QString::fromLatin1("\\<"));
 
339
    static QRegExp gt(QString::fromLatin1("\\>"));
 
340
 
 
341
#if 0
 
342
 
 
343
    ret.replace(amp, "&amp;");
 
344
    ret.replace(lt, "&lt;");
 
345
    ret.replace(gt, "&gt;");
 
346
#endif
 
347
 
 
348
    return ret;
 
349
}