File: bully.cpp
   1 /*
   2 The MIT License (MIT)
   3 
   4 Copyright © 2020-2025 pacman64
   5 
   6 Permission is hereby granted, free of charge, to any person obtaining a copy of
   7 this software and associated documentation files (the “Software”), to deal
   8 in the Software without restriction, including without limitation the rights to
   9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  10 of the Software, and to permit persons to whom the Software is furnished to do
  11 so, subject to the following conditions:
  12 
  13 The above copyright notice and this permission notice shall be included in all
  14 copies or substantial portions of the Software.
  15 
  16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22 SOFTWARE.
  23 */
  24 
  25 /*
  26 You can build this command-line app by running
  27 
  28 g++ -Wall -O2 -s -o bully bully.cpp
  29 */
  30 
  31 #include <algorithm>
  32 #include <cstdint>
  33 #include <cstring>
  34 #include <fstream>
  35 #include <iostream>
  36 #include <map>
  37 #include <new>
  38 #include <string>
  39 #include <vector>
  40 
  41 using namespace std;
  42 
  43 const string info = ""
  44 "bully [files...]\n"
  45 "\n"
  46 "Tally/count each distinct line, also emitting bullet points. The output is\n"
  47 " a TSV table, with headers.\n"
  48 "";
  49 
  50 typedef map<string, uint64_t> string_tally;
  51 
  52 void handle_input(istream& in, string& line, string_tally& tally) {
  53     while (getline(in, line)) {
  54         tally[line]++;
  55     }
  56 }
  57 
  58 bool handle_file(const char* path, string& line, string_tally& tally) {
  59     ifstream f(path);
  60     if (f.is_open()) {
  61         handle_input(f, line, tally);
  62         return true;
  63     }
  64 
  65     cerr << "\x1b[31mcan't open file named '" << path << "'\x1b[0m" << endl;
  66     return false;
  67 }
  68 
  69 // run returns the number of errors
  70 int run(int argc, char** argv) {
  71     string line;
  72     string_tally tally;
  73 
  74     if (argc > 1) {
  75         if (
  76             strcmp(argv[1], "-h") == 0 ||
  77             strcmp(argv[1], "-help") == 0 ||
  78             strcmp(argv[1], "--h") == 0 ||
  79             strcmp(argv[1], "--help") == 0
  80         ) {
  81             cout << info;
  82             return 0;
  83         }
  84     }
  85 
  86     size_t dashes = 0;
  87     for (int i = 1; i < argc; i++) {
  88         if (argv[i][0] == '-' && argv[i][1] == 0) {
  89             dashes++;
  90         }
  91     }
  92 
  93     if (dashes > 1) {
  94         cerr << "\x1b[31mcan't use dash (stdin) more than once\x1b[0m" << endl;
  95         return 1;
  96     }
  97 
  98     cin.tie(NULL);
  99     ios_base::sync_with_stdio(false);
 100 
 101     cout << "value\ttally\tbullets" << endl;
 102 
 103     size_t errors = 0;
 104     for (int i = 1; i < argc; i++) {
 105         if (argv[i][0] == '-' && argv[i][1] == 0) {
 106             handle_input(cin, line, tally);
 107             continue;
 108         }
 109 
 110         if (!handle_file(argv[i], line, tally)) {
 111             errors++;
 112         }
 113     }
 114 
 115     if (argc < 2) {
 116         handle_input(cin, line, tally);
 117     }
 118 
 119     vector<string> sorted;
 120     sorted.reserve(tally.size());
 121     for (auto kv = tally.begin(); kv != tally.end(); kv++) {
 122         sorted.push_back(kv->first);
 123     }
 124 
 125     sort(sorted.begin(), sorted.end(), [&tally](const auto& x, const auto& y) {
 126         auto a = tally[x];
 127         auto b = tally[y];
 128         if (a > b) {
 129             return true;
 130         }
 131         return x < y;
 132     });
 133 
 134     for (const auto& k : sorted) {
 135         auto n = tally[k];
 136         cout << k << '\t' << n << '\t';
 137         while (n > 0) {
 138             // cout << "●";
 139             cout << "";
 140             n--;
 141         }
 142         cout << endl;
 143 
 144         if (cout.eof()) {
 145             break;
 146         }
 147     }
 148 
 149     return errors;
 150 }
 151 
 152 int main(int argc, char** argv) {
 153     try {
 154         return run(argc, argv) == 0 ? 0 : 1;
 155     } catch (const bad_alloc& e) {
 156         cerr << "\x1b[31mout of memory\x1b[0m" << endl;
 157         return 1;
 158     }
 159 }