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
|
/***************************************************************************
* Copyright (C) 2003 by Sylvain Joyeux *
* [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. *
***************************************************************************/
#ifndef TQHtmlStream_H
#define TQHtmlStream_H
#include <tqtextstream.h>
#include <tqstringlist.h>
class TQHtmlStream;
class TQHtmlStreamManip;
/**
@author Sylvain Joyeux
*/
class TQHtmlStreamManip
{
protected:
virtual void apply(TQHtmlStream& stream) const = 0;
public:
virtual ~TQHtmlStreamManip() {};
void operator () (TQHtmlStream& stream) const
{ apply(stream); }
};
class TQHtmlStream
{
TQTextOStream m_stream;
enum States
{
NORMAL_FLOW,
TAG,
BLOCK,
PARAM
};
int m_state, m_enclosing_state;
bool m_newline;
TQString m_indent;
TQStringList m_blockstack;
void finalize_open()
{
if (m_state == PARAM)
m_state = m_enclosing_state;
if (m_state == BLOCK)
m_stream << ">";
else if (m_state == TAG)
m_stream << " />";
m_state = NORMAL_FLOW;
}
void indent()
{
if (m_newline)
{
m_stream << m_indent;
m_newline = false;
}
}
template<class T>
TQHtmlStream& output(const T& o)
{
indent();
if (m_state == PARAM)
{
m_stream << "=\"" << o << "\"";
m_state = m_enclosing_state;
return *this;
}
if (m_state == BLOCK)
{
m_stream << ">";
m_state = NORMAL_FLOW;
}
else if (m_state == TAG)
{
m_stream << "/>";
m_state = NORMAL_FLOW;
}
m_stream << o;
return *this;
}
public:
TQHtmlStream(TQString* buffer)
: m_stream(buffer), m_state(NORMAL_FLOW), m_newline(true) {}
~TQHtmlStream() {}
void tag(const TQString& name, const TQString& cl, const TQString& id)
{
finalize_open();
indent();
m_stream << '<' << name;
m_state = TAG;
if (!cl.isEmpty())
m_stream << " class=\"" << cl << "\"";
if (!id.isEmpty())
m_stream << " id=\"" << id << "\"";
}
void block(const TQString& name, const TQString& cl, const TQString& id)
{
finalize_open();
indent();
m_stream << '<' << name;
m_indent += '\t';
m_blockstack.push_front(name);
m_state = BLOCK;
if (!cl.isEmpty())
m_stream << " class=\"" << cl << "\"";
if (!id.isEmpty())
m_stream << " id=\"" << id << "\"";
}
void parameter(const TQString& param_name)
{
if (m_state == NORMAL_FLOW) return;
m_stream << " " << param_name;
m_enclosing_state = m_state;
m_state = PARAM;
}
void close()
{
finalize_open();
m_indent.truncate(m_indent.length() - 1);
indent();
m_stream << "</" << m_blockstack.first() << ">";
m_blockstack.pop_front();
}
void close_all(bool indent)
{
while( ! m_blockstack.empty() )
{
if (indent)
(*this) << endl;
close();
}
}
void data()
{
finalize_open();
}
TQHtmlStream & operator<< ( TQChar c ) { return output(c); }
TQHtmlStream & operator<< ( char c ) { return output(c); }
TQHtmlStream & operator<< ( signed short i ) { return output(i); }
TQHtmlStream & operator<< ( unsigned short i ) { return output(i); }
TQHtmlStream & operator<< ( signed int i ) { return output(i); }
TQHtmlStream & operator<< ( unsigned int i ) { return output(i); }
TQHtmlStream & operator<< ( signed long i ) { return output(i); }
TQHtmlStream & operator<< ( unsigned long i ) { return output(i); }
TQHtmlStream & operator<< ( float f ) { return output(f); }
TQHtmlStream & operator<< ( double f ) { return output(f); }
TQHtmlStream & operator<< ( const char * s ) { return output(s); }
TQHtmlStream & operator<< ( const TQString & s ) { return output(s); }
TQHtmlStream & operator<< ( const TQCString & s ) { return output(s); }
TQHtmlStream & operator<< ( const TQHtmlStreamManip& op )
{
op(*this);
return *this;
}
TQHtmlStream & operator<< (TQTSManip m)
{
finalize_open();
m_stream << m;
return (*this);
}
TQHtmlStream & operator<< (TQTSFUNC f)
{
finalize_open();
int old_flags = m_stream.flags();
m_stream << f;
if (old_flags == m_stream.flags())
m_newline = true;
return (*this);
}
};
/***************************************************************************************
* Stream manipulators
*/
class TQHtmlStreamManip0 : public TQHtmlStreamManip
{
public:
typedef void (TQHtmlStream::*Method)();
private:
Method m_method;
void apply (TQHtmlStream& stream) const
{ (stream.*m_method)(); }
public:
TQHtmlStreamManip0(Method m)
: m_method(m) {}
};
class TQHtmlStreamManip1 : public TQHtmlStreamManip
{
public:
typedef void (TQHtmlStream::*Method)(const TQString& param);
private:
Method m_method;
TQString m_param;
void apply(TQHtmlStream& stream) const
{ (stream.*m_method)(m_param); }
public:
TQHtmlStreamManip1(Method m, const TQString& param)
: m_method(m), m_param(param) {}
};
class TQHtmlStreamManip3 : public TQHtmlStreamManip
{
public:
typedef void (TQHtmlStream::*Method)(const TQString& param0, const TQString& param1, const TQString& param2);
private:
Method m_method;
TQString m_param0, m_param1, m_param2;
void apply(TQHtmlStream& stream) const
{ (stream.*m_method)(m_param0, m_param1, m_param2); }
public:
TQHtmlStreamManip3(Method m, const TQString& param0, const TQString& param1, const TQString& param2)
: m_method(m),
m_param0(param0), m_param1(param1), m_param2(param2) {}
};
class CloseAll : public TQHtmlStreamManip
{
private:
bool m_indent;
void apply(TQHtmlStream& stream) const
{ stream.close_all(m_indent); }
public:
CloseAll(bool indent) : m_indent(indent) {}
};
inline TQHtmlStreamManip3 tag(const TQString& name, const TQString& cl = TQString(), const TQString& id = TQString())
{ return TQHtmlStreamManip3(&TQHtmlStream::tag, name, cl, id); }
inline TQHtmlStreamManip3 block(const TQString& name, const TQString& cl = TQString(), const TQString& id = TQString())
{ return TQHtmlStreamManip3(&TQHtmlStream::block, name, cl, id); }
inline TQHtmlStreamManip1 param(const TQString& name)
{ return TQHtmlStreamManip1(&TQHtmlStream::parameter, name); }
inline TQHtmlStreamManip0 close()
{ return TQHtmlStreamManip0(&TQHtmlStream::close); }
inline TQHtmlStreamManip0 data()
{ return TQHtmlStreamManip0(&TQHtmlStream::data); }
inline CloseAll close_all(bool indent = true)
{ return CloseAll(indent); }
#endif
|