]>
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 wxChar
*src
= source
.c_str();
46 int i
, tg
, pos
, stpos
;
47 int lng
= source
.Length();
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
));
60 m_Cache
[tg
].Key
= stpos
= pos
++;
63 src
[pos
] != wxT('>') &&
64 src
[pos
] != wxT(' ') && src
[pos
] != wxT('\r') &&
65 src
[pos
] != wxT('\n') && src
[pos
] != wxT('\t')) {
66 dummy
[i
] = src
[pos
++];
67 if ((dummy
[i
] >= wxT('a')) && (dummy
[i
] <= wxT('z'))) dummy
[i
] -= (wxT('a') - wxT('A'));
71 m_Cache
[tg
].Name
= new wxChar
[i
+1];
72 memcpy(m_Cache
[tg
].Name
, dummy
, (i
+1)*sizeof(wxChar
));
74 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
76 if (src
[stpos
+1] == wxT('/')) { // ending tag:
77 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
78 // find matching begin tag:
79 for (i
= tg
; i
>= 0; i
--)
80 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, dummy
+1) == 0)) {
81 m_Cache
[i
].End1
= stpos
;
82 m_Cache
[i
].End2
= pos
+ 1;
87 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
94 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
95 for (i
= 0; i
< m_CacheSize
; i
++) {
96 delete[] m_Cache
[i
].Name
;
97 m_Cache
[i
].Name
= NULL
;
103 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
105 if (m_Cache
== NULL
) return;
106 if (m_Cache
[m_CachePos
].Key
!= at
) {
107 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
108 do {m_CachePos
+= delta
;} while (m_Cache
[m_CachePos
].Key
!= at
);
110 *end1
= m_Cache
[m_CachePos
].End1
;
111 *end2
= m_Cache
[m_CachePos
].End2
;
117 //-----------------------------------------------------------------------------
119 //-----------------------------------------------------------------------------
121 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
123 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
128 // fill-in name, params and begin pos:
129 m_Name
= m_Params
= wxEmptyString
;
131 if (source
[i
] == '/') {m_Ending
= TRUE
; i
++;}
132 else m_Ending
= FALSE
;
134 while ((i
< end_pos
) &&
135 ((c
= source
[i
++]) != ' ' && c
!= '\r' && c
!= '\n' && c
!= '\t' &&
137 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
141 if (source
[i
-1] != '>')
142 while ((i
< end_pos
) && ((c
= source
[i
++]) != '>')) {
143 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
144 if (c
== '\r' || c
== '\n' || c
== '\t') c
= ' '; // make future parsing a bit simpler
147 while ((i
< end_pos
) && ((c
= source
[i
++]) != '"')) m_Params
+= c
;
150 else if (c
== '\'') {
151 while ((i
< end_pos
) && ((c
= source
[i
++]) != '\'')) m_Params
+= c
;
157 cache
-> QueryTag(pos
, &m_End1
, &m_End2
);
158 if (m_End1
> end_pos
) m_End1
= end_pos
;
159 if (m_End2
> end_pos
) m_End2
= end_pos
;
164 bool wxHtmlTag::HasParam(const wxString
& par
) const
166 const wxChar
*st
= m_Params
, *p
= par
;
167 const wxChar
*st2
, *p2
;
169 if (*st
== 0) return FALSE
;
170 if (*p
== 0) return FALSE
;
171 for (st2
= st
, p2
= p
; ; st2
++) {
172 if (*p2
== 0) return TRUE
;
173 if (*st2
== 0) return FALSE
;
174 if (*p2
!= *st2
) p2
= p
;
175 if (*p2
== *st2
) p2
++;
176 if (*st2
== ' ') p2
= p
;
177 else if (*st2
== '=') {
179 while (*st2
!= ' ') {
182 while (*st2
!= '"') st2
++;
185 if (*st2
== 0) return FALSE
;
193 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
195 const wxChar
*st
= m_Params
, *p
= par
;
196 const wxChar
*st2
, *p2
;
200 if (*st
== 0) return "";
201 if (*p
== 0) return "";
202 for (st2
= st
, p2
= p
; ; st2
++) {
203 if (*p2
== 0) { // found
205 st2
++; // '=' character
208 if (!with_commas
&& (*(st2
) == '"')) {
213 else if (!with_commas
&& (*(st2
) == '\'')) {
219 if (comma
&& *st2
== comma_char
) comma
= FALSE
;
220 else if ((*st2
== ' ') && (!comma
)) break;
223 if (!with_commas
&& (*(st2
-1) == comma_char
)) fnd
.RemoveLast();
226 if (*st2
== 0) return "";
227 if (*p2
!= *st2
) p2
= p
;
228 if (*p2
== *st2
) p2
++;
229 if (*st2
== ' ') p2
= p
;
230 else if (*st2
== '=') {
232 while (*st2
!= ' ') {
235 while (*st2
!= '"') st2
++;
237 else if (*st2
== '\'') {
239 while (*st2
!= '\'') st2
++;
249 int wxHtmlTag::ScanParam(const wxString
& par
, wxChar
*format
, void *param
) const
251 wxString parval
= GetParam(par
);
252 return wxSscanf((const wxChar
*)parval
, format
, param
);