1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-12-01 15:35:47 +00:00
Aquaria/ExternalLibs/ttvfs/VFSLoader.cpp

147 lines
3.3 KiB
C++
Raw Normal View History

// VFSLoader.cpp - late loading of files not in the tree
// For conditions of distribution and use, see copyright notice in VFS.h
#include "VFSInternal.h"
#include "VFSTools.h"
#include "VFSFile.h"
#include "VFSDir.h"
#include "VFSLoader.h"
2014-04-15 13:04:33 +00:00
//#include <stdio.h>
2014-04-06 17:19:33 +00:00
#if !defined(_WIN32) && defined(VFS_IGNORE_CASE)
#include <dirent.h>
// based on code in PhysicsFS: http://icculus.org/physfs/
static bool locateOneElement(char *buf)
{
char *ptr;
DIR *dirp;
ptr = strrchr(buf, '/'); // find entry at end of path.
//printf("locateOneElem: buf='%s' ptr='%s'\n", ptr, buf);
if (ptr == NULL)
{
dirp = opendir(".");
ptr = buf;
}
else
{
if(ptr != buf) // strip only if not abs path
*ptr = '\0';
//printf("opendir: '%s'\n", buf);
dirp = opendir(buf);
*ptr = '/';
ptr++; // point past dirsep to entry itself.
}
//printf("dirp = %p\n", dirp);
struct dirent *dent;
while ((dent = readdir(dirp)) != NULL)
{
if (strcasecmp(dent->d_name, ptr) == 0)
{
strcpy(ptr, dent->d_name); // found a match. Overwrite with this case.
closedir(dirp);
return true;
}
}
// no match at all...
closedir(dirp);
return false;
}
static bool findFileHarder(char *fn)
{
char *ptr = fn;
bool found = true;
while ((ptr = strchr(ptr + 1, '/')) != 0)
{
*ptr = '\0';
found = locateOneElement(fn);
*ptr = '/'; // restore path separator
if (!found)
return false;
}
// check final element...
found = found && locateOneElement(fn);
//printf("tt: Fixed case '%s' [%s]\n", fn, found ? "found" : "NOT FOUND"); // TEMP
return found;
}
#endif
2014-04-15 13:04:33 +00:00
VFS_NAMESPACE_START
VFSLoader::VFSLoader()
: root(NULL)
{
}
2014-04-06 17:19:33 +00:00
DiskLoader::DiskLoader()
{
root = new DiskDir("", this);
}
File *DiskLoader::Load(const char *fn, const char * /*ignored*/)
{
if(FileExists(fn))
2014-04-06 17:19:33 +00:00
return new DiskFile(fn); // must contain full file name
2014-04-06 17:19:33 +00:00
DiskFile *vf = NULL;
#if !defined(_WIN32) && defined(VFS_IGNORE_CASE)
size_t s = strlen(fn);
char *t = (char*)VFS_STACK_ALLOC(s+1);
memcpy(t, fn, s+1); // copy terminating '\0' as well
if(findFileHarder(&t[0])) // fixes the filename on the way
2014-04-06 17:19:33 +00:00
vf = new DiskFile(&t[0]);
VFS_STACK_FREE(t);
#endif
return vf;
}
2014-04-06 17:19:33 +00:00
Dir *DiskLoader::LoadDir(const char *fn, const char * /*ignored*/)
{
2014-04-15 13:04:33 +00:00
//printf("DiskLoader: Trying [%s]...\n", fn);
2014-04-06 17:19:33 +00:00
if(!IsDirectory(fn))
return NULL;
2014-04-06 17:19:33 +00:00
DiskDir *ret = NULL;
#if !defined(_WIN32) && defined(VFS_IGNORE_CASE)
size_t s = strlen(fn);
char *t = (char*)VFS_STACK_ALLOC(s+1);
memcpy(t, fn, s+1); // copy terminating '\0' as well
if(findFileHarder(&t[0])) // fixes the filename on the way
2014-04-06 17:19:33 +00:00
fn = &t[0];
2014-04-07 00:25:58 +00:00
#endif
2014-04-15 13:04:33 +00:00
assert(getRoot()->_getDirEx(fn, fn, false, false, false).first == NULL); // makes no sense to fire up the loader if it's already in the tree
ret = safecastNonNull<DiskDir*>(getRoot()->_createAndInsertSubtree(fn));
2014-04-07 00:25:58 +00:00
#if !defined(_WIN32) && defined(VFS_IGNORE_CASE)
VFS_STACK_FREE(t);
#endif
2014-04-15 13:04:33 +00:00
/*if(ret)
printf("DiskLoader: [%s] OK as [%s]\n", fn, ret->fullname());
else
printf("DiskLoader: [%s] FAILED\n", fn);*/
2014-04-07 00:25:58 +00:00
return ret;
}
VFS_NAMESPACE_END