From 9c5bed131b27c062c7de39b0495d2440e5532fad Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Tue, 17 Jul 2018 13:05:58 +0200 Subject: init commit: added hist programm --- main.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 main.cpp (limited to 'main.cpp') 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 +#include +#include + +// save lines with count +struct HistEntry { + std::string content; + int counter; +}; + +// global hist vector +std::vector histogramm; + +// iterate over hist and increment counter or add new Entry +void handle_bytes(std::string cont) +{ + for(std::vector::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::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 << " " << std::endl; + std::cout << "if 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; +} + -- cgit v1.2.3-70-g09d2