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 c++ -Wall -s -O3 -march=native -mtune=native -flto -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 #ifdef RED_ERRORS
  42 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  43 #ifdef __APPLE__
  44 #define ERROR_STYLE "\x1b[31m"
  45 #endif
  46 #define RESET_STYLE "\x1b[0m"
  47 #else
  48 #define ERROR_STYLE ""
  49 #define RESET_STYLE ""
  50 #endif
  51 
  52 using namespace std;
  53 
  54 const string info = ""
  55 "bully [files...]\n"
  56 "\n"
  57 "Tally/count each distinct line, also emitting bullet points. The output is\n"
  58 " a TSV table, with headers.\n"
  59 "";
  60 
  61 typedef map<string, uint64_t> string_tally;
  62 
  63 void handle_input(istream& in, string& line, string_tally& tally) {
  64     while (getline(in, line)) {
  65         tally[line]++;
  66     }
  67 }
  68 
  69 bool handle_file(const char* path, string& line, string_tally& tally) {
  70     ifstream f(path);
  71     if (f.is_open()) {
  72         handle_input(f, line, tally);
  73         return true;
  74     }
  75 
  76     cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl;
  77     return false;
  78 }
  79 
  80 // run returns the number of errors
  81 int run(int argc, char** argv) {
  82     string line;
  83     string_tally tally;
  84 
  85     if (argc > 1) {
  86         if (
  87             strcmp(argv[1], "-h") == 0 ||
  88             strcmp(argv[1], "-help") == 0 ||
  89             strcmp(argv[1], "--h") == 0 ||
  90             strcmp(argv[1], "--help") == 0
  91         ) {
  92             cout << info;
  93             return 0;
  94         }
  95     }
  96 
  97     size_t dashes = 0;
  98     for (int i = 1; i < argc; i++) {
  99         if (argv[i][0] == '-' && argv[i][1] == 0) {
 100             dashes++;
 101         }
 102     }
 103 
 104     if (dashes > 1) {
 105         cerr << ERROR_STYLE "can't use dash (stdin) more than once" RESET_STYLE << endl;
 106         return 1;
 107     }
 108 
 109     cin.tie(NULL);
 110     ios_base::sync_with_stdio(false);
 111 
 112     cout << "value\ttally\tbullets\n";
 113     cout.flush();
 114 
 115     size_t errors = 0;
 116     for (int i = 1; i < argc; i++) {
 117         if (argv[i][0] == '-' && argv[i][1] == 0) {
 118             handle_input(cin, line, tally);
 119             continue;
 120         }
 121 
 122         if (!handle_file(argv[i], line, tally)) {
 123             errors++;
 124         }
 125     }
 126 
 127     if (argc < 2) {
 128         handle_input(cin, line, tally);
 129     }
 130 
 131     vector<string> lines;
 132     lines.reserve(tally.size());
 133     for (auto kv = tally.begin(); kv != tally.end(); kv++) {
 134         lines.push_back(kv->first);
 135     }
 136 
 137     sort(lines.begin(), lines.end(), [&tally](const auto& x, const auto& y) {
 138         auto a = tally[x];
 139         auto b = tally[y];
 140         if (a > b) {
 141             return true;
 142         }
 143         return x < y;
 144     });
 145 
 146     for (const auto& k : lines) {
 147         auto n = tally[k];
 148         cout << k << '\t' << n << '\t';
 149         while (n > 0) {
 150             // cout << "●";
 151             cout << "";
 152             n--;
 153         }
 154         cout << '\n';
 155 
 156         if (cout.eof()) {
 157             break;
 158         }
 159     }
 160 
 161     cout.flush();
 162     return errors;
 163 }
 164 
 165 int main(int argc, char** argv) {
 166     try {
 167         return run(argc, argv) == 0 ? 0 : 1;
 168     } catch (const bad_alloc& e) {
 169         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 170         return 1;
 171     }
 172 }