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>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2021-10-31 21:22:53 +03:00
|
|
|
static constexpr size_t LINE_LENGTH_BYTES = 16;
|
|
|
|
|
2020-10-24 21:14:52 +03: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-23 01:37:11 +03:00
|
|
|
file = Core::File::standard_input();
|
2020-10-24 21:14:52 +03:00
|
|
|
} else {
|
2021-05-12 12:26:43 +03:00
|
|
|
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
|
2020-10-24 21:14:52 +03: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 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;
|
|
|
|
static_assert(LINE_LENGTH_BYTES * 2 <= contents.size(), "Buffer is too small?!");
|
|
|
|
size_t contents_size = 0;
|
|
|
|
|
|
|
|
int nread;
|
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;
|
|
|
|
// Print as many complete lines as we can (possibly none).
|
|
|
|
size_t offset;
|
|
|
|
for (offset = 0; offset + LINE_LENGTH_BYTES - 1 < contents_size; offset += LINE_LENGTH_BYTES) {
|
|
|
|
print_line(&contents[offset], LINE_LENGTH_BYTES);
|
2020-10-24 21:14:52 +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
|
|
|
}
|