1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-06-08 01:22:02 +00:00

Update ttvfs to current HEAD

This commit is contained in:
fgenesis 2013-06-24 19:54:25 +02:00
parent 1897329071
commit 9d2dcc2d0f
5 changed files with 44 additions and 97 deletions

View file

@ -29,6 +29,7 @@
// performance matters, and you implemented actual locking into the Mutex class. // performance matters, and you implemented actual locking into the Mutex class.
// If no Mutex implementation is provided, its operations are no-ops, beware! // If no Mutex implementation is provided, its operations are no-ops, beware!
// Note: This adds a *lot* of overhead. Better ensure thread safety yourself, externally. Really! // Note: This adds a *lot* of overhead. Better ensure thread safety yourself, externally. Really!
// (Also note that this feature is *UNTESTED*. Don't activate.)
//#define VFS_THREADSAFE //#define VFS_THREADSAFE
// By default, ttvfs uses a std::map to store stuff. // By default, ttvfs uses a std::map to store stuff.

View file

@ -394,7 +394,7 @@ unsigned int VFSDirReal::load(bool recursive)
unsigned int sum = li.size(); unsigned int sum = li.size();
li.clear(); li.clear();
GetDirList(fullname(), li, false); GetDirList(fullname(), li, 0);
for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it) for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it)
{ {
// subdir was already present, move over and erase // subdir was already present, move over and erase

View file

@ -18,9 +18,9 @@
# include <windows.h> # include <windows.h>
#else #else
# ifdef __HAIKU__ # ifdef __HAIKU__
# include <dirent.h> # include <dirent.h>
# else # else
# include <sys/dir.h> # include <sys/dir.h>
# endif # endif
# include <unistd.h> # include <unistd.h>
#endif #endif
@ -30,24 +30,12 @@
VFS_NAMESPACE_START VFS_NAMESPACE_START
std::string stringToLower(std::string s) void stringToLower(std::string& s)
{
std::transform(s.begin(), s.end(), s.begin(), tolower);
return s;
}
std::string stringToUpper(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), toupper);
return s;
}
void makeLowercase(std::string& s)
{ {
std::transform(s.begin(), s.end(), s.begin(), tolower); std::transform(s.begin(), s.end(), s.begin(), tolower);
} }
void makeUppercase(std::string& s) void stringToUpper(std::string& s)
{ {
std::transform(s.begin(), s.end(), s.begin(), toupper); std::transform(s.begin(), s.end(), s.begin(), toupper);
} }
@ -109,7 +97,7 @@ static bool _IsDir(const char *path, dirent *dp)
char *pathname = (char*)alloca(len1 + 1 + len2 + 1 + 13); char *pathname = (char*)alloca(len1 + 1 + len2 + 1 + 13);
strcpy (pathname, path); strcpy (pathname, path);
/* Avoid UNC-path "//name" on Cygwin. */ /* Avoid UNC-path "//name" on Cygwin. */
if (len1 > 0 && pathname[len1 - 1] != '/') if (len1 > 0 && pathname[len1 - 1] != '/')
strcat (pathname, "/"); strcat (pathname, "/");
@ -125,9 +113,9 @@ static bool _IsFile(const char *path, dirent *dp)
{ {
return !_IsDir(path, dp); return !_IsDir(path, dp);
} }
#endif #endif // DT_DIR
#endif #endif // !_WIN32
// returns list of *plain* file names in given directory, // returns list of *plain* file names in given directory,
// without paths, and without anything else // without paths, and without anything else
@ -177,7 +165,7 @@ void GetFileList(const char *path, StringList& files)
// returns a list of directory names in the given directory, *without* the source dir. // returns a list of directory names in the given directory, *without* the source dir.
// if getting the dir list recursively, all paths are added, except *again* the top source dir beeing queried. // if getting the dir list recursively, all paths are added, except *again* the top source dir beeing queried.
void GetDirList(const char *path, StringList &dirs, bool recursive /* = false */) void GetDirList(const char *path, StringList &dirs, int depth /* = 0 */)
{ {
#if !_WIN32 #if !_WIN32
DIR * dirp; DIR * dirp;
@ -185,6 +173,8 @@ void GetDirList(const char *path, StringList &dirs, bool recursive /* = false */
dirp = opendir(path); dirp = opendir(path);
if(dirp) if(dirp)
{ {
std::string pathstr(path);
MakeSlashTerminated(pathstr);
while((dp = readdir(dirp))) // assignment is intentional while((dp = readdir(dirp))) // assignment is intentional
{ {
if (_IsDir(path, dp)) // only add if it is a directory if (_IsDir(path, dp)) // only add if it is a directory
@ -192,11 +182,13 @@ void GetDirList(const char *path, StringList &dirs, bool recursive /* = false */
if(strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0) if(strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{ {
dirs.push_back(dp->d_name); dirs.push_back(dp->d_name);
if (recursive) // needing a better way to do that if (depth) // needing a better way to do that
{ {
std::deque<std::string> newdirs; std::string d = dp->d_name;
GetDirList(dp->d_name, newdirs, true); std::string subdir = pathstr + d;
std::string d(dp->d_name); MakeSlashTerminated(d);
StringList newdirs;
GetDirList(subdir.c_str(), newdirs, depth - 1);
for(std::deque<std::string>::iterator it = newdirs.begin(); it != newdirs.end(); ++it) for(std::deque<std::string>::iterator it = newdirs.begin(); it != newdirs.end(); ++it)
dirs.push_back(d + *it); dirs.push_back(d + *it);
} }
@ -207,12 +199,10 @@ void GetDirList(const char *path, StringList &dirs, bool recursive /* = false */
} }
#else #else
std::string pathstr(path);
std::string search(path); MakeSlashTerminated(pathstr);
MakeSlashTerminated(search);
search += "*";
WIN32_FIND_DATA fil; WIN32_FIND_DATA fil;
HANDLE hFil = FindFirstFile(search.c_str(),&fil); HANDLE hFil = FindFirstFile((pathstr + '*').c_str(),&fil);
if(hFil != INVALID_HANDLE_VALUE) if(hFil != INVALID_HANDLE_VALUE)
{ {
do do
@ -222,14 +212,15 @@ void GetDirList(const char *path, StringList &dirs, bool recursive /* = false */
if (!strcmp(fil.cFileName, ".") || !strcmp(fil.cFileName, "..")) if (!strcmp(fil.cFileName, ".") || !strcmp(fil.cFileName, ".."))
continue; continue;
std::string d(fil.cFileName); dirs.push_back(fil.cFileName);
dirs.push_back(d);
if (recursive) // need a better way to do that if (depth) // need a better way to do that
{ {
std::string d = fil.cFileName;
std::string subdir = pathstr + d;
MakeSlashTerminated(d);
StringList newdirs; StringList newdirs;
GetDirList(d.c_str(), newdirs, true); GetDirList(subdir.c_str(), newdirs, depth - 1);
for(std::deque<std::string>::iterator it = newdirs.begin(); it != newdirs.end(); ++it) for(std::deque<std::string>::iterator it = newdirs.begin(); it != newdirs.end(); ++it)
dirs.push_back(d + *it); dirs.push_back(d + *it);
} }
@ -280,8 +271,10 @@ bool CreateDirRec(const char *dir)
StringList li; StringList li;
StrSplit(dir, "/\\", li, false); StrSplit(dir, "/\\", li, false);
std::string d; std::string d;
d.reserve(strlen(dir)); d.reserve(strlen(dir) + 1);
bool last; if(*dir == '/')
d += '/';
bool last = false;
for(StringList::iterator it = li.begin(); it != li.end(); ++it) for(StringList::iterator it = li.begin(); it != li.end(); ++it)
{ {
d += *it; d += *it;
@ -407,55 +400,6 @@ std::string StripLastPath(const std::string& s)
return s.substr(0, pos); return s.substr(0, pos);
} }
void GetFileListRecursive(std::string dir, StringList& files, bool withQueriedDir /* = false */)
{
std::stack<std::string> stk;
if(withQueriedDir)
{
stk.push(dir);
while(stk.size())
{
dir = stk.top();
stk.pop();
MakeSlashTerminated(dir);
StringList li;
GetFileList(dir.c_str(), li);
for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it)
files.push_back(dir + *it);
li.clear();
GetDirList(dir.c_str(), li, true);
for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it)
stk.push(dir + *it);
}
}
else
{
std::string topdir = dir;
MakeSlashTerminated(topdir);
stk.push("");
while(stk.size())
{
dir = stk.top();
stk.pop();
MakeSlashTerminated(dir);
StringList li;
dir = topdir + dir;
GetFileList(dir.c_str(), li);
for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it)
files.push_back(dir + *it);
li.clear();
GetDirList(dir.c_str(), li, true);
for(std::deque<std::string>::iterator it = li.begin(); it != li.end(); ++it)
stk.push(dir + *it);
}
}
}
// from http://board.byuu.org/viewtopic.php?f=10&t=1089&start=15 // from http://board.byuu.org/viewtopic.php?f=10&t=1089&start=15
bool WildcardMatch(const char *str, const char *pattern) bool WildcardMatch(const char *str, const char *pattern)
{ {
@ -464,7 +408,8 @@ bool WildcardMatch(const char *str, const char *pattern)
{ {
if(*pattern != *str && *pattern != '?') if(*pattern != *str && *pattern != '?')
return false; return false;
pattern++, str++; ++pattern;
++str;
} }
while(*str) while(*str)
@ -472,7 +417,7 @@ bool WildcardMatch(const char *str, const char *pattern)
if(*pattern == '*') if(*pattern == '*')
{ {
if(!*++pattern) if(!*++pattern)
return 1; return true;
mp = pattern; mp = pattern;
cp = str + 1; cp = str + 1;
} }
@ -488,7 +433,8 @@ bool WildcardMatch(const char *str, const char *pattern)
} }
} }
while(*pattern++ == '*'); while(*pattern == '*')
++pattern;
return !*pattern; return !*pattern;
} }

View file

@ -1,6 +1,9 @@
// VFSTools.h - useful functions and misc stuff // VFSTools.h - useful functions and misc stuff
// For conditions of distribution and use, see copyright notice in VFS.h // For conditions of distribution and use, see copyright notice in VFS.h
// Not all of these functions are used by ttvfs, but are added for user convenience.
// Everyone needs some path/file mangling functions at some point.
#ifndef VFS_TOOLS_H #ifndef VFS_TOOLS_H
#define VFS_TOOLS_H #define VFS_TOOLS_H
@ -13,12 +16,10 @@ VFS_NAMESPACE_START
typedef std::deque<std::string> StringList; typedef std::deque<std::string> StringList;
std::string stringToUpper(const std::string& s); void stringToUpper(std::string& s);
std::string stringToLower(const std::string& s); void stringToLower(std::string& s);
void makeUppercase(std::string& s);
void makeLowercase(std::string& s);
void GetFileList(const char *, StringList& files); void GetFileList(const char *, StringList& files);
void GetDirList(const char *, StringList& dirs, bool recursive = false); void GetDirList(const char *, StringList& dirs, int depth = 0); // recursion depth: 0 = subdirs of current, 1 = subdirs one level down, ..., -1 = deep recursion
bool FileExists(const char *); bool FileExists(const char *);
bool IsDirectory(const char *); bool IsDirectory(const char *);
bool CreateDir(const char*); bool CreateDir(const char*);
@ -30,7 +31,6 @@ const char *PathToFileName(const char *str);
void MakeSlashTerminated(std::string& s); void MakeSlashTerminated(std::string& s);
std::string StripFileExtension(const std::string& s); std::string StripFileExtension(const std::string& s);
std::string StripLastPath(const std::string& s); std::string StripLastPath(const std::string& s);
void GetFileListRecursive(std::string dir, StringList& files, bool withQueriedDir = false);
bool WildcardMatch(const char *str, const char *pattern); bool WildcardMatch(const char *str, const char *pattern);
size_t strnNLcpy(char *dst, const char *src, unsigned int n = -1); size_t strnNLcpy(char *dst, const char *src, unsigned int n = -1);
char *fastcat(char *s, const char *add); char *fastcat(char *s, const char *add);

View file

@ -163,9 +163,9 @@ const void *VFSFileZip::getBuf(allocator_func alloc /* = NULL */, delete_func de
_delfunc = del; _delfunc = del;
if(!zip_reader_reopen_vfsfile(_zip, 0)) if(!zip_reader_reopen_vfsfile(_zip, 0))
return false; // can happen if the underlying zip file was deleted return NULL; // can happen if the underlying zip file was deleted
if(!mz_zip_reader_extract_to_mem(_zip, _zipstat.m_file_index, _buf, sz, 0)) if(!mz_zip_reader_extract_to_mem(_zip, _zipstat.m_file_index, _buf, sz, 0))
return false; // this should not happen return NULL; // this should not happen
if(_mode.find("b") == std::string::npos) // text mode? if(_mode.find("b") == std::string::npos) // text mode?
{ {