diff options
| author | Léo Lam <leolino.lam@gmail.com> | 2018-03-26 12:46:16 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-03-26 12:46:16 +0200 |
| commit | eb489c0a5ecc077f6c59d3bf10bd112981e95445 (patch) | |
| tree | 1c2ae0545826b57799b41ff6da56c52a13f1e885 /Source/Core | |
| parent | 3272fda3ee277fe625c7d470b687dd947df63fd6 (diff) | |
| parent | 0f256276144f456ecc492363a44f444e3a6eeac4 (diff) | |
Merge pull request #6529 from lioncash/utils
DSPCodeUtil: Don't return data via an out parameter
Diffstat (limited to 'Source/Core')
| -rw-r--r-- | Source/Core/Core/DSP/DSPCodeUtil.cpp | 29 | ||||
| -rw-r--r-- | Source/Core/Core/DSP/DSPCodeUtil.h | 7 |
2 files changed, 20 insertions, 16 deletions
diff --git a/Source/Core/Core/DSP/DSPCodeUtil.cpp b/Source/Core/Core/DSP/DSPCodeUtil.cpp index b33dcb416f..8bafaac698 100644 --- a/Source/Core/Core/DSP/DSPCodeUtil.cpp +++ b/Source/Core/Core/DSP/DSPCodeUtil.cpp @@ -102,42 +102,45 @@ bool Compare(const std::vector<u16>& code1, const std::vector<u16>& code2) return code1.size() == code2.size() && code1.size() == count_equal; } -void CodeToBinaryStringBE(const std::vector<u16>& code, std::string& str) +std::string CodeToBinaryStringBE(const std::vector<u16>& code) { - str.resize(code.size() * 2); + std::string str(code.size() * 2, '\0'); + for (size_t i = 0; i < code.size(); i++) { str[i * 2 + 0] = code[i] >> 8; str[i * 2 + 1] = code[i] & 0xff; } + + return str; } -void BinaryStringBEToCode(const std::string& str, std::vector<u16>& code) +std::vector<u16> BinaryStringBEToCode(const std::string& str) { - code.resize(str.size() / 2); + std::vector<u16> code(str.size() / 2); + for (size_t i = 0; i < code.size(); i++) { code[i] = ((u16)(u8)str[i * 2 + 0] << 8) | ((u16)(u8)str[i * 2 + 1]); } + + return code; } -bool LoadBinary(const std::string& filename, std::vector<u16>& code) +std::optional<std::vector<u16>> LoadBinary(const std::string& filename) { std::string buffer; if (!File::ReadFileToString(filename, buffer)) - return false; + return std::nullopt; - BinaryStringBEToCode(buffer, code); - return true; + return std::make_optional(BinaryStringBEToCode(buffer)); } bool SaveBinary(const std::vector<u16>& code, const std::string& filename) { - std::string buffer; - CodeToBinaryStringBE(code, buffer); - if (!File::WriteStringToFile(buffer, filename)) - return false; - return true; + const std::string buffer = CodeToBinaryStringBE(code); + + return File::WriteStringToFile(buffer, filename); } bool DumpDSPCode(const u8* code_be, int size_in_bytes, u32 crc) diff --git a/Source/Core/Core/DSP/DSPCodeUtil.h b/Source/Core/Core/DSP/DSPCodeUtil.h index 08dc8edd2d..3d20ffa9cf 100644 --- a/Source/Core/Core/DSP/DSPCodeUtil.h +++ b/Source/Core/Core/DSP/DSPCodeUtil.h @@ -4,6 +4,7 @@ #pragma once +#include <optional> #include <string> #include <vector> @@ -16,11 +17,11 @@ bool Disassemble(const std::vector<u16>& code, bool line_numbers, std::string& t bool Compare(const std::vector<u16>& code1, const std::vector<u16>& code2); // Big-endian, for writing straight to file using File::WriteStringToFile. -void CodeToBinaryStringBE(const std::vector<u16>& code, std::string& str); -void BinaryStringBEToCode(const std::string& str, std::vector<u16>& code); +std::string CodeToBinaryStringBE(const std::vector<u16>& code); +std::vector<u16> BinaryStringBEToCode(const std::string& str); // Load code (big endian binary). -bool LoadBinary(const std::string& filename, std::vector<u16>& code); +std::optional<std::vector<u16>> LoadBinary(const std::string& filename); bool SaveBinary(const std::vector<u16>& code, const std::string& filename); bool DumpDSPCode(const u8* code_be, int size_in_bytes, u32 crc); |
