Skip to content

Commit

Permalink
wc: Fix code style.
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
emanuele6 authored and awesomekling committed Jan 12, 2021
1 parent 24d6814 commit 725eb70
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions Userland/wc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, Emanuele Torre <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -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<Count>& counts)
static Count get_total_count(const Vector<Count>& counts)
{
Count total_count { "total" };
for (auto& count : counts) {
Expand All @@ -105,42 +107,36 @@ int main(int argc, char** argv)
return 1;
}

Vector<const char*> files;
Vector<const char*> 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<Count> 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;
}

0 comments on commit 725eb70

Please sign in to comment.