File: dedup.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 ./dedup ./dedup.cpp
  29 */
  30 
  31 #include <fstream>
  32 #include <iostream>
  33 #include <set>
  34 #include <string>
  35 #include <unordered_set>
  36 #include <unistd.h>
  37 
  38 #ifdef RED_ERRORS
  39 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  40 #ifdef __APPLE__
  41 #define ERROR_STYLE "\x1b[31m"
  42 #endif
  43 #define RESET_STYLE "\x1b[0m"
  44 #else
  45 #define ERROR_STYLE ""
  46 #define RESET_STYLE ""
  47 #endif
  48 
  49 using namespace std;
  50 
  51 const string info = ""
  52 "dedup [files...]\n"
  53 "\n"
  54 "DEDUPlicate lines, emitting each distinct line once, when it first appears.\n"
  55 "Unlike `uniq`, this app doesn't require pre-sorted lines to work correctly,\n"
  56 "and keeps lines in their original order.\n"
  57 "";
  58 
  59 typedef unordered_set<string> string_set;
  60 
  61 void dedup(istream& in, string& line, string_set& seen, bool live) {
  62     if (live) {
  63         while (!cout.eof() && getline(in, line)) {
  64             if (seen.find(line) == seen.end()) {
  65                 cout << line << endl;
  66                 seen.insert(line);
  67             }
  68         }
  69         return;
  70     }
  71 
  72     while (!cout.eof() && getline(in, line)) {
  73         if (seen.find(line) == seen.end()) {
  74             cout << line << '\n';
  75             seen.insert(line);
  76         }
  77     }
  78 }
  79 
  80 bool dedup(const char* path, string& line, string_set& seen, bool live) {
  81     ifstream f(path);
  82     if (!f.is_open()) {
  83         cerr << ERROR_STYLE "can't open file named '" << path <<
  84             "'" RESET_STYLE << endl;
  85         return false;
  86     }
  87 
  88     dedup(f, line, seen, live);
  89     return true;
  90 }
  91 
  92 bool run(size_t argc, char** argv) {
  93     size_t start_args = 1;
  94 
  95     if (argc > start_args) {
  96         const string& s = argv[start_args];
  97 
  98         if (s == "-h" || s == "-help" || s == "--h" || s == "--help") {
  99             cout << info;
 100             return true;
 101         }
 102 
 103         if (s == "--") {
 104             start_args++;
 105         }
 106     }
 107 
 108     string line;
 109     string_set seen;
 110     string_set files;
 111 
 112     size_t errors = 0;
 113     const bool live_lines = lseek(STDOUT_FILENO, 0, SEEK_CUR) != 0;
 114 
 115     // speed up IO: the stdio API isn't used directly, so there's no need to
 116     // synchronize with it
 117     ios_base::sync_with_stdio(false);
 118 
 119     for (size_t i = start_args; i < argc && !cout.eof(); i++) {
 120         string arg(argv[i]);
 121 
 122         // avoid handling any file more than once
 123         if (files.find(arg) != files.end()) {
 124             continue;
 125         }
 126         files.insert(arg);
 127 
 128         if (arg == "-") {
 129             dedup(cin, line, seen, live_lines);
 130             continue;
 131         }
 132 
 133         if (!dedup(argv[i], line, seen, live_lines)) {
 134             errors++;
 135         }
 136     }
 137 
 138     if (argc - start_args == 0) {
 139         dedup(cin, line, seen, live_lines);
 140     }
 141 
 142     if (!live_lines) {
 143         cout.flush();
 144     }
 145 
 146     return errors == 0;
 147 }
 148 
 149 int main(int argc, char** argv) {
 150     try {
 151         return run(argc, argv) ? 0 : 1;
 152     } catch (const bad_alloc& e) {
 153         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 154         return 2;
 155     }
 156 }