2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-10-27 01:27:47 +00:00
|
|
|
* Copyright (c) 2022, Peter Elliott <pelliott@serenityos.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2023-02-09 02:11:50 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-04-25 22:53:57 +00:00
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/Vector.h>
|
2022-10-27 01:27:47 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-02-09 02:02:46 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-20 20:13:12 +00:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2022-10-27 01:27:47 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
struct Line {
|
|
|
|
StringView key;
|
|
|
|
long int numeric_key;
|
|
|
|
DeprecatedString line;
|
|
|
|
bool numeric;
|
|
|
|
|
|
|
|
bool operator<(Line const& other) const
|
|
|
|
{
|
|
|
|
if (numeric)
|
|
|
|
return numeric_key < other.numeric_key;
|
|
|
|
|
|
|
|
return key < other.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(Line const& other) const
|
|
|
|
{
|
|
|
|
if (numeric)
|
|
|
|
return numeric_key == other.numeric_key;
|
|
|
|
|
|
|
|
return key == other.key;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct AK::Traits<Line> : public GenericTraits<Line> {
|
|
|
|
static unsigned hash(Line l)
|
|
|
|
{
|
|
|
|
if (l.numeric)
|
|
|
|
return l.numeric_key;
|
|
|
|
|
|
|
|
return l.key.hash();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
size_t key_field { 0 };
|
|
|
|
bool unique { false };
|
|
|
|
bool numeric { false };
|
|
|
|
StringView separator { "\0", 1 };
|
|
|
|
Vector<DeprecatedString> files;
|
|
|
|
};
|
|
|
|
|
|
|
|
static ErrorOr<void> load_file(Options options, StringView filename, Vector<Line>& lines, HashTable<Line>& seen)
|
|
|
|
{
|
2023-02-09 02:02:46 +00:00
|
|
|
auto file = TRY(Core::BufferedFile::create(
|
|
|
|
TRY(Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read))));
|
2022-10-27 01:27:47 +00:00
|
|
|
|
|
|
|
// FIXME: Unlimited line length
|
|
|
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
|
|
|
while (TRY(file->can_read_line())) {
|
|
|
|
DeprecatedString line = TRY(file->read_line(buffer));
|
|
|
|
|
|
|
|
StringView key = line;
|
|
|
|
if (options.key_field != 0) {
|
|
|
|
auto split = (options.separator[0])
|
|
|
|
? line.split_view(options.separator[0])
|
|
|
|
: line.split_view(isspace);
|
|
|
|
if (options.key_field - 1 >= split.size()) {
|
|
|
|
key = ""sv;
|
|
|
|
} else {
|
|
|
|
key = split[options.key_field - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Line l = { key, key.to_int().value_or(0), line, options.numeric };
|
|
|
|
|
|
|
|
if (!options.unique || !seen.contains(l)) {
|
|
|
|
lines.append(l);
|
|
|
|
if (options.unique)
|
|
|
|
seen.set(l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
2019-04-25 22:53:57 +00:00
|
|
|
|
2022-01-20 20:13:12 +00:00
|
|
|
ErrorOr<int> serenity_main([[maybe_unused]] Main::Arguments arguments)
|
2019-04-25 22:53:57 +00:00
|
|
|
{
|
2022-10-27 01:27:47 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
|
|
|
|
|
|
|
Options options;
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.add_option(options.key_field, "The field to sort by", "key-field", 'k', "keydef");
|
|
|
|
args_parser.add_option(options.unique, "Don't emit duplicate lines", "unique", 'u');
|
|
|
|
args_parser.add_option(options.numeric, "treat the key field as a number", "numeric", 'n');
|
|
|
|
args_parser.add_option(options.separator, "The separator to split fields by", "sep", 't', "char");
|
|
|
|
args_parser.add_positional_argument(options.files, "Files to sort", "file", Core::ArgsParser::Required::No);
|
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
|
|
|
Vector<Line> lines;
|
|
|
|
HashTable<Line> seen;
|
|
|
|
|
|
|
|
if (options.files.size() == 0) {
|
|
|
|
TRY(load_file(options, "-"sv, lines, seen));
|
|
|
|
} else {
|
|
|
|
for (auto& file : options.files) {
|
|
|
|
TRY(load_file(options, file, lines, seen));
|
2020-10-15 06:31:33 +00:00
|
|
|
}
|
2019-04-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:31:29 +00:00
|
|
|
quick_sort(lines);
|
2019-04-25 22:53:57 +00:00
|
|
|
|
|
|
|
for (auto& line : lines) {
|
2022-10-27 01:27:47 +00:00
|
|
|
outln("{}", line.line);
|
2019-04-25 22:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|