|
| 1 | +/* This file is part of YUView - The YUV player with advanced analytics toolset |
| 2 | + * <https://github.com/IENT/YUView> |
| 3 | + * Copyright (C) 2015 Institut für Nachrichtentechnik, RWTH Aachen University, GERMANY |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation; either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * In addition, as a special exception, the copyright holders give |
| 11 | + * permission to link the code of portions of this program with the |
| 12 | + * OpenSSL library under certain conditions as described in each |
| 13 | + * individual source file, and distribute linked combinations including |
| 14 | + * the two. |
| 15 | + * |
| 16 | + * You must obey the GNU General Public License in all respects for all |
| 17 | + * of the code used other than OpenSSL. If you modify file(s) with this |
| 18 | + * exception, you may extend this exception to your version of the |
| 19 | + * file(s), but you are not obligated to do so. If you do not wish to do |
| 20 | + * so, delete this exception statement from your version. If you delete |
| 21 | + * this exception statement from all source files in the program, then |
| 22 | + * also delete it here. |
| 23 | + * |
| 24 | + * This program is distributed in the hope that it will be useful, |
| 25 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | + * GNU General Public License for more details. |
| 28 | + * |
| 29 | + * You should have received a copy of the GNU General Public License |
| 30 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 31 | + */ |
| 32 | + |
| 33 | +#include "DataSourceLocalFile.h" |
| 34 | + |
| 35 | +namespace filesource |
| 36 | +{ |
| 37 | + |
| 38 | +DataSourceLocalFile::DataSourceLocalFile(const std::filesystem::path &filePath) |
| 39 | +{ |
| 40 | + this->file.open(filePath.string(), std::ios_base::in | std::ios_base::binary); |
| 41 | + if (this->isOk()) |
| 42 | + this->path = filePath; |
| 43 | +} |
| 44 | + |
| 45 | +std::vector<InfoItem> DataSourceLocalFile::getInfoList() const |
| 46 | +{ |
| 47 | + if (!this->isOk()) |
| 48 | + return {}; |
| 49 | + |
| 50 | + std::vector<InfoItem> infoList; |
| 51 | + infoList.push_back( |
| 52 | + InfoItem({"File Path", this->path.string(), "The absolute path of the local file"})); |
| 53 | + if (const auto size = this->fileSize()) |
| 54 | + infoList.push_back(InfoItem({"File Size", std::to_string(*size)})); |
| 55 | + |
| 56 | + return infoList; |
| 57 | +} |
| 58 | + |
| 59 | +bool DataSourceLocalFile::atEnd() const |
| 60 | +{ |
| 61 | + return this->file.eof(); |
| 62 | +} |
| 63 | + |
| 64 | +bool DataSourceLocalFile::isOk() const |
| 65 | +{ |
| 66 | + if (this->file.fail()) |
| 67 | + return this->file.eof(); |
| 68 | + return true; |
| 69 | +} |
| 70 | + |
| 71 | +std::int64_t DataSourceLocalFile::position() const |
| 72 | +{ |
| 73 | + return this->filePosition; |
| 74 | +} |
| 75 | + |
| 76 | +bool DataSourceLocalFile::seek(const std::int64_t pos) |
| 77 | +{ |
| 78 | + if (!this->isOk()) |
| 79 | + return false; |
| 80 | + |
| 81 | + this->file.clear(); |
| 82 | + this->file.seekg(static_cast<std::streampos>(pos)); |
| 83 | + this->filePosition = this->file.tellg(); |
| 84 | + |
| 85 | + return this->isOk(); |
| 86 | +} |
| 87 | + |
| 88 | +std::int64_t DataSourceLocalFile::read(ByteVector &buffer, const std::int64_t nrBytes) |
| 89 | +{ |
| 90 | + if (!this->isOk()) |
| 91 | + return 0; |
| 92 | + |
| 93 | + const auto usize = static_cast<size_t>(nrBytes); |
| 94 | + if (static_cast<std::int64_t>(buffer.size()) < nrBytes) |
| 95 | + buffer.resize(usize); |
| 96 | + |
| 97 | + this->file.read(reinterpret_cast<char *>(buffer.data()), usize); |
| 98 | + |
| 99 | + const auto bytesRead = this->file.gcount(); |
| 100 | + buffer.resize(bytesRead); |
| 101 | + |
| 102 | + this->filePosition += bytesRead; |
| 103 | + return static_cast<std::int64_t>(bytesRead); |
| 104 | +} |
| 105 | + |
| 106 | +std::optional<std::int64_t> DataSourceLocalFile::fileSize() const |
| 107 | +{ |
| 108 | + if (this->path.empty()) |
| 109 | + return {}; |
| 110 | + |
| 111 | + const auto size = std::filesystem::file_size(this->path); |
| 112 | + return static_cast<std::int64_t>(size); |
| 113 | +} |
| 114 | + |
| 115 | +[[nodiscard]] std::filesystem::path DataSourceLocalFile::filePath() const |
| 116 | +{ |
| 117 | + return this->path; |
| 118 | +} |
| 119 | + |
| 120 | +} // namespace filesource |
0 commit comments