From 39369f1da63e8949d00378824714b37aabac7db1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 29 Apr 2021 11:28:01 +0200 Subject: [PATCH] Utilities: Use Vector positional arguments in some places There are more places we can use these, I just picked some that felt like they improved the code. --- Userland/Utilities/cat.cpp | 12 +++++------- Userland/Utilities/checksum.cpp | 6 +++--- Userland/Utilities/gunzip.cpp | 4 ++-- Userland/Utilities/gzip.cpp | 4 ++-- Userland/Utilities/tar.cpp | 2 +- Userland/Utilities/zip.cpp | 4 ++-- 6 files changed, 15 insertions(+), 17 deletions(-) diff --git a/Userland/Utilities/cat.cpp b/Userland/Utilities/cat.cpp index c683382c68..59c203d70f 100644 --- a/Userland/Utilities/cat.cpp +++ b/Userland/Utilities/cat.cpp @@ -6,13 +6,11 @@ #include #include -#include #include #include #include #include #include -#include #include int main(int argc, char** argv) @@ -22,7 +20,7 @@ int main(int argc, char** argv) return 1; } - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.set_general_help("Concatenate files or pipes to stdout."); @@ -31,12 +29,12 @@ int main(int argc, char** argv) Vector fds; if (!paths.is_empty()) { - for (const char* path : paths) { + for (auto const& path : paths) { int fd; - if (StringView { path } == "-") { + if (path == "-") { fd = 0; - } else if ((fd = open(path, O_RDONLY)) == -1) { - fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); + } else if ((fd = open(path.characters(), O_RDONLY)) == -1) { + warnln("Failed to open {}: {}", path, strerror(errno)); continue; } fds.append(fd); diff --git a/Userland/Utilities/checksum.cpp b/Userland/Utilities/checksum.cpp index 21d182e905..583269f51f 100644 --- a/Userland/Utilities/checksum.cpp +++ b/Userland/Utilities/checksum.cpp @@ -36,7 +36,7 @@ int main(int argc, char** argv) auto hash_name = program_name.substring_view(0, program_name.length() - 3).to_string().to_uppercase(); auto paths_help_string = String::formatted("File(s) to print {} checksum of", hash_name); - Vector paths; + Vector paths; Core::ArgsParser args_parser; args_parser.add_positional_argument(paths, paths_help_string.characters(), "path", Core::ArgsParser::Required::No); @@ -52,8 +52,8 @@ int main(int argc, char** argv) auto has_error = false; auto file = Core::File::construct(); - for (auto path : paths) { - if (StringView { path } == "-") { + for (auto const& path : paths) { + if (path == "-") { success = file->open(STDIN_FILENO, Core::IODevice::OpenMode::ReadOnly, Core::File::ShouldCloseFileDescriptor::No); } else { file->set_filename(path); diff --git a/Userland/Utilities/gunzip.cpp b/Userland/Utilities/gunzip.cpp index c4826d9956..589e0ddd3d 100644 --- a/Userland/Utilities/gunzip.cpp +++ b/Userland/Utilities/gunzip.cpp @@ -25,7 +25,7 @@ static bool decompress_file(Buffered& input_stream, Buffe int main(int argc, char** argv) { - Vector filenames; + Vector filenames; bool keep_input_files { false }; bool write_to_stdout { false }; @@ -38,7 +38,7 @@ int main(int argc, char** argv) if (write_to_stdout) keep_input_files = true; - for (String filename : filenames) { + for (auto filename : filenames) { if (!filename.ends_with(".gz")) filename = String::formatted("{}.gz", filename); diff --git a/Userland/Utilities/gzip.cpp b/Userland/Utilities/gzip.cpp index c386ce0f49..7de3827470 100644 --- a/Userland/Utilities/gzip.cpp +++ b/Userland/Utilities/gzip.cpp @@ -12,7 +12,7 @@ int main(int argc, char** argv) { - Vector filenames; + Vector filenames; bool keep_input_files { false }; bool write_to_stdout { false }; @@ -25,7 +25,7 @@ int main(int argc, char** argv) if (write_to_stdout) keep_input_files = true; - for (const String input_filename : filenames) { + for (auto const& input_filename : filenames) { auto output_filename = String::formatted("{}.gz", input_filename); // We map the whole file instead of streaming to reduce size overhead (gzip header) and increase the deflate block size (better compression) diff --git a/Userland/Utilities/tar.cpp b/Userland/Utilities/tar.cpp index 3f2ea95562..7877787b37 100644 --- a/Userland/Utilities/tar.cpp +++ b/Userland/Utilities/tar.cpp @@ -175,7 +175,7 @@ int main(int argc, char** argv) } }; - for (const String path : paths) { + for (auto const& path : paths) { if (Core::File::is_directory(path)) { add_directory(path, add_directory); } else { diff --git a/Userland/Utilities/zip.cpp b/Userland/Utilities/zip.cpp index c15fc8fda8..0824e7c80a 100644 --- a/Userland/Utilities/zip.cpp +++ b/Userland/Utilities/zip.cpp @@ -16,7 +16,7 @@ int main(int argc, char** argv) { const char* zip_path; - Vector source_paths; + Vector source_paths; bool recurse = false; bool force = false; @@ -104,7 +104,7 @@ int main(int argc, char** argv) } }; - for (const String source_path : source_paths) { + for (auto const& source_path : source_paths) { if (Core::File::is_directory(source_path)) { add_directory(source_path, add_directory); } else {