[go: up one dir, main page]

File: tohtml.cpp

package info (click to toggle)
tora 1.3.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,632 kB
  • ctags: 7,487
  • sloc: cpp: 68,518; perl: 1,475; ansic: 291; sh: 173; makefile: 51
file content (304 lines) | stat: -rw-r--r-- 7,116 bytes parent folder | download
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//***************************************************************************
/*
 * TOra - An Oracle Toolkit for DBA's and developers
 * Copyright (C) 2000-2001,2001 Underscore AB
 * 
 * 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;  only version 2 of
 * the License is valid for this program.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *      As a special exception, you have permission to link this program
 *      with the Oracle Client libraries and distribute executables, as long
 *      as you follow the requirements of the GNU GPL in regard to all of the
 *      software in the executable aside from Oracle client libraries.
 *
 *      Specifically you are not permitted to link this program with the
 *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
 *      And you are not permitted to distribute binaries compiled against
 *      these libraries without written consent from Underscore AB. Observe
 *      that this does not disallow linking to the Qt Free Edition.
 *
 * All trademarks belong to their respective owners.
 *
 ****************************************************************************/

#include "tohtml.h"

#include <ctype.h>
#include <string.h>

#include <qregexp.h>

toHtml::toHtml(const QCString &data)
{
  Length=strlen(data);
  Data=new char[Length+1];
  strcpy(Data,data);
  Position=0;
  LastChar=0;
}

toHtml::~toHtml()
{
  delete Data;
}

void toHtml::skipSpace(void)
{
  if (Position>=Length)
    return;
  char c=LastChar;
  if (!c)
    c=Data[Position];
  if (isspace(c)) {
    Position++;
    LastChar=0;
    while(Position<Length&&isspace(Data[Position]))
      Position++;
  }
}

bool toHtml::eof(void)
{
  if (Position>Length)
    throw QString("Invalidly went beyond end of file");
  return Position==Length;
}

void toHtml::nextToken(void)
{
  if (eof())
    throw QString("Reading HTML after eof");
  QualifierNum=0;
  char c=LastChar;
  if (!c)
    c=Data[Position];
  if (c=='<') {
    IsTag=true;
    Position++;
    LastChar=0;
    skipSpace();
    if (Position>=Length)
      throw QString("Lone < at end");
    if (Data[Position]!='/') {
      Open=true;
    } else {
      Open=false;
      Position++;
    }
    skipSpace();
    {
      size_t start=Position;
      while(Position<Length&&!isspace(Data[Position])&&Data[Position]!='>') {
	Data[Position]=tolower(Data[Position]);
	Position++;
      }
      Tag=mid(start,Position-start);
    }
    for(;;) {
      skipSpace();
      if (Position>=Length)
	throw QString("Unended tag at end");

      c=LastChar;
      if (!c)
	c=Data[Position];
      if (c=='>') {
	LastChar=0;
	Position++;
	break;
      }

      // Must always be an empty char here, so LastChar not needed to be checked.

      {
	size_t start=Position;
	
	while(Position<Length&&
	      !isspace(Data[Position])&&
	      Data[Position]!='='&&
	      Data[Position]!='>') {
	  Data[Position]=tolower(Data[Position]);
	  Position++;
	}
	Qualifiers[QualifierNum].Name=mid(start,Position-start);
      }
      skipSpace();
      if (Position>=Length)
	throw QString("Unended tag qualifier at end");
      c=LastChar;
      if (!c)
	c=Data[Position];
      if (c=='=') {
	LastChar=0;
	Position++;
	skipSpace();
	if (Position>=Length)
	  throw QString("Unended tag qualifier data at end");
	c=Data[Position];
	if (c=='\''||c=='\"') {
	  Position++;
	  size_t start=Position;
	  while(Data[Position]!=c) {
	    Position++;
	    if (Position>=Length)
	      throw QString("Unended quoted string at end");
	  }
	  Qualifiers[QualifierNum].Value=mid(start,Position-start);
	  Position++;
	  LastChar=0;
	} else {
	  size_t start=Position;
	  while(!isspace(Data[Position])&&Data[Position]!='>') {
	    Position++;
	    if (Position>=Length)
	      throw QString("Unended qualifier data at end");
	  }
	  Qualifiers[QualifierNum].Value=mid(start,Position-start);
	}
      }
      QualifierNum++;
      if (QualifierNum>=TO_HTML_MAX_QUAL)
	throw QString("Exceded qualifier max in toHtml");
    }
  } else {
    IsTag=false;
    size_t start=Position;
    Position++;
    LastChar=0;
    while(Position<Length) {
      if (Data[Position]=='<')
	break;
      Position++;
    }
    Text=mid(start,Position-start);
  }
}

const char *toHtml::value(const QCString &q)
{
  for (int i=0;i<QualifierNum;i++) {
    if (q==Qualifiers[i].Name)
      return Qualifiers[i].Value;
  }
  return NULL;
}

QCString toHtml::text()
{
  QCString ret;
  for (const char *cur=Text;*cur;cur++) {
    char c=*cur;
    if (c=='&') {
      const char *start=cur+1;
      while(*cur&&*cur!=';')
	cur++;
      QCString tmp(start,cur-start);
      if (tmp[0]=='#') {
	tmp=tmp.right(tmp.length()-1);
	ret+=QChar(char(tmp.toInt()));
      } else if (tmp=="auml")
	ret+="";
      // The rest of the & codes...
    } else
      ret+=QChar(c);
  }
  return ret;
}

const char *toHtml::mid(size_t start,size_t size)
{
  if (size==0)
    return "";
  if (start>=Length)
    throw QString("Tried to access string out of bounds in mid (start=%1)").arg(start);
  if (size>Length)
    throw QString("Tried to access string out of bounds in mid (size=%1)").arg(size);
  if (start+size>Length)
    throw QString("Tried to access string out of bounds in mid (total=%1+%2>%3)").
      arg(start).
      arg(size).
      arg(Length);

  LastChar=Data[start+size];
  Data[start+size]=0;
  return Data+start;
}

bool toHtml::search(const QCString &all,const QString &str)
{
  QCString data(str.lower().latin1());
  enum {
    beginning,
    inTag,
    inString,
    inWord
  } lastState=beginning,state=beginning;
  unsigned int pos=0;
  char endString;
  for (size_t i=0;i<all.length();i++) {
    char c=tolower(all.at(i));
    if (c=='\''||c=='\"') {
      endString=c;
      state=inString;
    } else if (c=='<') {
      state=inTag;
    } else {
      switch (state) {
      case inString:
	if (c==endString)
	  state=lastState;
	break;
      case beginning:
	if (data.at(pos)!=c) {
	  pos=0;
	  state=inWord;
	} else {
	  pos++;
	  if (pos>=data.length()) {
	    if (i+1>=all.length()||!isalnum(all.at(i+1)))
	      return true;
	    pos=0;
	  }
	  break;
	}
	// Intentionally no break here
      case inWord:
	if (!isalnum(c))
	  state=beginning;
	break;
      case inTag:
	if (c=='>')
	  state=beginning;
	break;
      }
    }
  }
  return false;
}

QString toHtml::escape(const QString &html)
{
  QString ret=html;

  static QRegExp amp("\\&");
  static QRegExp lt("\\<");
  static QRegExp gt("\\>");

#if 0
  ret.replace(amp,"&amp;");
  ret.replace(lt,"&lt;");
  ret.replace(gt,"&gt;");
#endif
  return ret;
}