From c3f6c1be3a72da00b32eef61b9a4a381a76880eb Mon Sep 17 00:00:00 2001 From: Harishankar Date: Fri, 22 May 2020 15:02:09 +0530 Subject: [PATCH] Program now takes options instead of hardcoded inputs Used getopt() to take command line option for the source, destination path and also template directory from the end user --- biaweb.cpp | 30 ++++---------- biawebmain.hpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ biawebstrings.hpp | 10 ++++- 3 files changed, 115 insertions(+), 24 deletions(-) create mode 100644 biawebmain.hpp diff --git a/biaweb.cpp b/biaweb.cpp index d0c284e..8e87ab8 100644 --- a/biaweb.cpp +++ b/biaweb.cpp @@ -1,23 +1,7 @@ -#include -#include -#include -#include "biawebdocumenttree.hpp" -using namespace biaweb; - -int main (int argc, char *argv[]) { - // Compute the speed of generating the document tree and output HTML from sources - std::chrono::steady_clock::time_point t1 (std::chrono::steady_clock::now()); - - // An empty document tree - std::unique_ptr tree (new DocumentTree ("")); - tree.get()->document_tree_builder ("Test/Reviews"); - std::cout << VISUALIZE_TREE << std::endl; - tree.get()->visualize_tree (); - Template tpl ("templates"); - tree.get()->create_tree_html (&tpl, "Test/Out"); - std::chrono::steady_clock::time_point t2 (std::chrono::steady_clock::now()); - std::chrono::duration dur (t2 - t1); - std::cout << GENERATED_IN << dur.count () << std::endl; - - return 0; -} +#include "biawebmain.hpp" +#include +using namespace biaweb; +int main (int argc, char **argv) { + Main prog (argc, argv); + return prog.run (); +} \ No newline at end of file diff --git a/biawebmain.hpp b/biawebmain.hpp new file mode 100644 index 0000000..d96dbb0 --- /dev/null +++ b/biawebmain.hpp @@ -0,0 +1,99 @@ +#ifndef __BIAWEBMAIN__ +#define __BIAWEBMAIN__ +#include +#include +#include "biawebdocumenttree.hpp" +#include "biawebstrings.hpp" +namespace biaweb { + // Class to parse the command line options and run DocumentTree + class Main { + private: + bool quiet_mode; + bool display_usage; + std::string app_name; + std::string target_dir; + std::string template_dir; + std::string source_dir; + + public: + // main constructor - build options from command line + Main (int argc, char **argv) ; + + // run the program with the options built from constructor + // and return the result + int run (); + }; + + // Execute the program and return the result + int Main::run () { + // if usage of the program is to be displayed then display it and quit + if (this->display_usage == true ) { + std::cerr << PROGRAM_USAGE; + std::cerr << this->app_name << " " << PROGRAM_ARGS << std::endl; + std::cerr << PROGRAM_EXPLANATION << std::endl; + return 1; + } + // else run the program + else { + // timing for the program + std::chrono::time_point t1 + (std::chrono::steady_clock::now()); + + // create a blank document tree and build the tree from source dir + std::unique_ptr doctree (new DocumentTree("")); + doctree.get()->document_tree_builder (this->source_dir); + // if not quiet mode, display the tree visualization + if (! this->quiet_mode) { + std::cout << VISUALIZE_TREE << std::endl; + doctree.get()->visualize_tree (); + } + // generate the template + Template t (this->template_dir); + doctree.get()->create_tree_html (&t, this->target_dir); + + if (! this->quiet_mode) + std::cout << OUTPUT_IN << " " << this->target_dir << std::endl; + + // end time + std::chrono::time_point t2 + (std::chrono::steady_clock::now()); + std::chrono::duration dur (t2 - t1); + // display the execution time + if (! this->quiet_mode) + std::cout << GENERATED_IN << dur.count() << std::endl; + } + return 0; + } + + // build the options from command line using getopt + Main::Main (int argc, char **argv) { + this->display_usage = true; + this->quiet_mode = false; + this->app_name = argv[0]; + this->source_dir = ""; + this->target_dir = ""; + this->template_dir = ""; + int c; + + while ((c = getopt (argc, argv, "i:t:o:q")) != -1) { + switch (c) { + case 'i' : this->source_dir = optarg; + break; + case 't' : this->template_dir = optarg; + break; + case 'o' : this->target_dir = optarg; + break; + case 'q' : this->quiet_mode = true; + } + } + // if the user has not given the required parameters then set to display usage + // mode + if (this->source_dir.empty () || this->target_dir.empty () + || this->template_dir.empty ()) + this->display_usage = true; + else + this->display_usage = false; + } +} + +#endif diff --git a/biawebstrings.hpp b/biawebstrings.hpp index 55f4177..a421ee2 100644 --- a/biawebstrings.hpp +++ b/biawebstrings.hpp @@ -7,7 +7,15 @@ namespace biaweb { const char* NO_SUCH_PATH_ERROR = "No such path! Specify an existing file path" ; const char* DATE_IN_FORMAT = "%Y-%m-%d %H:%M %Z"; const char* GENERATED_IN = "Generated in (s): "; - const char* VISUALIZE_TREE = "Generated document tree: "; + const char* VISUALIZE_TREE = "Document tree: "; + const char* PROGRAM_ARGS = " -i -o -t "; + const char* OUTPUT_IN = "Output in: "; + const char* PROGRAM_USAGE = "Usage: "; + const char* PROGRAM_EXPLANATION = "Where \n \ +\t - source directory tree with markdown sources\n \ +\t - destination in which to create the HTML output\n \ +\t - directory containing the templates\n \ +NOTE: directory argument need not terminate with the separator (/)"; // NAMED INDEX FOR DOCUMENT STRING BITS (LOADED FROM TEMPLATE FILE stringbits.txt) const unsigned int HOME = 0; -- 2.20.1