diff options
| author | 2016-04-14 16:28:15 -0400 | |
|---|---|---|
| committer | 2016-04-14 16:28:15 -0400 | |
| commit | aff35d3e58f65b72e22b079cafbf5c35135c3814 (patch) | |
| tree | 0ea7f1fee63d93709d7b7f2f751897b5fb8afe91 /src | |
| parent | Merge pull request #1546 from bunnei/refactor-shader-jit (diff) | |
| parent | file_util: In-class initialize data members (diff) | |
| download | yuzu-aff35d3e58f65b72e22b079cafbf5c35135c3814.tar.gz yuzu-aff35d3e58f65b72e22b079cafbf5c35135c3814.tar.xz yuzu-aff35d3e58f65b72e22b079cafbf5c35135c3814.zip | |
Merge pull request #1665 from lioncash/file
IOFile: Minor API changes
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/file_util.cpp | 43 | ||||
| -rw-r--r-- | src/common/file_util.h | 26 | ||||
| -rw-r--r-- | src/video_core/debug_utils/debug_utils.cpp | 17 |
3 files changed, 38 insertions, 48 deletions
diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 9ada09f8a..53700c865 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp | |||
| @@ -824,13 +824,12 @@ size_t WriteStringToFile(bool text_file, const std::string &str, const char *fil | |||
| 824 | 824 | ||
| 825 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) | 825 | size_t ReadFileToString(bool text_file, const char *filename, std::string &str) |
| 826 | { | 826 | { |
| 827 | FileUtil::IOFile file(filename, text_file ? "r" : "rb"); | 827 | IOFile file(filename, text_file ? "r" : "rb"); |
| 828 | auto const f = file.GetHandle(); | ||
| 829 | 828 | ||
| 830 | if (!f) | 829 | if (!file) |
| 831 | return false; | 830 | return false; |
| 832 | 831 | ||
| 833 | str.resize(static_cast<u32>(GetSize(f))); | 832 | str.resize(static_cast<u32>(file.GetSize())); |
| 834 | return file.ReadArray(&str[0], str.size()); | 833 | return file.ReadArray(&str[0], str.size()); |
| 835 | } | 834 | } |
| 836 | 835 | ||
| @@ -877,15 +876,10 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam | |||
| 877 | } | 876 | } |
| 878 | 877 | ||
| 879 | IOFile::IOFile() | 878 | IOFile::IOFile() |
| 880 | : m_file(nullptr), m_good(true) | 879 | { |
| 881 | {} | 880 | } |
| 882 | |||
| 883 | IOFile::IOFile(std::FILE* file) | ||
| 884 | : m_file(file), m_good(true) | ||
| 885 | {} | ||
| 886 | 881 | ||
| 887 | IOFile::IOFile(const std::string& filename, const char openmode[]) | 882 | IOFile::IOFile(const std::string& filename, const char openmode[]) |
| 888 | : m_file(nullptr), m_good(true) | ||
| 889 | { | 883 | { |
| 890 | Open(filename, openmode); | 884 | Open(filename, openmode); |
| 891 | } | 885 | } |
| @@ -896,7 +890,6 @@ IOFile::~IOFile() | |||
| 896 | } | 890 | } |
| 897 | 891 | ||
| 898 | IOFile::IOFile(IOFile&& other) | 892 | IOFile::IOFile(IOFile&& other) |
| 899 | : m_file(nullptr), m_good(true) | ||
| 900 | { | 893 | { |
| 901 | Swap(other); | 894 | Swap(other); |
| 902 | } | 895 | } |
| @@ -935,26 +928,12 @@ bool IOFile::Close() | |||
| 935 | return m_good; | 928 | return m_good; |
| 936 | } | 929 | } |
| 937 | 930 | ||
| 938 | std::FILE* IOFile::ReleaseHandle() | 931 | u64 IOFile::GetSize() const |
| 939 | { | ||
| 940 | std::FILE* const ret = m_file; | ||
| 941 | m_file = nullptr; | ||
| 942 | return ret; | ||
| 943 | } | ||
| 944 | |||
| 945 | void IOFile::SetHandle(std::FILE* file) | ||
| 946 | { | ||
| 947 | Close(); | ||
| 948 | Clear(); | ||
| 949 | m_file = file; | ||
| 950 | } | ||
| 951 | |||
| 952 | u64 IOFile::GetSize() | ||
| 953 | { | 932 | { |
| 954 | if (IsOpen()) | 933 | if (IsOpen()) |
| 955 | return FileUtil::GetSize(m_file); | 934 | return FileUtil::GetSize(m_file); |
| 956 | else | 935 | |
| 957 | return 0; | 936 | return 0; |
| 958 | } | 937 | } |
| 959 | 938 | ||
| 960 | bool IOFile::Seek(s64 off, int origin) | 939 | bool IOFile::Seek(s64 off, int origin) |
| @@ -965,12 +944,12 @@ bool IOFile::Seek(s64 off, int origin) | |||
| 965 | return m_good; | 944 | return m_good; |
| 966 | } | 945 | } |
| 967 | 946 | ||
| 968 | u64 IOFile::Tell() | 947 | u64 IOFile::Tell() const |
| 969 | { | 948 | { |
| 970 | if (IsOpen()) | 949 | if (IsOpen()) |
| 971 | return ftello(m_file); | 950 | return ftello(m_file); |
| 972 | else | 951 | |
| 973 | return -1; | 952 | return -1; |
| 974 | } | 953 | } |
| 975 | 954 | ||
| 976 | bool IOFile::Flush() | 955 | bool IOFile::Flush() |
diff --git a/src/common/file_util.h b/src/common/file_util.h index 880b8a1e3..b54a9fb72 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h | |||
| @@ -176,7 +176,6 @@ class IOFile : public NonCopyable | |||
| 176 | { | 176 | { |
| 177 | public: | 177 | public: |
| 178 | IOFile(); | 178 | IOFile(); |
| 179 | explicit IOFile(std::FILE* file); | ||
| 180 | IOFile(const std::string& filename, const char openmode[]); | 179 | IOFile(const std::string& filename, const char openmode[]); |
| 181 | 180 | ||
| 182 | ~IOFile(); | 181 | ~IOFile(); |
| @@ -192,6 +191,9 @@ public: | |||
| 192 | template <typename T> | 191 | template <typename T> |
| 193 | size_t ReadArray(T* data, size_t length) | 192 | size_t ReadArray(T* data, size_t length) |
| 194 | { | 193 | { |
| 194 | static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); | ||
| 195 | static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); | ||
| 196 | |||
| 195 | if (!IsOpen()) { | 197 | if (!IsOpen()) { |
| 196 | m_good = false; | 198 | m_good = false; |
| 197 | return -1; | 199 | return -1; |
| @@ -207,9 +209,8 @@ public: | |||
| 207 | template <typename T> | 209 | template <typename T> |
| 208 | size_t WriteArray(const T* data, size_t length) | 210 | size_t WriteArray(const T* data, size_t length) |
| 209 | { | 211 | { |
| 210 | static_assert(std::is_standard_layout<T>::value, "Given array does not consist of standard layout objects"); | 212 | static_assert(std::is_standard_layout<T>(), "Given array does not consist of standard layout objects"); |
| 211 | // TODO: gcc 4.8 does not support is_trivially_copyable, but we really should check for it here. | 213 | static_assert(std::is_trivially_copyable<T>(), "Given array does not consist of trivially copyable objects"); |
| 212 | //static_assert(std::is_trivially_copyable<T>::value, "Given array does not consist of trivially copyable objects"); | ||
| 213 | 214 | ||
| 214 | if (!IsOpen()) { | 215 | if (!IsOpen()) { |
| 215 | m_good = false; | 216 | m_good = false; |
| @@ -243,25 +244,20 @@ public: | |||
| 243 | 244 | ||
| 244 | // m_good is set to false when a read, write or other function fails | 245 | // m_good is set to false when a read, write or other function fails |
| 245 | bool IsGood() const { return m_good; } | 246 | bool IsGood() const { return m_good; } |
| 246 | operator void*() { return m_good ? m_file : nullptr; } | 247 | explicit operator bool() const { return IsGood(); } |
| 247 | |||
| 248 | std::FILE* ReleaseHandle(); | ||
| 249 | |||
| 250 | std::FILE* GetHandle() { return m_file; } | ||
| 251 | |||
| 252 | void SetHandle(std::FILE* file); | ||
| 253 | 248 | ||
| 254 | bool Seek(s64 off, int origin); | 249 | bool Seek(s64 off, int origin); |
| 255 | u64 Tell(); | 250 | u64 Tell() const; |
| 256 | u64 GetSize(); | 251 | u64 GetSize() const; |
| 257 | bool Resize(u64 size); | 252 | bool Resize(u64 size); |
| 258 | bool Flush(); | 253 | bool Flush(); |
| 259 | 254 | ||
| 260 | // clear error state | 255 | // clear error state |
| 261 | void Clear() { m_good = true; std::clearerr(m_file); } | 256 | void Clear() { m_good = true; std::clearerr(m_file); } |
| 262 | 257 | ||
| 263 | std::FILE* m_file; | 258 | private: |
| 264 | bool m_good; | 259 | std::FILE* m_file = nullptr; |
| 260 | bool m_good = true; | ||
| 265 | }; | 261 | }; |
| 266 | 262 | ||
| 267 | } // namespace | 263 | } // namespace |
diff --git a/src/video_core/debug_utils/debug_utils.cpp b/src/video_core/debug_utils/debug_utils.cpp index 693f93597..c8752c003 100644 --- a/src/video_core/debug_utils/debug_utils.cpp +++ b/src/video_core/debug_utils/debug_utils.cpp | |||
| @@ -586,6 +586,21 @@ TextureInfo TextureInfo::FromPicaRegister(const Regs::TextureConfig& config, | |||
| 586 | return info; | 586 | return info; |
| 587 | } | 587 | } |
| 588 | 588 | ||
| 589 | #ifdef HAVE_PNG | ||
| 590 | // Adapter functions to libpng to write/flush to File::IOFile instances. | ||
| 591 | static void WriteIOFile(png_structp png_ptr, png_bytep data, png_size_t length) { | ||
| 592 | auto* fp = static_cast<FileUtil::IOFile*>(png_get_io_ptr(png_ptr)); | ||
| 593 | if (!fp->WriteBytes(data, length)) | ||
| 594 | png_error(png_ptr, "Failed to write to output PNG file."); | ||
| 595 | } | ||
| 596 | |||
| 597 | static void FlushIOFile(png_structp png_ptr) { | ||
| 598 | auto* fp = static_cast<FileUtil::IOFile*>(png_get_io_ptr(png_ptr)); | ||
| 599 | if (!fp->Flush()) | ||
| 600 | png_error(png_ptr, "Failed to flush to output PNG file."); | ||
| 601 | } | ||
| 602 | #endif | ||
| 603 | |||
| 589 | void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data) { | 604 | void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data) { |
| 590 | #ifndef HAVE_PNG | 605 | #ifndef HAVE_PNG |
| 591 | return; | 606 | return; |
| @@ -629,7 +644,7 @@ void DumpTexture(const Pica::Regs::TextureConfig& texture_config, u8* data) { | |||
| 629 | goto finalise; | 644 | goto finalise; |
| 630 | } | 645 | } |
| 631 | 646 | ||
| 632 | png_init_io(png_ptr, fp.GetHandle()); | 647 | png_set_write_fn(png_ptr, static_cast<void*>(&fp), WriteIOFile, FlushIOFile); |
| 633 | 648 | ||
| 634 | // Write header (8 bit color depth) | 649 | // Write header (8 bit color depth) |
| 635 | png_set_IHDR(png_ptr, info_ptr, texture_config.width, texture_config.height, | 650 | png_set_IHDR(png_ptr, info_ptr, texture_config.width, texture_config.height, |