Skip to content

Commit

Permalink
actually fix Rule 15.5 ..
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Dec 14, 2024
1 parent 2bc4d4c commit af0c5f6
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions src/binary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,15 @@

/**
* @brief Look for a bit pattern that set 16:9 aspect ratio for Tomb Raider 4.
*
* @param[in] Open file object reference.
* @retval 0 Success.
* @retval 1 Pattern was not found in the file.
* @retval 2 Could not open the file.
* @retval 3 Could not write to the file.
*
* @return error qint64.
*/
qint64 findReplacePattern(QFile* const file) {
qint64 status = 0; // Initialize the status variable
qint64 status = 0;
QByteArray fileContent = file->readAll();
file->close();

Expand All @@ -40,59 +39,53 @@ qint64 findReplacePattern(QFile* const file) {

// Find the pattern in the file content
int index = fileContent.indexOf(pattern);
if (index == -1) {
qDebug() << "Pattern not found in the file.";
return 1; // Pattern not found
}

// Replace the pattern
fileContent.replace(index, pattern.size(), replacement);
if (index != -1) {
// Replace the pattern
fileContent.replace(index, pattern.size(), replacement);

// Reopen the file for writing
if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore
qCritical() << "Error opening file for writing!";
status = 2;
} else if (file->write(fileContent) == -1) {
qCritical() << "Error writing to file!";
status = 3;
// Reopen the file for writing
if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore
qCritical() << "Error opening file for writing!";
status = 2;
} else if (file->write(fileContent) == -1) {
qCritical() << "Error writing to file!";
status = 3;
} else {
qDebug() << "Widescreen patch applied successfully!";
}
} else {
qDebug() << "Widescreen patch applied successfully!";
qDebug() << "Pattern not found in the file.";
status = 1;
}

file->close();
return status;
}

/**
* @brief Widescreen in binary set function.
*
* @param[in] File path to windows exe.
* @retval 0 Success.
* @retval 1 Path was not an safe file regular file.
* @retval 2 Could not preform the first read only opening of the file.
*
* @return error qint64.
*/
qint64 widescreen_set(const QString& path) {
qint64 status = 0; // Initialize the status variable

// Validate the file path
QFileInfo fileInfo(path);
QFile file(path);

// Open the file
if (!fileInfo.exists() || !fileInfo.isFile()) {
qCritical() << "Error: The exe path is not a regular file: " << path;
return 1; // Invalid file path
}

// Open the file for reading in binary mode
QFile file(path);
if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore
} else if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore
qCritical() << "Error opening file for reading!";
return 2; // File open error
} else {
// Perform the pattern replacement and propagate the status
status = findReplacePattern(&file);
}

// Perform the pattern replacement and propagate the status
status = findReplacePattern(&file);

return status; // Single return point
return status;
}

#endif // SRC_BINARY_HPP_

0 comments on commit af0c5f6

Please sign in to comment.