22#include < sstream>
33#include < vector>
44#include < thread>
5- #include " modules/summarizer.h"
6- #include " utils/utils.h"
5+ #include " modules/summarizer.hpp"
6+ #include " utils/utils.hpp"
7+ #include < fcntl.h>
8+ #include < boost/program_options.hpp>
9+ namespace po = boost::program_options;
710
811using namespace std ;
912bool continue_reading = true ;
1013
11- void handle_printing (Summarizer *summarizer)
14+ void handle_printing (Summarizer *summarizer, int delay )
1215{
1316 printElements (vector<string>{" Average" , " Min" , " Median" , " p95" , " p99" , " Max" , " \n " });
1417 while (true )
1518 {
1619 cout << " \r " ;
1720 summarizer->print_summery ();
18- this_thread::sleep_for (chrono::seconds (1 ));
19- if (continue_reading == false )
21+ this_thread::sleep_for (chrono::seconds (delay));
22+ if (continue_reading == false ){
23+ cout<<endl;
2024 break ;
25+ }
2126 }
2227}
2328
24- int main ()
25- {
29+ void start (int delay) {
2630 int number;
2731 string line;
2832 Summarizer summarizer;
2933
30- thread timer (handle_printing, &summarizer);
34+ thread timer (handle_printing, &summarizer, delay );
3135
3236 while (continue_reading)
3337 {
@@ -46,5 +50,51 @@ int main()
4650 }
4751
4852 timer.join ();
53+ }
54+
55+ int main (int ac, char * av[])
56+ {
57+ try {
58+ po::options_description desc (" Allowed options" );
59+ desc.add_options ()
60+ (" help,h" , " produce help message" )
61+ (" file,f" , po::value<string>(), " read input from a file" )
62+ (" delay,d" , po::value<int >(), " delay time between re-calculating" )
63+ ;
64+
65+ po::variables_map vm;
66+ po::store (po::parse_command_line (ac, av, desc), vm);
67+ po::notify (vm);
68+
69+ if (vm.count (" help" )) {
70+ cout << desc << " \n " ;
71+ return 0 ;
72+ }
73+
74+ if (vm.count (" file" )) {
75+ string file_name = vm[" file" ].as <string>();
76+ // close the stdin so when we open the file it take the lowest
77+ // available file descriptor which is 0
78+ close (0 );
79+ int fd = open (file_name.c_str (), 0 );
80+ if (fd < 0 ) {
81+ cout<<" cat: cannot open " <<file_name<<endl;
82+ exit (1 );
83+ }
84+
85+ }
86+ int delay = 1 ;
87+ if (vm.count (" delay" )) {
88+ delay = vm[" delay" ].as <int >();
89+ }
90+ start (delay);
91+ } catch (exception& e) {
92+ cerr << " Error: " << e.what () << " \n " ;
93+ return 1 ;
94+ }
95+ catch (...) {
96+ cerr << " Exception of unknown type!\n " ;
97+ }
98+
4999 return 0 ;
50100}
0 commit comments