From 9f3499821b2503cce9e2327bb47912b785efe993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 22 Dec 2017 22:15:14 +0100 Subject: [PATCH] [wip] New filesystem abstraction library. --- src/Corrade/CMakeLists.txt | 4 + src/Corrade/Filesystem/AbstractFilesystem.h | 138 ++++++++++++++++++++ src/Corrade/Filesystem/CMakeLists.txt | 0 3 files changed, 142 insertions(+) create mode 100644 src/Corrade/Filesystem/AbstractFilesystem.h create mode 100644 src/Corrade/Filesystem/CMakeLists.txt diff --git a/src/Corrade/CMakeLists.txt b/src/Corrade/CMakeLists.txt index ca2689e8f..febb3aa42 100644 --- a/src/Corrade/CMakeLists.txt +++ b/src/Corrade/CMakeLists.txt @@ -52,6 +52,10 @@ if(WITH_UTILITY) # Cyclic dependency of Containers and Utility add_subdirectory(Containers) endif() +if(WITH_FILESYSTEM) + add_subdirectory(Filesystem) +endif() + if(WITH_INTERCONNECT) add_subdirectory(Interconnect) endif() diff --git a/src/Corrade/Filesystem/AbstractFilesystem.h b/src/Corrade/Filesystem/AbstractFilesystem.h new file mode 100644 index 000000000..c91c82cc9 --- /dev/null +++ b/src/Corrade/Filesystem/AbstractFilesystem.h @@ -0,0 +1,138 @@ +#ifndef Corrade_Filesystem_AbstractFilesystem_h +#define Corrade_Filesystem_AbstractFilesystem_h +/* + This file is part of Corrade. + + Copyright © 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, + 2017 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +/** @file + * @brief Class @ref Corrade::Filesystem::AbstractFilesystem + */ + +#include + +#include "Corrade/PluginManager/AbstractPlugin.h" + +namespace Corrade { namespace Filesystem { + +/** +@todo What we want: +- make this usable from multi-file importers, which would do the stuff synchronously ?! This won't be useful in Emscripten... +- have the sync versions at all? if yes, fire assert on them when the plugin is async? +*/ +class AbstractFilesystem: public PluginManager::AbstractPlugin { + CORRADE_PLUGIN_INTERFACE("cz.mosra.corrade.Filesystem.AbstractFilesystem/1.0") + + public: + enum class Feature: std::uint32_t { + OpenPath = 1 << 0, + OpenData = 1 << 1, + Write = 1 << 2, + Hierarchic = 1 << 3, // ? + ListDirectories = 1 << 4, + CreateFiles = 1 << 5, + Asynchronous = 1 << 6 + }; + + typedef Containers::EnumSet Features; + + std::string configurationDir() const; // w/o opening? + std::string home() const; // w/o opening? + + Features features() const { return doFeatures(); } + + bool isOpened() const; + + bool openPath(const std::string& path); + + bool openData(Containers::ArrayView data, const std::string& path = {}); + + void close(); + + /** @{ @name Filesystem manipulation */ + + std::vector list(const std::string& path = {}) const; + + std::future> listAsync(const std::string& path = {}) const; + + bool fileExists(const std::string& file) const; + + std::future fileExistsAsync(const std::string& file) const; + + bool createPath(const std::string& path); + + std::future createPathAsync(const std::string& path); + + bool remove(const std::string& path); + + std::future removeAsync(const std::string& path); + + bool move(const std::string& oldPath, const std::string& newPath); + + std::future moveAsync(const std::string& oldPath, const std::string& newPath); + + /*@}*/ + + /** @{ @name File manipulation */ + + Containers::Array read(const std::string& file); + + std::string readString(const std::string& file); + + bool write(const std::string& file, Containers::ArrayView data); + + bool writeString(const std::string& file, const std::string& data); + + /*@}*/ + + private: + virtual Features doFeatures() const = 0; + + virtual bool doIsOpened() = 0; + + virtual void doOpenPath(const std::string& path); + + virtual void doOpenData(Containers::ArrayView data, const std::string& path = {}); + + virtual void doClose() = 0; + + virtual std::vector doList(const std::string& path) const; + + virtual bool doFileExists(const std::string& file) const; + + virtual bool doCreatePath(const std::string& path); + + virtual bool doRemove(const std::string& path); + + virtual bool doMove(const std::string& oldPath, const std::string& newPath); + + virtual Containers::Array read(const std::string& file); + + virtual bool write(const std::string& file, Containers::ArrayView data); +}; + +CORRADE_ENUMSET_OPERATORS(AbstractFilesystem::Feature) + +}} + +#endif diff --git a/src/Corrade/Filesystem/CMakeLists.txt b/src/Corrade/Filesystem/CMakeLists.txt new file mode 100644 index 000000000..e69de29bb