]>
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; | |
55 | while (pos < lng) { | |
66a77a74 | 56 | if (src[pos] == wxT('<')) { // tag found: |
5526e819 VS |
57 | if (m_CacheSize % CACHE_INCREMENT == 0) |
58 | m_Cache = (sCacheItem*) realloc(m_Cache, (m_CacheSize + CACHE_INCREMENT) * sizeof(sCacheItem)); | |
59 | tg = m_CacheSize++; | |
60 | m_Cache[tg].Key = stpos = pos++; | |
61 | dummy[0] = 0; i = 0; | |
f6bcfd97 BP |
62 | while (src[pos] != wxT('>') && |
63 | src[pos] != wxT(' ') && src[pos] != wxT('\r') && | |
64 | src[pos] != wxT('\n') && src[pos] != wxT('\t')) { | |
5526e819 | 65 | dummy[i] = src[pos++]; |
66a77a74 | 66 | if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A')); |
5526e819 VS |
67 | i++; |
68 | } | |
69 | dummy[i] = 0; | |
66a77a74 OK |
70 | m_Cache[tg].Name = new wxChar[i+1]; |
71 | memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar)); | |
5526e819 | 72 | |
66a77a74 | 73 | while (src[pos] != wxT('>')) pos++; |
5526e819 | 74 | |
66a77a74 | 75 | if (src[stpos+1] == wxT('/')) { // ending tag: |
5526e819 VS |
76 | m_Cache[tg].End1 = m_Cache[tg].End2 = -2; |
77 | // find matching begin tag: | |
78 | for (i = tg; i >= 0; i--) | |
66a77a74 | 79 | if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) { |
5526e819 VS |
80 | m_Cache[i].End1 = stpos; |
81 | m_Cache[i].End2 = pos + 1; | |
82 | break; | |
83 | } | |
84 | } | |
85 | else { | |
86 | m_Cache[tg].End1 = m_Cache[tg].End2 = -1; | |
87 | } | |
88 | } | |
89 | ||
90 | pos++; | |
91 | } | |
92 | ||
93 | // ok, we're done, now we'll free .Name members of cache - we don't need it anymore: | |
94 | for (i = 0; i < m_CacheSize; i++) { | |
2776d7c3 | 95 | delete[] m_Cache[i].Name; |
5526e819 VS |
96 | m_Cache[i].Name = NULL; |
97 | } | |
98 | } | |
99 | ||
100 | ||
101 | ||
102 | void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2) | |
103 | { | |
104 | if (m_Cache == NULL) return; | |
105 | if (m_Cache[m_CachePos].Key != at) { | |
106 | int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1; | |
107 | do {m_CachePos += delta;} while (m_Cache[m_CachePos].Key != at); | |
108 | } | |
109 | *end1 = m_Cache[m_CachePos].End1; | |
110 | *end2 = m_Cache[m_CachePos].End2; | |
111 | } | |
112 | ||
113 | ||
114 | ||
115 | ||
116 | //----------------------------------------------------------------------------- | |
117 | // wxHtmlTag | |
118 | //----------------------------------------------------------------------------- | |
119 | ||
120 | IMPLEMENT_CLASS(wxHtmlTag,wxObject) | |
121 | ||
122 | wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos, wxHtmlTagsCache* cache) : wxObject() | |
123 | { | |
124 | int i; | |
125 | char c; | |
126 | ||
127 | // fill-in name, params and begin pos: | |
128 | m_Name = m_Params = wxEmptyString; | |
129 | i = pos+1; | |
130 | if (source[i] == '/') {m_Ending = TRUE; i++;} | |
131 | else m_Ending = FALSE; | |
132 | ||
f6bcfd97 BP |
133 | while ((i < end_pos) && |
134 | ((c = source[i++]) != ' ' && c != '\r' && c != '\n' && c != '\t' && | |
135 | c != '>')) { | |
5526e819 VS |
136 | if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A'); |
137 | m_Name += c; | |
138 | } | |
139 | ||
140 | if (source[i-1] != '>') | |
141 | while ((i < end_pos) && ((c = source[i++]) != '>')) { | |
142 | if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A'); | |
f6bcfd97 | 143 | if (c == '\r' || c == '\n' || c == '\t') c = ' '; // make future parsing a bit simpler |
5526e819 VS |
144 | m_Params += c; |
145 | if (c == '"') { | |
146 | while ((i < end_pos) && ((c = source[i++]) != '"')) m_Params += c; | |
147 | m_Params += c; | |
148 | } | |
72aa4a98 VS |
149 | else if (c == '\'') { |
150 | while ((i < end_pos) && ((c = source[i++]) != '\'')) m_Params += c; | |
151 | m_Params += c; | |
152 | } | |
5526e819 VS |
153 | } |
154 | m_Begin = i; | |
155 | ||
156 | cache -> QueryTag(pos, &m_End1, &m_End2); | |
157 | if (m_End1 > end_pos) m_End1 = end_pos; | |
158 | if (m_End2 > end_pos) m_End2 = end_pos; | |
159 | } | |
160 | ||
161 | ||
162 | ||
163 | bool wxHtmlTag::HasParam(const wxString& par) const | |
164 | { | |
66a77a74 OK |
165 | const wxChar *st = m_Params, *p = par; |
166 | const wxChar *st2, *p2; | |
5526e819 VS |
167 | |
168 | if (*st == 0) return FALSE; | |
169 | if (*p == 0) return FALSE; | |
170 | for (st2 = st, p2 = p; ; st2++) { | |
171 | if (*p2 == 0) return TRUE; | |
172 | if (*st2 == 0) return FALSE; | |
173 | if (*p2 != *st2) p2 = p; | |
174 | if (*p2 == *st2) p2++; | |
175 | if (*st2 == ' ') p2 = p; | |
176 | else if (*st2 == '=') { | |
177 | p2 = p; | |
178 | while (*st2 != ' ') { | |
179 | if (*st2 == '"') { | |
180 | st2++; | |
181 | while (*st2 != '"') st2++; | |
182 | } | |
183 | st2++; | |
184 | if (*st2 == 0) return FALSE; | |
185 | } | |
186 | } | |
187 | } | |
188 | } | |
189 | ||
190 | ||
191 | ||
192 | wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const | |
193 | { | |
66a77a74 OK |
194 | const wxChar *st = m_Params, *p = par; |
195 | const wxChar *st2, *p2; | |
5526e819 | 196 | bool comma; |
72aa4a98 | 197 | char comma_char; |
5526e819 VS |
198 | |
199 | if (*st == 0) return ""; | |
200 | if (*p == 0) return ""; | |
201 | for (st2 = st, p2 = p; ; st2++) { | |
202 | if (*p2 == 0) { // found | |
203 | wxString fnd = ""; | |
204 | st2++; // '=' character | |
205 | comma = FALSE; | |
72aa4a98 VS |
206 | comma_char = '\0'; |
207 | if (!with_commas && (*(st2) == '"')) { | |
208 | st2++; | |
209 | comma = TRUE; | |
210 | comma_char = '"'; | |
211 | } | |
212 | else if (!with_commas && (*(st2) == '\'')) { | |
213 | st2++; | |
214 | comma = TRUE; | |
215 | comma_char = '\''; | |
216 | } | |
5526e819 | 217 | while (*st2 != 0) { |
72aa4a98 | 218 | if (comma && *st2 == comma_char) comma = FALSE; |
5526e819 VS |
219 | else if ((*st2 == ' ') && (!comma)) break; |
220 | fnd += (*(st2++)); | |
221 | } | |
72aa4a98 | 222 | if (!with_commas && (*(st2-1) == comma_char)) fnd.RemoveLast(); |
5526e819 VS |
223 | return fnd; |
224 | } | |
225 | if (*st2 == 0) return ""; | |
226 | if (*p2 != *st2) p2 = p; | |
227 | if (*p2 == *st2) p2++; | |
228 | if (*st2 == ' ') p2 = p; | |
229 | else if (*st2 == '=') { | |
230 | p2 = p; | |
231 | while (*st2 != ' ') { | |
232 | if (*st2 == '"') { | |
233 | st2++; | |
234 | while (*st2 != '"') st2++; | |
235 | } | |
72aa4a98 VS |
236 | else if (*st2 == '\'') { |
237 | st2++; | |
238 | while (*st2 != '\'') st2++; | |
239 | } | |
5526e819 VS |
240 | st2++; |
241 | } | |
242 | } | |
243 | } | |
244 | } | |
245 | ||
246 | ||
247 | ||
66a77a74 | 248 | int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const |
5526e819 | 249 | { |
5526e819 | 250 | wxString parval = GetParam(par); |
66a77a74 | 251 | return wxSscanf((const wxChar*)parval, format, param); |
5526e819 VS |
252 | } |
253 | ||
4d223b67 | 254 | #endif |