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

use std::String instead const char*

parent a540312b
Loading
Loading
Loading
Loading
+32 −43
Original line number Diff line number Diff line
@@ -573,7 +573,7 @@ void libhtmlpp::HtmlString::_serialelize(std::vector<char> in, libhtmlpp::HtmlEl
                if(vend!=0){
                    std::copy(in.begin()+vstart,in.begin()+vend,std::back_inserter(val));
                }
                out->setAttribute(key.data(),key.size(),val.data(),val.size());
                out->setAttribute(key.data(),val.data());
            }
    }
}
@@ -602,8 +602,8 @@ void libhtmlpp::HtmlEncode(const std::string &input, HtmlString* output){
}


libhtmlpp::HtmlElement::HtmlElement(const char *tagname) : HtmlElement(){
    std::copy(tagname,tagname+strlen(tagname),std::insert_iterator<std::vector<char>>(_TagName,_TagName.begin()) );
libhtmlpp::HtmlElement::HtmlElement(const std::string &tagname) : HtmlElement(){
    std::copy(tagname.begin(),tagname.end(),std::insert_iterator<std::vector<char>>(_TagName,_TagName.begin()) );
}

libhtmlpp::HtmlElement::HtmlElement() : Element() {
@@ -870,9 +870,9 @@ NEWEL:
            ((libhtmlpp::HtmlElement*)dest)->_TagName=(((libhtmlpp::HtmlElement*)src)->_TagName);
            for(libhtmlpp::HtmlElement::Attributes *cattr=((libhtmlpp::HtmlElement*)src)->_firstAttr; cattr; cattr=cattr->_nextAttr){
                if(!cattr->_Value.empty())
                    ((libhtmlpp::HtmlElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Key.size(),cattr->_Value.data(),cattr->_Value.size());
                    ((libhtmlpp::HtmlElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Value.data());
                else
                    ((libhtmlpp::HtmlElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Key.size(),nullptr,0);
                    ((libhtmlpp::HtmlElement*)dest)->setAttribute(cattr->_Key.data(),"");
            }

            if(((libhtmlpp::HtmlElement*)src)->_childElement){
@@ -903,9 +903,9 @@ NEWEL:
            ((libhtmlpp::ScriptElement*)dest)->_TagName=(((libhtmlpp::ScriptElement*)src)->_TagName);
            for(libhtmlpp::ScriptElement::Attributes *cattr=((libhtmlpp::ScriptElement*)src)->_firstAttr; cattr; cattr=cattr->_nextAttr){
                if(!cattr->_Value.empty())
                    ((libhtmlpp::ScriptElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Key.size(),cattr->_Value.data(),cattr->_Value.size());
                    ((libhtmlpp::ScriptElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Value.data());
                else
                    ((libhtmlpp::ScriptElement*)dest)->setAttribute(cattr->_Key.data(),cattr->_Key.size(),nullptr,0);
                    ((libhtmlpp::ScriptElement*)dest)->setAttribute(cattr->_Key.data(),"");
            }
            ((ScriptElement*)dest)->_Script=(((ScriptElement*)src)->_Script);
        }else if(src->getType()==libhtmlpp::TextEl && dest->getType()== libhtmlpp::TextEl){
@@ -1104,7 +1104,7 @@ void libhtmlpp::Element::remove(libhtmlpp::Element* el){
libhtmlpp::TextElement::TextElement() : Element() {
}

libhtmlpp::TextElement::TextElement(const char* txt) : TextElement(){
libhtmlpp::TextElement::TextElement(const std::string& txt) : TextElement(){
    setText(txt);
}

@@ -1126,15 +1126,13 @@ libhtmlpp::TextElement & libhtmlpp::TextElement::operator=(const libhtmlpp::Elem
    return *this;
}

void libhtmlpp::TextElement::setText(const char* txt){
    std::copy(txt,txt+strlen(txt),
              std::insert_iterator<std::vector<char>>(_Text,_Text.begin()));
void libhtmlpp::TextElement::setText(const std::string& txt){
    std::copy(txt.begin(),txt.end(),std::back_inserter(_Text));
}

const char * libhtmlpp::TextElement::getText(){
    _CStr=_Text;
    _CStr.push_back('\0');
    return _CStr.data();
const std::string libhtmlpp::TextElement::getText(){
    _CStr=_Text.data();
    return _CStr;
}

int libhtmlpp::TextElement::getType() const{
@@ -1509,7 +1507,7 @@ void libhtmlpp::print(Element* el, HtmlString &output,bool formated) {

}

libhtmlpp::HtmlElement *libhtmlpp::HtmlElement::getElementbyID(const char *id) const{
libhtmlpp::HtmlElement *libhtmlpp::HtmlElement::getElementbyID(const std::string &id) const{
    std::stack <Element*> childs;
    const Element *curel=this;
SEARCHBYID:
@@ -1517,8 +1515,8 @@ SEARCHBYID:
        if(((HtmlElement*)curel)->_childElement){
            childs.push(((HtmlElement*)curel)->_childElement);
        }
        const char *key=((HtmlElement*)curel)->getAtributte("id");
        if(key && strcmp(key,id)==0){
        const std::string key=((HtmlElement*)curel)->getAtributte("id");
        if(!key.empty() && key==id){
            return (HtmlElement*)curel;
        }
    }
@@ -1537,7 +1535,7 @@ SEARCHBYID:
    return nullptr;
}

libhtmlpp::HtmlElement *libhtmlpp::HtmlElement::getElementbyTag(const char *tag) const{
libhtmlpp::HtmlElement *libhtmlpp::HtmlElement::getElementbyTag(const std::string &tag) const{
    std::stack <Element*> childs;
    const Element *curel=this;
SEARCHBYTAG:
@@ -1546,7 +1544,7 @@ SEARCHBYTAG:
            childs.push(((HtmlElement*)curel)->_childElement);
        }
        const char *tname=((HtmlElement*)curel)->getTagname();
        if(tname && strcmp(tname,tag)==0){
        if(tname && tag==tname){
            return (HtmlElement*)curel;
        }
    }
@@ -1564,19 +1562,12 @@ SEARCHBYTAG:
    return nullptr;
}

void libhtmlpp::HtmlElement::setAttribute(const char* name, const char* value) {
    if(value)
        setAttribute(name,strlen(name),value,strlen(value));
    else
        setAttribute(name,strlen(name),nullptr,0);
}

void libhtmlpp::HtmlElement::setAttribute(const char* name,size_t nlen, const char* value,size_t vlen) {
void libhtmlpp::HtmlElement::setAttribute(const std::string &name, const std::string &value) {
    Attributes* cattr = nullptr;

    for (Attributes* curattr = _firstAttr; curattr; curattr=curattr->_nextAttr) {
        if(curattr->_Key.size() == nlen){
            if ( strncmp(curattr->_Key.data(),name,curattr->_Key.size()) ==0 ) {
        if(curattr->_Key.size() == name.length()){
            if ( name == curattr->_Key.data() ) {
                cattr = curattr;
            }
        }
@@ -1590,33 +1581,31 @@ void libhtmlpp::HtmlElement::setAttribute(const char* name,size_t nlen, const ch
            _lastAttr = _firstAttr;
        }
        cattr = _lastAttr;
        std::copy(name,name+nlen,std::insert_iterator<std::vector<char>>(cattr->_Key,cattr->_Key.begin()) );
        std::copy(name.begin(),name.end(),std::insert_iterator<std::vector<char>>(cattr->_Key,cattr->_Key.begin()) );
    }
    if(vlen>0)
        std::copy(value,value+vlen,std::insert_iterator<std::vector<char>>(cattr->_Value ,cattr->_Value .begin()) );
    if(value.length()>0)
        std::copy(value.begin(),value.end(),std::insert_iterator<std::vector<char>>(cattr->_Value ,cattr->_Value .begin()) );
    else
        cattr->_Value.clear();
}

void libhtmlpp::HtmlElement::setIntAttribute(const char* name, int value) {
void libhtmlpp::HtmlElement::setIntAttribute(const std::string& name, int value) {
    char buf[255];
    snprintf(buf,255,"%d",value);
    setAttribute(name,buf);
}

const char* libhtmlpp::HtmlElement::getAtributte(const char* name) const{
const std::string libhtmlpp::HtmlElement::getAtributte(const std::string& name) const{
    for (Attributes* curattr = _firstAttr; curattr; curattr = curattr->_nextAttr) {
        if ( strncmp(name,curattr->_Key.data(),curattr->_Key.size()) == 0 ) {
            curattr->_CStr=curattr->_Value;
            curattr->_CStr.push_back('\0');
            return curattr->_CStr.data();
        if ( name==curattr->_Key.data() ) {
            return curattr->_Value.data();
        }
    }
    return nullptr;
    return "";
}

int libhtmlpp::HtmlElement::getIntAtributte(const char* name) {
    return atoi(getAtributte(name));
int libhtmlpp::HtmlElement::getIntAtributte(const std::string& name) {
    return atoi(getAtributte(name).c_str());
}

libhtmlpp::HtmlElement::Attributes::Attributes() {
+12 −14
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ namespace libhtmlpp {
    class HtmlElement : public Element {
    public:
        HtmlElement();
        HtmlElement(const char* tag);
        HtmlElement(const std::string &tag);
        HtmlElement(const HtmlElement &hel);
        HtmlElement(const HtmlElement *hel);
        ~HtmlElement();
@@ -94,14 +94,13 @@ namespace libhtmlpp {
        bool operator==(const HtmlElement *hel);
        bool operator==(const HtmlElement &hel);

        void         setAttribute(const char* name, const char* value);
        void         setAttribute(const char* name,size_t nlen, const char* value,size_t vlen);
        void         setAttribute(const std::string &name, const std::string &value);

        void         setIntAttribute(const char* name, int value);
        void         setIntAttribute(const std::string &name, int value);

        const char*  getAtributte(const char* name) const;
        const std::string getAtributte(const std::string &name) const;

        int          getIntAtributte(const char* name);
        int          getIntAtributte(const std::string &name);

        void         insertChild(const Element* el);
        void         insertChild(const Element& el);
@@ -111,8 +110,8 @@ namespace libhtmlpp {
        void         setTagname(const char *name);
        const char  *getTagname();

        HtmlElement *getElementbyID(const char *id) const;
        HtmlElement *getElementbyTag(const char *tag) const;
        HtmlElement *getElementbyID(const std::string &id) const;
        HtmlElement *getElementbyTag(const std::string &tag) const;

        int    getType() const;
        void   remove(Element* el);
@@ -125,7 +124,6 @@ namespace libhtmlpp {
            ~Attributes();
            std::vector<char> _Key;
            std::vector<char> _Value;
            std::vector<char> _CStr;
            Attributes*       _nextAttr;
        };

@@ -147,20 +145,20 @@ namespace libhtmlpp {
    class TextElement : public Element {
    public:
        TextElement();
        TextElement(const char *txt);
        TextElement(const std::string &txt);
        TextElement(const TextElement &texel);
        ~TextElement();

        TextElement& operator=(const Element &hel);
        TextElement& operator=(const Element *hel);

        const char *getText();
        void        setText(const char *txt);
        const std::string    getText();
        void                       setText(const std::string &txt);

        int         getType() const;
    protected:
        std::vector<char> _Text;
        std::vector<char> _CStr;
        const char            * _CStr;
        friend class HtmlString;
        friend void  print(Element* el, HtmlString &output,bool formated);
        friend void _copy(libhtmlpp::Element *dest,const libhtmlpp::Element *src);