]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows Licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation
15 #include <wx/wxprec.h>
28 #include "wx/html/htmltag.h"
29 #include <stdio.h> // for vsscanf
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
41 #define CACHE_INCREMENT 64
43 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
45 const char *src
= source
.c_str();
46 int i
, tg
, pos
, stpos
;
47 int lng
= source
.Length();
56 if (src
[pos
] == '<') { // tag found:
57 if (m_CacheSize
% CACHE_INCREMENT
== 0)
58 m_Cache
= (sCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(sCacheItem
));
60 m_Cache
[tg
].Key
= stpos
= pos
++;
62 while ((src
[pos
] != '>') && (src
[pos
] != ' ')) {
63 dummy
[i
] = src
[pos
++];
64 if ((dummy
[i
] >= 'a') && (dummy
[i
] <= 'z')) dummy
[i
] -= ('a' - 'A');
68 m_Cache
[tg
].Name
= new char[i
+1];
69 memcpy(m_Cache
[tg
].Name
, dummy
, i
+1);
71 while (src
[pos
] != '>') pos
++;
73 if (src
[stpos
+1] == '/') { // 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) && (strcmp(m_Cache
[i
].Name
, dummy
+1) == 0)) {
78 m_Cache
[i
].End1
= stpos
;
79 m_Cache
[i
].End2
= pos
+ 1;
84 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
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
;
100 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
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
);
107 *end1
= m_Cache
[m_CachePos
].End1
;
108 *end2
= m_Cache
[m_CachePos
].End2
;
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
118 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
120 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
125 // fill-in name, params and begin pos:
126 m_Name
= m_Params
= wxEmptyString
;
128 if (source
[i
] == '/') {m_Ending
= TRUE
; i
++;}
129 else m_Ending
= FALSE
;
131 while ((i
< end_pos
) && ((c
= source
[i
++]) != ' ') && (c
!= '>')) {
132 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
136 if (source
[i
-1] != '>')
137 while ((i
< end_pos
) && ((c
= source
[i
++]) != '>')) {
138 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
141 while ((i
< end_pos
) && ((c
= source
[i
++]) != '"')) m_Params
+= c
;
144 else if (c
== '\'') {
145 while ((i
< end_pos
) && ((c
= source
[i
++]) != '\'')) m_Params
+= c
;
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
;
158 bool wxHtmlTag::HasParam(const wxString
& par
) const
160 const char *st
= m_Params
, *p
= par
;
161 const char *st2
, *p2
;
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
== '=') {
173 while (*st2
!= ' ') {
176 while (*st2
!= '"') st2
++;
179 if (*st2
== 0) return FALSE
;
187 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
189 const char *st
= m_Params
, *p
= par
;
190 const char *st2
, *p2
;
194 if (*st
== 0) return "";
195 if (*p
== 0) return "";
196 for (st2
= st
, p2
= p
; ; st2
++) {
197 if (*p2
== 0) { // found
199 st2
++; // '=' character
202 if (!with_commas
&& (*(st2
) == '"')) {
207 else if (!with_commas
&& (*(st2
) == '\'')) {
213 if (comma
&& *st2
== comma_char
) comma
= FALSE
;
214 else if ((*st2
== ' ') && (!comma
)) break;
217 if (!with_commas
&& (*(st2
-1) == comma_char
)) fnd
.RemoveLast();
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
== '=') {
226 while (*st2
!= ' ') {
229 while (*st2
!= '"') st2
++;
231 else if (*st2
== '\'') {
233 while (*st2
!= '\'') st2
++;
243 int wxHtmlTag::ScanParam(const wxString
& par
, char *format
, void *param
) const
245 wxString parval
= GetParam(par
);
246 return sscanf((const char*)parval
, format
, param
);