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(istream& in, string& line, set<string>& seen, bool live) {
  59     if (!live) {
  60         while (!cout.eof() && getline(in, line)) {
  61             if (seen.find(line) == seen.end()) {
  62                 cout << line << '\n';
  63                 seen.insert(line);
  64             }
  65         }
  66 
  67         return;
  68     }
  69 
  70     while (!cout.eof() && getline(in, line)) {
  71         if (seen.find(line) == seen.end()) {
  72             cout << line << endl;
  73             seen.insert(line);
  74         }
  75     }
  76 }
  77 
  78 bool handle_file(const char* path, string& line, set<string>& seen, bool live) {
  79     ifstream f(path);
  80     if (f.is_open()) {
  81         handle_input(f, line, seen, live);
  82         return true;
  83     }
  84 
  85     cerr << ERROR_STYLE "can't open file named '" << path << "'" RESET_STYLE << endl;
  86     return false;
  87 }
  88 
  89 // run returns the number of errors
  90 int run(size_t argc, char** argv, bool live_lines) {
  91     string line;
  92     set<string> seen;
  93     set<string> files;
  94 
  95     size_t start_args = 1;
  96     if (argc > start_args) {
  97         const string& s = argv[start_args];
  98         if (s == "-h" || s == "-help" || s == "--h" || s == "--help") {
  99             cout << info;
 100             return 0;
 101         }
 102         if (s == "--") {
 103             start_args++;
 104         }
 105     }
 106 
 107     size_t dashes = 0;
 108     size_t errors = 0;
 109 
 110     for (size_t i = start_args; i < argc && !cout.eof(); i++) {
 111         if (argv[i][0] == '-' && argv[i][1] == 0) {
 112             dashes++;
 113             if (dashes < 2) {
 114                 handle_input(cin, line, seen, live_lines);
 115             }
 116             continue;
 117         }
 118 
 119         // avoid handling any file more than once
 120         string f(argv[i]);
 121         if (files.find(f) != files.end()) {
 122             continue;
 123         }
 124         files.insert(f);
 125 
 126         if (!handle_file(argv[i], line, seen, live_lines)) {
 127             errors++;
 128         }
 129     }
 130 
 131     if (argc - start_args == 0) {
 132         handle_input(cin, line, seen, live_lines);
 133     }
 134 
 135     if (!live_lines) {
 136         cout.flush();
 137     }
 138     return errors;
 139 }
 140 
 141 int main(int argc, char** argv) {
 142     const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0;
 143     if (!live_lines) {
 144         setvbuf(stdout, NULL, _IOFBF, 0);
 145     }
 146     cin.tie(NULL);
 147     ios_base::sync_with_stdio(false);
 148 
 149     try {
 150         return run(argc, argv, live_lines) == 0 ? 0 : 1;
 151     } catch (const bad_alloc& e) {
 152         cerr << ERROR_STYLE "out of memory" RESET_STYLE << endl;
 153         return 1;
 154     }
 155 }