Skip to content

Commit af0c5f6

Browse files
committed
actually fix Rule 15.5 ..
1 parent 2bc4d4c commit af0c5f6

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

src/binary.hpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@
2121

2222
/**
2323
* @brief Look for a bit pattern that set 16:9 aspect ratio for Tomb Raider 4.
24-
*
24+
* @param[in] Open file object reference.
2525
* @retval 0 Success.
2626
* @retval 1 Pattern was not found in the file.
2727
* @retval 2 Could not open the file.
2828
* @retval 3 Could not write to the file.
29-
*
3029
* @return error qint64.
3130
*/
3231
qint64 findReplacePattern(QFile* const file) {
33-
qint64 status = 0; // Initialize the status variable
32+
qint64 status = 0;
3433
QByteArray fileContent = file->readAll();
3534
file->close();
3635

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

4140
// Find the pattern in the file content
4241
int index = fileContent.indexOf(pattern);
43-
if (index == -1) {
44-
qDebug() << "Pattern not found in the file.";
45-
return 1; // Pattern not found
46-
}
47-
48-
// Replace the pattern
49-
fileContent.replace(index, pattern.size(), replacement);
42+
if (index != -1) {
43+
// Replace the pattern
44+
fileContent.replace(index, pattern.size(), replacement);
5045

51-
// Reopen the file for writing
52-
if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore
53-
qCritical() << "Error opening file for writing!";
54-
status = 2;
55-
} else if (file->write(fileContent) == -1) {
56-
qCritical() << "Error writing to file!";
57-
status = 3;
46+
// Reopen the file for writing
47+
if (!file->open(QIODevice::WriteOnly)) { // flawfinder: ignore
48+
qCritical() << "Error opening file for writing!";
49+
status = 2;
50+
} else if (file->write(fileContent) == -1) {
51+
qCritical() << "Error writing to file!";
52+
status = 3;
53+
} else {
54+
qDebug() << "Widescreen patch applied successfully!";
55+
}
5856
} else {
59-
qDebug() << "Widescreen patch applied successfully!";
57+
qDebug() << "Pattern not found in the file.";
58+
status = 1;
6059
}
61-
6260
file->close();
6361
return status;
6462
}
6563

6664
/**
6765
* @brief Widescreen in binary set function.
68-
*
66+
* @param[in] File path to windows exe.
6967
* @retval 0 Success.
7068
* @retval 1 Path was not an safe file regular file.
7169
* @retval 2 Could not preform the first read only opening of the file.
72-
*
7370
* @return error qint64.
7471
*/
7572
qint64 widescreen_set(const QString& path) {
7673
qint64 status = 0; // Initialize the status variable
77-
78-
// Validate the file path
7974
QFileInfo fileInfo(path);
75+
QFile file(path);
76+
77+
// Open the file
8078
if (!fileInfo.exists() || !fileInfo.isFile()) {
8179
qCritical() << "Error: The exe path is not a regular file: " << path;
8280
return 1; // Invalid file path
83-
}
84-
85-
// Open the file for reading in binary mode
86-
QFile file(path);
87-
if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore
81+
} else if (!file.open(QIODevice::ReadOnly)) { // flawfinder: ignore
8882
qCritical() << "Error opening file for reading!";
8983
return 2; // File open error
84+
} else {
85+
// Perform the pattern replacement and propagate the status
86+
status = findReplacePattern(&file);
9087
}
91-
92-
// Perform the pattern replacement and propagate the status
93-
status = findReplacePattern(&file);
94-
95-
return status; // Single return point
88+
return status;
9689
}
9790

9891
#endif // SRC_BINARY_HPP_

0 commit comments

Comments
 (0)