1 /* 2 * Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr> 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License as 6 * published by the Free Software Foundation; either version 2 of the 7 * License, or (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, but 10 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied 11 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and 12 * NON-INFRINGEMENT. See the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 */ 19 20 #ifndef ZIM_FILE_PART_H_ 21 #define ZIM_FILE_PART_H_ 22 23 #include <string> 24 #include <cstdio> 25 26 #include <zim/zim.h> 27 28 #include "zim_types.h" 29 #include "fs.h" 30 31 namespace zim { 32 33 template<typename FS=DEFAULTFS> 34 class FilePart { 35 public: FilePart(const std::string & filename)36 FilePart(const std::string& filename) : 37 m_filename(filename), 38 m_fhandle(FS::openFile(filename)), 39 m_size(m_fhandle.getSize()) {} FilePart(int fd)40 FilePart(int fd) : 41 m_filename(""), 42 m_fhandle(fd), 43 m_size(m_fhandle.getSize()) {} 44 ~FilePart() = default; filename()45 const std::string& filename() const { return m_filename; }; fhandle()46 const typename FS::FD& fhandle() const { return m_fhandle; }; 47 size()48 zsize_t size() const { return m_size; }; fail()49 bool fail() const { return !m_size; }; good()50 bool good() const { return bool(m_size); }; 51 52 private: 53 const std::string m_filename; 54 typename FS::FD m_fhandle; 55 zsize_t m_size; 56 }; 57 58 }; 59 60 #endif //ZIM_FILE_PART_H_ 61