Skip to content

Commit

Permalink
Utilize return guards over if-else nests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightkingale committed May 12, 2024
1 parent 72862e5 commit b973d16
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 94 deletions.
115 changes: 56 additions & 59 deletions source/backup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ write_backup(FILE* account, const std::string& backup_path, char* buffer)

// Open the backup file for writing.
FILE *backup = fopen(backup_path.c_str(), "wb");

if (backup == NULL) {
draw_error_menu("Error opening backup account.dat file!");
handle_cleanup(account, backup, buffer, true);
Expand Down Expand Up @@ -90,71 +91,67 @@ backup_account()
draw_error_menu("Error opening system account.dat file!");
handle_cleanup(account, NULL, NULL, true);
return false;
}

} else {
std::string content;
char *buffer = (char *)malloc(BUFFER_SIZE);
if (buffer == NULL) {
draw_error_menu("Error allocating memory!");
handle_cleanup(account, NULL, buffer, true);
return false;

} else {
// Read the entire file into a string.
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0) {
content.append(buffer, bytesRead);
}
std::string content;
char *buffer = (char *)malloc(BUFFER_SIZE);
if (buffer == NULL) {
draw_error_menu("Error allocating memory!");
handle_cleanup(account, NULL, buffer, true);
return false;
}

bool network_account_found = false;
if (content.find("account.nintendo.net") != std::string::npos) {
// Nintendo Network ID is linked to the account.
backup_path = NNID_BACKUP;
network_account_found = true;

} else if (content.find("pretendo-cdn.b-cdn.net") != std::string::npos) {
// Pretendo Network ID is linked to the account.
backup_path = PNID_BACKUP;
network_account_found = true;

} else {
// The check failed, domain not accounted for?
draw_error_menu("No network account found!");
handle_cleanup(account, NULL, buffer, true);
return false;
}
// Read the entire file into a string.
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0) {
content.append(buffer, bytesRead);
}

bool network_account_found = false;
if (content.find("account.nintendo.net") != std::string::npos) {
// Nintendo Network ID is linked to the account.
backup_path = NNID_BACKUP;
network_account_found = true;
} else if (content.find("pretendo-cdn.b-cdn.net") != std::string::npos) {
// Pretendo Network ID is linked to the account.
backup_path = PNID_BACKUP;
network_account_found = true;
}

if (network_account_found) {
// Check if the backup file exists.
std::ifstream ifile(backup_path);
if (ifile) {
backup_confirm = false;

while (WHBProcIsRunning()) {
draw_overwrite_menu(backup_path.c_str());
int button = read_input();

if (button == VPAD_BUTTON_A) {
backup_confirm = true;
break;
} else if (button == VPAD_BUTTON_B) {
break;
}
}

} else {
backup_confirm = true;
}
if (!network_account_found) {
// The check failed, domain not accounted for?
draw_error_menu("No network account found!");
handle_cleanup(account, NULL, buffer, true);
return false;
}

// Check if the backup file exists.
std::ifstream ifile(backup_path);
if (ifile) {
backup_confirm = false;

while (WHBProcIsRunning()) {
draw_overwrite_menu(backup_path.c_str());
int button = read_input();

if (button == VPAD_BUTTON_A) {
backup_confirm = true;
break;
} else if (button == VPAD_BUTTON_B) {
break;
}
}
// Write the backup file.
if (backup_confirm) {
if (write_backup(account, backup_path, buffer))
handle_cleanup(account, NULL, buffer, false);
return true;
}
} else {
backup_confirm = true;
}

handle_cleanup(account, NULL, buffer, !backup_confirm);
// Write the backup file.
if (backup_confirm) {
if (write_backup(account, backup_path, buffer))
handle_cleanup(account, NULL, buffer, false);
return true;
}

handle_cleanup(account, NULL, buffer, !backup_confirm);
return true;
}
68 changes: 33 additions & 35 deletions source/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,42 @@ switch_account(const char* backup_file, const char* account_type)
handle_cleanup(backup, account_type, NULL, true);
return false;
}
else {
// Open the account.dat file for writing.
char *buffer = (char *)malloc(BUFFER_SIZE);
if (buffer == NULL) {
draw_error_menu("Error allocating memory!");
handle_cleanup(backup, account_type, buffer, true);
return false;

} else {
FILE *account = fopen(ACCOUNT_FILE.c_str(), "wb");
if (account == NULL) {
draw_error_menu("Error opening system account.dat file!");
handle_cleanup(backup, account_type, buffer, true);
return false;
// Open the account.dat file for writing.
char *buffer = (char *)malloc(BUFFER_SIZE);
if (buffer == NULL) {
draw_error_menu("Error allocating memory!");
handle_cleanup(backup, account_type, buffer, true);
return false;
}

} else {
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0)
fwrite(buffer, 1, bytesRead, account);
fclose(account);
FILE *account = fopen(ACCOUNT_FILE.c_str(), "wb");
if (account == NULL) {
draw_error_menu("Error opening system account.dat file!");
handle_cleanup(backup, account_type, buffer, true);
return false;
}

// We'll attempt to automatically swap the network using Inkay's configuration.
bool inkay_configured = false;
FILE *inkay = fopen(INKAY_CONFIG.c_str(), "wb");
if (inkay != NULL) {
// Write the network configuration to the file.
const char *inkay_content = "{\"storageitems\":{\"connect_to_network\":%d}}";
fprintf(inkay, inkay_content, strcmp(account_type, "Pretendo Network ID") == 0 ? 1 : 0);
fclose(inkay);
inkay = NULL;
inkay_configured = true;
}
draw_success_menu("switch", inkay_configured);
}
}
// Clean-up and exit.
handle_cleanup(backup, account_type, buffer, false);
size_t bytesRead = 0;
while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0)
fwrite(buffer, 1, bytesRead, account);
fclose(account);

return true;
// We'll attempt to automatically swap the network using Inkay's configuration.
bool inkay_configured = false;
FILE *inkay = fopen(INKAY_CONFIG.c_str(), "wb");
if (inkay != NULL) {
// Write the network configuration to the file.
const char *inkay_content = "{\"storageitems\":{\"connect_to_network\":%d}}";
fprintf(inkay, inkay_content, strcmp(account_type, "Pretendo Network ID") == 0 ? 1 : 0);
fclose(inkay);
inkay = NULL;
inkay_configured = true;
}
draw_success_menu("switch", inkay_configured);

// Clean-up and exit.
handle_cleanup(backup, account_type, buffer, false);

return true;
}

0 comments on commit b973d16

Please sign in to comment.