sort.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/DeprecatedString.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/QuickSort.h>
  10. #include <AK/Vector.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/System.h>
  14. #include <LibMain/Main.h>
  15. #include <ctype.h>
  16. struct Line {
  17. StringView key;
  18. long int numeric_key;
  19. DeprecatedString line;
  20. bool numeric;
  21. bool operator<(Line const& other) const
  22. {
  23. if (numeric)
  24. return numeric_key < other.numeric_key;
  25. return key < other.key;
  26. }
  27. bool operator==(Line const& other) const
  28. {
  29. if (numeric)
  30. return numeric_key == other.numeric_key;
  31. return key == other.key;
  32. }
  33. private:
  34. };
  35. template<>
  36. struct AK::Traits<Line> : public GenericTraits<Line> {
  37. static unsigned hash(Line l)
  38. {
  39. if (l.numeric)
  40. return l.numeric_key;
  41. return l.key.hash();
  42. }
  43. };
  44. struct Options {
  45. size_t key_field { 0 };
  46. bool unique { false };
  47. bool numeric { false };
  48. bool reverse { false };
  49. StringView separator { "\0", 1 };
  50. Vector<DeprecatedString> files;
  51. };
  52. static ErrorOr<void> load_file(Options options, StringView filename, Vector<Line>& lines, HashTable<Line>& seen)
  53. {
  54. auto file = TRY(Core::BufferedFile::create(
  55. TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read))));
  56. // FIXME: Unlimited line length
  57. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  58. while (TRY(file->can_read_line())) {
  59. DeprecatedString line = TRY(file->read_line(buffer));
  60. StringView key = line;
  61. if (options.key_field != 0) {
  62. auto split = (options.separator[0])
  63. ? line.split_view(options.separator[0])
  64. : line.split_view(isspace);
  65. if (options.key_field - 1 >= split.size()) {
  66. key = ""sv;
  67. } else {
  68. key = split[options.key_field - 1];
  69. }
  70. }
  71. Line l = { key, key.to_int().value_or(0), line, options.numeric };
  72. if (!options.unique || !seen.contains(l)) {
  73. lines.append(l);
  74. if (options.unique)
  75. seen.set(l);
  76. }
  77. }
  78. return {};
  79. }
  80. ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments)
  81. {
  82. TRY(Core::System::pledge("stdio rpath"));
  83. Options options;
  84. Core::ArgsParser args_parser;
  85. args_parser.add_option(options.key_field, "The field to sort by", "key-field", 'k', "keydef");
  86. args_parser.add_option(options.unique, "Don't emit duplicate lines", "unique", 'u');
  87. args_parser.add_option(options.numeric, "treat the key field as a number", "numeric", 'n');
  88. args_parser.add_option(options.separator, "The separator to split fields by", "sep", 't', "char");
  89. args_parser.add_option(options.reverse, "Sort in reverse order", "reverse", 'r');
  90. args_parser.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No);
  91. args_parser.parse(arguments);
  92. Vector<Line> lines;
  93. HashTable<Line> seen;
  94. if (options.files.size() == 0) {
  95. TRY(load_file(options, "-"sv, lines, seen));
  96. } else {
  97. for (auto& file : options.files) {
  98. TRY(load_file(options, file, lines, seen));
  99. }
  100. }
  101. quick_sort(lines);
  102. auto print_lines = [](auto const& lines) {
  103. for (auto& line : lines)
  104. outln("{}", line.line);
  105. };
  106. if (options.reverse)
  107. print_lines(lines.in_reverse());
  108. else
  109. print_lines(lines);
  110. return 0;
  111. }