]>
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>
28 #include <stdio.h> // for vsscanf
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
40 #define CACHE_INCREMENT 64
42 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
44 const char *src
= source
.c_str();
45 int i
, tg
, pos
, stpos
;
46 int lng
= source
.Length();
55 if (src
[pos
] == '<') { // tag found:
56 if (m_CacheSize
% CACHE_INCREMENT
== 0)
57 m_Cache
= (sCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(sCacheItem
));
59 m_Cache
[tg
].Key
= stpos
= pos
++;
61 while ((src
[pos
] != '>') && (src
[pos
] != ' ')) {
62 dummy
[i
] = src
[pos
++];
63 if ((dummy
[i
] >= 'a') && (dummy
[i
] <= 'z')) dummy
[i
] -= ('a' - 'A');
67 m_Cache
[tg
].Name
= new char[i
+1];
68 memcpy(m_Cache
[tg
].Name
, dummy
, i
+1);
70 while (src
[pos
] != '>') pos
++;
72 if (src
[stpos
+1] == '/') { // ending tag:
73 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
74 // find matching begin tag:
75 for (i
= tg
; i
>= 0; i
--)
76 if ((m_Cache
[i
].End1
== -1) && (strcmp(m_Cache
[i
].Name
, dummy
+1) == 0)) {
77 m_Cache
[i
].End1
= stpos
;
78 m_Cache
[i
].End2
= pos
+ 1;
83 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
90 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
91 for (i
= 0; i
< m_CacheSize
; i
++) {
92 delete[] m_Cache
[i
].Name
;
93 m_Cache
[i
].Name
= NULL
;
99 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
101 if (m_Cache
== NULL
) return;
102 if (m_Cache
[m_CachePos
].Key
!= at
) {
103 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
104 do {m_CachePos
+= delta
;} while (m_Cache
[m_CachePos
].Key
!= at
);
106 *end1
= m_Cache
[m_CachePos
].End1
;
107 *end2
= m_Cache
[m_CachePos
].End2
;
113 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
117 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
119 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
124 // fill-in name, params and begin pos:
125 m_Name
= m_Params
= wxEmptyString
;
127 if (source
[i
] == '/') {m_Ending
= TRUE
; i
++;}
128 else m_Ending
= FALSE
;
130 while ((i
< end_pos
) && ((c
= source
[i
++]) != ' ') && (c
!= '>')) {
131 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
135 if (source
[i
-1] != '>')
136 while ((i
< end_pos
) && ((c
= source
[i
++]) != '>')) {
137 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
140 while ((i
< end_pos
) && ((c
= source
[i
++]) != '"')) m_Params
+= c
;
146 cache
-> QueryTag(pos
, &m_End1
, &m_End2
);
147 if (m_End1
> end_pos
) m_End1
= end_pos
;
148 if (m_End2
> end_pos
) m_End2
= end_pos
;
153 bool wxHtmlTag::HasParam(const wxString
& par
) const
155 const char *st
= m_Params
, *p
= par
;
156 const char *st2
, *p2
;
158 if (*st
== 0) return FALSE
;
159 if (*p
== 0) return FALSE
;
160 for (st2
= st
, p2
= p
; ; st2
++) {
161 if (*p2
== 0) return TRUE
;
162 if (*st2
== 0) return FALSE
;
163 if (*p2
!= *st2
) p2
= p
;
164 if (*p2
== *st2
) p2
++;
165 if (*st2
== ' ') p2
= p
;
166 else if (*st2
== '=') {
168 while (*st2
!= ' ') {
171 while (*st2
!= '"') st2
++;
174 if (*st2
== 0) return FALSE
;
182 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
184 const char *st
= m_Params
, *p
= par
;
185 const char *st2
, *p2
;
188 if (*st
== 0) return "";
189 if (*p
== 0) return "";
190 for (st2
= st
, p2
= p
; ; st2
++) {
191 if (*p2
== 0) { // found
193 st2
++; // '=' character
195 if (!with_commas
&& (*(st2
) == '"')) {st2
++; comma
= TRUE
;}
197 if (*st2
== '"') comma
= !comma
;
198 else if ((*st2
== ' ') && (!comma
)) break;
201 if (!with_commas
&& (*(st2
-1) == '"')) fnd
.RemoveLast();
204 if (*st2
== 0) return "";
205 if (*p2
!= *st2
) p2
= p
;
206 if (*p2
== *st2
) p2
++;
207 if (*st2
== ' ') p2
= p
;
208 else if (*st2
== '=') {
210 while (*st2
!= ' ') {
213 while (*st2
!= '"') st2
++;
223 void wxHtmlTag::ScanParam(const wxString
& par
, char *format
, ...) const
226 wxString parval
= GetParam(par
);
228 va_start(argptr
, format
);
230 //#if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__VISUALC__)
232 sscanf((const char*)parval
, format
, va_arg(argptr
, void *));
234 vsscanf((const char*)parval
, format
, argptr
);
238 --- vsscanf is not defined under Cygwin or Mingw32 or M$ Visual C++ environment
239 if this module doesn't compile with your compiler,
240 modify the def statement and let me know. Thanks...
242 So far wxHtml functions are scanning only _one_ value
243 so I workarounded this by supposing that there is only