]>
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();
57 if (src
[pos
] == wxT('<')) // tag found:
59 if (m_CacheSize
% CACHE_INCREMENT
== 0)
60 m_Cache
= (sCacheItem
*) realloc(m_Cache
, (m_CacheSize
+ CACHE_INCREMENT
) * sizeof(sCacheItem
));
62 m_Cache
[tg
].Key
= stpos
= pos
++;
65 src
[pos
] != wxT('>') &&
66 src
[pos
] != wxT(' ') && src
[pos
] != wxT('\r') &&
67 src
[pos
] != wxT('\n') && src
[pos
] != wxT('\t'))
69 dummy
[i
] = src
[pos
++];
70 if ((dummy
[i
] >= wxT('a')) && (dummy
[i
] <= wxT('z'))) dummy
[i
] -= (wxT('a') - wxT('A'));
74 m_Cache
[tg
].Name
= new wxChar
[i
+1];
75 memcpy(m_Cache
[tg
].Name
, dummy
, (i
+1)*sizeof(wxChar
));
77 while (pos
< lng
&& src
[pos
] != wxT('>')) pos
++;
79 if (src
[stpos
+1] == wxT('/')) // ending tag:
81 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -2;
82 // find matching begin tag:
83 for (i
= tg
; i
>= 0; i
--)
84 if ((m_Cache
[i
].End1
== -1) && (wxStrcmp(m_Cache
[i
].Name
, dummy
+1) == 0))
86 m_Cache
[i
].End1
= stpos
;
87 m_Cache
[i
].End2
= pos
+ 1;
93 m_Cache
[tg
].End1
= m_Cache
[tg
].End2
= -1;
100 // ok, we're done, now we'll free .Name members of cache - we don't need it anymore:
101 for (i
= 0; i
< m_CacheSize
; i
++)
103 delete[] m_Cache
[i
].Name
;
104 m_Cache
[i
].Name
= NULL
;
110 void wxHtmlTagsCache::QueryTag(int at
, int* end1
, int* end2
)
112 if (m_Cache
== NULL
) return;
113 if (m_Cache
[m_CachePos
].Key
!= at
)
115 int delta
= (at
< m_Cache
[m_CachePos
].Key
) ? -1 : 1;
116 do {m_CachePos
+= delta
;} while (m_Cache
[m_CachePos
].Key
!= at
);
118 *end1
= m_Cache
[m_CachePos
].End1
;
119 *end2
= m_Cache
[m_CachePos
].End2
;
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 IMPLEMENT_CLASS(wxHtmlTag
,wxObject
)
131 wxHtmlTag::wxHtmlTag(const wxString
& source
, int pos
, int end_pos
, wxHtmlTagsCache
* cache
) : wxObject()
136 // fill-in name, params and begin pos:
137 m_Name
= m_Params
= wxEmptyString
;
139 if (source
[i
] == '/') { m_Ending
= TRUE
; i
++; }
140 else m_Ending
= FALSE
;
142 // find tag's name and convert it to uppercase:
143 while ((i
< end_pos
) &&
144 ((c
= source
[i
++]) != ' ' && c
!= '\r' && c
!= '\n' && c
!= '\t' &&
147 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
151 // if the tag has parameters, read them and "normalize" them,
152 // i.e. convert to uppercase, replace whitespaces by spaces and
153 // remove whitespaces around '=':
154 if (source
[i
-1] != '>')
155 while ((i
< end_pos
) && ((c
= source
[i
++]) != '>'))
157 if ((c
>= 'a') && (c
<= 'z')) c
-= ('a' - 'A');
158 if (c
== '\r' || c
== '\n' || c
== '\t') c
= ' '; // make future parsing a bit simpler
162 // remove spaces around the '=' character:
163 if (m_Params
.Length() > 1 &&
164 m_Params
[m_Params
.Length()-2] == ' ')
166 m_Params
.RemoveLast();
167 while (m_Params
.Length() > 0 && m_Params
.Last() == ' ')
168 m_Params
.RemoveLast();
171 while ((i
< end_pos
) && (source
[i
++] == ' ')) {}
172 if (i
< end_pos
) i
--;
174 // ...and copy the value to m_Params:
175 while ((i
< end_pos
) && ((c
= source
[i
++]) != '"')) m_Params
+= c
;
180 while ((i
< end_pos
) && ((c
= source
[i
++]) != '\'')) m_Params
+= c
;
186 cache
->QueryTag(pos
, &m_End1
, &m_End2
);
187 if (m_End1
> end_pos
) m_End1
= end_pos
;
188 if (m_End2
> end_pos
) m_End2
= end_pos
;
193 bool wxHtmlTag::HasParam(const wxString
& par
) const
195 const wxChar
*st
= m_Params
, *p
= par
;
196 const wxChar
*st2
, *p2
;
198 if (*st
== 0) return FALSE
;
199 if (*p
== 0) return FALSE
;
200 for (st2
= st
, p2
= p
; ; st2
++)
202 if (*p2
== 0) return TRUE
;
203 if (*st2
== 0) return FALSE
;
204 if (*p2
!= *st2
) p2
= p
;
205 if (*p2
== *st2
) p2
++;
206 if (*st2
== ' ') p2
= p
;
207 else if (*st2
== '=')
215 while (*st2
!= '"') st2
++;
218 if (*st2
== 0) return FALSE
;
226 wxString
wxHtmlTag::GetParam(const wxString
& par
, bool with_commas
) const
228 const wxChar
*st
= m_Params
, *p
= par
;
229 const wxChar
*st2
, *p2
;
233 if (*st
== 0) return "";
234 if (*p
== 0) return "";
235 for (st2
= st
, p2
= p
; ; st2
++)
237 if (*p2
== 0) // found
240 st2
++; // '=' character
243 if (!with_commas
&& (*(st2
) == '"'))
249 else if (!with_commas
&& (*(st2
) == '\''))
258 if (comma
&& *st2
== comma_char
) comma
= FALSE
;
259 else if ((*st2
== ' ') && (!comma
)) break;
262 if (!with_commas
&& (*(st2
-1) == comma_char
)) fnd
.RemoveLast();
265 if (*st2
== 0) return "";
266 if (*p2
!= *st2
) p2
= p
;
267 if (*p2
== *st2
) p2
++;
268 if (*st2
== ' ') p2
= p
;
269 else if (*st2
== '=')
277 while (*st2
!= '"') st2
++;
279 else if (*st2
== '\'')
282 while (*st2
!= '\'') st2
++;
292 int wxHtmlTag::ScanParam(const wxString
& par
, wxChar
*format
, void *param
) const
294 wxString parval
= GetParam(par
);
295 return wxSscanf((const wxChar
*)parval
, format
, param
);