diff options
| author | 2018-07-21 23:04:24 -0400 | |
|---|---|---|
| committer | 2018-07-21 23:08:55 -0400 | |
| commit | c5de0a67a82c641518590a12cf394eed4b85ccaf (patch) | |
| tree | beb7ef55d87872f810c19fd76aa3e6b0af620ea9 /src/common/file_util.cpp | |
| parent | Merge pull request #759 from lioncash/redundant (diff) | |
| download | yuzu-c5de0a67a82c641518590a12cf394eed4b85ccaf.tar.gz yuzu-c5de0a67a82c641518590a12cf394eed4b85ccaf.tar.xz yuzu-c5de0a67a82c641518590a12cf394eed4b85ccaf.zip | |
file_util: Remove goto usages from Copy()
We can just leverage std::unique_ptr to automatically close these for us
in error cases instead of jumping to the end of the function to call
fclose on them.
Diffstat (limited to 'src/common/file_util.cpp')
| -rw-r--r-- | src/common/file_util.cpp | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index d8163a4a8..819ffd6ff 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -2,6 +2,8 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <array> | ||
| 6 | #include <memory> | ||
| 5 | #include <sstream> | 7 | #include <sstream> |
| 6 | #include <unordered_map> | 8 | #include <unordered_map> |
| 7 | #include "common/assert.h" | 9 | #include "common/assert.h" |
| @@ -275,14 +277,10 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename) { | |||
| 275 | GetLastErrorMsg()); | 277 | GetLastErrorMsg()); |
| 276 | return false; | 278 | return false; |
| 277 | #else | 279 | #else |
| 278 | 280 | using CFilePointer = std::unique_ptr<FILE, decltype(&std::fclose)>; | |
| 279 | // buffer size | ||
| 280 | #define BSIZE 1024 | ||
| 281 | |||
| 282 | char buffer[BSIZE]; | ||
| 283 | 281 | ||
| 284 | // Open input file | 282 | // Open input file |
| 285 | FILE* input = fopen(srcFilename.c_str(), "rb"); | 283 | CFilePointer input{fopen(srcFilename.c_str(), "rb"), std::fclose}; |
| 286 | if (!input) { | 284 | if (!input) { |
| 287 | LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, | 285 | LOG_ERROR(Common_Filesystem, "opening input failed {} --> {}: {}", srcFilename, |
| 288 | destFilename, GetLastErrorMsg()); | 286 | destFilename, GetLastErrorMsg()); |
| @@ -290,44 +288,36 @@ bool Copy(const std::string& srcFilename, const std::string& destFilename) { | |||
| 290 | } | 288 | } |
| 291 | 289 | ||
| 292 | // open output file | 290 | // open output file |
| 293 | FILE* output = fopen(destFilename.c_str(), "wb"); | 291 | CFilePointer output{fopen(destFilename.c_str(), "wb"), std::fclose}; |
| 294 | if (!output) { | 292 | if (!output) { |
| 295 | fclose(input); | ||
| 296 | LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, | 293 | LOG_ERROR(Common_Filesystem, "opening output failed {} --> {}: {}", srcFilename, |
| 297 | destFilename, GetLastErrorMsg()); | 294 | destFilename, GetLastErrorMsg()); |
| 298 | return false; | 295 | return false; |
| 299 | } | 296 | } |
| 300 | 297 | ||
| 301 | // copy loop | 298 | // copy loop |
| 302 | while (!feof(input)) { | 299 | std::array<char, 1024> buffer; |
| 300 | while (!feof(input.get())) { | ||
| 303 | // read input | 301 | // read input |
| 304 | size_t rnum = fread(buffer, sizeof(char), BSIZE, input); | 302 | size_t rnum = fread(buffer.data(), sizeof(char), buffer.size(), input.get()); |
| 305 | if (rnum != BSIZE) { | 303 | if (rnum != buffer.size()) { |
| 306 | if (ferror(input) != 0) { | 304 | if (ferror(input.get()) != 0) { |
| 307 | LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", | 305 | LOG_ERROR(Common_Filesystem, "failed reading from source, {} --> {}: {}", |
| 308 | srcFilename, destFilename, GetLastErrorMsg()); | 306 | srcFilename, destFilename, GetLastErrorMsg()); |
| 309 | goto bail; | 307 | return false; |
| 310 | } | 308 | } |
| 311 | } | 309 | } |
| 312 | 310 | ||
| 313 | // write output | 311 | // write output |
| 314 | size_t wnum = fwrite(buffer, sizeof(char), rnum, output); | 312 | size_t wnum = fwrite(buffer.data(), sizeof(char), rnum, output.get()); |
| 315 | if (wnum != rnum) { | 313 | if (wnum != rnum) { |
| 316 | LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, | 314 | LOG_ERROR(Common_Filesystem, "failed writing to output, {} --> {}: {}", srcFilename, |
| 317 | destFilename, GetLastErrorMsg()); | 315 | destFilename, GetLastErrorMsg()); |
| 318 | goto bail; | 316 | return false; |
| 319 | } | 317 | } |
| 320 | } | 318 | } |
| 321 | // close files | 319 | |
| 322 | fclose(input); | ||
| 323 | fclose(output); | ||
| 324 | return true; | 320 | return true; |
| 325 | bail: | ||
| 326 | if (input) | ||
| 327 | fclose(input); | ||
| 328 | if (output) | ||
| 329 | fclose(output); | ||
| 330 | return false; | ||
| 331 | #endif | 321 | #endif |
| 332 | } | 322 | } |
| 333 | 323 | ||