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 g++ -Wall -O2 -s -o dedup dedup.cpp 29 */ 30 31 #include <fstream> 32 #include <iostream> 33 #include <new> 34 #include <string> 35 36 #ifdef FLAT_SET 37 #include <flat_set> 38 #else 39 #include <set> 40 #endif 41 42 using namespace std; 43 44 const string info = "" 45 "dedup [files...]\n" 46 "\n" 47 "DEDUPlicate lines, emitting each distinct line once, on first occurrence.\n" 48 "Unlike `uniq`, it doesn't require pre-sorted lines to work correctly, and\n" 49 "keeps lines in their original order." 50 ""; 51 52 #ifdef FLAT_SET 53 typedef flat_set<string> string_set; 54 #else 55 typedef set<string> string_set; 56 #endif 57 58 void handle_input(istream& in, string& line, string_set& seen) { 59 const auto& not_found = seen.end(); 60 while (!cout.eof() && getline(in, line)) { 61 if (seen.find(line) == not_found) { 62 cout << line << endl; 63 seen.insert(line); 64 } 65 } 66 } 67 68 bool handle_file(const char* path, string& line, string_set& seen) { 69 ifstream f(path); 70 if (f.is_open()) { 71 handle_input(f, line, seen); 72 return true; 73 } 74 75 cerr << "\x1b[31mcan't open file named '" << path << "'\x1b[0m" << endl; 76 return false; 77 } 78 79 // run returns the number of errors 80 int run(size_t argc, char** argv) { 81 string line; 82 string_set seen; 83 84 if (argc > 1) { 85 const string& s = argv[1]; 86 if (s == "-h" || s == "-help" || s == "--h" || s == "--help") { 87 cout << info; 88 return 0; 89 } 90 } 91 92 cin.tie(NULL); 93 ios_base::sync_with_stdio(false); 94 95 size_t dashes = 0; 96 size_t errors = 0; 97 for (size_t i = 1; i < argc && !cout.eof(); i++) { 98 if (argv[i][0] == '-' && argv[i][1] == 0) { 99 dashes++; 100 if (dashes < 2) { 101 handle_input(cin, line, seen); 102 } 103 continue; 104 } 105 106 if (!handle_file(argv[i], line, seen)) { 107 errors++; 108 } 109 } 110 111 if (argc < 2) { 112 handle_input(cin, line, seen); 113 } 114 115 return errors; 116 } 117 118 int main(int argc, char** argv) { 119 try { 120 return run(argc, argv) == 0 ? 0 : 1; 121 } catch (const bad_alloc& e) { 122 cerr << "\x1b[31mout of memory\x1b[0m" << endl; 123 return 1; 124 } 125 }