]>
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
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 struct wxHtmlCacheItem
39 // this is "pos" value passed to wxHtmlTag's constructor.
40 // it is position of '<' character of the tag
43 // end positions for the tag:
44 // end1 is '<' of ending tag,
45 // end2 is '>' or both are
46 // -1 if there is no ending tag for this one...
47 // or -2 if this is ending tag </...>
55 IMPLEMENT_CLASS(wxHtmlTagsCache
,wxObject
)
57 #define CACHE_INCREMENT 64
59 wxHtmlTagsCache::wxHtmlTagsCache(const wxString
& source
)
61 const wxChar
*src
= source
.c_str();
62 int i
, tg
, pos
, stpos
;
63 int lng
= source
.Length();
73 if (src
[pos
] == wxT('<')) // tag found:
75 if (m_CacheSize
% CACHE_INCREMENT
== 0)
76 m_Cache
= (wxHtmlCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(wxHtmlCacheItem
));
78 m_Cache
[tg
].Key
= stpos
= pos
++;
81 src
[pos
] != wxT('>') &&
82 src
[pos
] != wxT(' ') && src
[pos
] != wxT('\r') &&
83 src
[pos
] != wxT('\n') && src
[pos
] != wxT('\t'))
85 dummy
[i
] = src
[pos
++];
86 if ((dummy
[i
] >= wxT('a')) && (dummy
[i
] <= wxT('z'))) dummy
[i
] -= (wxT('a') - wxT('A'));
90 m_Cache
[tg
].Name
= new wxChar
[i
+1];
91 memcpy(m_Cache
[tg
].Name
, dummy
, (i
+1)*sizeof(wxChar
));
93 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
95 if (src
[stpos
+1] == wxT('/')) // ending tag:
97 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
98 // find matching begin tag:
99 for (i
= tg
; i
>= 0; i
--)
100 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, dummy
+1) == 0))
102 m_Cache
[i
].End1
= stpos
;
103 m_Cache
[i
].End2
= pos
+ 1;
109 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
116 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
117 for (i
= 0; i
< m_CacheSize
; i
++)
119 delete[] m_Cache
[i
].Name
;
120 m_Cache
[i
].Name
= NULL
;
126 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
128 if (m_Cache
== NULL
) return;
129 if (m_Cache
[m_CachePos
].Key
!= at
)
131 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
132 do {m_CachePos
+= delta
;} while (m_Cache
[m_CachePos
].Key
!= at
);
134 *end1
= m_Cache
[m_CachePos
].End1
;
135 *end2
= m_Cache
[m_CachePos
].End2
;
141 //-----------------------------------------------------------------------------
143 //-----------------------------------------------------------------------------
145 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
147 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
152 // fill-in name, params and begin pos:
153 m_Name
= m_Params
= wxEmptyString
;
155 if (source
[i
] == wxT('/')) { m_Ending
= TRUE
; i
++; }
156 else m_Ending
= FALSE
;
158 // find tag's name and convert it to uppercase:
159 while ((i
< end_pos
) &&
160 ((c
= source
[i
++]) != wxT(' ') && c
!= wxT('\r') &&
161 c
!= wxT('\n') && c
!= wxT('\t') &&
164 if ((c
>= wxT('a')) && (c
<= wxT('z'))) c
-= (wxT('a') - wxT('A'));
168 // if the tag has parameters, read them and "normalize" them,
169 // i.e. convert to uppercase, replace whitespaces by spaces and
170 // remove whitespaces around '=':
171 if (source
[i
-1] != wxT('>'))
172 while ((i
< end_pos
) && ((c
= source
[i
++]) != wxT('>')))
174 if ((c
>= wxT('a')) && (c
<= wxT('z')))
175 c
-= (wxT('a') - wxT('A'));
176 if (c
== wxT('\r') || c
== wxT('\n') || c
== wxT('\t'))
177 c
= wxT(' '); // make future parsing a bit simpler
181 // remove spaces around the '=' character:
182 if (m_Params
.Length() > 1 &&
183 m_Params
[m_Params
.Length()-2] == wxT(' '))
185 m_Params
.RemoveLast();
186 while (m_Params
.Length() > 0 && m_Params
.Last() == wxT(' '))
187 m_Params
.RemoveLast();
188 m_Params
+= wxT('"');
190 while ((i
< end_pos
) && (source
[i
++] == wxT(' '))) {}
191 if (i
< end_pos
) i
--;
193 // ...and copy the value to m_Params:
194 while ((i
< end_pos
) && ((c
= source
[i
++]) != wxT('"')))
198 else if (c
== wxT('\''))
200 while ((i
< end_pos
) && ((c
= source
[i
++]) != wxT('\'')))
207 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
208 if (m_End1
> end_pos
) m_End1
= end_pos
;
209 if (m_End2
> end_pos
) m_End2
= end_pos
;
214 bool wxHtmlTag::HasParam(const wxString
& par
) const
216 const wxChar
*st
= m_Params
, *p
= par
;
217 const wxChar
*st2
, *p2
;
218 const wxChar invalid
= wxT('\1');
220 if (*st
== 0) return FALSE
;
221 if (*p
== 0) return FALSE
;
222 for (st2
= st
, p2
= p
; ; st2
++)
224 if (*p2
== 0) return TRUE
;
225 if (*st2
== 0) return FALSE
;
226 if (*p2
!= *st2
) p2
= &invalid
;
227 if (*p2
== *st2
) p2
++;
228 if (*st2
== wxT(' ')) p2
= p
;
229 else if (*st2
== wxT('='))
232 while (*st2
!= wxT(' '))
234 if (*st2
== wxT('"'))
237 while (*st2
!= wxT('"')) st2
++;
240 if (*st2
== 0) return FALSE
;
248 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
250 const wxChar
*st
= m_Params
, *p
= par
;
251 const wxChar
*st2
, *p2
;
252 const wxChar invalid
= wxT('\1');
256 if (*st
== 0) return wxEmptyString
;
257 if (*p
== 0) return wxEmptyString
;
258 for (st2
= st
, p2
= p
; ; st2
++)
260 if (*p2
== 0) // found
262 wxString fnd
= wxEmptyString
;
263 st2
++; // '=' character
265 comma_char
= wxT('\0');
266 if (!with_commas
&& (*(st2
) == wxT('"')))
270 comma_char
= wxT('"');
272 else if (!with_commas
&& (*(st2
) == wxT('\'')))
276 comma_char
= wxT('\'');
281 if (comma
&& *st2
== comma_char
) comma
= FALSE
;
282 else if ((*st2
== wxT(' ')) && (!comma
)) break;
285 if (!with_commas
&& (*(st2
-1) == comma_char
)) fnd
.RemoveLast();
288 if (*st2
== 0) return wxEmptyString
;
289 if (*p2
!= *st2
) p2
= &invalid
;
290 if (*p2
== *st2
) p2
++;
291 if (*st2
== wxT(' ')) p2
= p
;
292 else if (*st2
== wxT('='))
295 while (*st2
!= wxT(' '))
297 if (*st2
== wxT('"'))
300 while (*st2
!= wxT('"')) st2
++;
302 else if (*st2
== wxT('\''))
305 while (*st2
!= wxT('\'')) st2
++;
315 int wxHtmlTag::ScanParam(const wxString
& par
, wxChar
*format
, void *param
) const
317 wxString parval
= GetParam(par
);
318 return wxSscanf(parval
, format
, param
);