]>
git.saurik.com Git - wxWidgets.git/blob - src/html/htmltag.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtmlTag class (represents single tag)
4 // Author: Vaclav Slavik
5 // Copyright: (c) 1999 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation
14 #include <wx/wxprec.h>
27 #include <wx/html/htmltag.h>
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
39 #define CACHE_INCREMENT 64
41 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
43 const char *src
= source
.c_str();
44 int i
, tg
, pos
, stpos
;
45 int lng
= source
.Length();
54 if (src
[pos
] == '<') { // tag found:
55 if (m_CacheSize
% CACHE_INCREMENT
== 0)
56 m_Cache
= (sCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(sCacheItem
));
58 m_Cache
[tg
].Key
= stpos
= pos
++;
60 while ((src
[pos
] != '>') && (src
[pos
] != ' ')) {
61 dummy
[i
] = src
[pos
++];
62 if ((dummy
[i
] >= 'a') && (dummy
[i
] <= 'z')) dummy
[i
] -= ('a' - 'A');
66 m_Cache
[tg
].Name
= (char*) malloc(i
+1);
67 memcpy(m_Cache
[tg
].Name
, dummy
, i
+1);
69 while (src
[pos
] != '>') pos
++;
71 if (src
[stpos
+1] == '/') { // ending tag:
72 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
73 // find matching begin tag:
74 for (i
= tg
; i
>= 0; i
--)
75 if ((m_Cache
[i
].End1
== -1) && (strcmp(m_Cache
[i
].Name
, dummy
+1) == 0)) {
76 m_Cache
[i
].End1
= stpos
;
77 m_Cache
[i
].End2
= pos
+ 1;
82 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
89 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
90 for (i
= 0; i
< m_CacheSize
; i
++) {
91 free(m_Cache
[i
].Name
);
92 m_Cache
[i
].Name
= NULL
;
98 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
100 if (m_Cache
== NULL
) return;
101 if (m_Cache
[m_CachePos
].Key
!= at
) {
102 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
103 do {m_CachePos
+= delta
;} while (m_Cache
[m_CachePos
].Key
!= at
);
105 *end1
= m_Cache
[m_CachePos
].End1
;
106 *end2
= m_Cache
[m_CachePos
].End2
;
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
116 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
118 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
123 // fill-in name, params and begin pos:
124 m_Name
= m_Params
= wxEmptyString
;
126 if (source
[i
] == '/') {m_Ending
= TRUE
; i
++;}
127 else m_Ending
= FALSE
;
129 while ((i
< end_pos
) && ((c
= source
[i
++]) != ' ') && (c
!= '>')) {
130 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
134 if (source
[i
-1] != '>')
135 while ((i
< end_pos
) && ((c
= source
[i
++]) != '>')) {
136 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
139 while ((i
< end_pos
) && ((c
= source
[i
++]) != '"')) m_Params
+= c
;
145 cache
-> QueryTag(pos
, &m_End1
, &m_End2
);
146 if (m_End1
> end_pos
) m_End1
= end_pos
;
147 if (m_End2
> end_pos
) m_End2
= end_pos
;
152 bool wxHtmlTag::HasParam(const wxString
& par
) const
154 const char *st
= m_Params
, *p
= par
;
155 const char *st2
, *p2
;
157 if (*st
== 0) return FALSE
;
158 if (*p
== 0) return FALSE
;
159 for (st2
= st
, p2
= p
; ; st2
++) {
160 if (*p2
== 0) return TRUE
;
161 if (*st2
== 0) return FALSE
;
162 if (*p2
!= *st2
) p2
= p
;
163 if (*p2
== *st2
) p2
++;
164 if (*st2
== ' ') p2
= p
;
165 else if (*st2
== '=') {
167 while (*st2
!= ' ') {
170 while (*st2
!= '"') st2
++;
173 if (*st2
== 0) return FALSE
;
181 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
183 const char *st
= m_Params
, *p
= par
;
184 const char *st2
, *p2
;
187 if (*st
== 0) return "";
188 if (*p
== 0) return "";
189 for (st2
= st
, p2
= p
; ; st2
++) {
190 if (*p2
== 0) { // found
192 st2
++; // '=' character
194 if (!with_commas
&& (*(st2
) == '"')) {st2
++; comma
= TRUE
;}
196 if (*st2
== '"') comma
= !comma
;
197 else if ((*st2
== ' ') && (!comma
)) break;
200 if (!with_commas
&& (*(st2
-1) == '"')) fnd
.RemoveLast();
203 if (*st2
== 0) return "";
204 if (*p2
!= *st2
) p2
= p
;
205 if (*p2
== *st2
) p2
++;
206 if (*st2
== ' ') p2
= p
;
207 else if (*st2
== '=') {
209 while (*st2
!= ' ') {
212 while (*st2
!= '"') st2
++;
222 void wxHtmlTag::ScanParam(const wxString
& par
, char *format
, ...) const
225 wxString parval
= GetParam(par
);
227 va_start(argptr
, format
);
229 #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__VISUALC__)
230 sscanf((const char*)parval
, format
, va_arg(argptr
, void *));
232 vsscanf((const char*)parval
, format
, argptr
);
236 --- vsscanf is not defined under Cygwin or Mingw32 or M$ Visual C++ environment
237 if this module doesn't compile with your compiler,
238 modify the def statement and let me know. Thanks...
240 So far wxHtml functions are scanning only _one_ value
241 so I workarounded this by supposing that there is only