Skip to content

Commit

Permalink
bin2pat: Improve returning of values from functions.
Browse files Browse the repository at this point in the history
After #474, printErrorAndDie() no longer returns anything, so we can change the
return value from `int` to `void` and reflect this change in other parts of the
code.

Also, add return statements after terminating functions to improve the code
readability.
  • Loading branch information
s3rvac committed Jan 28, 2019
1 parent f533636 commit c937fcb
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/bin2pat/bin2pat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@ void printUsage(
<< " If multiple notes are given, only last one is used.\n\n";
}

int printErrorAndDie(
void printErrorAndDie(
const std::string &message)
{
std::cerr << "Error: " << message << ".\n";
printUsage(std::cerr);
std::exit(1);
}

int needValue(
void needValue(
const std::string &arg)
{
return printErrorAndDie("argument " + arg + " requires value");
printErrorAndDie("argument " + arg + " requires value");
}

int processArgs(
void processArgs(
const std::vector<std::string> &args)
{
std::string note;
Expand All @@ -61,29 +61,32 @@ int processArgs(
for (std::size_t i = 0, e = args.size(); i < e; ++i) {
if (args[i] == "--help" || args[i] == "-h") {
printUsage(std::cout);
return 0;
return;
}
else if (args[i] == "-o" || args[i] == "--output") {
if (i + 1 < e) {
outPath = args[++i];
}
else {
return needValue(args[i]);
needValue(args[i]);
return;
}
}
else if (args[i] == "-n" || args[i] == "--note") {
if (i + 1 < e) {
note = args[++i];
}
else {
return needValue(args[i]);
needValue(args[i]);
return;
}
}
else {
// Input file. Check file on system level.
if(!FilesystemPath(args[i]).isFile()) {
printErrorAndDie("argument '" + args[i]
+ "' is neither valid file nor argument");
return;
}
else {
inPaths.push_back(args[i]);
Expand All @@ -92,7 +95,8 @@ int processArgs(
}

if (inPaths.empty()) {
return printErrorAndDie("input files missing");
printErrorAndDie("input files missing");
return;
}

// Prepare builder.
Expand Down Expand Up @@ -131,25 +135,26 @@ int processArgs(
// Check processing results.
if (!atLeastOne) {
printErrorAndDie("no valid files were processed");
return;
}

// Print results.
if (!outPath.empty()) {
std::ofstream outputFile(outPath);
if (!outputFile) {
printErrorAndDie("could not open output file");
return;
}
outputFile << builder.get(false)->getText() << "\n";
}
else {
std::cout << builder.get(false)->getText() << "\n";
}

return 0;
}

int main(int argc, char *argv[])
{
std::vector<std::string> args(argv + 1, argv + argc);
return processArgs(args);
processArgs(args);
return 0;
}

0 comments on commit c937fcb

Please sign in to comment.