1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:17:45 +00:00

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.
This commit is contained in:
Emanuele Torre 2021-01-11 22:28:24 +01:00 committed by Andreas Kling
parent 24d6814ee5
commit 725eb702b7

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Emanuele Torre <torreemanuele6@gmail.com>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -33,60 +34,61 @@
struct Count { struct Count {
String name; String name;
bool exists = true; bool exists { true };
unsigned int lines = 0; unsigned lines { 0 };
unsigned int characters = 0; unsigned characters { 0 };
unsigned int words = 0; unsigned words { 0 };
size_t bytes = 0; size_t bytes { 0 };
}; };
bool output_line = false; bool g_output_line = false;
bool output_byte = false; bool g_output_byte = false;
bool output_word = 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); printf("%7i ", count.lines);
if (output_word) if (g_output_word)
printf("%7i ", count.words); printf("%7i ", count.words);
if (output_byte) if (g_output_byte)
printf("%7lu ", count.bytes); printf("%7lu ", count.bytes);
printf("%14s\n", count.name.characters()); printf("%14s\n", count.name.characters());
} }
static Count get_count(const String& file_name) static Count get_count(const String& file_specifier)
{ {
Count count; Count count;
FILE* file_pointer = nullptr; FILE* file_pointer = nullptr;
if (file_name == "-") { if (file_specifier == "-") {
count.name = ""; count.name = "";
file_pointer = stdin; file_pointer = stdin;
} else { } else {
count.name = file_name; count.name = file_specifier;
if ((file_pointer = fopen(file_name.characters(), "r")) == nullptr) { if ((file_pointer = fopen(file_specifier.characters(), "r")) == nullptr) {
fprintf(stderr, "wc: unable to open %s\n", file_name.characters()); fprintf(stderr, "wc: unable to open %s\n", file_specifier.characters());
count.exists = false; count.exists = false;
return count; return count;
} }
} }
bool start_a_new_word = true; bool start_a_new_word = true;
for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) { for (int ch = fgetc(file_pointer); ch != EOF; ch = fgetc(file_pointer)) {
count.bytes++; count.bytes++;
if (isspace(ch)) { if (isspace(ch)) {
start_a_new_word = true; start_a_new_word = true;
if (ch == '\n')
count.lines++;
} else if (start_a_new_word) { } else if (start_a_new_word) {
start_a_new_word = false; start_a_new_word = false;
count.words++; count.words++;
} }
if (ch == '\n')
count.lines++;
} }
return count; return count;
} }
static Count get_total_count(Vector<Count>& counts) static Count get_total_count(const Vector<Count>& counts)
{ {
Count total_count { "total" }; Count total_count { "total" };
for (auto& count : counts) { for (auto& count : counts) {
@ -105,42 +107,36 @@ int main(int argc, char** argv)
return 1; return 1;
} }
Vector<const char*> files; Vector<const char*> file_specifiers;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_option(output_line, "Output line count", "lines", 'l'); args_parser.add_option(g_output_line, "Output line count", "lines", 'l');
args_parser.add_option(output_byte, "Output byte count", "bytes", 'c'); args_parser.add_option(g_output_byte, "Output byte count", "bytes", 'c');
args_parser.add_option(output_word, "Output word count", "words", 'w'); args_parser.add_option(g_output_word, "Output word count", "words", 'w');
args_parser.add_positional_argument(files, "File to process", "file", Core::ArgsParser::Required::No); args_parser.add_positional_argument(file_specifiers, "File to process", "file", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(argc, argv);
if (!output_line && !output_byte && !output_word) if (!g_output_line && !g_output_byte && !g_output_word)
output_line = output_byte = output_word = true; g_output_line = g_output_byte = g_output_word = true;
Vector<Count> counts; Vector<Count> counts;
for (auto& file : files) { for (const auto& file_specifier : file_specifiers)
Count count = get_count(file); counts.append(get_count(file_specifier));
counts.append(count);
}
if (pledge("stdio", nullptr) < 0) { if (pledge("stdio", nullptr) < 0) {
perror("pledge"); perror("pledge");
return 1; return 1;
} }
if (files.size() > 1) { if (file_specifiers.is_empty())
Count total_count = get_total_count(counts); counts.append(get_count("-"));
counts.append(total_count); else if (file_specifiers.size() > 1)
} counts.append(get_total_count(counts));
if (files.is_empty()) { for (const auto& count : counts) {
Count count = get_count("-");
counts.append(count);
}
for (auto& count : counts)
if (count.exists) if (count.exists)
wc_out(count); wc_out(count);
}
return 0; return 0;
} }