2020-10-25 12:22:34 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-25 12:22:34 +03:00
|
|
|
*/
|
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
#include <AK/Array.h>
|
2020-10-24 21:14:52 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
2022-01-13 23:37:31 +03:00
|
|
|
#include <LibMain/Main.h>
|
2020-10-24 21:14:52 +03:00
|
|
|
#include <ctype.h>
|
2021-11-07 04:15:10 +03:00
|
|
|
#include <string.h>
|
2020-10-24 21:14:52 +03:00
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
static constexpr size_t LINE_LENGTH_BYTES = 16;
|
|
|
|
|
2021-11-19 08:47:40 +03:00
|
|
|
enum class State {
|
|
|
|
Print,
|
|
|
|
PrintFiller,
|
|
|
|
SkipPrint
|
|
|
|
};
|
|
|
|
|
2022-01-13 23:37:31 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments args)
|
2020-10-24 21:14:52 +03:00
|
|
|
{
|
|
|
|
Core::ArgsParser args_parser;
|
2022-04-01 20:58:27 +03:00
|
|
|
char const* path = nullptr;
|
2021-11-19 08:54:46 +03:00
|
|
|
bool verbose = false;
|
2020-10-24 21:14:52 +03:00
|
|
|
args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No);
|
2021-11-19 08:54:46 +03:00
|
|
|
args_parser.add_option(verbose, "Display all input data", "verbose", 'v');
|
2020-10-24 21:14:52 +03:00
|
|
|
|
2022-01-13 23:37:31 +03:00
|
|
|
args_parser.parse(args);
|
2020-10-24 21:14:52 +03:00
|
|
|
|
|
|
|
RefPtr<Core::File> file;
|
|
|
|
|
2022-01-13 23:37:31 +03:00
|
|
|
if (!path)
|
2020-12-23 01:37:11 +03:00
|
|
|
file = Core::File::standard_input();
|
2022-01-13 23:37:31 +03:00
|
|
|
else
|
|
|
|
file = TRY(Core::File::open(path, Core::OpenMode::ReadOnly));
|
2020-10-24 21:14:52 +03:00
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
auto print_line = [](u8* buf, size_t size) {
|
|
|
|
VERIFY(size <= LINE_LENGTH_BYTES);
|
|
|
|
for (size_t i = 0; i < LINE_LENGTH_BYTES; ++i) {
|
|
|
|
if (i < size)
|
|
|
|
out("{:02x} ", buf[i]);
|
2020-10-24 21:14:52 +03:00
|
|
|
else
|
2021-05-31 17:43:25 +03:00
|
|
|
out(" ");
|
2020-10-24 21:14:52 +03:00
|
|
|
|
|
|
|
if (i == 7)
|
2021-05-31 17:43:25 +03:00
|
|
|
out(" ");
|
2020-10-24 21:14:52 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 03:05:45 +03:00
|
|
|
out(" |");
|
2020-10-24 21:14:52 +03:00
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
if (isprint(buf[i]))
|
|
|
|
putchar(buf[i]);
|
2020-10-24 21:14:52 +03:00
|
|
|
else
|
2021-10-31 03:05:45 +03:00
|
|
|
putchar('.');
|
2020-10-24 21:14:52 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 03:05:45 +03:00
|
|
|
putchar('|');
|
2020-10-24 21:14:52 +03:00
|
|
|
putchar('\n');
|
|
|
|
};
|
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
Array<u8, BUFSIZ> contents;
|
2021-11-19 08:47:40 +03:00
|
|
|
Span<u8> previous_line;
|
2021-10-31 21:22:53 +03:00
|
|
|
static_assert(LINE_LENGTH_BYTES * 2 <= contents.size(), "Buffer is too small?!");
|
|
|
|
size_t contents_size = 0;
|
|
|
|
|
|
|
|
int nread;
|
2021-11-19 08:47:40 +03:00
|
|
|
auto state = State::Print;
|
2021-11-07 01:24:18 +03:00
|
|
|
while (true) {
|
2021-10-31 21:22:53 +03:00
|
|
|
nread = file->read(&contents[contents_size], BUFSIZ - contents_size);
|
2021-11-07 01:24:18 +03:00
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
2021-10-31 21:22:53 +03:00
|
|
|
contents_size += nread;
|
2021-11-19 08:47:40 +03:00
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
size_t offset;
|
|
|
|
for (offset = 0; offset + LINE_LENGTH_BYTES - 1 < contents_size; offset += LINE_LENGTH_BYTES) {
|
2021-11-19 08:54:46 +03:00
|
|
|
if (verbose) {
|
|
|
|
print_line(&contents[offset], LINE_LENGTH_BYTES);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-19 08:47:40 +03:00
|
|
|
auto current_line = contents.span().slice(offset, LINE_LENGTH_BYTES);
|
|
|
|
bool is_same_contents = (current_line == previous_line);
|
|
|
|
if (!is_same_contents)
|
|
|
|
state = State::Print;
|
|
|
|
else if (is_same_contents && (state != State::SkipPrint))
|
|
|
|
state = State::PrintFiller;
|
|
|
|
|
|
|
|
// Coalesce repeating lines
|
|
|
|
switch (state) {
|
|
|
|
case State::Print:
|
|
|
|
print_line(&contents[offset], LINE_LENGTH_BYTES);
|
|
|
|
break;
|
|
|
|
case State::PrintFiller:
|
|
|
|
outln("*");
|
|
|
|
state = State::SkipPrint;
|
|
|
|
break;
|
|
|
|
case State::SkipPrint:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
previous_line = current_line;
|
2020-10-24 21:14:52 +03:00
|
|
|
}
|
2021-11-19 08:47:40 +03:00
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
contents_size -= offset;
|
2021-11-07 01:24:18 +03:00
|
|
|
VERIFY(contents_size < LINE_LENGTH_BYTES);
|
|
|
|
// If we managed to make the buffer exactly full, &contents[BUFSIZ] would blow up.
|
|
|
|
if (contents_size > 0) {
|
|
|
|
// Regions cannot overlap due to above static_assert.
|
|
|
|
memcpy(&contents[0], &contents[offset], contents_size);
|
|
|
|
}
|
|
|
|
}
|
2021-10-31 21:22:53 +03:00
|
|
|
VERIFY(contents_size <= LINE_LENGTH_BYTES - 1);
|
|
|
|
if (contents_size > 0)
|
|
|
|
print_line(&contents[0], contents_size);
|
2020-10-24 21:14:52 +03:00
|
|
|
|
|
|
|
return 0;
|
2020-10-25 12:22:34 +03:00
|
|
|
}
|