Functionality for generating website completed
[biaweb2.git] / biawebdoclist.hpp
1 #ifndef __BIAWEBDOCITEMLIST__
2 #define __BIAWEBDOCITEMLIST__
3 #include <ctime>
4 #include <string>
5 #include <fstream>
6 #include <iostream>
7 #include <list>
8 #include "biawebstrings.hpp"
9 #include "biawebdocument.hpp"
10
11 // to implement a list of items (documents) with creation/modified date/time display
12 namespace biaweb {
13 // to implement a single document item
14 class DocListItem {
15 protected:
16 // Just the required fields to build the item
17 std::string title;
18 std::string url;
19 std::time_t ctime;
20 std::time_t mtime;
21 public:
22 DocListItem (std::string title, std::string url,
23 std::time_t ctime, std::time_t mtime ) {
24 this->title = title;
25 this->url = url;
26 this->ctime = ctime;
27 this->mtime = mtime;
28 }
29
30 std::time_t get_mtime() {
31 return this->mtime;
32 }
33 void set_mtime(std::time_t mtime) {
34 this->mtime = mtime;
35 }
36 std::time_t get_ctime() {
37 return this->ctime;
38 }
39 void set_ctime(std::time_t ctime) {
40 this->ctime = ctime;
41 }
42
43 std::string get_url() {
44 return this->url;
45 }
46
47 void set_url(std::string url) {
48 this->url = url;
49 }
50
51 std::string get_title() {
52 return this->title;
53 }
54 void set_title(std::string title) {
55 this->title = title;
56 }
57
58 // output to HTML
59 std::string to_html (std::string templatedir);
60 };
61
62 // output to HTML vide the template
63 std::string DocListItem::to_html (std::string templatedir) {
64 std::ifstream templ (templatedir + "/doclistitem.tpl.html");
65 std::string templstr ((std::istreambuf_iterator<char> (templ)),
66 (std::istreambuf_iterator<char> ()) );
67 templ.close ();
68
69 // Allocate enough size for the output buffer
70 std::unique_ptr<char[]> outputstr (new char [templstr.size() +
71 this->title.size() +
72 this->url.size () +
73 200]);
74
75 char ctm_str[100];
76 char mtm_str[100];
77 strftime (ctm_str, 100, DATE_FORMAT, std::localtime (&this->ctime));
78 strftime (mtm_str, 100, DATE_FORMAT, std::localtime (&this->mtime));
79
80 sprintf (outputstr.get(), templstr.c_str(),
81 this->url.c_str(),
82 this->title.c_str(),
83 ctm_str, mtm_str);
84
85 std::string outputhtml;
86 outputhtml.append (outputstr.get());
87 return outputhtml;
88 }
89
90 // to implement a document list (or table)
91 class DocList {
92 protected:
93 std::string title;
94 std::list<DocListItem> items;
95 public:
96 void set_title (std::string title) {
97 this->title = title;
98 }
99 // add a document item
100 void add_document_item (DocListItem docitem) {
101 this->items.insert (this->items.cend(), docitem);
102 }
103 // render to HTML from a template
104 std::string to_html (std::string templatedir);
105 };
106
107 std::string DocList::to_html (std::string templatedir) {
108 std::ifstream templ (templatedir + "/doclist.tpl.html");
109 std::string templstr ( (std::istreambuf_iterator<char> (templ) ),
110 (std::istreambuf_iterator<char> ()));
111
112 std::string docitems = "";
113 std::string outputhtml = "";
114 // if the number of elements is non zero
115 if (this->items.size () != 0) {
116 for (DocListItem item : this->items)
117 docitems += item.to_html (templatedir);
118
119 // Allocate space for output buffer
120 std::unique_ptr<char[]> outputstr (new char [
121 templstr.size () +
122 docitems.size () +
123 this->title.size ()
124 ]);
125 sprintf (outputstr.get(), templstr.c_str(),
126 this->title.c_str(),
127 docitems.c_str() );
128
129 outputhtml.append (outputstr.get());
130 }
131 return outputhtml;
132 }
133 }
134
135 #endif