-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistogram.cpp
125 lines (100 loc) · 2.95 KB
/
histogram.cpp
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
124
/* Copyright (C) 2012,2013 Krzysztof Stachowiak */
/*
* This file is part of stat-toolkit.
*
* stat-toolkit is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* stat-toolkit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with stat-toolkit; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <iostream>
using std::istream;
using std::ostream;
using std::cin;
using std::cout;
using std::endl;
#include <sstream>
using std::stringstream;
#include <string>
using std::string;
#include <unistd.h>
#include "histogram.h"
/// The common usage string.
const string usage("Usage: histogram [-w bucket-width]");
/// @brief A structure for storing the input arguments.
struct arguments {
double bucket_size; ///< The width of the bucket.
char delim; ///< The input/output delimiter.
};
/// @brief Reads the input arguments and stores them in a convenient struct.
///
/// @param[in] argc Classical arguments count.
/// @param[in] argv Classical arguments vector.
arguments parse_args(int argc, char** argv) {
arguments args;
args.bucket_size = 1.0;
args.delim = '\t';
stringstream bucketss;
int c;
while((c = getopt(argc, argv, "d:w:")) != -1) {
switch(c) {
case 'd':
if(string(optarg).size() != 1)
throw string("Delimiterm ust be given by a single character");
args.delim = optarg[0];
if(!isprint(args.delim) && args.delim != '\t')
throw string("Illegal character requested as a delimiter");
break;
case 'w':
bucketss << optarg;
bucketss >> args.bucket_size;
if(bucketss.fail())
throw string("Failed parsing the bucket width argument.");
break;
case '?':
throw string("Options require arguments.");
default:
throw usage;
}
}
return args;
}
/// @brief Reads numbers from stdin and stuffs them in the histogram.
///
/// @param[in] h The histogram to be filled.
/// @param[in] in The input stream to be processed.
void process_input(hist::histogram& h, istream& in) {
while(true) {
double value;
in >> value;
if(in.eof())
break;
if(in.fail())
throw string("Failed reading a number from stdin.");
h.put(value);
}
}
int main(int argc, char** argv) {
// Don't print internal getopt error messages.
opterr = 0;
try {
arguments args = parse_args(argc, argv);
hist::histogram h(args.bucket_size);
process_input(h, cin);
for(const auto& pr : h.get_buckets())
cout << pr.first << args.delim << pr.second << endl;
} catch(string& ex) {
cout << ex << endl;
return 1;
}
return 0;
}