Readme.md - added Section for customization and notes
[biaweb2.git] / biawebmain.hpp
1 #ifndef __BIAWEBMAIN__
2 #define __BIAWEBMAIN__
3 #include <unistd.h>
4 #include <chrono>
5 #include "biawebdocumenttree.hpp"
6 #include "biawebstrings.hpp"
7 namespace biaweb {
8 // Class to parse the command line options and run DocumentTree
9 class Main {
10 private:
11 bool quiet_mode;
12 bool display_usage;
13 std::string app_name;
14 std::string target_dir;
15 std::string template_dir;
16 std::string source_dir;
17
18 public:
19 // main constructor - build options from command line
20 Main (int argc, char **argv) ;
21
22 // run the program with the options built from constructor
23 // and return the result
24 int run ();
25 };
26
27 // Execute the program and return the result
28 int Main::run () {
29 // if usage of the program is to be displayed then display it and quit
30 if (this->display_usage == true ) {
31 std::cerr << PROGRAM_USAGE;
32 std::cerr << this->app_name << " " << PROGRAM_ARGS << std::endl;
33 std::cerr << PROGRAM_EXPLANATION << std::endl;
34 return 1;
35 }
36 // else run the program
37 else {
38 // timing for the program
39 std::chrono::time_point<std::chrono::steady_clock> t1
40 (std::chrono::steady_clock::now());
41
42 // create a blank document tree and build the tree from source dir
43 std::unique_ptr<DocumentTree> doctree (new DocumentTree(""));
44 doctree.get()->document_tree_builder (this->source_dir);
45 // if not quiet mode, display the tree visualization
46 if (! this->quiet_mode) {
47 std::cout << VISUALIZE_TREE << std::endl;
48 doctree.get()->visualize_tree ();
49 }
50 // generate the template
51 Template t (this->template_dir);
52 doctree.get()->create_tree_html (&t, this->target_dir);
53
54 if (! this->quiet_mode)
55 std::cout << OUTPUT_IN << " " << this->target_dir << std::endl;
56
57 // end time
58 std::chrono::time_point<std::chrono::steady_clock> t2
59 (std::chrono::steady_clock::now());
60 std::chrono::duration<double> dur (t2 - t1);
61 // display the execution time
62 if (! this->quiet_mode)
63 std::cout << GENERATED_IN << dur.count() << std::endl;
64 }
65 return 0;
66 }
67
68 // build the options from command line using getopt
69 Main::Main (int argc, char **argv) {
70 this->display_usage = true;
71 this->quiet_mode = false;
72 this->app_name = argv[0];
73 this->source_dir = "";
74 this->target_dir = "";
75 this->template_dir = "";
76 int c;
77
78 while ((c = getopt (argc, argv, "i:t:o:q")) != -1) {
79 switch (c) {
80 case 'i' : this->source_dir = optarg;
81 break;
82 case 't' : this->template_dir = optarg;
83 break;
84 case 'o' : this->target_dir = optarg;
85 break;
86 case 'q' : this->quiet_mode = true;
87 }
88 }
89 // if the user has not given the required parameters then set to display usage
90 // mode
91 if (this->source_dir.empty () || this->target_dir.empty ()
92 || this->template_dir.empty ())
93 this->display_usage = true;
94 else
95 this->display_usage = false;
96 }
97 }
98
99 #endif