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