From 703f5441b8c611ab60d43a4ded15bf6ccffda618 Mon Sep 17 00:00:00 2001 From: sezero Date: Fri, 31 Dec 2010 10:29:38 +0000 Subject: [PATCH] common.c, common.h: Added LOADFILE_* defines for use with COM_LoadFile, for easier reading. Added COM_LoadZoneFile(), COM_LoadMallocFile() and COM_LoadBufFile() from uhexen2. Added comments after their prototypes in common.h. git-svn-id: svn+ssh://svn.code.sf.net/p/quakespasm/code/trunk@361 af15c1b1-3010-417e-b628-4374ebc0bcbd --- quakespasm/Quake/common.c | 95 ++++++++++++++++++++++++++++++--------- quakespasm/Quake/common.h | 18 ++++++++ 2 files changed, 92 insertions(+), 21 deletions(-) diff --git a/quakespasm/Quake/common.c b/quakespasm/Quake/common.c index dd31f6d6..a0a4259c 100644 --- a/quakespasm/Quake/common.c +++ b/quakespasm/Quake/common.c @@ -1634,9 +1634,18 @@ Filename are reletive to the quake directory. Allways appends a 0 byte. ============ */ -cache_user_t *loadcache; -byte *loadbuf; -int loadsize; +#define LOADFILE_ZONE 0 +#define LOADFILE_HUNK 1 +#define LOADFILE_TEMPHUNK 2 +#define LOADFILE_CACHE 3 +#define LOADFILE_STACK 4 +#define LOADFILE_BUF 5 +#define LOADFILE_MALLOC 6 + +static byte *loadbuf; +static cache_user_t *loadcache; +static int loadsize; + byte *COM_LoadFile (const char *path, int usehunk) { int h; @@ -1654,23 +1663,38 @@ byte *COM_LoadFile (const char *path, int usehunk) // extract the filename base name for hunk tag COM_FileBase (path, base); - if (usehunk == 1) - buf = (byte *) Hunk_AllocName (len+1, base); - else if (usehunk == 2) - buf = (byte *) Hunk_TempAlloc (len+1); - else if (usehunk == 0) - buf = (byte *) Z_Malloc (len+1); - else if (usehunk == 3) - buf = (byte *) Cache_Alloc (loadcache, len+1, base); - else if (usehunk == 4) + switch (usehunk) { - if (len+1 > loadsize) - buf = (byte *) Hunk_TempAlloc (len+1); - else + case LOADFILE_HUNK: + buf = (byte *) Hunk_AllocName (len+1, base); + break; + case LOADFILE_TEMPHUNK: + buf = (byte *) Hunk_TempAlloc (len+1); + break; + case LOADFILE_ZONE: + buf = (byte *) Z_Malloc (len+1); + break; + case LOADFILE_CACHE: + buf = (byte *) Cache_Alloc (loadcache, len+1, base); + break; + case LOADFILE_STACK: + if (len < loadsize) buf = loadbuf; - } - else + else + buf = (byte *) Hunk_TempAlloc (len+1); + break; + case LOADFILE_BUF: + if (len < loadsize && loadbuf != NULL) + buf = loadbuf; + else + buf = (byte *) Hunk_AllocName(len + 1, base); + break; + case LOADFILE_MALLOC: + buf = (byte *) malloc (len+1); + break; + default: Sys_Error ("COM_LoadFile: bad usehunk"); + } if (!buf) Sys_Error ("COM_LoadFile: not enough space for %s", path); @@ -1685,18 +1709,23 @@ byte *COM_LoadFile (const char *path, int usehunk) byte *COM_LoadHunkFile (const char *path) { - return COM_LoadFile (path, 1); + return COM_LoadFile (path, LOADFILE_HUNK); +} + +byte *COM_LoadZoneFile (const char *path) +{ + return COM_LoadFile (path, LOADFILE_ZONE); } byte *COM_LoadTempFile (const char *path) { - return COM_LoadFile (path, 2); + return COM_LoadFile (path, LOADFILE_TEMPHUNK); } void COM_LoadCacheFile (const char *path, struct cache_user_s *cu) { loadcache = cu; - COM_LoadFile (path, 3); + COM_LoadFile (path, LOADFILE_CACHE); } // uses temp hunk if larger than bufsize @@ -1706,11 +1735,35 @@ byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize) loadbuf = (byte *)buffer; loadsize = bufsize; - buf = COM_LoadFile (path, 4); + buf = COM_LoadFile (path, LOADFILE_STACK); return buf; } +// loads into a previously allocated buffer. if space is insufficient +// or the buffer is NULL, loads onto the hunk. bufsize is the actual +// size (without the +1). +byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize) +{ + byte *buf; + + loadbuf = (byte *)buffer; + loadsize = (*bufsize) + 1; + buf = COM_LoadFile (path, LOADFILE_BUF); + *bufsize = (buf == NULL) ? 0 : com_filesize; + if (loadbuf && buf && buf != loadbuf) + Sys_Printf("LoadBufFile: insufficient buffer for %s not used.\n", path); + + return buf; +} + +// returns malloc'd memory +byte *COM_LoadMallocFile (const char *path) +{ + return COM_LoadFile (path, LOADFILE_MALLOC); +} + + /* ================= COM_LoadPackFile -- johnfitz -- modified based on topaz's tutorial diff --git a/quakespasm/Quake/common.h b/quakespasm/Quake/common.h index 3b0efff2..c067b31b 100644 --- a/quakespasm/Quake/common.h +++ b/quakespasm/Quake/common.h @@ -190,10 +190,28 @@ int COM_OpenFile (const char *filename, int *hndl); int COM_FOpenFile (const char *filename, FILE **file); void COM_CloseFile (int h); +// these procedures open a file using COM_FindFile and loads it into a proper +// buffer. the buffer is allocated with a total size of com_filesize + 1. the +// procedures differ by their buffer allocation method. byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize); + // uses the specified stack stack buffer with the specified size + // of bufsize. if bufsize is too short, uses temp hunk. the bufsize + // must include the +1 byte *COM_LoadTempFile (const char *path); + // allocates the buffer on the temp hunk. byte *COM_LoadHunkFile (const char *path); + // allocates the buffer on the hunk. +byte *COM_LoadZoneFile (const char *path); + // allocates the buffer on the zone. void COM_LoadCacheFile (const char *path, struct cache_user_s *cu); + // uses cache mem for allocating the buffer. +byte *COM_LoadMallocFile (const char *path); + // allocates the buffer on the system mem (malloc). +byte *COM_LoadBufFile (const char *path, void *buffer, int *bufsize); + // uses the specified pre-allocated buffer with bufsize + 1 size. + // bufsize is the actual expected size (without the + 1). if the + // space is too short or the buffer is NULL, loads onto the hunk. + // sets bufsize to com_filesize for success, or to 0 for failure. /* The following FS_*() stdio replacements are necessary if one is * to perform non-sequential reads on files reopened on pak files