// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
-#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
- #pragma implementation "htmlparser.h"
-#endif
-#include "wx/wx.h"
+// For compilers that support precompilation, includes "wx/wx.h".
+#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
-#include "wx/textfile.h"
#include "wx/wfstream.h"
+#include "wx/textfile.h"
#include "wx/txtstrm.h"
-
-#include <ctype.h>
-
#include "htmlparser.h"
/// Useful insertion operators for wxOutputStream.
return ParseString(text);
}
else
- return FALSE;
+ return false;
}
bool wxSimpleHtmlParser::ParseString(const wxString& str)
bool bResult = ParseHtml(m_topLevel);
- wxASSERT(bResult); // Failed to parse the TAGs.
+ wxASSERT(bResult); // Failed to parse the TAGs.
// Hint: Check if every open tag has a close tag!
return bResult;
bool wxSimpleHtmlParser::ParseHtml(wxSimpleHtmlTag* parent)
{
if (!parent)
- return FALSE;
+ return false;
while (!Eof())
{
if (IsCloseTagNeeded(tag->GetName()))
{
if (!parent->GetParent())
- return FALSE;
+ return false;
parent->GetParent()->AppendTag(tag);
- return TRUE;
+ return true;
}
else
parent->AppendTag(tag);
- }
+ }
}
else if (IsTagStartBracket(GetChar(m_pos)))
{
if (IsCloseTagNeeded(tag->GetName()))
{
if (!ParseHtml(tag))
- return FALSE; // Something didn't go ok, so don't continue.
+ return false; // Something didn't go ok, so don't continue.
}
}
else
if(parent->GetParent())
parent->GetParent()->AppendTag(tag);
else
- parent->AppendTag(tag); // When this occurs it is probably the
+ parent->AppendTag(tag); // When this occurs it is probably the
// empty lines at the end of the file...
}
}
- return TRUE;
+ return true;
}
// Plain text, up until an angled bracket
{
while (!Eof() && GetChar(m_pos) != wxT('<'))
{
- text += GetChar(m_pos);
+ text += (wxChar)GetChar(m_pos);
m_pos ++;
}
DecodeSpecialChars(text);
- return TRUE;
+ return true;
}
wxSimpleHtmlTag* wxSimpleHtmlParser::ParseTagHeader()
EatWhitespace();
wxString word;
- ReadWord(word, TRUE);
+ ReadWord(word, true);
EatWhitespace();
wxSimpleHtmlTag* wxSimpleHtmlParser::ParseTagClose()
{
- Matches(wxT("</"), TRUE);
+ Matches(wxT("</"), true);
EatWhitespace();
wxString word;
- ReadWord(word, TRUE);
+ ReadWord(word, true);
EatWhitespace();
m_pos ++;
if (IsString())
{
- ReadString(attrName, TRUE);
+ ReadString(attrName, true);
tag->AppendAttribute(attrName, wxEmptyString);
}
else if (IsNumeric(GetChar(m_pos)))
{
- ReadNumber(attrName, TRUE);
+ ReadNumber(attrName, true);
tag->AppendAttribute(attrName, wxEmptyString);
}
else
{
// Try to read an attribute name/value pair, or at least a name
// without the value
- ReadLiteral(attrName, TRUE);
+ ReadLiteral(attrName, true);
EatWhitespace();
if (GetChar(m_pos) == wxT('='))
EatWhitespace();
if (IsString())
- ReadString(attrValue, TRUE);
+ ReadString(attrValue, true);
else if (!Eof() && !IsTagEndBracket(GetChar(m_pos)))
- ReadLiteral(attrValue, TRUE);
+ ReadLiteral(attrValue, true);
}
if (!attrName.IsEmpty())
tag->AppendAttribute(attrName, attrValue);
}
}
- return TRUE;
+ return true;
}
// e.g. <!DOCTYPE ....>
wxSimpleHtmlTag* wxSimpleHtmlParser::ParseDirective()
{
- Matches(wxT("<!"), TRUE);
+ Matches(wxT("<!"), true);
EatWhitespace();
wxString word;
- ReadWord(word, TRUE);
+ ReadWord(word, true);
EatWhitespace();
// e.g. <?xml .... ?>
wxSimpleHtmlTag* wxSimpleHtmlParser::ParseXMLDeclaration()
{
- Matches(wxT("<?"), TRUE);
+ Matches(wxT("<?"), true);
EatWhitespace();
wxString word;
- ReadWord(word, TRUE);
+ ReadWord(word, true);
EatWhitespace();
bool wxSimpleHtmlParser::ParseComment()
{
// Eat the comment tag start
- Matches(wxT("<!--"), TRUE);
+ Matches(wxT("<!--"), true);
- while (!Eof() && !Matches(wxT("-->"), TRUE))
+ while (!Eof() && !Matches(wxT("-->"), true))
{
m_pos ++;
}
- return TRUE;
+ return true;
}
bool wxSimpleHtmlParser::EatWhitespace()
{
while (!Eof() && IsWhitespace(GetChar(m_pos)))
m_pos ++;
- return TRUE;
+ return true;
}
bool wxSimpleHtmlParser::EatWhitespace(int& pos)
{
while (!Eof(pos) && IsWhitespace(GetChar(pos)))
pos ++;
- return TRUE;
+ return true;
}
bool wxSimpleHtmlParser::ReadString(wxString& str, bool eatIt)
if (eatIt)
m_pos = pos;
DecodeSpecialChars(str);
- return TRUE;
+ return true;
}
else
- return FALSE;
+ return false;
}
bool wxSimpleHtmlParser::ReadWord(wxString& str, bool eatIt)
int pos = m_pos;
if (!IsAlpha(GetChar(pos)))
- return FALSE;
+ return false;
str += (wxChar) GetChar(pos) ;
pos ++;
if (eatIt)
m_pos = pos;
DecodeSpecialChars(str);
- return TRUE;
+ return true;
}
bool wxSimpleHtmlParser::ReadNumber(wxString& str, bool eatIt)
int pos = m_pos;
if (!IsNumeric(GetChar(pos)))
- return FALSE;
+ return false;
str += (wxChar) GetChar(pos) ;
pos ++;
if (eatIt)
m_pos = pos;
DecodeSpecialChars(str);
- return TRUE;
+ return true;
}
// Could be number, string, whatever, but read up until whitespace or end of tag (but not a quoted string)
while (!Eof(pos) && !IsWhitespace(GetChar(pos)) && !IsTagEndBracket(GetChar(pos)) && GetChar(pos) != wxT('='))
{
- str += GetChar(pos);
+ str += (wxChar)GetChar(pos);
pos ++;
}
if (eatIt)
m_pos = pos;
DecodeSpecialChars(str);
- return TRUE;
+ return true;
}
bool wxSimpleHtmlParser::IsComment()
bool wxSimpleHtmlParser::IsCloseTagNeeded(const wxString &name)
{
- if (name.IsSameAs(wxT("P"), FALSE)) // e.g <P>
- return FALSE;
+ if (name.IsSameAs(wxT("P"), false)) // e.g <P>
+ return false;
// ToDo add more items here.
- return TRUE;
+ return true;
}
// Encode/Decode Special Characters.
// See here for the used table: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsql/ac_xml1_1nqk.asp
/* static */ void wxSimpleHtmlParser::DecodeSpecialChars(wxString &value)
{
- // XML translation
- value.Replace(wxT(">"), wxT(">"), TRUE);
- value.Replace(wxT("<"), wxT("<"), TRUE);
- value.Replace(wxT("""), wxT("\""), TRUE);
- value.Replace(wxT("'"), wxT("'"), TRUE);
- value.Replace(wxT("&"), wxT("&"), TRUE); // Note: do this as last to prevent replace problems.
+ // XML translation
+ value.Replace(wxT(">"), wxT(">"), true);
+ value.Replace(wxT("<"), wxT("<"), true);
+ value.Replace(wxT("""), wxT("\""), true);
+ value.Replace(wxT("'"), wxT("'"), true);
+ value.Replace(wxT("&"), wxT("&"), true); // Note: do this as last to prevent replace problems.
}
/* static */ wxString wxSimpleHtmlParser::EncodeSpecialChars(const wxString &value)
{
wxString newvalue = value;
- // XML translation
- newvalue.Replace(wxT("&"), wxT("&"), TRUE); // Note: do this as first to prevent replace problems.
- newvalue.Replace(wxT(">"), wxT(">"), TRUE);
- newvalue.Replace(wxT("<"), wxT("<"), TRUE);
- newvalue.Replace(wxT("\""),wxT("""), TRUE);
- newvalue.Replace(wxT("'"), wxT("'"), TRUE);
-
+ // XML translation
+ newvalue.Replace(wxT("&"), wxT("&"), true); // Note: do this as first to prevent replace problems.
+ newvalue.Replace(wxT(">"), wxT(">"), true);
+ newvalue.Replace(wxT("<"), wxT("<"), true);
+ newvalue.Replace(wxT("\""),wxT("""), true);
+ newvalue.Replace(wxT("'"), wxT("'"), true);
+
return newvalue;
}
if (fstream.Ok())
{
Write(fstream);
- return TRUE;
+ return true;
}
else
- return FALSE;
+ return false;
}
/*
{
child->m_next = NULL;
delete child;
-
+
if (prev != NULL)
prev->m_next = next;
else
while (last->m_next)
last = last->m_next;
- last->m_next = tag;
+ last->m_next = tag;
}
else
{
if (attr)
{
value = attr->GetValue();
- return TRUE;
+ return true;
}
else
- return FALSE;
+ return false;
}
-// Search forward from this tag until we find a tag with this name & attribute
+// Search forward from this tag until we find a tag with this name & attribute
wxSimpleHtmlTag* wxSimpleHtmlTag::FindTag(const wxString& tagName, const wxString& attrName)
{
wxSimpleHtmlTag* tag = m_next;
while (tag)
{
if (tag->GetType() == wxSimpleHtmlTag_Close && tag->NameIs(tagName))
- return TRUE;
+ return true;
if (tag->GetType() == wxSimpleHtmlTag_Text)
text += tag->GetText();
tag = tag->m_next;
}
- return TRUE;
+ return true;
}
void wxSimpleHtmlTag::Write(wxOutputStream& stream)
{
// Some helpers to layout the open and close tags.
- static bool sbUseTab = TRUE;
+ static bool sbUseTab = true;
static size_t snTabLevel = 0;
#if 0 // Enable if no tabs should be used to align the tags.
attr->Write(stream);
if (i < GetAttributeCount() - 1)
stream << wxT(" ");
- }
+ }
if(!m_children)
{
- sbUseTab = FALSE; // We're putting the open a close tag on the same line,
+ sbUseTab = false; // We're putting the open a close tag on the same line,
// so we don't wan't any tabs
stream << wxT(">");
}
else
{
- // sbUseTab = TRUE;
+ // sbUseTab = true;
stream << wxT(">\n");
}
snTabLevel++;
stream << wxT("\t");
}
stream << wxT("</") << wxSimpleHtmlParser::EncodeSpecialChars(m_name) << wxT(">\n");
- sbUseTab = TRUE;
+ sbUseTab = true;
break;
}
default: