1 #ifndef _VKTBUFFEREDREADER_HPP 2 #define _VKTBUFFEREDREADER_HPP 3 /*------------------------------------------------------------------------ 4 * Vulkan Conformance Tests 5 * ------------------------ 6 * 7 * Copyright (c) 2024 The Khronos Group Inc. 8 * Copyright (c) 2024 Igalia S.L 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); 11 * you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 * 22 */ 23 #include <fstream> 24 #include <vector> 25 #include <memory> 26 #include <sstream> 27 28 #include "deFilePath.hpp" 29 30 #include "tcuDefs.hpp" 31 32 namespace vkt 33 { 34 namespace video 35 { 36 37 class BufferedReader 38 { 39 public: 40 // Open and read from filename BufferedReader(const std::string & filename)41 BufferedReader(const std::string &filename) 42 : m_istream(std::make_unique<std::ifstream>(resourceRelativePath(filename).getPath(), std::ios_base::binary)) 43 { 44 if (!m_istream->good()) 45 { 46 throw tcu::ResourceError(std::string("failed to open input")); 47 } 48 } 49 50 // Read from in-memory stream. BufferedReader(const char * bytes,size_t length)51 BufferedReader(const char *bytes, size_t length) 52 { 53 std::string asString(bytes, length); 54 m_istream.reset(new std::istringstream(asString, std::ios::binary)); 55 } 56 read(std::vector<uint8_t> & buffer)57 void read(std::vector<uint8_t> &buffer) 58 { 59 m_istream->read(reinterpret_cast<char *>(buffer.data()), buffer.size()); 60 } 61 read(uint8_t * out,size_t n)62 void read(uint8_t *out, size_t n) 63 { 64 m_istream->read(reinterpret_cast<char *>(out), n); 65 } 66 readChecked(uint8_t * out,size_t n,const char * msg)67 void readChecked(uint8_t *out, size_t n, const char *msg) 68 { 69 read(out, n); 70 if (isError()) 71 TCU_THROW(InternalError, msg); 72 } 73 readByteChecked(const char * msg)74 uint8_t readByteChecked(const char *msg) 75 { 76 uint8_t v{}; 77 read(&v, 1); 78 if (isEof()) 79 return 0; 80 if (isError()) 81 TCU_THROW(InternalError, msg); 82 return v; 83 } 84 isError() const85 bool isError() const 86 { 87 return m_istream->bad() || m_istream->fail(); 88 } isEof() const89 bool isEof() const 90 { 91 return m_istream->eof(); 92 } 93 94 private: resourceRelativePath(const std::string filename) const95 const de::FilePath resourceRelativePath(const std::string filename) const 96 { 97 std::vector<std::string> resourcePathComponents = {"vulkan", "video", filename}; 98 de::FilePath resourcePath = de::FilePath::join(resourcePathComponents); 99 return resourcePath; 100 } 101 102 std::unique_ptr<std::istream> m_istream; 103 }; 104 105 } // namespace video 106 } // namespace vkt 107 108 #endif // _VKTBUFFEREDREADER_HPP 109