]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
added operator[](unsigned int) const -- testing it now on Linux/axp,
[wxWidgets.git] / src / html / htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmltag.cpp
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18 #if wxUSE_HTML
19
20 #ifdef __BORDLANDC__
21 #pragma hdrstop
22 #endif
23
24 #ifndef WXPRECOMP
25 #include "wx/wx.h"
26 #endif
27
28 #include "wx/html/htmltag.h"
29 #include <stdio.h> // for vsscanf
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 {
45 const wxChar *src = source.c_str();
46 int i, tg, pos, stpos;
47 int lng = source.Length();
48 wxChar dummy[256];
49
50 m_Cache = NULL;
51 m_CacheSize = 0;
52 m_CachePos = 0;
53
54 pos = 0;
55 while (pos < lng) {
56 if (src[pos] == wxT('<')) { // tag found:
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;
62 while ((src[pos] != wxT('>')) && (src[pos] != wxT(' '))) {
63 dummy[i] = src[pos++];
64 if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A'));
65 i++;
66 }
67 dummy[i] = 0;
68 m_Cache[tg].Name = new wxChar[i+1];
69 memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar));
70
71 while (src[pos] != wxT('>')) pos++;
72
73 if (src[stpos+1] == wxT('/')) { // ending tag:
74 m_Cache[tg].End1 = m_Cache[tg].End2 = -2;
75 // find matching begin tag:
76 for (i = tg; i >= 0; i--)
77 if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) {
78 m_Cache[i].End1 = stpos;
79 m_Cache[i].End2 = pos + 1;
80 break;
81 }
82 }
83 else {
84 m_Cache[tg].End1 = m_Cache[tg].End2 = -1;
85 }
86 }
87
88 pos++;
89 }
90
91 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
92 for (i = 0; i < m_CacheSize; i++) {
93 delete[] m_Cache[i].Name;
94 m_Cache[i].Name = NULL;
95 }
96 }
97
98
99
100 void wxHtmlTagsCache::QueryTag(int at, int* end1, int* end2)
101 {
102 if (m_Cache == NULL) return;
103 if (m_Cache[m_CachePos].Key != at) {
104 int delta = (at < m_Cache[m_CachePos].Key) ? -1 : 1;
105 do {m_CachePos += delta;} while (m_Cache[m_CachePos].Key != at);
106 }
107 *end1 = m_Cache[m_CachePos].End1;
108 *end2 = m_Cache[m_CachePos].End2;
109 }
110
111
112
113
114 //-----------------------------------------------------------------------------
115 // wxHtmlTag
116 //-----------------------------------------------------------------------------
117
118 IMPLEMENT_CLASS(wxHtmlTag,wxObject)
119
120 wxHtmlTag::wxHtmlTag(const wxString& source, int pos, int end_pos, wxHtmlTagsCache* cache) : wxObject()
121 {
122 int i;
123 char c;
124
125 // fill-in name, params and begin pos:
126 m_Name = m_Params = wxEmptyString;
127 i = pos+1;
128 if (source[i] == '/') {m_Ending = TRUE; i++;}
129 else m_Ending = FALSE;
130
131 while ((i < end_pos) && ((c = source[i++]) != ' ') && (c != '>')) {
132 if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A');
133 m_Name += c;
134 }
135
136 if (source[i-1] != '>')
137 while ((i < end_pos) && ((c = source[i++]) != '>')) {
138 if ((c >= 'a') && (c <= 'z')) c -= ('a' - 'A');
139 m_Params += c;
140 if (c == '"') {
141 while ((i < end_pos) && ((c = source[i++]) != '"')) m_Params += c;
142 m_Params += c;
143 }
144 else if (c == '\'') {
145 while ((i < end_pos) && ((c = source[i++]) != '\'')) m_Params += c;
146 m_Params += c;
147 }
148 }
149 m_Begin = i;
150
151 cache -> QueryTag(pos, &m_End1, &m_End2);
152 if (m_End1 > end_pos) m_End1 = end_pos;
153 if (m_End2 > end_pos) m_End2 = end_pos;
154 }
155
156
157
158 bool wxHtmlTag::HasParam(const wxString& par) const
159 {
160 const wxChar *st = m_Params, *p = par;
161 const wxChar *st2, *p2;
162
163 if (*st == 0) return FALSE;
164 if (*p == 0) return FALSE;
165 for (st2 = st, p2 = p; ; st2++) {
166 if (*p2 == 0) return TRUE;
167 if (*st2 == 0) return FALSE;
168 if (*p2 != *st2) p2 = p;
169 if (*p2 == *st2) p2++;
170 if (*st2 == ' ') p2 = p;
171 else if (*st2 == '=') {
172 p2 = p;
173 while (*st2 != ' ') {
174 if (*st2 == '"') {
175 st2++;
176 while (*st2 != '"') st2++;
177 }
178 st2++;
179 if (*st2 == 0) return FALSE;
180 }
181 }
182 }
183 }
184
185
186
187 wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
188 {
189 const wxChar *st = m_Params, *p = par;
190 const wxChar *st2, *p2;
191 bool comma;
192 char comma_char;
193
194 if (*st == 0) return "";
195 if (*p == 0) return "";
196 for (st2 = st, p2 = p; ; st2++) {
197 if (*p2 == 0) { // found
198 wxString fnd = "";
199 st2++; // '=' character
200 comma = FALSE;
201 comma_char = '\0';
202 if (!with_commas && (*(st2) == '"')) {
203 st2++;
204 comma = TRUE;
205 comma_char = '"';
206 }
207 else if (!with_commas && (*(st2) == '\'')) {
208 st2++;
209 comma = TRUE;
210 comma_char = '\'';
211 }
212 while (*st2 != 0) {
213 if (comma && *st2 == comma_char) comma = FALSE;
214 else if ((*st2 == ' ') && (!comma)) break;
215 fnd += (*(st2++));
216 }
217 if (!with_commas && (*(st2-1) == comma_char)) fnd.RemoveLast();
218 return fnd;
219 }
220 if (*st2 == 0) return "";
221 if (*p2 != *st2) p2 = p;
222 if (*p2 == *st2) p2++;
223 if (*st2 == ' ') p2 = p;
224 else if (*st2 == '=') {
225 p2 = p;
226 while (*st2 != ' ') {
227 if (*st2 == '"') {
228 st2++;
229 while (*st2 != '"') st2++;
230 }
231 else if (*st2 == '\'') {
232 st2++;
233 while (*st2 != '\'') st2++;
234 }
235 st2++;
236 }
237 }
238 }
239 }
240
241
242
243 int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const
244 {
245 wxString parval = GetParam(par);
246 return wxSscanf((const wxChar*)parval, format, param);
247 }
248
249 #endif