Commit b5de5e4a authored by jan.koester's avatar jan.koester
Browse files

some newapi

parent 6ed31477
Loading
Loading
Loading
Loading
+66 −48
Changes for src/html.cpp: 66 added lines, 48 removed lines.
Original line number Diff line number Diff line
@@ -78,7 +78,6 @@ void libhtmlpp::HtmlString::clear(){
        delete[] _HTable[i];
    }
    delete[]   _HTable;
    delete     _HtmlRootNode;
    _InitString();
}

@@ -134,44 +133,10 @@ libhtmlpp::HtmlString &libhtmlpp::HtmlString::operator<<(char src){
    return *this;
}

void libhtmlpp::HtmlString::_printHtml(HtmlElement* child) {
    for (HtmlElement* cur = child; cur; cur = cur->_nextElement) {

        if (cur->_Child) {
            _Cstr.append("<");
            _Cstr.append(cur->_TagName.c_str());
            _Cstr.append(" ");
            for (HtmlElement::HtmlAttributes* curattr = cur->_firstAttr; curattr; curattr = curattr->_nextAttr) {
                _Cstr.append(curattr->_Key.c_str());
                _Cstr.append("\"=");
                _Cstr.append(curattr->_Value.c_str());
                _Cstr.append("\" ");
            }
            _Cstr.append(">");
            _printHtml(cur->_Child);
            _Cstr.append("</");
            _Cstr.append(cur->_TagName.c_str());
            _Cstr.append(">");
            return;
        }
        _Cstr.append("<");
        _Cstr.append(cur->_TagName.c_str());
        _Cstr.append(" ");
        for (HtmlElement::HtmlAttributes* curattr = cur->_firstAttr; curattr; curattr=curattr->_nextAttr) {
            _Cstr.append(curattr->_Key.c_str());
            _Cstr.append("\"=");
            _Cstr.append(curattr->_Value.c_str());
            _Cstr.append("\" ");
        }        
        _Cstr.append(">");
    }
}

const char *libhtmlpp::HtmlString::c_str() {
    _Cstr.clear();
    _printHtml(_HtmlRootNode);
    sys::cout << _Cstr << sys::endl;
    return _Data.c_str();
    _Cstr = _RootNode->printHtmlElement();
    return _Cstr.c_str();
}


@@ -183,12 +148,13 @@ size_t libhtmlpp::HtmlString::length() {
    return _Data.length();
}

void libhtmlpp::HtmlString::parse(){
libhtmlpp::HtmlElement *libhtmlpp::HtmlString::parse(){
    HTMLException excp;
    _parseTree();
    delete _HtmlRootNode;
    ssize_t pos = 0;
    _HtmlRootNode = _buildTree(pos);
    delete[] _RootNode;
    _RootNode = _buildTree(pos);
    return _RootNode;
}

libhtmlpp::HtmlElement *libhtmlpp::HtmlString::_buildTree(ssize_t &pos){
@@ -275,6 +241,9 @@ void libhtmlpp::HtmlString::_serialelize(sys::array<char> in, HtmlElement **out)
        }else if (i == (in.length()-1)){
            *out = new HtmlElement(in.substr(s, i-s).c_str());
            break;
        }else if (in.substr(i,(i+3)-i) == "!--") {
            (*out) = new HtmlComment(in.substr(in.length() - 1).c_str());
            return;
        }
    }

@@ -286,6 +255,7 @@ void libhtmlpp::HtmlString::_serialelize(sys::array<char> in, HtmlElement **out)
    int startpos = -1;
    sys::array<char> key;
    int vst=-1;
    bool comment = false;
    while (i <= in.length()) {
        switch (in[i]) {
            case ' ': {
@@ -323,7 +293,7 @@ void libhtmlpp::HtmlString::_serialelize(sys::array<char> in, HtmlElement **out)
void libhtmlpp::HtmlString::_InitString(){
    _HTable=nullptr;
    _HTableSize=0;
    _HtmlRootNode=nullptr;
    _RootNode=nullptr;
}

void libhtmlpp::HtmlString::_parseTree(){
@@ -407,11 +377,11 @@ libhtmlpp::HtmlElement::~HtmlElement(){
}

libhtmlpp::HtmlPage::HtmlPage(){
    _HtmlDocument=nullptr;
    _RootNode=nullptr;
}

libhtmlpp::HtmlPage::~HtmlPage(){
    delete _HtmlDocument;
    delete _RootNode;
}

void libhtmlpp::HtmlPage::addElement(HtmlElement *element){
@@ -419,12 +389,12 @@ void libhtmlpp::HtmlPage::addElement(HtmlElement *element){
}

const char *libhtmlpp::HtmlPage::printHtml(){
    return _HtmlDocument->c_str();
    return _RootNode->printHtmlElement();
}

void libhtmlpp::HtmlPage::loadFile(const char* path){
    delete _HtmlDocument;
    _HtmlDocument= new HtmlString();
    delete _RootNode;
    HtmlString tmp;
    sys::file fs;
    try{
        fs.open(path,0);
@@ -440,13 +410,49 @@ void libhtmlpp::HtmlPage::loadFile(const char* path){
        readed += rd;
        if (rd > 0) {
            buf[rd] = '\0';
            _HtmlDocument->assign(buf,rd);
            tmp.assign(buf,rd);
        } else {
            HTMLException excp;
            throw excp[HTMLException::Critical] << "Could not read file";
        }
    }
    _HtmlDocument->parse();
    _RootNode=tmp.parse();
}

void libhtmlpp::HtmlElement::_print(HtmlElement* child) {
    for (HtmlElement* cur = child; cur; cur = cur->_nextElement) {
        if (cur->_Child) {
            _Cstr.append("<");
            _Cstr.append(cur->_TagName.c_str());
            for (HtmlElement::HtmlAttributes* curattr = cur->_firstAttr; curattr; curattr = curattr->_nextAttr) {
                _Cstr.append(" ");
                _Cstr.append(curattr->_Key.c_str());
                _Cstr.append("\"=");
                _Cstr.append(curattr->_Value.c_str());
            }
            _Cstr.append(">");
            _print(cur->_Child);
            _Cstr.append("</");
            _Cstr.append(cur->_TagName.c_str());
            _Cstr.append(">");
            return;
        }
        _Cstr.append("<");
        _Cstr.append(cur->_TagName.c_str());
        for (HtmlElement::HtmlAttributes* curattr = cur->_firstAttr; curattr; curattr = curattr->_nextAttr) {
            _Cstr.append(" ");
            _Cstr.append(curattr->_Key.c_str());
            _Cstr.append("\"=");
            _Cstr.append(curattr->_Value.c_str());
        }
        _Cstr.append(">");
    }
}

const char* libhtmlpp::HtmlElement::printHtmlElement() {
    _Cstr.clear();
    _print(this);
    return _Cstr.c_str();
}

void libhtmlpp::HtmlElement::setAttribute(const char* name, const char* value) {
@@ -494,6 +500,18 @@ libhtmlpp::HtmlElement::HtmlAttributes::~HtmlAttributes() {
    delete _nextAttr;
}

libhtmlpp::HtmlComment::HtmlComment(const char* comment) : HtmlElement("!--") {

}

libhtmlpp::HtmlComment::~HtmlComment() {

}

const char* libhtmlpp::HtmlComment::printHtmlElement() {
    return _Cstr.c_str();
};

libhtmlpp::HtmlTable::HtmlTable() : HtmlElement("table") {
}

+20 −6
Changes for src/html.h: 20 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -34,6 +34,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace libhtmlpp {
    class HtmlElement {
    public:
        HtmlElement(const char* tag);
        ~HtmlElement();

        void        setAttribute(const char *name,const char *value);
        void        setIntAttribute(const char* name,int value);

@@ -42,9 +45,8 @@ namespace libhtmlpp {

        const char *printHtmlElement();
    protected:
        HtmlElement(const char *tag);
        ~HtmlElement();

        void              _print(HtmlElement* child);
        HtmlElement      *_Child;
        HtmlElement      *_prevElement;
        HtmlElement      *_nextElement;
@@ -60,6 +62,8 @@ namespace libhtmlpp {
    private:
        sys::array<char> _TagName;
        sys::array<char> _Text;
        sys::array<char> _Cstr;

        HtmlAttributes  *_firstAttr;
        HtmlAttributes  *_lastAttr;

@@ -93,18 +97,17 @@ namespace libhtmlpp {
        size_t             size();
        size_t             length();
        void               clear();
        void               parse();
        HtmlElement       *parse();
     private:
        void              _InitString();
        void              _parseTree();
        void              _printHtml(HtmlElement* child);
        void              _serialelize(sys::array<char> in,HtmlElement **out);
        HtmlElement      *_buildTree(ssize_t &pos);
        sys::array<char>        _Data;
        sys::array<char>        _Cstr;
        ssize_t               **_HTable;
        size_t                  _HTableSize;
        HtmlElement            *_HtmlRootNode;
        HtmlElement            *_RootNode;
    };

    class HtmlPage {
@@ -120,7 +123,18 @@ namespace libhtmlpp {
        void addElement(HtmlElement *element);
        const char *printHtml();
    private:
        HtmlString    *_HtmlDocument;
        sys::array<char>     _DocType;
        HtmlElement         *_RootNode;
    };

    class HtmlComment : public HtmlElement {
    public:
        HtmlComment(const char *comment);
        ~HtmlComment();
        const char* printHtmlElement();
    private:
        sys::array<char> _Cstr;
        sys::array<char> _Comment;
    };

    class HtmlDivLayer : public HtmlElement {