Functionality for generating website completed
[biaweb2.git] / biawebdoclist.hpp
diff --git a/biawebdoclist.hpp b/biawebdoclist.hpp
new file mode 100644 (file)
index 0000000..6082b39
--- /dev/null
@@ -0,0 +1,135 @@
+#ifndef __BIAWEBDOCITEMLIST__
+#define __BIAWEBDOCITEMLIST__
+#include <ctime>
+#include <string>
+#include <fstream>
+#include <iostream>
+#include <list>
+#include "biawebstrings.hpp"
+#include "biawebdocument.hpp"
+
+// to implement a list of items (documents) with creation/modified date/time display
+namespace biaweb {
+    // to implement a single document item
+    class DocListItem {
+      protected:
+      // Just the required fields to build the item
+        std::string title; 
+        std::string url;
+        std::time_t ctime;
+        std::time_t mtime;
+      public:
+        DocListItem (std::string title, std::string url, 
+                    std::time_t ctime, std::time_t mtime ) {
+            this->title = title;
+            this->url = url;
+            this->ctime = ctime; 
+            this->mtime = mtime;
+        }
+
+        std::time_t get_mtime() {
+            return this->mtime;
+        }
+        void set_mtime(std::time_t mtime) {
+            this->mtime = mtime;
+        }
+        std::time_t get_ctime() {
+            return this->ctime;
+        }
+        void set_ctime(std::time_t ctime) {
+            this->ctime = ctime;
+        }
+
+        std::string get_url() {
+                 return this->url;
+        }
+
+        void set_url(std::string url) {
+          this->url = url;
+        }
+
+        std::string get_title() {
+            return this->title;
+        }
+        void set_title(std::string title) {
+            this->title = title;
+        }
+        
+        // output to HTML
+        std::string to_html (std::string templatedir);
+    };
+    
+    // output to HTML vide the template 
+    std::string DocListItem::to_html (std::string templatedir) {
+        std::ifstream templ (templatedir + "/doclistitem.tpl.html");
+        std::string templstr ((std::istreambuf_iterator<char> (templ)),
+                            (std::istreambuf_iterator<char> ()) );
+        templ.close ();
+        
+        // Allocate enough size for the output buffer
+        std::unique_ptr<char[]> outputstr (new char [templstr.size() +
+                                                     this->title.size() +
+                                                     this->url.size () +
+                                                     200]);
+        
+        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;
+    }
+
+    // to implement a document list (or table)
+    class DocList {
+      protected:
+        std::string title;
+        std::list<DocListItem> items;
+      public:
+        void set_title (std::string title) {
+            this->title = title;
+        }
+        // add a document item
+        void add_document_item (DocListItem docitem) {
+            this->items.insert (this->items.cend(), docitem);
+        }
+        // render to HTML from a template
+        std::string to_html (std::string templatedir);
+    };
+
+    std::string DocList::to_html (std::string templatedir) {
+        std::ifstream templ (templatedir + "/doclist.tpl.html");
+        std::string templstr ( (std::istreambuf_iterator<char> (templ) ),
+                                (std::istreambuf_iterator<char> ()));
+
+        std::string docitems = "";
+        std::string outputhtml = "";
+        // if the number of elements is non zero
+        if (this->items.size () != 0) {
+            for (DocListItem item : this->items)
+                docitems += item.to_html (templatedir);
+            
+            // Allocate space for output buffer
+            std::unique_ptr<char[]> 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());
+        }
+        return outputhtml;
+    }
+}
+
+#endif
\ No newline at end of file