-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (110 loc) · 3.36 KB
/
main.cpp
File metadata and controls
123 lines (110 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <sstream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <fcntl.h>
#include "modules/summarizer.hpp"
#include "utils/utils.hpp"
#include <boost/program_options.hpp>
#include <atomic>
namespace po = boost::program_options;
using namespace std;
mutex continue_m;
condition_variable continue_cv;
bool continue_reading = true;
void handle_printing(Summarizer *summarizer, int delay, int precision)
{
print_elements(vector<string>{"Count", "Mean", "Min", "Max", "P95", "P99", "\n"}, precision);
unique_lock<mutex> lock(continue_m);
while (true)
{
cout << "\r";
summarizer->print_summary(precision);
if (continue_reading == false)
{
cout << endl;
break;
}
// Atomically releases lock, blocks the current executing thread.
// The thread will be unblocked when notify_one() is executed, or when the delay expires.
// When unblocked, regardless of the reason, lock is reacquired
continue_cv.wait_for(lock, chrono::seconds(delay));
}
}
void start(int delay, int precision)
{
string line;
Summarizer summarizer;
thread timer(handle_printing, &summarizer, delay, precision);
while (true)
{
if (!getline(cin, line))
{
// either an error hapened or we reached EOF
lock_guard<mutex> lock(continue_m);
continue_reading = false;
continue_cv.notify_one();
break;
}
auto [number, ok] = is_number(line);
if (!ok || line == "")
{
continue;
}
summarizer.add_number(number);
}
timer.join();
}
int main(int ac, char *av[])
{
int delay, precision;
try
{
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "Produce help message")
("file,f", po::value<string>(), "Read input from a file")
("delay,d", po::value<int>(&delay)->default_value(1), "Delay time between re-calculating")
("precision,p", po::value<int>(&precision)->default_value(2), "Control the precision parameter")
;
po::positional_options_description p;
p.add("file", 1);
po::variables_map vm;
po::store(po::command_line_parser(ac, av).
options(desc).positional(p).run(), vm);
po::notify(vm);
if (vm.count("help"))
{
cout << "summarize - Summarize a stream of numbers by printing some statistics every specified interval\n";
cout << "Usage: summarize [file_name] [options]\n";
cout << desc << "\n";
return 0;
}
if (vm.count("file"))
{
string file_name = vm["file"].as<string>();
// close the stdin so when we open the file it take the lowest
// available file descriptor which is 0
close(0);
int fd = open(file_name.c_str(), 0);
if (fd < 0)
{
cout << "cat: cannot open " << file_name << endl;
exit(1);
}
}
start(delay, precision);
}
catch (std::exception &e)
{
cerr << "Error: " << e.what() << "\n";
return 1;
}
catch (...)
{
cerr << "Exception of unknown type!\n";
}
return 0;
}