From 725eb702b7f332fda95583fb042d7dae71819fdf Mon Sep 17 00:00:00 2001 From: Emanuele Torre Date: Mon, 11 Jan 2021 22:28:24 +0100 Subject: [PATCH] wc: Fix code style. `unsigned int` -> `unsigned`. Use brace initialisers instead of equal initialisers for struct members. Prefix global variables with `g_`. Wrap multi-line statements in curly braces. Also: Use const references instead of references when possible. Rename `file_name` to `file_specifier`: "-" is not a file name. Rename `files` to `file_specifiers`. Avoid some useless checks. --- Userland/wc.cpp | 78 +++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/Userland/wc.cpp b/Userland/wc.cpp index 708df9c96be94a..89a1cfd67561fe 100644 --- a/Userland/wc.cpp +++ b/Userland/wc.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Emanuele Torre * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -33,60 +34,61 @@ struct Count { String name; - bool exists = true; - unsigned int lines = 0; - unsigned int characters = 0; - unsigned int words = 0; - size_t bytes = 0; + bool exists { true }; + unsigned lines { 0 }; + unsigned characters { 0 }; + unsigned words { 0 }; + size_t bytes { 0 }; }; -bool output_line = false; -bool output_byte = false; -bool output_word = false; +bool g_output_line = false; +bool g_output_byte = false; +bool g_output_word = false; -static void wc_out(Count& count) +static void wc_out(const Count& count) { - if (output_line) + if (g_output_line) printf("%7i ", count.lines); - if (output_word) + if (g_output_word) printf("%7i ", count.words); - if (output_byte) + if (g_output_byte) printf("%7lu ", count.bytes); printf("%14s\n", count.name.characters()); } -static Count get_count(const String& file_name) +static Count get_count(const String& file_specifier) { Count count; FILE* file_pointer = nullptr; - if (file_name == "-") { + if (file_specifier == "-") { count.name = ""; file_pointer = stdin; } else { - count.name = file_name; - if ((file_pointer = fopen(file_name.characters(), "r")) == nullptr) { - fprintf(stderr, "wc: unable to open %s\n", file_name.characters()); + count.name = file_specifier; + if ((file_pointer = fopen(file_specifier.characters(), "r")) == nullptr) { + fprintf(stderr, "wc: unable to open %s\n", file_specifier.characters()); count.exists = false; return count; } } + bool start_a_new_word = true; for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) { count.bytes++; if (isspace(ch)) { start_a_new_word = true; + if (ch == '\n') + count.lines++; } else if (start_a_new_word) { start_a_new_word = false; count.words++; } - if (ch == '\n') - count.lines++; } return count; } -static Count get_total_count(Vector& counts) +static Count get_total_count(const Vector& counts) { Count total_count { "total" }; for (auto& count : counts) { @@ -105,42 +107,36 @@ int main(int argc, char** argv) return 1; } - Vector files; + Vector file_specifiers; Core::ArgsParser args_parser; - args_parser.add_option(output_line, "Output line count", "lines", 'l'); - args_parser.add_option(output_byte, "Output byte count", "bytes", 'c'); - args_parser.add_option(output_word, "Output word count", "words", 'w'); - args_parser.add_positional_argument(files, "File to process", "file", Core::ArgsParser::Required::No); + args_parser.add_option(g_output_line, "Output line count", "lines", 'l'); + args_parser.add_option(g_output_byte, "Output byte count", "bytes", 'c'); + args_parser.add_option(g_output_word, "Output word count", "words", 'w'); + args_parser.add_positional_argument(file_specifiers, "File to process", "file", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); - if (!output_line && !output_byte && !output_word) - output_line = output_byte = output_word = true; + if (!g_output_line && !g_output_byte && !g_output_word) + g_output_line = g_output_byte = g_output_word = true; Vector counts; - for (auto& file : files) { - Count count = get_count(file); - counts.append(count); - } + for (const auto& file_specifier : file_specifiers) + counts.append(get_count(file_specifier)); if (pledge("stdio", nullptr) < 0) { perror("pledge"); return 1; } - if (files.size() > 1) { - Count total_count = get_total_count(counts); - counts.append(total_count); - } - - if (files.is_empty()) { - Count count = get_count("-"); - counts.append(count); - } + if (file_specifiers.is_empty()) + counts.append(get_count("-")); + else if (file_specifiers.size() > 1) + counts.append(get_total_count(counts)); - for (auto& count : counts) + for (const auto& count : counts) { if (count.exists) wc_out(count); + } return 0; }