]> git.saurik.com Git - wxWidgets.git/blame - src/html/htmltag.cpp
Initial revision
[wxWidgets.git] / src / html / htmltag.cpp
CommitLineData
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
39IMPLEMENT_CLASS(wxHtmlTagsCache,wxObject)
40
41#define CACHE_INCREMENT 64
42
43wxHtmlTagsCache::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;
66a77a74 62 while ((src[pos] != wxT('>')) && (src[pos] != wxT(' '))) {
5526e819 63 dummy[i] = src[pos++];
66a77a74 64 if ((dummy[i] >= wxT('a')) && (dummy[i] <= wxT('z'))) dummy[i] -= (wxT('a') - wxT('A'));
5526e819
VS
65 i++;
66 }
67 dummy[i] = 0;
66a77a74
OK
68 m_Cache[tg].Name = new wxChar[i+1];
69 memcpy(m_Cache[tg].Name, dummy, (i+1)*sizeof(wxChar));
5526e819 70
66a77a74 71 while (src[pos] != wxT('>')) pos++;
5526e819 72
66a77a74 73 if (src[stpos+1] == wxT('/')) { // ending tag:
5526e819
VS
74 m_Cache[tg].End1 = m_Cache[tg].End2 = -2;
75 // find matching begin tag:
76 for (i = tg; i >= 0; i--)
66a77a74 77 if ((m_Cache[i].End1 == -1) && (wxStrcmp(m_Cache[i].Name, dummy+1) == 0)) {
5526e819
VS
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++) {
2776d7c3 93 delete[] m_Cache[i].Name;
5526e819
VS
94 m_Cache[i].Name = NULL;
95 }
96}
97
98
99
100void 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
118IMPLEMENT_CLASS(wxHtmlTag,wxObject)
119
120wxHtmlTag::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 }
72aa4a98
VS
144 else if (c == '\'') {
145 while ((i < end_pos) && ((c = source[i++]) != '\'')) m_Params += c;
146 m_Params += c;
147 }
5526e819
VS
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
158bool wxHtmlTag::HasParam(const wxString& par) const
159{
66a77a74
OK
160 const wxChar *st = m_Params, *p = par;
161 const wxChar *st2, *p2;
5526e819
VS
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
187wxString wxHtmlTag::GetParam(const wxString& par, bool with_commas) const
188{
66a77a74
OK
189 const wxChar *st = m_Params, *p = par;
190 const wxChar *st2, *p2;
5526e819 191 bool comma;
72aa4a98 192 char comma_char;
5526e819
VS
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;
72aa4a98
VS
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 }
5526e819 212 while (*st2 != 0) {
72aa4a98 213 if (comma && *st2 == comma_char) comma = FALSE;
5526e819
VS
214 else if ((*st2 == ' ') && (!comma)) break;
215 fnd += (*(st2++));
216 }
72aa4a98 217 if (!with_commas && (*(st2-1) == comma_char)) fnd.RemoveLast();
5526e819
VS
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 }
72aa4a98
VS
231 else if (*st2 == '\'') {
232 st2++;
233 while (*st2 != '\'') st2++;
234 }
5526e819
VS
235 st2++;
236 }
237 }
238 }
239}
240
241
242
66a77a74 243int wxHtmlTag::ScanParam(const wxString& par, wxChar *format, void *param) const
5526e819 244{
5526e819 245 wxString parval = GetParam(par);
66a77a74 246 return wxSscanf((const wxChar*)parval, format, param);
5526e819
VS
247}
248
4d223b67 249#endif