CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/File/VFS/VFS.h
Views: 1401
#pragma once12#include <vector>3#include <cstdint>45#include "Common/File/DirListing.h"67// Basic read-only virtual file system. Used to manage assets on Android, where we have to8// read them manually out of the APK zipfile, while being able to run on other9// platforms as well with the appropriate directory set-up.1011// Note that this is kinda similar in concept to Core/MetaFileSystem.h, but that one12// is specifically for operations done by the emulated PSP, while this is for operations13// on the system level, like loading assets, and maybe texture packs. Also, as mentioned,14// this one is read-only, so a bit smaller and simpler.1516// VFSBackend instances can be used on their own, without the VFS, to serve as an abstraction of17// a single directory or ZIP file.1819// The VFSFileReference level of abstraction is there to hold things like zip file indices,20// for fast re-open etc.2122class VFSFileReference {23public:24virtual ~VFSFileReference() {}25};2627class VFSOpenFile {28public:29virtual ~VFSOpenFile() {}30};3132// Common interface parts between VFSBackend and VFS.33// Sometimes you don't need the VFS multiplexing and only have a VFSBackend *, sometimes you do need it,34// and it would be cool to be able to use the same interface, like when loading INI files.35class VFSInterface {36public:37virtual ~VFSInterface() {}38virtual uint8_t *ReadFile(const char *path, size_t *size) = 0;39// If listing already contains files, it'll be cleared.40virtual bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) = 0;41};4243class VFSBackend : public VFSInterface {44public:45virtual VFSFileReference *GetFile(const char *path) = 0;46virtual bool GetFileInfo(VFSFileReference *vfsReference, File::FileInfo *fileInfo) = 0;47virtual void ReleaseFile(VFSFileReference *vfsReference) = 0;4849// Must write the size of the file to *size. Both backends can do this efficiently here,50// avoiding a call to GetFileInfo.51virtual VFSOpenFile *OpenFileForRead(VFSFileReference *vfsReference, size_t *size) = 0;52virtual void Rewind(VFSOpenFile *vfsOpenFile) = 0;53virtual size_t Read(VFSOpenFile *vfsOpenFile, void *buffer, size_t length) = 0;54virtual void CloseFile(VFSOpenFile *vfsOpenFile) = 0;5556// Filter support is optional but nice to have57virtual bool GetFileInfo(const char *path, File::FileInfo *info) = 0;58virtual std::string toString() const = 0;59};6061class VFS : public VFSInterface {62public:63~VFS() { Clear(); }64void Register(const char *prefix, VFSBackend *reader);65void Clear();6667// Use delete [] to release the returned memory.68// Always allocates an extra zero byte at the end, so that it69// can be used for text like shader sources.70uint8_t *ReadFile(const char *filename, size_t *size) override;71bool GetFileInfo(const char *filename, File::FileInfo *fileInfo);72bool GetFileListing(const char *path, std::vector<File::FileInfo> *listing, const char *filter = nullptr) override;7374private:75struct VFSEntry {76const char *prefix;77VFSBackend *reader;78};79std::vector<VFSEntry> entries_;80};8182extern VFS g_VFS;838485