summaryrefslogtreecommitdiff
path: root/src/common/file_util.cpp
diff options
context:
space:
mode:
authorGravatar Vitor K2020-03-25 16:33:37 -0300
committerGravatar FearlessTobi2020-04-01 02:58:42 +0200
commitbd0c56c6e7ad506d7123c7e3aca85bd037275a5c (patch)
tree62310f3ebd52770cbf3ff8313c03be889a4e83c5 /src/common/file_util.cpp
parentMerge pull request #3565 from ReinUsesLisp/image-format (diff)
downloadyuzu-bd0c56c6e7ad506d7123c7e3aca85bd037275a5c.tar.gz
yuzu-bd0c56c6e7ad506d7123c7e3aca85bd037275a5c.tar.xz
yuzu-bd0c56c6e7ad506d7123c7e3aca85bd037275a5c.zip
common: Port some changes from dolphin (#5127)
* IOFile: Make the move constructor and move assignment operator noexcept Certain parts of the standard library try to determine whether or not a transfer operation should either be a copy or a move. The prevalent notion of move constructors/assignment operators is that they should not throw, they simply move an already existing resource somewhere else. This is typically done with 'std::move_if_noexcept'. Like the name says, if a type's move constructor is noexcept, then the functions retrieves an r-value reference (for move semantics), or an l-value (for copy semantics) if it is not noexcept. As IOFile deletes the copy constructor and copy assignment operators, using IOFile with certain parts of the standard library can fail in unexcepted ways (especially when used with various container implementations). This prevents that. * fix various instances of -1 being assigned to unsigned types * do not assign in conditional statements * File/IOFile: Check _tfopen_s properly * common/file_util.cpp: address review comments Co-authored-by: Lioncash <mathew1800@gmail.com> Co-authored-by: Shawn Hoffman <godisgovernment@gmail.com> Co-authored-by: Sepalani <sepalani@hotmail.fr>
Diffstat (limited to 'src/common/file_util.cpp')
-rw-r--r--src/common/file_util.cpp22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp
index 41167f57a..35eee0096 100644
--- a/src/common/file_util.cpp
+++ b/src/common/file_util.cpp
@@ -3,6 +3,7 @@
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <array> 5#include <array>
6#include <limits>
6#include <memory> 7#include <memory>
7#include <sstream> 8#include <sstream>
8#include <unordered_map> 9#include <unordered_map>
@@ -530,11 +531,11 @@ void CopyDir(const std::string& source_path, const std::string& dest_path) {
530std::optional<std::string> GetCurrentDir() { 531std::optional<std::string> GetCurrentDir() {
531// Get the current working directory (getcwd uses malloc) 532// Get the current working directory (getcwd uses malloc)
532#ifdef _WIN32 533#ifdef _WIN32
533 wchar_t* dir; 534 wchar_t* dir = _wgetcwd(nullptr, 0);
534 if (!(dir = _wgetcwd(nullptr, 0))) { 535 if (!dir) {
535#else 536#else
536 char* dir; 537 char* dir = getcwd(nullptr, 0);
537 if (!(dir = getcwd(nullptr, 0))) { 538 if (!dir) {
538#endif 539#endif
539 LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg()); 540 LOG_ERROR(Common_Filesystem, "GetCurrentDirectory failed: {}", GetLastErrorMsg());
540 return {}; 541 return {};
@@ -918,19 +919,22 @@ void IOFile::Swap(IOFile& other) noexcept {
918 919
919bool IOFile::Open(const std::string& filename, const char openmode[], int flags) { 920bool IOFile::Open(const std::string& filename, const char openmode[], int flags) {
920 Close(); 921 Close();
922 bool m_good;
921#ifdef _WIN32 923#ifdef _WIN32
922 if (flags != 0) { 924 if (flags != 0) {
923 m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(), 925 m_file = _wfsopen(Common::UTF8ToUTF16W(filename).c_str(),
924 Common::UTF8ToUTF16W(openmode).c_str(), flags); 926 Common::UTF8ToUTF16W(openmode).c_str(), flags);
927 m_good = m_file != nullptr;
925 } else { 928 } else {
926 _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(), 929 m_good = _wfopen_s(&m_file, Common::UTF8ToUTF16W(filename).c_str(),
927 Common::UTF8ToUTF16W(openmode).c_str()); 930 Common::UTF8ToUTF16W(openmode).c_str()) == 0;
928 } 931 }
929#else 932#else
930 m_file = fopen(filename.c_str(), openmode); 933 m_file = std::fopen(filename.c_str(), openmode);
934 m_good = m_file != nullptr;
931#endif 935#endif
932 936
933 return IsOpen(); 937 return m_good;
934} 938}
935 939
936bool IOFile::Close() { 940bool IOFile::Close() {
@@ -956,7 +960,7 @@ u64 IOFile::Tell() const {
956 if (IsOpen()) 960 if (IsOpen())
957 return ftello(m_file); 961 return ftello(m_file);
958 962
959 return -1; 963 return std::numeric_limits<u64>::max();
960} 964}
961 965
962bool IOFile::Flush() { 966bool IOFile::Flush() {