-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupby.cpp
203 lines (154 loc) · 4.7 KB
/
groupby.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* 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::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <memory>
using std::unique_ptr;
using std::move;
#include <map>
using std::map;
#include <unistd.h>
#include "util.h"
#include "groupby.h"
// The input arguments analysis.
// =============================
/// The object that represents the inputa rguments of the program.
struct arguments {
/// The field delimiter - default: tab character.
char delim;
/// Groupping fields.
vector<uint32_t> groupbys;
/// The aggregator definitions. Format:
/// [(field_index, aggregator)]
vector<string> aggr_strs;
};
/// Parses the program arguments building a proper object that reflects them.
arguments parse_args(int argc, char** argv) {
arguments args;
args.delim = '\t'; // Providing the default delimiter.
stringstream converter;
uint32_t index;
int c;
while((c = getopt(argc, argv, "a:d:g:")) != -1) {
switch(c) {
case 'a':
args.aggr_strs.emplace_back(optarg);
break;
case 'd':
if(string(optarg).size() != 1)
throw string("The delimiter is expected to be a single character.");
args.delim = optarg[0];
if(!isprint(args.delim) && args.delim != '\t')
throw string("Cannot use the given character as a delimiter.");
break;
case 'g':
converter.seekg(0);
converter.seekp(0);
converter << optarg;
converter >> index;
if(converter.fail())
throw string("Failed parsing groupping field index.");
args.groupbys.push_back(index);
break;
case '?':
if(optopt == 'a')
throw string("Option -a requires an aggregator argument.");
if(optopt == 'd')
throw string("Option -d requires a delimiter argument.");
if(optopt == 'g')
throw string("Option -g requires a groupper argument.");
// Notice a fallthrough. It is here although it should
// not happen unless someone changes the getopt options definition.
default:
throw string("Illegal option -") + (char)optopt + ".";
}
}
return args;
}
// The aggregation phase.
// ======================
/// Fetches the data from the input stream and finally prints out
/// the aggregations to the provided output stream.
groupby::groupper process_stream(istream& in, const arguments& args) {
groupby::groupper groupper(args.groupbys, args.aggr_strs);
string line;
while(true) {
getline(in, line);
if(!in.good())
break;
vector<string> row = split(line, args.delim);
groupper.consume_row(row);
}
return groupper;
}
// The result printing phase.
// ==========================
void print_results(groupby::groupper groupper,
ostream& out,
const arguments& args) {
// Print the groupping report.
// ---------------------------
// Print the header row.
for(uint32_t g : args.groupbys)
out << g << args.delim;
for(uint32_t i = 0; i < args.aggr_strs.size(); ++i) {
out << '"' << args.aggr_strs.at(i) << '"';
if(i < (args.aggr_strs.size() - 1))
out << args.delim;
}
out << endl;
// Print the groups.
groupper.for_each_group([&out,&args](const groupby::group& g) {
for(const auto& d : g.get_definition())
out << d.second << args.delim;
uint32_t aggr_size = g.get_aggregators().size();
for(uint32_t i = 0; i < aggr_size; ++i) {
out << g.get_aggregators().at(i).second->get();
if(i < (aggr_size - 1))
out << args.delim;
}
out << endl;
});
}
int main(int argc, char** argv) {
// Don't print internal getopt error messages.
opterr = 0;
try {
// Read and validate the arguments.
arguments args = parse_args(argc, argv);
if(args.groupbys.empty() || args.aggr_strs.empty())
throw string("Missing groupping or aggregation definitions.");
// Process the input stream.
auto groupper = process_stream(cin, args);
// Print the results.
print_results(move(groupper), cout, args);
return 0;
} catch(string& ex) {
cout << "Error : " << ex << endl;
return 1;
}
}