From 14826ced68a1e38bc600db13183d5c7854315c0c Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Wed, 7 Jun 2023 00:29:42 -0400 Subject: [PATCH] cut: Use the Stream API instead of a `FILE *` --- Userland/Utilities/cut.cpp | 50 ++++++++++++++------------------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/Userland/Utilities/cut.cpp b/Userland/Utilities/cut.cpp index 12c2c9323b..da5b81d752 100644 --- a/Userland/Utilities/cut.cpp +++ b/Userland/Utilities/cut.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -120,22 +121,22 @@ static bool expand_list(DeprecatedString& list, Vector& ranges) return true; } -static void process_line_bytes(char* line, size_t length, Vector const& ranges) +static void process_line_bytes(StringView line, Vector const& ranges) { for (auto& i : ranges) { - if (i.m_from >= length) + if (i.m_from >= line.length()) continue; - auto to = min(i.m_to, length); + auto to = min(i.m_to, line.length()); auto sub_string = DeprecatedString(line).substring(i.m_from - 1, to - i.m_from + 1); out("{}", sub_string); } outln(); } -static void process_line_fields(char* line, size_t length, Vector const& ranges, char delimiter) +static void process_line_fields(StringView line, Vector const& ranges, char delimiter) { - auto string_split = DeprecatedString(line, length).split(delimiter); + auto string_split = DeprecatedString(line).split(delimiter); Vector output_fields; for (auto& range : ranges) { @@ -224,44 +225,29 @@ ErrorOr serenity_main(Main::Arguments arguments) } if (files.is_empty()) - files.append(DeprecatedString()); + files.append(""sv); /* Process each file */ - for (auto& file : files) { - FILE* fp = stdin; - if (!file.is_null()) { - fp = fopen(DeprecatedString(file).characters(), "r"); - if (!fp) { - warnln("cut: Could not open file '{}'", file); - continue; - } + for (auto const filename : files) { + auto maybe_file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read); + if (maybe_file.is_error()) { + warnln("cut: Could not open file '{}'", filename.is_empty() ? "stdin"sv : filename); + continue; } + auto file = TRY(Core::InputBufferedFile::create(maybe_file.release_value())); - char* line = nullptr; - ssize_t line_length = 0; - size_t line_capacity = 0; - while ((line_length = getline(&line, &line_capacity, fp)) != -1) { - if (line_length < 0) { - warnln("cut: Failed to read line from file '{}'", file); - break; - } - line[line_length - 1] = '\0'; - line_length--; + Array buffer; + while (!file->is_eof()) { + auto line = TRY(file->read_line(buffer)); if (selected_bytes) { - process_line_bytes(line, line_length, disjoint_ranges); + process_line_bytes(line, disjoint_ranges); } else if (selected_fields) { - process_line_fields(line, line_length, disjoint_ranges, delimiter[0]); + process_line_fields(line, disjoint_ranges, delimiter[0]); } else { VERIFY_NOT_REACHED(); } } - - if (line) - free(line); - - if (!file.is_null()) - fclose(fp); } return 0;