diff options
| author | Benedict Börger <benedict@0xb8000.de> | 2018-07-17 13:05:58 +0200 |
|---|---|---|
| committer | Benedict Börger <benedict@0xb8000.de> | 2018-07-17 13:05:58 +0200 |
| commit | 9c5bed131b27c062c7de39b0495d2440e5532fad (patch) | |
| tree | 9afdad5792e525d4233e803702017c5b35a5af15 /main.cpp | |
init commit: added hist programm
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..cb233e9 --- /dev/null +++ b/main.cpp @@ -0,0 +1,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; +} + |
