2009-04-01 22:03:56 +00:00
|
|
|
#include "quakedef.h"
|
|
|
|
#include "fs.h"
|
2011-05-30 04:32:04 +00:00
|
|
|
#include "errno.h"
|
2009-04-01 22:03:56 +00:00
|
|
|
|
2009-11-04 21:16:50 +00:00
|
|
|
#ifdef WEBSVONLY
|
|
|
|
#define Z_Free free
|
|
|
|
#define Z_Malloc malloc
|
|
|
|
#else
|
2009-06-03 09:09:35 +00:00
|
|
|
#if !defined(_WIN32) || defined(_SDL)
|
2009-05-24 10:11:17 +00:00
|
|
|
#define VFSSTDIO_Open VFSOS_Open
|
|
|
|
#define stdiofilefuncs osfilefuncs
|
|
|
|
#endif
|
|
|
|
#define FSSTDIO_OpenTemp FS_OpenTemp
|
2009-11-04 21:16:50 +00:00
|
|
|
#endif
|
2009-05-24 10:11:17 +00:00
|
|
|
|
2009-04-01 22:03:56 +00:00
|
|
|
typedef struct {
|
|
|
|
vfsfile_t funcs;
|
|
|
|
FILE *handle;
|
2009-05-24 10:11:17 +00:00
|
|
|
} vfsstdiofile_t;
|
|
|
|
static int VFSSTDIO_ReadBytes (struct vfsfile_s *file, void *buffer, int bytestoread)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
return fread(buffer, 1, bytestoread, intfile->handle);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static int VFSSTDIO_WriteBytes (struct vfsfile_s *file, const void *buffer, int bytestoread)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
return fwrite(buffer, 1, bytestoread, intfile->handle);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static qboolean VFSSTDIO_Seek (struct vfsfile_s *file, unsigned long pos)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
return fseek(intfile->handle, pos, SEEK_SET) == 0;
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static unsigned long VFSSTDIO_Tell (struct vfsfile_s *file)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
return ftell(intfile->handle);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static void VFSSTDIO_Flush(struct vfsfile_s *file)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
fflush(intfile->handle);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static unsigned long VFSSTDIO_GetSize (struct vfsfile_s *file)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
|
|
|
|
unsigned int curpos;
|
|
|
|
unsigned int maxlen;
|
|
|
|
curpos = ftell(intfile->handle);
|
|
|
|
fseek(intfile->handle, 0, SEEK_END);
|
|
|
|
maxlen = ftell(intfile->handle);
|
|
|
|
fseek(intfile->handle, curpos, SEEK_SET);
|
|
|
|
|
|
|
|
return maxlen;
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static void VFSSTDIO_Close(vfsfile_t *file)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *intfile = (vfsstdiofile_t*)file;
|
2009-04-01 22:03:56 +00:00
|
|
|
fclose(intfile->handle);
|
|
|
|
Z_Free(file);
|
|
|
|
}
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsfile_t *FSSTDIO_OpenTemp(void)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *file;
|
2009-04-01 22:03:56 +00:00
|
|
|
|
|
|
|
f = tmpfile();
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
file = Z_Malloc(sizeof(vfsstdiofile_t));
|
|
|
|
file->funcs.ReadBytes = VFSSTDIO_ReadBytes;
|
|
|
|
file->funcs.WriteBytes = VFSSTDIO_WriteBytes;
|
|
|
|
file->funcs.Seek = VFSSTDIO_Seek;
|
|
|
|
file->funcs.Tell = VFSSTDIO_Tell;
|
|
|
|
file->funcs.GetLen = VFSSTDIO_GetSize;
|
|
|
|
file->funcs.Close = VFSSTDIO_Close;
|
|
|
|
file->funcs.Flush = VFSSTDIO_Flush;
|
2009-04-01 22:03:56 +00:00
|
|
|
file->handle = f;
|
|
|
|
|
|
|
|
return (vfsfile_t*)file;
|
|
|
|
}
|
|
|
|
|
2011-09-16 05:56:54 +00:00
|
|
|
#if 0//def ANDROID
|
2011-09-05 01:48:23 +00:00
|
|
|
vfsfile_t *Sys_OpenAsset(const char *fname);
|
|
|
|
#endif
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsfile_t *VFSSTDIO_Open(const char *osname, const char *mode)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2009-05-24 10:11:17 +00:00
|
|
|
vfsstdiofile_t *file;
|
2009-04-01 22:03:56 +00:00
|
|
|
qboolean read = !!strchr(mode, 'r');
|
|
|
|
qboolean write = !!strchr(mode, 'w');
|
|
|
|
qboolean append = !!strchr(mode, 'a');
|
|
|
|
qboolean text = !!strchr(mode, 't');
|
|
|
|
char newmode[3];
|
|
|
|
int modec = 0;
|
|
|
|
|
2011-09-16 05:56:54 +00:00
|
|
|
#if 0//def ANDROID
|
2011-09-05 01:48:23 +00:00
|
|
|
// if (!strncmp("asset/", osname, 6))
|
|
|
|
{
|
|
|
|
if (append || write)
|
|
|
|
return NULL;
|
|
|
|
return Sys_OpenAsset(osname);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-04-01 22:03:56 +00:00
|
|
|
if (read)
|
|
|
|
newmode[modec++] = 'r';
|
|
|
|
if (write)
|
|
|
|
newmode[modec++] = 'w';
|
|
|
|
if (append)
|
|
|
|
newmode[modec++] = 'a';
|
|
|
|
if (text)
|
|
|
|
newmode[modec++] = 't';
|
|
|
|
else
|
|
|
|
newmode[modec++] = 'b';
|
|
|
|
newmode[modec++] = '\0';
|
|
|
|
|
|
|
|
f = fopen(osname, newmode);
|
|
|
|
if (!f)
|
|
|
|
return NULL;
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
file = Z_Malloc(sizeof(vfsstdiofile_t));
|
|
|
|
file->funcs.ReadBytes = strchr(mode, 'r')?VFSSTDIO_ReadBytes:NULL;
|
|
|
|
file->funcs.WriteBytes = (strchr(mode, 'w')||strchr(mode, 'a'))?VFSSTDIO_WriteBytes:NULL;
|
|
|
|
file->funcs.Seek = VFSSTDIO_Seek;
|
|
|
|
file->funcs.Tell = VFSSTDIO_Tell;
|
|
|
|
file->funcs.GetLen = VFSSTDIO_GetSize;
|
|
|
|
file->funcs.Close = VFSSTDIO_Close;
|
|
|
|
file->funcs.Flush = VFSSTDIO_Flush;
|
2009-04-01 22:03:56 +00:00
|
|
|
file->handle = f;
|
|
|
|
|
|
|
|
return (vfsfile_t*)file;
|
|
|
|
}
|
|
|
|
|
2009-11-04 21:16:50 +00:00
|
|
|
|
|
|
|
#ifndef WEBSVONLY
|
2009-05-24 10:11:17 +00:00
|
|
|
static vfsfile_t *FSSTDIO_OpenVFS(void *handle, flocation_t *loc, const char *mode)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
char diskname[MAX_OSPATH];
|
|
|
|
|
|
|
|
//path is already cleaned, as anything that gets a valid loc needs cleaning up first.
|
|
|
|
|
|
|
|
snprintf(diskname, sizeof(diskname), "%s/%s", (char*)handle, loc->rawname);
|
|
|
|
|
|
|
|
return VFSOS_Open(diskname, mode);
|
|
|
|
}
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
static void FSSTDIO_PrintPath(void *handle)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-11-04 21:16:50 +00:00
|
|
|
Con_Printf("%s\n", (char*)handle);
|
2009-04-01 22:03:56 +00:00
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static void FSSTDIO_ClosePath(void *handle)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
Z_Free(handle);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static int FSSTDIO_RebuildFSHash(const char *filename, int filesize, void *data)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
if (filename[strlen(filename)-1] == '/')
|
|
|
|
{ //this is actually a directory
|
|
|
|
|
|
|
|
char childpath[256];
|
2011-07-22 15:11:35 +00:00
|
|
|
Q_snprintfz(childpath, sizeof(childpath), "%s*", filename);
|
2009-05-24 10:11:17 +00:00
|
|
|
Sys_EnumerateFiles((char*)data, childpath, FSSTDIO_RebuildFSHash, data);
|
2009-04-01 22:03:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!Hash_GetInsensative(&filesystemhash, filename))
|
|
|
|
{
|
|
|
|
bucket_t *bucket = (bucket_t*)BZ_Malloc(sizeof(bucket_t) + strlen(filename)+1);
|
|
|
|
strcpy((char *)(bucket+1), filename);
|
|
|
|
#ifdef _WIN32
|
|
|
|
Q_strlwr((char *)(bucket+1));
|
|
|
|
#endif
|
|
|
|
Hash_AddInsensative(&filesystemhash, (char *)(bucket+1), data, bucket);
|
|
|
|
|
|
|
|
fs_hash_files++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fs_hash_dups++;
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static void FSSTDIO_BuildHash(void *handle)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
2009-05-24 10:11:17 +00:00
|
|
|
Sys_EnumerateFiles(handle, "*", FSSTDIO_RebuildFSHash, handle);
|
2009-04-01 22:03:56 +00:00
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static qboolean FSSTDIO_FLocate(void *handle, flocation_t *loc, const char *filename, void *hashedresult)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char netpath[MAX_OSPATH];
|
|
|
|
|
|
|
|
|
|
|
|
if (hashedresult && (void *)hashedresult != handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (!static_registered)
|
|
|
|
{ // if not a registered version, don't ever go beyond base
|
|
|
|
if ( strchr (filename, '/') || strchr (filename,'\\'))
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// check a file in the directory tree
|
|
|
|
snprintf (netpath, sizeof(netpath)-1, "%s/%s",(char*)handle, filename);
|
|
|
|
|
2011-09-05 01:48:23 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
{
|
|
|
|
vfsfile_t *f = VFSSTDIO_Open(netpath, "rb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
|
|
|
len = VFS_GETLEN(f);
|
|
|
|
VFS_CLOSE(f);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
{
|
|
|
|
FILE *f = fopen(netpath, "rb");
|
|
|
|
if (!f)
|
|
|
|
return false;
|
2009-04-01 22:03:56 +00:00
|
|
|
|
2011-09-05 01:48:23 +00:00
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
len = ftell(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
#endif
|
2009-04-01 22:03:56 +00:00
|
|
|
if (loc)
|
|
|
|
{
|
|
|
|
loc->len = len;
|
|
|
|
loc->offset = 0;
|
|
|
|
loc->index = 0;
|
|
|
|
Q_strncpyz(loc->rawname, filename, sizeof(loc->rawname));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static void FSSTDIO_ReadFile(void *handle, flocation_t *loc, char *buffer)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
2011-05-15 13:23:13 +00:00
|
|
|
size_t result;
|
|
|
|
|
2009-04-01 22:03:56 +00:00
|
|
|
f = fopen(loc->rawname, "rb");
|
|
|
|
if (!f) //err...
|
|
|
|
return;
|
|
|
|
fseek(f, loc->offset, SEEK_SET);
|
2011-05-15 13:23:13 +00:00
|
|
|
result = fread(buffer, 1, loc->len, f); // do soemthing with result
|
2011-05-30 04:32:04 +00:00
|
|
|
|
|
|
|
if (result != loc->len)
|
2011-06-01 13:21:54 +00:00
|
|
|
Con_Printf("FSSTDIO_ReadFile() fread: Filename: %s, expected %i, result was %u (%s)\n",loc->rawname,loc->len,(unsigned int)result,strerror(errno));
|
2011-05-30 04:32:04 +00:00
|
|
|
|
2009-04-01 22:03:56 +00:00
|
|
|
fclose(f);
|
|
|
|
}
|
2009-05-24 10:11:17 +00:00
|
|
|
static int FSSTDIO_EnumerateFiles (void *handle, const char *match, int (*func)(const char *, int, void *), void *parm)
|
2009-04-01 22:03:56 +00:00
|
|
|
{
|
|
|
|
return Sys_EnumerateFiles(handle, match, func, parm);
|
|
|
|
}
|
|
|
|
|
2009-05-24 10:11:17 +00:00
|
|
|
searchpathfuncs_t stdiofilefuncs = {
|
|
|
|
FSSTDIO_PrintPath,
|
|
|
|
FSSTDIO_ClosePath,
|
|
|
|
FSSTDIO_BuildHash,
|
|
|
|
FSSTDIO_FLocate,
|
|
|
|
FSSTDIO_ReadFile,
|
|
|
|
FSSTDIO_EnumerateFiles,
|
2009-04-01 22:03:56 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2009-05-24 10:11:17 +00:00
|
|
|
FSSTDIO_OpenVFS
|
2009-04-01 22:03:56 +00:00
|
|
|
};
|
2009-11-04 21:16:50 +00:00
|
|
|
#endif
|