mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:17:34 +00:00
nl: Use the Stream API instead of a FILE *
This commit is contained in:
parent
14826ced68
commit
bccd12f297
1 changed files with 17 additions and 22 deletions
|
@ -7,10 +7,8 @@
|
||||||
#include <AK/DeprecatedString.h>
|
#include <AK/DeprecatedString.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
#include <LibCore/ArgsParser.h>
|
#include <LibCore/ArgsParser.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
enum NumberStyle {
|
enum NumberStyle {
|
||||||
NumberAllLines,
|
NumberAllLines,
|
||||||
|
@ -25,7 +23,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
StringView separator = " "sv;
|
StringView separator = " "sv;
|
||||||
int start_number = 1;
|
int start_number = 1;
|
||||||
int number_width = 6;
|
int number_width = 6;
|
||||||
Vector<DeprecatedString> files;
|
Vector<StringView> filenames;
|
||||||
|
|
||||||
Core::ArgsParser args_parser;
|
Core::ArgsParser args_parser;
|
||||||
|
|
||||||
|
@ -54,28 +52,25 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
args_parser.add_option(separator, "Separator between line numbers and lines", "separator", 's', "string");
|
args_parser.add_option(separator, "Separator between line numbers and lines", "separator", 's', "string");
|
||||||
args_parser.add_option(start_number, "Initial line number", "startnum", 'v', "number");
|
args_parser.add_option(start_number, "Initial line number", "startnum", 'v', "number");
|
||||||
args_parser.add_option(number_width, "Number width", "width", 'w', "number");
|
args_parser.add_option(number_width, "Number width", "width", 'w', "number");
|
||||||
args_parser.add_positional_argument(files, "Files to process", "file", Core::ArgsParser::Required::No);
|
args_parser.add_positional_argument(filenames, "Files to process", "file", Core::ArgsParser::Required::No);
|
||||||
args_parser.parse(arguments);
|
args_parser.parse(arguments);
|
||||||
|
|
||||||
Vector<FILE*> file_pointers;
|
if (filenames.is_empty())
|
||||||
if (!files.is_empty()) {
|
filenames.append(""sv);
|
||||||
for (auto& file : files) {
|
|
||||||
FILE* file_pointer = fopen(file.characters(), "r");
|
for (auto const filename : filenames) {
|
||||||
if (!file_pointer) {
|
auto maybe_file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read);
|
||||||
warnln("Failed to open {}: {}", file, strerror(errno));
|
if (maybe_file.is_error()) {
|
||||||
continue;
|
warnln("Failed to open {}: {}", filename, maybe_file.release_error());
|
||||||
}
|
continue;
|
||||||
file_pointers.append(file_pointer);
|
}
|
||||||
}
|
|
||||||
} else {
|
auto file = maybe_file.release_value();
|
||||||
file_pointers.append(stdin);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& file_pointer : file_pointers) {
|
|
||||||
int line_number = start_number - increment; // so the line number can start at 1 when added below
|
int line_number = start_number - increment; // so the line number can start at 1 when added below
|
||||||
int previous_character = 0;
|
int previous_character = 0;
|
||||||
int next_character = 0;
|
u8 next_character;
|
||||||
while ((next_character = fgetc(file_pointer)) != EOF) {
|
for (Bytes bytes = TRY(file->read_some({ &next_character, 1 })); bytes.size() != 0; bytes = TRY(file->read_some(bytes))) {
|
||||||
if (previous_character == 0 || previous_character == '\n') {
|
if (previous_character == 0 || previous_character == '\n') {
|
||||||
if (next_character == '\n' && number_style != NumberAllLines) {
|
if (next_character == '\n' && number_style != NumberAllLines) {
|
||||||
// Skip printing line count on empty lines.
|
// Skip printing line count on empty lines.
|
||||||
|
@ -90,7 +85,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
putchar(next_character);
|
putchar(next_character);
|
||||||
previous_character = next_character;
|
previous_character = next_character;
|
||||||
}
|
}
|
||||||
fclose(file_pointer);
|
|
||||||
if (previous_character != '\n')
|
if (previous_character != '\n')
|
||||||
outln(); // for cases where files have no trailing newline
|
outln(); // for cases where files have no trailing newline
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue