blob: cb233e97e5c3a91a961dbaac0f8bf333f52462cb (
plain)
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
|
#include <iostream>
#include <vector>
#include <fstream>
// save lines with count
struct HistEntry {
std::string content;
int counter;
};
// global hist vector
std::vector<HistEntry> histogramm;
// iterate over hist and increment counter or add new Entry
void handle_bytes(std::string cont)
{
for(std::vector<HistEntry >::iterator it = histogramm.begin(); it != histogramm.end(); it++) {
// compare memory
if (it->content == cont) {
it->counter++;
return;
}
}
// nothing found, so add new entry
HistEntry tmp;
tmp.content = cont;
tmp.counter = 1;
histogramm.push_back(tmp);
}
void print_histogramm()
{
for(std::vector<HistEntry >::iterator it = histogramm.begin();
it != histogramm.end(); it++) {
std::cout << it->counter << "\t" << it->content << std::endl;
}
}
void print_usage(std::string prog_name)
{
std::cout << "usage: " << prog_name << " <file-name>" << std::endl;
std::cout << "if <file-name> is \"-\" read from stdin" << std::endl;
}
int main(int argc, char **argv)
{
if(argc != 2) {
print_usage(std::string(argv[0]));
return -1;
}
// select input stream based on command line arg
bool input_stdin = false;
std::string file(argv[1]);
std::ifstream fs;
if (file == "-")
input_stdin = true;
else
fs.open(file, std::ios_base::in);
std::istream &read_from = input_stdin ? std::cin : fs;
// handle input
for(std::string line; std::getline(read_from, line);) {
handle_bytes(line);
}
print_histogramm();
return 0;
}
|