2020-10-25 09:22:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-10-25 09:22:34 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
#include <AK/Array.h>
|
2020-10-24 18:14:52 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibCore/File.h>
|
|
|
|
#include <ctype.h>
|
2021-11-07 01:15:10 +00:00
|
|
|
#include <string.h>
|
2020-10-24 18:14:52 +00:00
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
static constexpr size_t LINE_LENGTH_BYTES = 16;
|
|
|
|
|
2021-11-19 05:47:40 +00:00
|
|
|
enum class State {
|
|
|
|
Print,
|
|
|
|
PrintFiller,
|
|
|
|
SkipPrint
|
|
|
|
};
|
|
|
|
|
2020-10-24 18:14:52 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
const char* path = nullptr;
|
|
|
|
args_parser.add_positional_argument(path, "Input", "input", Core::ArgsParser::Required::No);
|
|
|
|
|
|
|
|
args_parser.parse(argc, argv);
|
|
|
|
|
|
|
|
RefPtr<Core::File> file;
|
|
|
|
|
|
|
|
if (!path) {
|
2020-12-22 22:37:11 +00:00
|
|
|
file = Core::File::standard_input();
|
2020-10-24 18:14:52 +00:00
|
|
|
} else {
|
2021-05-12 09:26:43 +00:00
|
|
|
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
|
2020-10-24 18:14:52 +00:00
|
|
|
if (file_or_error.is_error()) {
|
|
|
|
warnln("Failed to open {}: {}", path, file_or_error.error());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
file = file_or_error.value();
|
|
|
|
}
|
|
|
|
|
2021-10-31 18:22:53 +00: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 18:14:52 +00:00
|
|
|
else
|
2021-05-31 14:43:25 +00:00
|
|
|
out(" ");
|
2020-10-24 18:14:52 +00:00
|
|
|
|
|
|
|
if (i == 7)
|
2021-05-31 14:43:25 +00:00
|
|
|
out(" ");
|
2020-10-24 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 00:05:45 +00:00
|
|
|
out(" |");
|
2020-10-24 18:14:52 +00:00
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
if (isprint(buf[i]))
|
|
|
|
putchar(buf[i]);
|
2020-10-24 18:14:52 +00:00
|
|
|
else
|
2021-10-31 00:05:45 +00:00
|
|
|
putchar('.');
|
2020-10-24 18:14:52 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 00:05:45 +00:00
|
|
|
putchar('|');
|
2020-10-24 18:14:52 +00:00
|
|
|
putchar('\n');
|
|
|
|
};
|
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
Array<u8, BUFSIZ> contents;
|
2021-11-19 05:47:40 +00:00
|
|
|
Span<u8> previous_line;
|
2021-10-31 18:22:53 +00:00
|
|
|
static_assert(LINE_LENGTH_BYTES * 2 <= contents.size(), "Buffer is too small?!");
|
|
|
|
size_t contents_size = 0;
|
|
|
|
|
|
|
|
int nread;
|
2021-11-19 05:47:40 +00:00
|
|
|
auto state = State::Print;
|
2021-11-06 22:24:18 +00:00
|
|
|
while (true) {
|
2021-10-31 18:22:53 +00:00
|
|
|
nread = file->read(&contents[contents_size], BUFSIZ - contents_size);
|
2021-11-06 22:24:18 +00:00
|
|
|
if (nread <= 0)
|
|
|
|
break;
|
2021-10-31 18:22:53 +00:00
|
|
|
contents_size += nread;
|
2021-11-19 05:47:40 +00:00
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
size_t offset;
|
|
|
|
for (offset = 0; offset + LINE_LENGTH_BYTES - 1 < contents_size; offset += LINE_LENGTH_BYTES) {
|
2021-11-19 05:47:40 +00: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 18:14:52 +00:00
|
|
|
}
|
2021-11-19 05:47:40 +00:00
|
|
|
|
2021-10-31 18:22:53 +00:00
|
|
|
contents_size -= offset;
|
2021-11-06 22:24:18 +00: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 18:22:53 +00:00
|
|
|
VERIFY(contents_size <= LINE_LENGTH_BYTES - 1);
|
|
|
|
if (contents_size > 0)
|
|
|
|
print_line(&contents[0], contents_size);
|
2020-10-24 18:14:52 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-10-25 09:22:34 +00:00
|
|
|
}
|