| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #ifndef ENGINE_STORAGE_H |
| 4 | #define ENGINE_STORAGE_H |
| 5 | |
| 6 | #include "kernel.h" |
| 7 | |
| 8 | #include <base/hash.h> |
| 9 | #include <base/types.h> |
| 10 | |
| 11 | #include <memory> |
| 12 | #include <set> |
| 13 | #include <string> |
| 14 | |
| 15 | enum |
| 16 | { |
| 17 | MAX_PATHS = 16 |
| 18 | }; |
| 19 | |
| 20 | class IStorage : public IInterface |
| 21 | { |
| 22 | MACRO_INTERFACE("storage" ) |
| 23 | public: |
| 24 | enum |
| 25 | { |
| 26 | TYPE_SAVE = 0, |
| 27 | TYPE_ALL = -1, |
| 28 | TYPE_ABSOLUTE = -2, |
| 29 | /** |
| 30 | * Translates to TYPE_SAVE if a path is relative |
| 31 | * and to TYPE_ABSOLUTE if a path is absolute. |
| 32 | * Only usable with OpenFile, ReadFile, ReadFileStr, |
| 33 | * GetCompletePath, FileExists and FolderExists. |
| 34 | */ |
| 35 | TYPE_SAVE_OR_ABSOLUTE = -3, |
| 36 | /** |
| 37 | * Translates to TYPE_ALL if a path is relative |
| 38 | * and to TYPE_ABSOLUTE if a path is absolute. |
| 39 | * Only usable with OpenFile, ReadFile, ReadFileStr, |
| 40 | * GetCompletePath, FileExists and FolderExists. |
| 41 | */ |
| 42 | TYPE_ALL_OR_ABSOLUTE = -4, |
| 43 | }; |
| 44 | |
| 45 | enum class EInitializationType |
| 46 | { |
| 47 | BASIC, |
| 48 | SERVER, |
| 49 | CLIENT, |
| 50 | }; |
| 51 | |
| 52 | virtual int NumPaths() const = 0; |
| 53 | |
| 54 | virtual void ListDirectory(int Type, const char *pPath, FS_LISTDIR_CALLBACK pfnCallback, void *pUser) = 0; |
| 55 | virtual void ListDirectoryInfo(int Type, const char *pPath, FS_LISTDIR_CALLBACK_FILEINFO pfnCallback, void *pUser) = 0; |
| 56 | virtual IOHANDLE OpenFile(const char *pFilename, int Flags, int Type, char *pBuffer = nullptr, int BufferSize = 0) = 0; |
| 57 | virtual bool FileExists(const char *pFilename, int Type) = 0; |
| 58 | virtual bool FolderExists(const char *pFilename, int Type) = 0; |
| 59 | virtual bool ReadFile(const char *pFilename, int Type, void **ppResult, unsigned *pResultLen) = 0; |
| 60 | virtual char *ReadFileStr(const char *pFilename, int Type) = 0; |
| 61 | virtual bool RetrieveTimes(const char *pFilename, int Type, time_t *pCreated, time_t *pModified) = 0; |
| 62 | virtual bool CalculateHashes(const char *pFilename, int Type, SHA256_DIGEST *pSha256, unsigned *pCrc = nullptr) = 0; |
| 63 | virtual bool FindFile(const char *pFilename, const char *pPath, int Type, char *pBuffer, int BufferSize) = 0; |
| 64 | virtual size_t FindFiles(const char *pFilename, const char *pPath, int Type, std::set<std::string> *pEntries) = 0; |
| 65 | virtual bool RemoveFile(const char *pFilename, int Type) = 0; |
| 66 | virtual bool RemoveFolder(const char *pFilename, int Type) = 0; |
| 67 | virtual bool RenameFile(const char *pOldFilename, const char *pNewFilename, int Type) = 0; |
| 68 | virtual bool CreateFolder(const char *pFoldername, int Type) = 0; |
| 69 | virtual void GetCompletePath(int Type, const char *pDir, char *pBuffer, unsigned BufferSize) = 0; |
| 70 | |
| 71 | virtual bool RemoveBinaryFile(const char *pFilename) = 0; |
| 72 | virtual bool RenameBinaryFile(const char *pOldFilename, const char *pNewFilename) = 0; |
| 73 | virtual const char *GetBinaryPath(const char *pFilename, char *pBuffer, unsigned BufferSize) = 0; |
| 74 | virtual const char *GetBinaryPathAbsolute(const char *pFilename, char *pBuffer, unsigned BufferSize) = 0; |
| 75 | |
| 76 | static void StripPathAndExtension(const char *pFilename, char *pBuffer, int BufferSize); |
| 77 | static const char *FormatTmpPath(char *aBuf, unsigned BufSize, const char *pPath); |
| 78 | }; |
| 79 | |
| 80 | extern IStorage *CreateStorage(IStorage::EInitializationType InitializationType, int NumArgs, const char **ppArguments); |
| 81 | extern std::unique_ptr<IStorage> CreateLocalStorage(); |
| 82 | extern std::unique_ptr<IStorage> CreateTempStorage(const char *pDirectory, int NumArgs, const char **ppArguments); |
| 83 | |
| 84 | #endif |
| 85 | |