# wiesel / src / common / wiesel / io / file.h

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 /** * Copyright (C) 2012 * Christian Fischer * * https://bitbucket.org/baldur/wiesel/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General * Public License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA */ #ifndef __WIESEL_IO_FILE_H__ #define __WIESEL_IO_FILE_H__ #include #include "wiesel/util/shared_object.h" #include "datasource.h" #include #include #include namespace wiesel { class FileSystem; class Directory; class File; /// Alias type for file lists. typedef std::list FileList; /** * @brief A class representing a file within a \ref FileSystem. */ class WIESEL_COMMON_EXPORT File : public virtual SharedObject { private: File() {} protected: File(Directory *parent); public: virtual ~File(); // getters public: /** * @brief get the file's name. * For example: "myfile.txt" */ virtual std::string getName() const = 0; /** * @brief get the file's full path including it's name. * For example: "/home/user/myfile.txt" * To get the path only, try getParent()->getFullPath() */ virtual std::string getFullPath(); /** * @brief get the file's full path on the platform's native file system. * If there's no physical file system, for example on a in-memory stored * file system, this function will return an empty string. * @return the native path or an empty string, if there is no real filesystem. */ virtual std::string getNativePath(); /** * @brief get the file's extension. * For example: "txt" (no loading dot) */ virtual std::string getExtension() const; /** * @brief Get a \ref DataSource referencing to this file. * Usually this would be a \ref FileDataSource, but that's not guaranteed. */ virtual DataSource *asDataSource(); /** * @brief get a \ref DataBuffer with the content of this file. */ virtual DataBuffer *getContent() = 0; /** * @brief get the full file content as string-object. * Binary files may return an empty string or garbage. * When the file couldn't be opened, an empty string will be returned. * To handle the case, a file was not readable, use \ref getContent(). */ virtual std::string getContentAsString(); /** * @brief read a text file's content as a vector of lines. * Binary files may return an empty list or garbage. */ virtual std::vector getLines(); /** * @brief get the parent-directory of this file. */ inline Directory* getParent() { return parent; } public: /** * @brief Sort a list of files by their names. */ static WIESEL_COMMON_EXPORT void sortByName(FileList &list, bool asc=true); private: Directory* parent; }; /** * @brief Predicate function to be used for sorting lists of files by their names. */ inline bool FileSortByNameAscPredicate(const File *lhs, const File *rhs) { return lhs->getName() < rhs->getName(); } /** * @brief Predicate function to be used for sorting lists of files by their names. */ inline bool FileSortByNameDescPredicate(const File *lhs, const File *rhs) { return lhs->getName() > rhs->getName(); } /** * @brief A \ref DataSource object which provides access to a \ref FileObject. */ class WIESEL_COMMON_EXPORT FileDataSource : public DataSource { private: FileDataSource() : file(NULL) {}; public: /** * @brief Create a new \ref FileDataSource. */ FileDataSource(File *file); ~FileDataSource(); virtual DataBuffer *getDataBuffer(); /** * @brief Get the \ref File object associated with this file buffer. */ virtual File *getFile(); private: File* file; }; } /* namespace wiesel */ #endif /* __WIESEL_IO_FILE_H__ */