X-Git-Url: https://harishankar.org/repos/?p=biaweb2.git;a=blobdiff_plain;f=biawebdoclist.hpp;h=7c4efcdc01fed3cc48618d1dab7aa241bbae921b;hp=6082b398f1a6564fd82529ee93a8b9301d52fddf;hb=fe9a0fef0b31ee3b94b3e73b7e319087a49a3054;hpb=b6425f960a30045333fb2ef531832a0b5858200d diff --git a/biawebdoclist.hpp b/biawebdoclist.hpp index 6082b39..7c4efcd 100644 --- a/biawebdoclist.hpp +++ b/biawebdoclist.hpp @@ -4,7 +4,10 @@ #include #include #include +#include #include +#include +#include #include "biawebstrings.hpp" #include "biawebdocument.hpp" @@ -21,7 +24,7 @@ namespace biaweb { public: DocListItem (std::string title, std::string url, std::time_t ctime, std::time_t mtime ) { - this->title = title; + this->title = escape_html (title); this->url = url; this->ctime = ctime; this->mtime = mtime; @@ -52,10 +55,10 @@ namespace biaweb { return this->title; } void set_title(std::string title) { - this->title = title; + this->title = escape_html (title); } - // output to HTML + // output to HTML vide the template std::string to_html (std::string templatedir); }; @@ -66,24 +69,11 @@ namespace biaweb { (std::istreambuf_iterator ()) ); templ.close (); - // Allocate enough size for the output buffer - std::unique_ptr outputstr (new char [templstr.size() + - this->title.size() + - this->url.size () + - 200]); + std::string outputhtml = fmt::format (templstr, fmt::arg("url", this->url), + fmt::arg("doctitle", this->title), + fmt::arg("cdate", *std::localtime (&this->ctime)), + fmt::arg("mdate", *std::localtime (&this->mtime))); - char ctm_str[100]; - char mtm_str[100]; - strftime (ctm_str, 100, DATE_FORMAT, std::localtime (&this->ctime)); - strftime (mtm_str, 100, DATE_FORMAT, std::localtime (&this->mtime)); - - sprintf (outputstr.get(), templstr.c_str(), - this->url.c_str(), - this->title.c_str(), - ctm_str, mtm_str); - - std::string outputhtml; - outputhtml.append (outputstr.get()); return outputhtml; } @@ -94,7 +84,7 @@ namespace biaweb { std::list items; public: void set_title (std::string title) { - this->title = title; + this->title = escape_html (title); } // add a document item void add_document_item (DocListItem docitem) { @@ -109,24 +99,18 @@ namespace biaweb { std::string templstr ( (std::istreambuf_iterator (templ) ), (std::istreambuf_iterator ())); - std::string docitems = ""; + templ.close (); + std::string outputhtml = ""; // if the number of elements is non zero if (this->items.size () != 0) { + std::string docitems = ""; for (DocListItem item : this->items) docitems += item.to_html (templatedir); - - // Allocate space for output buffer - std::unique_ptr outputstr (new char [ - templstr.size () + - docitems.size () + - this->title.size () - ]); - sprintf (outputstr.get(), templstr.c_str(), - this->title.c_str(), - docitems.c_str() ); - - outputhtml.append (outputstr.get()); + + outputhtml = fmt::format (templstr, + fmt::arg ("title", this->title), + fmt::arg ("docitems", docitems)); } return outputhtml; }