]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: htmltag.cpp | |
3 | // Purpose: wxHtmlTag class (represents single tag) | |
4 | // Author: Vaclav Slavik | |
69941f05 | 5 | // RCS-ID: $Id$ |
5526e819 VS |
6 | // Copyright: (c) 1999 Vaclav Slavik |
7 | // Licence: wxWindows Licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation | |
13 | #endif | |
14 | ||
3096bd2f | 15 | #include "wx/wxprec.h" |
5526e819 VS |
16 | |
17 | #include "wx/defs.h" | |
18 | #if wxUSE_HTML | |
19 | ||
20 | #ifdef __BORDLANDC__ | |
21 | #pragma hdrstop | |
22 | #endif | |
23 | ||
24 | #ifndef WXPRECOMP | |
3096bd2f | 25 | #include "wx/wx.h" |
5526e819 VS |
26 | #endif |
27 | ||
69941f05 | 28 | #include "wx/html/htmltag.h" |
7e1e0960 | 29 | #include <stdio.h> // for vsscanf |
5526e819 VS |
30 | #include <stdarg.h> |
31 | ||
32 | ||
33 | ||
34 | ||
35 | //----------------------------------------------------------------------------- | |
36 | // wxHtmlTagsCache | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject) | |
40 | ||
41 | #define CACHE_INCREMENT 64 | |
42 | ||
43 | wxHtmlTagsCache::wxHtmlTagsCache(const wxString& source) | |
44 | { | |
66a77a74 | 45 | const wxChar *src = source.c_str(); |
5526e819 VS |
46 | int i, tg, pos, stpos; |
47 | int lng = source.Length(); | |
66a77a74 | 48 | wxChar dummy[256]; |
5526e819 VS |
49 | |
50 | m_Cache = NULL; | |
51 | m_CacheSize = 0; | |
52 | m_CachePos = 0; | |
53 | ||
54 | pos = 0; | |
4f9297b0 VS |
55 | while (pos < lng) |
56 | { | |
57 | if (src[pos] == wxT('<')) // tag found: | |
58 | { | |
5526e819 VS |
59 | if (m_CacheSize % CACHE_INCREMENT == 0) |
60 | m_Cache = (sCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(sCacheItem)); | |
61 | tg = m_CacheSize++; | |
62 | m_Cache[tg].Key = stpos = pos++; | |
63 | dummy[0] = 0; i = 0; | |
15db3cf5 VS |
64 | while (pos < lng && |
65 | src[pos] != wxT('>') && | |
f6bcfd97 | 66 | src[pos] != wxT(' ') && src[pos] != wxT('\r') && |
4f9297b0 VS |
67 | src[pos] != wxT('\n') && src[pos] != wxT('\t')) |
68 | { | |
5526e819 | 69 | dummy[i] = src[pos++]; |
66a77a74 | 70 | if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A')); |
5526e819 VS |
71 | i++; |
72 | } | |
73 | dummy[i] = 0; | |
66a77a74 OK |
74 | m_Cache[tg].Name = new wxChar[i+1]; |
75 | memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar)); | |
5526e819 | 76 | |
15db3cf5 | 77 | while (pos < lng && src[pos] != wxT('>')) pos++; |
5526e819 | 78 | |
4f9297b0 VS |
79 | if (src[stpos+1] == wxT('/')) // ending tag: |
80 | { | |
5526e819 VS |
81 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; |
82 | // find matching begin tag: | |
83 | for (i = tg; i >= 0; i--) | |
4f9297b0 VS |
84 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) |
85 | { | |
5526e819 VS |
86 | m_Cache[i].End1 = stpos; |
87 | m_Cache[i].End2 = pos + 1; | |
88 | break; | |
89 | } | |
90 | } | |
4f9297b0 VS |
91 | else |
92 | { | |
5526e819 VS |
93 | m_Cache[tg].End1 = m_Cache[tg].End2 = -1; |
94 | } | |
95 | } | |
96 | ||
97 | pos++; | |
98 | } | |
99 | ||
100 | // ok, we're done, now we'll free .Name members of cache - we don't need it anymore: | |
4f9297b0 VS |
101 | for (i = 0; i < m_CacheSize; i++) |
102 | { | |
2776d7c3 | 103 | delete[] m_Cache[i].Name; |
5526e819 VS |
104 | m_Cache[i].Name = NULL; |
105 | } | |
106 | } | |
107 | ||
108 | ||
109 | ||
110 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) | |
111 | { | |
112 | if (m_Cache == NULL) return; | |
4f9297b0 VS |
113 | if (m_Cache[m_CachePos].Key != at) |
114 | { | |
5526e819 VS |
115 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; |
116 | do {m_CachePos += delta;} while (m_Cache[m_CachePos].Key != at); | |
117 | } | |
118 | *end1 = m_Cache[m_CachePos].End1; | |
119 | *end2 = m_Cache[m_CachePos].End2; | |
120 | } | |
121 | ||
122 | ||
123 | ||
124 | ||
125 | //----------------------------------------------------------------------------- | |
126 | // wxHtmlTag | |
127 | //----------------------------------------------------------------------------- | |
128 | ||
129 | IMPLEMENT_CLASS(wxHtmlTag,wxObject) | |
130 | ||
131 | wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos, wxHtmlTagsCache* cache) : wxObject() | |
132 | { | |
133 | int i; | |
134 | char c; | |
135 | ||
136 | // fill-in name, params and begin pos: | |
137 | m_Name = m_Params = wxEmptyString; | |
138 | i = pos+1; | |
4f9297b0 | 139 | if (source[i] == '/') { m_Ending = TRUE; i++; } |
5526e819 VS |
140 | else m_Ending = FALSE; |
141 | ||
f6bcfd97 BP |
142 | while ((i < end_pos) && |
143 | ((c = source[i++]) != ' ' && c != '\r' && c != '\n' && c != '\t' && | |
4f9297b0 VS |
144 | c != '>')) |
145 | { | |
5526e819 VS |
146 | if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A'); |
147 | m_Name += c; | |
148 | } | |
149 | ||
150 | if (source[i-1] != '>') | |
4f9297b0 VS |
151 | while ((i < end_pos) && ((c = source[i++]) != '>')) |
152 | { | |
5526e819 | 153 | if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A'); |
f6bcfd97 | 154 | if (c == '\r' || c == '\n' || c == '\t') c = ' '; // make future parsing a bit simpler |
5526e819 | 155 | m_Params += c; |
4f9297b0 VS |
156 | if (c == '"') |
157 | { | |
5526e819 VS |
158 | while ((i < end_pos) && ((c = source[i++]) != '"')) m_Params += c; |
159 | m_Params += c; | |
160 | } | |
4f9297b0 VS |
161 | else if (c == '\'') |
162 | { | |
72aa4a98 VS |
163 | while ((i < end_pos) && ((c = source[i++]) != '\'')) m_Params += c; |
164 | m_Params += c; | |
165 | } | |
5526e819 VS |
166 | } |
167 | m_Begin = i; | |
168 | ||
4f9297b0 | 169 | cache->QueryTag(pos, &m_End1, &m_End2); |
5526e819 VS |
170 | if (m_End1 > end_pos) m_End1 = end_pos; |
171 | if (m_End2 > end_pos) m_End2 = end_pos; | |
172 | } | |
173 | ||
174 | ||
175 | ||
176 | bool wxHtmlTag::HasParam(const wxString& par) const | |
177 | { | |
66a77a74 OK |
178 | const wxChar *st = m_Params, *p = par; |
179 | const wxChar *st2, *p2; | |
5526e819 VS |
180 | |
181 | if (*st == 0) return FALSE; | |
182 | if (*p == 0) return FALSE; | |
4f9297b0 VS |
183 | for (st2 = st, p2 = p; ; st2++) |
184 | { | |
5526e819 VS |
185 | if (*p2 == 0) return TRUE; |
186 | if (*st2 == 0) return FALSE; | |
187 | if (*p2 != *st2) p2 = p; | |
188 | if (*p2 == *st2) p2++; | |
189 | if (*st2 == ' ') p2 = p; | |
4f9297b0 VS |
190 | else if (*st2 == '=') |
191 | { | |
5526e819 | 192 | p2 = p; |
4f9297b0 VS |
193 | while (*st2 != ' ') |
194 | { | |
195 | if (*st2 == '"') | |
196 | { | |
5526e819 VS |
197 | st2++; |
198 | while (*st2 != '"') st2++; | |
199 | } | |
200 | st2++; | |
201 | if (*st2 == 0) return FALSE; | |
202 | } | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | ||
208 | ||
209 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const | |
210 | { | |
66a77a74 OK |
211 | const wxChar *st = m_Params, *p = par; |
212 | const wxChar *st2, *p2; | |
5526e819 | 213 | bool comma; |
72aa4a98 | 214 | char comma_char; |
5526e819 VS |
215 | |
216 | if (*st == 0) return ""; | |
217 | if (*p == 0) return ""; | |
4f9297b0 VS |
218 | for (st2 = st, p2 = p; ; st2++) |
219 | { | |
220 | if (*p2 == 0) // found | |
221 | { | |
5526e819 VS |
222 | wxString fnd = ""; |
223 | st2++; // '=' character | |
224 | comma = FALSE; | |
4f9297b0 VS |
225 | comma_char = '\0'; |
226 | if (!with_commas && (*(st2) == '"')) | |
227 | { | |
228 | st2++; | |
229 | comma = TRUE; | |
230 | comma_char = '"'; | |
231 | } | |
232 | else if (!with_commas && (*(st2) == '\'')) | |
233 | { | |
234 | st2++; | |
235 | comma = TRUE; | |
236 | comma_char = '\''; | |
237 | } | |
238 | ||
239 | while (*st2 != 0) | |
240 | { | |
72aa4a98 | 241 | if (comma && *st2 == comma_char) comma = FALSE; |
5526e819 VS |
242 | else if ((*st2 == ' ') && (!comma)) break; |
243 | fnd += (*(st2++)); | |
244 | } | |
72aa4a98 | 245 | if (!with_commas && (*(st2-1) == comma_char)) fnd.RemoveLast(); |
5526e819 VS |
246 | return fnd; |
247 | } | |
248 | if (*st2 == 0) return ""; | |
249 | if (*p2 != *st2) p2 = p; | |
250 | if (*p2 == *st2) p2++; | |
251 | if (*st2 == ' ') p2 = p; | |
4f9297b0 VS |
252 | else if (*st2 == '=') |
253 | { | |
5526e819 | 254 | p2 = p; |
4f9297b0 VS |
255 | while (*st2 != ' ') |
256 | { | |
257 | if (*st2 == '"') | |
258 | { | |
5526e819 VS |
259 | st2++; |
260 | while (*st2 != '"') st2++; | |
261 | } | |
4f9297b0 VS |
262 | else if (*st2 == '\'') |
263 | { | |
72aa4a98 VS |
264 | st2++; |
265 | while (*st2 != '\'') st2++; | |
266 | } | |
5526e819 VS |
267 | st2++; |
268 | } | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | ||
274 | ||
66a77a74 | 275 | int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const |
5526e819 | 276 | { |
5526e819 | 277 | wxString parval = GetParam(par); |
66a77a74 | 278 | return wxSscanf((const wxChar*)parval, format, param); |
5526e819 VS |
279 | } |
280 | ||
4d223b67 | 281 | #endif |