先看看下面这个HTML字符串。
<HTML>=====<SPARATOR>s-value</SPARATOR>++++++<BODY A="AA">{123}<BR>[456]</BODY><IMG>*****</HTML>
很不标准吧?但浏览器却是能够正常显示的。Lingoes的结果里面充斥了大量的这样的字符串,但就如我常说的 -- “代码在手,天下我有”一样,修改了TinyHtmlParser,咱也能解析了~
这下不怕了,嘿嘿。。。下面是修改后的TinyHtmlParser,修改了CParserData结构,使其更加容易理解,而“容易理解”对于我来说很重要,因为我喜欢--第一感觉该如何实现,那么代码就该按照感觉去写。代码只是现实的一种描述而已,越容易,越简单,越好。
#ifndef __TINYHTMLPARSER_H__
#define __TINYHTMLPARSER_H__
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <iostream>
#include <string>
#include <queue>
#include <stack>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
namespace TinyHtmlParser
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
enum ExceptionNumber
{ EN_UNKNOWN = -1, EN_ATTRIB_VALUEMISS = 0, EN_ATTRIB_VALUEBROKEN,
EN_DOCUMENT_FORMATERROR
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
class CExceptionObject
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
public:
CExceptionObject(ExceptionNumber type, const std::wstring& info);
CExceptionObject(const CExceptionObject& right);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~CExceptionObject()
{}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int Number() const
{ return _type; }
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
const std::wstring& Info() const
{ return _info; }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
protected:
ExceptionNumber _type;
std::wstring _info;
private:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
CExceptionObject& operator = (const CExceptionObject& right)
{ return *this; }
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
enum ElementType
{ ET_UNKNOWN = -1, ET_TAG = 0, ET_NODE, ET_ELEMENT };//0:just a tag, 1:no value, 2:have value
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
class CAttributeObject
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
public:
CAttributeObject(const std::wstring& a, const std::wstring& v)
: attr(a), value(v), next(NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~CAttributeObject()
{}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void Show(std::wostream& os) const;
public:
std::wstring attr;
std::wstring value;
CAttributeObject* next;
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
class CElementObject
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
public:
CElementObject();
virtual ~CElementObject();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
virtual int Analyse();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CAttributeObject* FindAttribute(const std::wstring& attr) const;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void Show(std::wostream& os) const;
protected:
int AnalyseAttribute(const std::wstring& attr);
int MakeAttribute(const std::wstring& attr);
int MakeAttribute(const std::wstring& attr, const std::wstring& value);
void FreeAnalyseAttribute();
int AnalyseValue();
public:
ElementType type;
size_t level;
CElementObject* parent;
CElementObject* child;
CElementObject* sibling;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CAttributeObject* attrib;
public:
std::wstring tag;
std::wstring value;
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
class CParserData
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
enum DataType
{ DT_UNKNOWN = -1, DT_TAG = 0, DT_VALUE, DT_END, DT_DONE, DT_TAG_VALUE, DT_BROKEN };
typedef std::pair<size_t, size_t> TRange;//start + end;
typedef std::vector<TRange> TValueVector;
public:
CParserData()
: type(DT_UNKNOWN)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~CParserData()
{}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void Show(std::wostream& os) const;
void Show(std::wostream& os, const std::wstring& html) const;
public:
DataType type;
TRange tag;
TValueVector value;
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
class CDocumentObject
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
protected:
static const wchar_t TAG_LT = L'<';
static const wchar_t TAG_GT = L'>';
static const wchar_t TAG_SLASH = L'/';
static const wchar_t TAG_BSLASH = L'\\';
static const wchar_t TAG_AND = L'&';
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
typedef std::stack<CParserData> TDataStack;
typedef std::pair<size_t, CParserData> TNodeData;//level + tag;
typedef std::deque<TNodeData> TNodeQueue;
public:
typedef std::stack<const CElementObject*> TElementStack;
public:
CDocumentObject();
virtual ~CDocumentObject();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int Load(const std::wstring& str, bool strict = true);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
const CElementObject* Root() const
{ return _root; }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* FindFirstElement(const std::wstring& tag);
const CElementObject* FindNextElement();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* FindFirstElement(const CElementObject* element, const std::wstring& tag, TElementStack& tmpstack);
const CElementObject* FindNextElement(const CElementObject* element, const std::wstring& tag, TElementStack& tmpstack);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CAttributeObject* FindAttribute(const CElementObject* element, const std::wstring& attr);
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
bool IsMistake() const
{ return _bIsMistake; }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void Show(std::wostream& os) const;
protected:
int PreProcess(const std::wstring& str, std::wstring& html, bool strict);
int PreParser(const std::wstring& html, TNodeQueue& que, bool strict);
int Parser(const std::wstring& html, TNodeQueue& que, bool strict);
private:
int PreParserLT(const std::wstring& html, std::wstring::size_type& pos, CParserData& data);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int PushValueData(CParserData::DataType type, size_t start, size_t end, TDataStack& datastack) const;
int PushTagData(const std::wstring& html, CParserData& data, TDataStack& datastack, TNodeQueue& nodeque) const;
int PreParserBroken(const std::wstring& html, TDataStack& datastack, TNodeQueue& nodeque) const;
int CheckSpecialTag(const std::wstring& html, const CParserData& data) const;
int CheckTag(const std::wstring& html, const CParserData& tag, const CParserData& end) const;
CElementObject* MakeElement(const std::wstring& html, const TNodeData& node, CElementObject* parent, CElementObject* sibling) const;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CDocumentObject::ShowElement(std::wostream& os, const CElementObject* e) const;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void FreeElement(CElementObject* root);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* FindElement(const CElementObject* root, const CElementObject* pe, const std::wstring& tag, TElementStack& stack);
private:
CElementObject* _root;
private:
std::wstring _findtag;
TElementStack _findstack;
private:
bool _bIsMistake;
};
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#endif
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include <sstream>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
#include "TinyHtmlParser.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
namespace TinyHtmlParser
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CExceptionObject::CExceptionObject(TinyHtmlParser::ExceptionNumber type, const std::wstring &info)
: _type(type)
, _info(info)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CExceptionObject::CExceptionObject(const TinyHtmlParser::CExceptionObject &right)
: _type(right._type)
, _info(right._info)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
#define THROW_EXCEPTION(type, info) \
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{ \
std::wostringstream oss; \
oss << info; \
throw CExceptionObject(type, oss.str()); \
}
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////////////////
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CAttributeObject::Show(std::wostream& os) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
os << " attr : " << this->attr << " -- value = " << this->value << std::endl;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CElementObject::CElementObject()
: type(ET_UNKNOWN)
, level(0)
, parent(NULL)
, child(NULL)
, sibling(NULL)
, attrib(NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CElementObject::~CElementObject()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
FreeAnalyseAttribute();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CElementObject::Analyse()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring str = tag;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
std::wstring::size_type pos = str.find(L" ");
if(pos != std::wstring::npos)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tag = str.substr(0, pos);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
str = str.substr(pos + 1);
if(AnalyseAttribute(str) != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return -1;
}
}
if(type == ET_ELEMENT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(AnalyseValue() != 0)
return -1;
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CElementObject::AnalyseAttribute(const std::wstring& attr)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(attr.size() == 0)
return 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
std::wstring a, v;
std::wstring::size_type pos = attr.find(L"="), start = 0;
while(pos != std::wstring::npos)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(pos == attr.size() - 1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_ATTRIB_VALUEMISS, L"Attribue analyse failed - attribute string : " << attr);
return -1;
}
a = attr.substr(start, pos - start);
start = pos + 1;
if(attr[pos + 1] == L'\"')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pos = attr.find(L"\"", start + 1);
if(pos == std::wstring::npos)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_ATTRIB_VALUEBROKEN, L"Attribue analyse failed - attribute string : " << attr);
return -1;
}
v = attr.substr(start, pos - start + 1);
start = pos + 2;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pos = attr.find(L" ", start);
if(pos == std::wstring::npos)
pos = attr.size();
v = attr.substr(start, pos - start);
start = pos + 1;
}
if(MakeAttribute(a, v) != 0)
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(start >= attr.size())
break;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
pos = attr.find(L"=", start);
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CElementObject::MakeAttribute(const std::wstring &attr)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring::size_type pos = attr.find(L"=");
if(pos == std::wstring::npos)
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return MakeAttribute(attr.substr(0, pos), attr.substr(pos));
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CElementObject::MakeAttribute(const std::wstring &attr, const std::wstring& value)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::auto_ptr<CAttributeObject> obj(new CAttributeObject(attr, value));//attr.substr(0, pos), attr.substr(pos)));
if(attrib != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CAttributeObject* tmp = attrib;
while(tmp->next != NULL)
tmp = tmp->next;
tmp->next = obj.release();
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
attrib = obj.release();
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CElementObject::FreeAnalyseAttribute()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
CAttributeObject* tmp = attrib;
while(attrib != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
tmp = attrib->next;
delete attrib;
attrib = tmp;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CElementObject::AnalyseValue()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring::size_type pos = this->value.find(L" ");
while(pos != std::wstring::npos)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this->value.replace(pos, 6, L" ");
pos = this->value.find(L" ", pos + 1);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CAttributeObject* CElementObject::FindAttribute(const std::wstring& attr) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
const CAttributeObject* pa = this->attrib;
while(pa != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(pa->attr == attr)
return pa;
pa = pa->next;
}
return pa;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CElementObject::Show(std::wostream& os) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
os << "[" << this->level << "]" << "Tag : " << this->tag;
if(this->type == ET_ELEMENT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
os << " -- value = " << /**//*std::wstring*/(this->value);
os << std::endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CAttributeObject* attr = this->attrib;
while(attr != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
attr->Show(os);
attr = attr->next;
}
os << std::endl;
}
//
void CParserData::Show(std::wostream &os) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
os << "\nType = " << this->type;
os << "\nTag Start = " << this->tag.first << " - End = " << this->tag.second;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(TValueVector::const_iterator it = this->value.begin(); it != this->value.end(); ++ it)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
os << "\nValue Start = " << it->first << " - End = " << it->second;
}
os << std::endl;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CParserData::Show(std::wostream& os, const std::wstring& html) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
os << "\nType = " << this->type;
os << "\nTag = " << "[" << this->tag.first << "," << this->tag.second << "]" << html.substr(this->tag.first, this->tag.second - this->tag.first + 1);
for(TValueVector::const_iterator it = this->value.begin(); it != this->value.end(); ++ it)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
os << "\nValue = " << "[" << it->first << "," << it->second << "]" << html.substr(it->first, it->second - it->first + 1);
}
os << std::endl;
}
//
CDocumentObject::CDocumentObject()
: _root(NULL)
, _bIsMistake(false)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CDocumentObject::~CDocumentObject()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(_root != NULL)
FreeElement(_root);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::Load(const std::wstring &str, bool strict)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring html;
if(PreProcess(str, html, strict) != 0)
return -1;
TNodeQueue que;
if(PreParser(html, que, strict) != 0)
return -1;
if(Parser(html, que, strict) != 0)
return -1;
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PreProcess(const std::wstring& str, std::wstring& html, bool strict)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
//html = str;
bool tag = false;
for(std::wstring::const_iterator it = str.begin(); it != str.end(); ++ it)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(*it == TAG_LT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(tag == true)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Double '<'.");
return -1;
}
tag = true;
}
else if(*it == TAG_GT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(tag == false)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Miss '<' before '>'.");
return -1;
}
tag = false;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(tag == false)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//if(isspace((unsigned char)*it) != 0)
// continue;
if((unsigned char)(*it) == '\r' || (unsigned char)(*it) == '\n')
continue;
}
}
html += *it;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PreParser(const std::wstring& html, CDocumentObject::TNodeQueue& que, bool strict)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring::size_type pos = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(html.size() == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"HTML is empty.");
return -1;
}
if(html[pos] != TAG_LT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"First character of HTML is NOT '<' - pos : " << pos);
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
TDataStack datastack;
CParserData data;
size_t start = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(pos < html.size())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(html[pos] == TAG_LT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(pos > start)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(PushValueData(CParserData::DT_VALUE, start, pos -1, datastack) != 0)
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(PreParserLT(html, pos, data) != 0)
break;
if(PushTagData(html, data, datastack, que) != 0)
return -1;
++ pos;
start = pos;
}
//else if(html[pos] == TAG_GT || html[pos] == TAG_SLASH)
//{
// return -1;
//}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
++ pos;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(datastack.size() > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(strict)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Tags do NOT match each other.");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(pos > start)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(PushValueData(CParserData::DT_BROKEN, start, pos, datastack) != 0)
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(PreParserBroken(html, datastack, que) != 0)
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::Parser(const std::wstring& html, CDocumentObject::TNodeQueue& que, bool strict)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
CElementObject *pe = NULL, *pp = NULL, *ps = NULL;
size_t level = 0;
while(que.size()> 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
const TNodeData &node = que.front();
if(level < node.first)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pp = pe;
ps = NULL;
}
else if(level == node.first)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ps = pe;
}
else//>
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ps = pe;
pp = pe->parent;
int t = level - node.first;
while(t > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ps = ps->parent;
pp = pp->parent;
-- t;
}
}
level = node.first;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
pe = MakeElement(html, node, pp, ps);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(pe == NULL)
return -1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
que.pop_front();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(pp != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
while(pp->parent != NULL)
pp = pp->parent;
_root = pp;
}
else
_root = pe;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PreParserLT(const std::wstring& html, std::wstring::size_type& pos, CParserData& data)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(pos == html.size() - 1)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"'<' is the last character.");
return -1;
}
data.tag.first = pos;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
++ pos;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(html[pos] != TAG_SLASH)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data.type = CParserData::DT_TAG;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data.type = CParserData::DT_END;
++ pos;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(pos < html.size())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(html[pos] == TAG_GT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(html[pos - 1] == TAG_SLASH)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data.type = CParserData::DT_DONE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
data.tag.second = pos;
return 0;
}
else if(html[pos] == TAG_LT)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"'<' follows '<'.");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
++ pos;
}
//THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Miss '>' after '<'");
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PushValueData(CParserData::DataType type, size_t start, size_t end, TDataStack& datastack) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(datastack.size() == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Miss '<' before got value - pos : " << start);
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CParserData& prev = datastack.top();
if(prev.type == CParserData::DT_TAG)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
prev.value.push_back(std::make_pair(start, end));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
prev.type = CParserData::DT_TAG_VALUE;
}
else if(prev.type == CParserData::DT_TAG_VALUE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
prev.value.push_back(std::make_pair(start, end));
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Type does match : " << prev.type << " - pos : " << start);
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PushTagData(const std::wstring& html, CParserData& data, CDocumentObject::TDataStack& datastack, CDocumentObject::TNodeQueue& nodeque) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(CheckSpecialTag(html, data) == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
data.type = CParserData::DT_DONE;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(data.type == CParserData::DT_TAG)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
datastack.push(data);
}
else if(data.type == CParserData::DT_END)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(datastack.size() == 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Miss start-tag before end-tag - pos : " << data.tag.first);
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(CheckTag(html, datastack.top(), data) != 0)
return -1;
nodeque.push_front(std::make_pair(datastack.size() - 1, datastack.top()));
datastack.pop();
}
else if(data.type == CParserData::DT_DONE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nodeque.push_front(std::make_pair(datastack.size(), data));
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Wrong tag type : " << data.type << L" - pos : " << data.tag.first);
return -1;
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::PreParserBroken(const std::wstring& html, TDataStack& datastack, TNodeQueue& nodeque) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
while(datastack.size() > 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
CParserData& data = datastack.top();
if(data.type == CParserData::DT_TAG || data.type == CParserData::DT_TAG_VALUE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nodeque.push_front(std::make_pair(datastack.size() - 1, data));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
datastack.pop();
}
else if(data.type == CParserData::DT_BROKEN)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
nodeque.push_front(std::make_pair(datastack.size() - 1, data));
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
datastack.pop();
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return -1;
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::CheckSpecialTag(const std::wstring& html, const CParserData& data) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring tag = html.substr(data.tag.first + 1, data.tag.second - data.tag.first - 1);
std::wstring::size_type pos = tag.find(L" ");
if(pos != std::wstring::npos)
tag = tag.substr(0, pos);
if(tag == L"IMG")
return 0;
if(tag == L"PARAM")
return 0;
if(tag == L"BR")
return 0;
if(tag == L"HR")
return 0;
if(tag == L"P")
return 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return -1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int CDocumentObject::CheckTag(const std::wstring& html, const CParserData& tag, const CParserData& end) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::wstring str = html.substr(tag.tag.first + 1, tag.tag.second - tag.tag.first - 1);
std::wstring::size_type pos = str.find(L" ");
if(pos != std::wstring::npos)
str = str.substr(0, pos);
if(str != html.substr(end.tag.first + 2, end.tag.second - end.tag.first - 2))
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"do NOT match tag : " << str << L" and " << html.substr(end.tag.first + 2, end.tag.second - end.tag.first - 2));
return -1;
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
CElementObject* CDocumentObject::MakeElement(const std::wstring& html, const CDocumentObject::TNodeData &node, CElementObject *parent, CElementObject *sibling) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
std::auto_ptr<CElementObject> ele(new CElementObject);
ele->level = node.first;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(node.second.type == CParserData::DT_TAG)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ele->type = ET_NODE;
ele->tag = html.substr(node.second.tag.first + 1, node.second.tag.second - node.second.tag.first - 1);
}
else if(node.second.type == CParserData::DT_DONE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ele->type = ET_TAG;
ele->tag = html.substr(node.second.tag.first + 1, node.second.tag.second - node.second.tag.first - 1);
}
else if(node.second.type == CParserData::DT_TAG_VALUE)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ele->type = ET_ELEMENT;
ele->tag = html.substr(node.second.tag.first + 1, node.second.tag.second - node.second.tag.first - 1);
ele->value = L"";
for(CParserData::TValueVector::const_iterator it = node.second.value.begin(); it != node.second.value.end(); ++ it)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ele->value += html.substr(it->first, it->second - it->first + 1);
}
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
THROW_EXCEPTION(EN_DOCUMENT_FORMATERROR, L"Wrong Tag Type : " << node.second.type);
return NULL;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(ele->Analyse() != 0)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return NULL;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(parent != NULL)
parent->child = ele.get();
ele->parent = parent;
ele->sibling = sibling;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return ele.release();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CDocumentObject::Show(std::wostream &os) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(_root != NULL)
ShowElement(os, _root);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CDocumentObject::ShowElement(std::wostream& os, const CElementObject* e) const
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
const CElementObject* pe = e, *ps = e->sibling;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
pe->Show(os);
pe = pe->child;
if(pe != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ShowElement(os, pe);
}
if(ps != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ShowElement(os, ps);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void CDocumentObject::FreeElement(CElementObject* root)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
CElementObject* pe = root->child, *ps = root->sibling;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
// std::cout << "free:" << root->tag << std::endl;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(root != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
delete root;
root = NULL;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(pe != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FreeElement(pe);
}
if(ps != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FreeElement(ps);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* CDocumentObject::FindFirstElement(const std::wstring &tag)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(_root == NULL)
return NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
_findtag = tag;
while(!_findstack.empty())
_findstack.pop();
return FindElement(NULL, _root, _findtag, _findstack);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* CDocumentObject::FindNextElement()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(_findstack.empty())
return NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return FindElement(NULL, _findstack.top()->child, _findtag, _findstack);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* CDocumentObject::FindFirstElement(const CElementObject* element, const std::wstring& tag, TElementStack& tmpstack)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(element == NULL)
return NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(!tmpstack.empty())
tmpstack.pop();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return FindElement(element, element, tag, tmpstack);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* CDocumentObject::FindNextElement(const CElementObject* element, const std::wstring& tag, TElementStack& tmpstack)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(tmpstack.empty())
return NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return FindElement(element, tmpstack.top()->child, tag, tmpstack);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CElementObject* CDocumentObject::FindElement(const CElementObject* root, const CElementObject* pe, const std::wstring& tag, TElementStack& stack)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
while(pe != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
stack.push(pe);
if(pe->tag == tag)
return pe;
pe = pe->child;
}
while(!stack.empty() && stack.top() != root && pe == NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pe = stack.top()->sibling;
stack.pop();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(pe == NULL)
return NULL;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return FindElement(root, pe, tag, stack);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
const CAttributeObject* CDocumentObject::FindAttribute(const TinyHtmlParser::CElementObject *element, const std::wstring &attr)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedSubBlock.gif)
{
if(element == NULL)
return NULL;
const CAttributeObject* pa = element->attrib;
while(pa != NULL)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(pa->attr == attr)
return pa;
pa = pa->next;
}
return pa;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
明天合成到LingosHook中试试,今天就到这里了,累了,而且还要留点时间去看--《The Pacific》。