1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-15 22:19:07 +00:00
Aquaria/ExternalLibs/ttvfs/VFSDir.h

173 lines
4.7 KiB
C
Raw Normal View History

// VFSDir.h - basic directory interface + classes
// For conditions of distribution and use, see copyright notice in VFS.h
#ifndef VFSDIR_H
#define VFSDIR_H
#include "VFSBase.h"
#include <map>
#include <cstring>
#ifdef VFS_USE_HASHMAP
# include "VFSHashmap.h"
# include "VFSTools.h"
#endif
VFS_NAMESPACE_START
#ifdef VFS_IGNORE_CASE
struct ci_less
{
inline bool operator() (const char *a, const char *b) const
{
return VFS_STRICMP(a, b) < 0;
}
};
2014-04-06 17:19:33 +00:00
typedef ci_less map_compare;
inline int casecmp(const char *a, const char *b)
{
return VFS_STRICMP(a, b);
}
#else // VFS_IGNORE_CASE
struct cs_less
{
inline bool operator() (const char *a, const char *b) const
{
return strcmp(a, b) < 0;
}
};
2014-04-06 17:19:33 +00:00
typedef cs_less map_compare;
inline int casecmp(const char *a, const char *b)
{
return strcmp(a, b);
}
#endif // VFS_IGNORE_CASE
2014-04-06 17:19:33 +00:00
class Dir;
class DirBase;
class DirView;
class File;
class VFSLoader;
2014-04-06 17:19:33 +00:00
typedef void (*FileEnumCallback)(File *vf, void *user);
typedef void (*DirEnumCallback)(DirBase *vd, void *user);
2014-04-06 17:19:33 +00:00
// Avoid using std::string as key.
// The file names are known to remain constant during each object's lifetime,
// so just keep the pointers and use an appropriate comparator function.
typedef std::map<const char *, CountedPtr<DirBase>, map_compare> Dirs;
typedef std::map<const char *, CountedPtr<File>, map_compare> Files;
2014-04-06 17:19:33 +00:00
class DirBase : public VFSBase
{
public:
2014-04-06 17:19:33 +00:00
DirBase(const char *fullpath);
virtual ~DirBase();
2014-04-06 17:19:33 +00:00
/** Returns a file for this dir's subtree. Descends if necessary.
Returns NULL if the file is not found. */
2014-04-07 00:25:58 +00:00
File *getFile(const char *fn);
2014-04-06 17:19:33 +00:00
/** Returns a subdir, descends if necessary. If forceCreate is true,
create directory tree if it does not exist, and return the originally requested
subdir. Otherwise return NULL if not found. */
2014-04-15 13:04:33 +00:00
DirBase *getDir(const char *subdir);
std::pair<DirBase*, DirBase*> _getDirEx(const char *subdir, const char * const fullpath, bool forceCreate = false, bool lazyLoad = true, bool useSubtrees = true);
2014-04-06 17:19:33 +00:00
/** Returns a file from this dir's file map.
Expects the actual file name without path - does NOT descend. */
virtual File *getFileByName(const char *fn, bool lazyLoad = true) = 0;
virtual DirBase *getDirByName(const char *fn, bool lazyLoad = true, bool useSubtrees = true);
2014-04-07 00:25:58 +00:00
virtual File *getFileFromSubdir(const char *subdir, const char *file) = 0;
2014-04-06 17:19:33 +00:00
/** Iterate over all files or directories, calling a callback function,
optionally with additional userdata. If safe is true, iterate over a copy.
This is useful if the callback function modifies the tree, e.g.
adds or removes files. */
virtual void forEachDir(DirEnumCallback f, void *user = NULL, bool safe = false);
virtual void forEachFile(FileEnumCallback f, void *user = NULL, bool safe = false) = 0;
2014-04-06 17:19:33 +00:00
virtual void clearGarbage();
2014-04-06 17:19:33 +00:00
virtual bool _addToView(char *path, DirView& view) = 0;
2014-04-15 13:04:33 +00:00
DirBase *_createNewSubdir(const char *name) const;
DirBase *_createAndInsertSubtree(const char *name);
2014-04-06 17:19:33 +00:00
protected:
2014-04-06 17:19:33 +00:00
/** Creates a new dir of the same type to be used as child of this. */
virtual DirBase *createNew(const char *dir) const = 0;
2014-04-15 13:04:33 +00:00
2014-04-06 17:19:33 +00:00
Dirs _subdirs;
2014-04-15 13:04:33 +00:00
2014-04-06 17:19:33 +00:00
};
2014-04-06 17:19:33 +00:00
class Dir : public DirBase
{
public:
2014-04-06 17:19:33 +00:00
Dir(const char *fullpath, VFSLoader *ldr);
virtual ~Dir();
2014-04-06 17:19:33 +00:00
/** Adds a file directly to this directory, allows any name.
If another file with this name already exists, drop the old one out.
Returns whether the file was actually added (false if the same file already existed) */
bool add(File *f);
2014-04-06 17:19:33 +00:00
/** Like add(), but if the file name contains a path, descend the tree to the target dir.
Not-existing subdirs are created on the way. */
bool addRecursive(File *f, size_t skip = 0);
2014-04-06 17:19:33 +00:00
/** Enumerate directory with given path. Clears previously loaded entries. */
virtual void load() = 0;
2014-04-06 17:19:33 +00:00
void forEachFile(FileEnumCallback f, void *user = NULL, bool safe = false);
2014-04-15 13:04:33 +00:00
void forEachDir(DirEnumCallback f, void *user = NULL, bool safe = false);
2014-04-06 17:19:33 +00:00
virtual void clearGarbage();
2014-04-06 17:19:33 +00:00
bool _addToView(char *path, DirView& view);
DirBase *getDirByName(const char *dn, bool lazyLoad = true, bool useSubtrees = true);
File *getFileByName(const char *fn, bool lazyLoad = true);
2014-04-07 00:25:58 +00:00
File *getFileFromSubdir(const char *subdir, const char *file);
protected:
2014-04-06 17:19:33 +00:00
inline VFSLoader *getLoader() const { return _loader; }
Files _files;
2014-04-06 17:19:33 +00:00
private:
VFSLoader *_loader;
};
2014-04-06 17:19:33 +00:00
class DiskDir : public Dir
{
public:
2014-04-06 17:19:33 +00:00
DiskDir(const char *path, VFSLoader *ldr);
virtual ~DiskDir() {};
virtual void load();
virtual DiskDir *createNew(const char *dir) const;
virtual const char *getType() const { return "DiskDir"; }
};
VFS_NAMESPACE_END
#endif