File: tally.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 tally tally.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 "tally [files...]\n" 45 "\n" 46 "Tally/count each distinct line. The output is a TSV table, with headers.\n" 47 ""; 48 49 typedef map<string, uint64_t> string_tally; 50 51 void handle_input(istream& in, string& line, string_tally& tally) { 52 while (getline(in, line)) { 53 tally[line]++; 54 } 55 } 56 57 bool handle_file(const char* path, string& line, string_tally& tally) { 58 ifstream f(path); 59 if (f.is_open()) { 60 handle_input(f, line, tally); 61 return true; 62 } 63 64 cerr << "\x1b[31mcan't open file named '" << path << "'\x1b[0m" << endl; 65 return false; 66 } 67 68 // run returns the number of errors 69 int run(int argc, char** argv) { 70 string line; 71 string_tally tally; 72 73 if (argc > 1) { 74 if ( 75 strcmp(argv[1], "-h") == 0 || 76 strcmp(argv[1], "-help") == 0 || 77 strcmp(argv[1], "--h") == 0 || 78 strcmp(argv[1], "--help") == 0 79 ) { 80 cout << info; 81 return 0; 82 } 83 } 84 85 size_t dashes = 0; 86 for (int i = 1; i < argc; i++) { 87 if (argv[i][0] == '-' && argv[i][1] == 0) { 88 dashes++; 89 } 90 } 91 92 if (dashes > 1) { 93 cerr << "\x1b[31mcan't use dash (stdin) more than once\x1b[0m" << endl; 94 return 1; 95 } 96 97 cin.tie(NULL); 98 ios_base::sync_with_stdio(false); 99 100 cout << "value\ttally" << endl; 101 102 size_t errors = 0; 103 for (int i = 1; i < argc; i++) { 104 if (argv[i][0] == '-' && argv[i][1] == 0) { 105 handle_input(cin, line, tally); 106 continue; 107 } 108 109 if (!handle_file(argv[i], line, tally)) { 110 errors++; 111 } 112 } 113 114 if (argc < 2) { 115 handle_input(cin, line, tally); 116 } 117 118 vector<string> sorted; 119 sorted.reserve(tally.size()); 120 for (auto kv = tally.begin(); kv != tally.end(); kv++) { 121 sorted.push_back(kv->first); 122 } 123 124 sort(sorted.begin(), sorted.end(), [&tally](const auto& x, const auto& y) { 125 auto a = tally[x]; 126 auto b = tally[y]; 127 if (a > b) { 128 return true; 129 } 130 return x < y; 131 }); 132 133 for (const auto& k : sorted) { 134 cout << k << '\t' << tally[k] << endl; 135 if (cout.eof()) { 136 break; 137 } 138 } 139 140 return errors++; 141 } 142 143 int main(int argc, char** argv) { 144 try { 145 return run(argc, argv) == 0 ? 0 : 1; 146 } catch (const bad_alloc& e) { 147 cerr << "\x1b[31mout of memory\x1b[0m" << endl; 148 return 1; 149 } 150 }