1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-10-05 13:51:04 +00:00

Update ttvfs to new version

This commit is contained in:
fgenesis 2014-04-06 19:19:33 +02:00
commit 8026cdd905
43 changed files with 2124 additions and 2427 deletions

View file

@ -17,10 +17,6 @@ VFS_NAMESPACE_START
#ifdef VFS_IGNORE_CASE
# ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4996)
# endif
struct ci_less
{
@ -29,24 +25,13 @@ struct ci_less
return VFS_STRICMP(a, b) < 0;
}
};
struct ci_equal
{
inline bool operator() (const char *a, const char *b) const
{
return !VFS_STRICMP(a, b);
}
};
typedef ci_less map_compare;
inline int casecmp(const char *a, const char *b)
{
return VFS_STRICMP(a, b);
}
# ifdef _MSC_VER
# pragma warning(pop)
# endif
#else // VFS_IGNORE_CASE
struct cs_less
@ -56,6 +41,7 @@ struct cs_less
return strcmp(a, b) < 0;
}
};
typedef cs_less map_compare;
inline int casecmp(const char *a, const char *b)
{
@ -66,146 +52,109 @@ inline int casecmp(const char *a, const char *b)
#endif // VFS_IGNORE_CASE
#ifdef VFS_USE_HASHMAP
struct hashmap_eq
{
inline bool operator() (const char *a, const char *b, size_t h, const VFSBase *itm) const
{
// quick check - instead of just comparing the strings,
// check the hashes first. If they don't match there is no
// need to check the strings at all.
return itm->hash() == h && !casecmp(a, b);
}
};
class Dir;
class DirBase;
class DirView;
class File;
class VFSLoader;
struct charptr_hash
{
inline size_t operator()(const char *s)
{
// case sensitive or in-sensitive, depending on config
return STRINGHASH(s);
}
};
#endif // VFS_USE_HASHMAP
typedef void (*FileEnumCallback)(File *vf, void *user);
typedef void (*DirEnumCallback)(DirBase *vd, void *user);
class VFSDir;
class VFSFile;
typedef void (*FileEnumCallback)(VFSFile *vf, void *user);
typedef void (*DirEnumCallback)(VFSDir *vd, void *user);
// 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;
class VFSDir : public VFSBase
class DirBase : public VFSBase
{
public:
// bitmask
enum EntryFlags
{
NONE = 0,
MOUNTED = 1
};
template<typename T> struct MapEntry
{
MapEntry() {}
MapEntry(T *p, EntryFlags flg = NONE) : ptr(p), flags(flg) {}
inline bool isMounted() { return flags & MOUNTED; }
T *ptr;
EntryFlags flags;
};
// 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.
#ifdef VFS_USE_HASHMAP
// VFS_IGNORE_CASE already handled in hash generation
typedef HashMap<const char *, MapEntry<VFSDir>, charptr_hash, hashmap_eq> Dirs;
typedef HashMap<const char *, MapEntry<VFSFile>, charptr_hash, hashmap_eq> Files;
#else
# ifdef VFS_IGNORE_CASE
typedef std::map<const char *, MapEntry<VFSDir>, ci_less> Dirs;
typedef std::map<const char *, MapEntry<VFSFile>, ci_less> Files;
# else
typedef std::map<const char *, MapEntry<VFSDir>, cs_less> Dirs;
typedef std::map<const char *, MapEntry<VFSFile>, cs_less> Files;
# endif
#endif
VFSDir(const char *fullpath);
virtual ~VFSDir();
/** Enumerate directory with given path. Keeps previously loaded entries.
Returns the amount of files found. */
virtual unsigned int load(bool recursive);
/** Creates a new virtual directory of an internally specified type. */
virtual VFSDir *createNew(const char *dir) const;
/** For debugging. Does never return NULL. */
virtual const char *getType() const { return "VFSDir"; }
/** Can be overloaded if necessary. Called by VFSHelper::ClearGarbage() */
virtual void clearGarbage() {}
/** Can be overloaded to close resources this dir keeps open */
virtual bool close() { return true; }
DirBase(const char *fullpath);
virtual ~DirBase();
/** Returns a file for this dir's subtree. Descends if necessary.
Returns NULL if the file is not found. */
VFSFile *getFile(const char *fn);
File *getFile(const char *fn, bool lazyLoad = true);
/** 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. */
VFSDir *getDir(const char *subdir, bool forceCreate = false);
DirBase *getDir(const char *subdir, bool forceCreate = false, bool lazyLoad = true, bool useSubtrees = true);
/** Recursively drops all files/dirs that were mounted into this directory (and subdirs) */
void clearMounted();
/** 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);
/** 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. */
void forEachFile(FileEnumCallback f, void *user = NULL, bool safe = false);
void forEachDir(DirEnumCallback f, void *user = NULL, bool safe = false);
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;
virtual void clearGarbage();
/* Below is for internal use -- take care if using these externally! */
bool insert(VFSDir *subdir, bool overwrite, EntryFlags flag);
bool merge(VFSDir *dir, bool overwrite, EntryFlags flag);
/** Adds a file directly to this directory, allows any name.
If another file with this name already exists, optionally drop the old one out.
Returns whether the file was actually added. */
bool add(VFSFile *f, bool overwrite, EntryFlags flag);
/** 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(VFSFile *f, bool overwrite, EntryFlags flag);
virtual bool _addToView(char *path, DirView& view) = 0;
protected:
// std::map<const char*,X> or ttvfs::HashMap<const char*, X> stores for files and subdirs.
Files _files;
/** Creates a new dir of the same type to be used as child of this. */
virtual DirBase *createNew(const char *dir) const = 0;
Dirs _subdirs;
};
typedef VFSDir::Files::iterator FileIter;
typedef VFSDir::Dirs::iterator DirIter;
class VFSDirReal : public VFSDir
class Dir : public DirBase
{
public:
VFSDirReal(const char *dir);
virtual ~VFSDirReal() {};
virtual unsigned int load(bool recursive);
virtual VFSDir *createNew(const char *dir) const;
virtual const char *getType(void) const { return "VFSDirReal"; }
Dir(const char *fullpath, VFSLoader *ldr);
virtual ~Dir();
/** 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);
/** 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);
/** Enumerate directory with given path. Clears previously loaded entries. */
virtual void load() = 0;
void forEachFile(FileEnumCallback f, void *user = NULL, bool safe = false);
virtual void clearGarbage();
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);
protected:
inline VFSLoader *getLoader() const { return _loader; }
Files _files;
private:
VFSLoader *_loader;
};
class DiskDir : public Dir
{
public:
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