From eb475025b85686f4793bb83bac377033c17fbd2d Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 28 May 2019 17:45:36 -0400 Subject: [PATCH 1/3] Common/FileUtil: Make WriteStringToFile consistent with ReadFileToString Makes the parameter ordering consistent and less error-prone. --- Source/Core/Common/FileUtil.cpp | 2 +- Source/Core/Common/FileUtil.h | 2 +- Source/Core/Core/DSP/DSPCodeUtil.cpp | 4 ++-- Source/Core/UICommon/GameFile.cpp | 2 +- Source/Core/UpdaterCommon/UpdaterCommon.cpp | 6 +++--- Source/DSPTool/DSPTool.cpp | 10 +++++----- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 7030c9dc25..826d430b62 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -887,7 +887,7 @@ std::string GetThemeDir(const std::string& theme_name) return GetSysDirectory() + THEMES_DIR "/" DEFAULT_THEME_DIR "/"; } -bool WriteStringToFile(const std::string& str, const std::string& filename) +bool WriteStringToFile(const std::string& filename, const std::string& str) { return File::IOFile(filename, "wb").WriteBytes(str.data(), str.size()); } diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index 71e9709da3..cccb28e218 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -198,7 +198,7 @@ std::string GetBundleDirectory(); std::string GetExePath(); std::string GetExeDirectory(); -bool WriteStringToFile(const std::string& str, const std::string& filename); +bool WriteStringToFile(const std::string& filename, const std::string& str); bool ReadFileToString(const std::string& filename, std::string& str); // To deal with Windows being dumb at unicode: diff --git a/Source/Core/Core/DSP/DSPCodeUtil.cpp b/Source/Core/Core/DSP/DSPCodeUtil.cpp index ed2c38d9e3..d71da68c14 100644 --- a/Source/Core/Core/DSP/DSPCodeUtil.cpp +++ b/Source/Core/Core/DSP/DSPCodeUtil.cpp @@ -141,7 +141,7 @@ bool SaveBinary(const std::vector& code, const std::string& filename) { const std::string buffer = CodeToBinaryStringBE(code); - return File::WriteStringToFile(buffer, filename); + return File::WriteStringToFile(filename, buffer); } bool DumpDSPCode(const u8* code_be, int size_in_bytes, u32 crc) @@ -166,7 +166,7 @@ bool DumpDSPCode(const u8* code_be, int size_in_bytes, u32 crc) if (!Disassemble(code, true, text)) return false; - return File::WriteStringToFile(text, text_file); + return File::WriteStringToFile(text_file, text); } } // namespace DSP diff --git a/Source/Core/UICommon/GameFile.cpp b/Source/Core/UICommon/GameFile.cpp index 4e5ad6f9c2..fb8c3d0a67 100644 --- a/Source/Core/UICommon/GameFile.cpp +++ b/Source/Core/UICommon/GameFile.cpp @@ -265,7 +265,7 @@ void GameFile::DownloadDefaultCover() if (!response) return; - File::WriteStringToFile(std::string(response->begin(), response->end()), png_path); + File::WriteStringToFile(png_path, std::string(response->begin(), response->end())); } bool GameFile::DefaultCoverChanged() diff --git a/Source/Core/UpdaterCommon/UpdaterCommon.cpp b/Source/Core/UpdaterCommon/UpdaterCommon.cpp index 77fb317040..9ec2c79edb 100644 --- a/Source/Core/UpdaterCommon/UpdaterCommon.cpp +++ b/Source/Core/UpdaterCommon/UpdaterCommon.cpp @@ -255,7 +255,7 @@ bool DownloadContent(const std::vector& to_download, std::optional maybe_decompressed = GzipInflate(contents); if (!maybe_decompressed) return false; - std::string decompressed = std::move(*maybe_decompressed); + const std::string decompressed = std::move(*maybe_decompressed); // Check that the downloaded contents have the right hash. Manifest::Hash contents_hash = ComputeHash(decompressed); @@ -265,8 +265,8 @@ bool DownloadContent(const std::vector& to_download, return false; } - std::string out = temp_path + DIR_SEP + hash_filename; - if (!File::WriteStringToFile(decompressed, out)) + const std::string out = temp_path + DIR_SEP + hash_filename; + if (!File::WriteStringToFile(out, decompressed)) { fprintf(log_fp, "Could not write cache file %s.\n", out.c_str()); return false; diff --git a/Source/DSPTool/DSPTool.cpp b/Source/DSPTool/DSPTool.cpp index 0c02a617dd..618c97e7af 100644 --- a/Source/DSPTool/DSPTool.cpp +++ b/Source/DSPTool/DSPTool.cpp @@ -217,7 +217,7 @@ static void PrintResults(const std::string& input_name, const std::string& outpu if (output_name.empty()) printf("%s", results.c_str()); else - File::WriteStringToFile(results, output_name.c_str()); + File::WriteStringToFile(output_name, results); } static bool PerformDisassembly(const std::string& input_name, const std::string& output_name) @@ -237,7 +237,7 @@ static bool PerformDisassembly(const std::string& input_name, const std::string& if (output_name.empty()) printf("%s", text.c_str()); else - File::WriteStringToFile(text, output_name); + File::WriteStringToFile(output_name, text); printf("Disassembly completed successfully!\n"); return true; @@ -314,7 +314,7 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou } const std::string header = CodesToHeader(codes, files); - File::WriteStringToFile(header, output_header_name + ".h"); + File::WriteStringToFile(output_header_name + ".h", header); } else { @@ -334,12 +334,12 @@ static bool PerformAssembly(const std::string& input_name, const std::string& ou if (!output_name.empty()) { const std::string binary_code = DSP::CodeToBinaryStringBE(code); - File::WriteStringToFile(binary_code, output_name); + File::WriteStringToFile(output_name, binary_code); } if (!output_header_name.empty()) { const std::string header = CodeToHeader(code, input_name); - File::WriteStringToFile(header, output_header_name + ".h"); + File::WriteStringToFile(output_header_name + ".h", header); } } } From c0f499b7f7e84e490d20f3b999714f3f5b43544e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 28 May 2019 17:47:26 -0400 Subject: [PATCH 2/3] Common/FileUtil: Use std::string_view with WriteStringToFile Allows writing out other forms of strings (e.g. C strings) without the need to allocate a std::string and discard it after use. --- Source/Core/Common/FileUtil.cpp | 2 +- Source/Core/Common/FileUtil.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 826d430b62..01f4417e1d 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -887,7 +887,7 @@ std::string GetThemeDir(const std::string& theme_name) return GetSysDirectory() + THEMES_DIR "/" DEFAULT_THEME_DIR "/"; } -bool WriteStringToFile(const std::string& filename, const std::string& str) +bool WriteStringToFile(const std::string& filename, std::string_view str) { return File::IOFile(filename, "wb").WriteBytes(str.data(), str.size()); } diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index cccb28e218..57e645c285 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -198,7 +199,7 @@ std::string GetBundleDirectory(); std::string GetExePath(); std::string GetExeDirectory(); -bool WriteStringToFile(const std::string& filename, const std::string& str); +bool WriteStringToFile(const std::string& filename, std::string_view str); bool ReadFileToString(const std::string& filename, std::string& str); // To deal with Windows being dumb at unicode: From 734667998647e21b8ffec9a6ec4e32253af1a9d5 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Tue, 28 May 2019 17:49:54 -0400 Subject: [PATCH 3/3] Common/FileUtil: Use std::string::data within ReadFileToString With C++17, .data() for std::string now has a non-const overload, so we can make use of that instead of taking the address of the first element. --- Source/Core/Common/FileUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 01f4417e1d..75118bffac 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -900,7 +900,7 @@ bool ReadFileToString(const std::string& filename, std::string& str) return false; str.resize(file.GetSize()); - return file.ReadArray(&str[0], str.size()); + return file.ReadArray(str.data(), str.size()); } } // namespace File