Line data Source code
1 : // 2 : // Copyright 2024 OpenModelViewer Authors 3 : // 4 : // Licensed under the Apache License, Version 2.0 (the "License"); 5 : // you may not use this file except in compliance with the License. 6 : // You may obtain a copy of the License at 7 : // 8 : // http://www.apache.org/licenses/LICENSE-2.0 9 : // 10 : // Unless required by applicable law or agreed to in writing, software 11 : // distributed under the License is distributed on an "AS IS" BASIS, 12 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 : // See the License for the specific language governing permissions and 14 : // limitations under the License. 15 : // 16 : 17 : #include "openmodelviewer/core/io/ifile.hpp" 18 : 19 : namespace openmodelviewer::core::io 20 : { 21 9 : IOResult IFile::readFileToString(const std::filesystem::path& path, std::string& outString) 22 : { 23 18 : return openFileToExecute(path, [&](std::ifstream& in) -> IOResult { 24 : try 25 : { 26 8 : in.seekg(0, std::ios::end); 27 8 : std::size_t size = static_cast<std::size_t>(in.tellg()); 28 8 : in.seekg(0, std::ios::beg); 29 : 30 8 : outString.clear(); 31 8 : outString.resize(size); 32 8 : in.read(outString.data(), size); 33 : 34 8 : if (!in) 35 0 : return IOResult::ReadError; 36 : 37 8 : return IOResult::Success; 38 : } 39 0 : catch (...) 40 : { 41 0 : return IOResult::UnknownError; 42 0 : } 43 18 : }); 44 : } 45 : 46 2 : IOResult IFile::readFileToBuffer(const std::filesystem::path& path, std::vector<uint8_t>& outBuffer) 47 : { 48 4 : return openFileToExecute(path, [&](std::ifstream& in) -> IOResult { 49 : try 50 : { 51 2 : in.seekg(0, std::ios::end); 52 2 : std::size_t size = static_cast<std::size_t>(in.tellg()); 53 2 : in.seekg(0, std::ios::beg); 54 : 55 2 : outBuffer.clear(); 56 2 : outBuffer.resize(size); 57 2 : in.read(reinterpret_cast<char*>(outBuffer.data()), size); 58 : 59 2 : if (!in) 60 0 : return IOResult::ReadError; 61 : 62 2 : return IOResult::Success; 63 : } 64 0 : catch (...) 65 : { 66 0 : return IOResult::UnknownError; 67 0 : } 68 4 : }); 69 : } 70 : 71 12 : IOResult IFile::openFileToExecute(const std::filesystem::path& path, const std::function<IOResult(std::ifstream&)>& action) 72 : { 73 12 : std::ifstream in; 74 12 : IOResult result = openInputFileStream(path, in); 75 : 76 12 : if (result != IOResult::Success) 77 : { 78 1 : return result; 79 : } 80 : 81 11 : IOResult actionResult = action(in); 82 11 : in.close(); 83 : 84 11 : return actionResult; 85 12 : } 86 : 87 13 : IOResult IFile::openInputFileStream(const std::filesystem::path& path, std::ifstream& inStream) 88 : { 89 13 : if (path.empty()) 90 : { 91 0 : return IOResult::EmptyPath; 92 : } 93 : 94 13 : if (!std::filesystem::exists(path)) 95 : { 96 2 : return IOResult::FileNotFound; 97 : } 98 : 99 11 : if (std::filesystem::is_directory(path)) 100 : { 101 0 : return IOResult::FilePathExpected; 102 : } 103 : 104 11 : inStream.open(path, std::ios::binary); 105 11 : if (!inStream.is_open()) 106 : { 107 0 : return IOResult::PermissionDenied; 108 : } 109 : 110 11 : return IOResult::Success; 111 : } 112 : }