]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - fs.c
Added a mempool parameter to FS_LoadFile
[xonotic/darkplaces.git] / fs.c
diff --git a/fs.c b/fs.c
index 8ba6759bbc5051ab3c297bbfd80995ec673673a2..093d0ac9ad3a7cec37d5bd7d803d6d9efaf3e164 100644 (file)
--- a/fs.c
+++ b/fs.c
@@ -579,7 +579,7 @@ pack_t *FS_LoadPackPK3 (const char *packfile)
        if (real_nb_files <= 0)
                Sys_Error ("%s is not a valid PK3 file", packfile);
 
-       Con_Printf ("Added packfile %s (%i files)\n", packfile, real_nb_files);
+       Con_Printf("Added packfile %s (%i files)\n", packfile, real_nb_files);
        return pack;
 }
 
@@ -679,7 +679,7 @@ static packfile_t* FS_AddFileToPack (const char* name, pack_t* pack,
 ============
 FS_CreatePath
 
-Only used for FS_WriteFile.
+Only used for FS_Open.
 ============
 */
 void FS_CreatePath (char *path)
@@ -710,15 +710,15 @@ void FS_Path_f (void)
 {
        searchpath_t *s;
 
-       Con_Print("Current search path:\n");
+       Con_Print("Current search path:\n");
        for (s=fs_searchpaths ; s ; s=s->next)
        {
                if (s->pack)
                {
-                       Con_Printf ("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
+                       Con_Printf("%s (%i files)\n", s->pack->filename, s->pack->numfiles);
                }
                else
-                       Con_Printf ("%s\n", s->filename);
+                       Con_Printf("%s\n", s->filename);
        }
 }
 
@@ -784,7 +784,7 @@ pack_t *FS_LoadPackPAK (const char *packfile)
 
        Mem_Free(info);
 
-       Con_Printf ("Added packfile %s (%i files)\n", packfile, numpackfiles);
+       Con_Printf("Added packfile %s (%i files)\n", packfile, numpackfiles);
        return pack;
 }
 
@@ -1086,7 +1086,7 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
                                if (!diff)
                                {
                                        if (!quiet)
-                                               Sys_Printf ("FS_FindFile: %s in %s\n",
+                                               Sys_Printf("FS_FindFile: %s in %s\n",
                                                                        pak->files[middle].name, pak->filename);
 
                                        if (index != NULL)
@@ -1103,13 +1103,12 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
                }
                else
                {
-                       char* netpath;
-
-                       netpath = va ("%s/%s", search->filename, name);
+                       char netpath[MAX_OSPATH];
+                       snprintf(netpath, sizeof(netpath), "%s/%s", search->filename, name);
                        if (FS_SysFileExists (netpath))
                        {
                                if (!quiet)
-                                       Sys_Printf ("FS_FindFile: %s\n", netpath);
+                                       Sys_Printf("FS_FindFile: %s\n", netpath);
 
                                if (index != NULL)
                                        *index = -1;
@@ -1119,7 +1118,7 @@ static searchpath_t *FS_FindFile (const char *name, int* index, qboolean quiet)
        }
 
        if (!quiet)
-               Sys_Printf ("FS_FindFile: can't find %s\n", name);
+               Sys_Printf("FS_FindFile: can't find %s\n", name);
 
        if (index != NULL)
                *index = -1;
@@ -1155,7 +1154,11 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
 
        // Found in the filesystem?
        if (i < 0)
-               return FS_OpenRead (va ("%s/%s", search->filename, filename), -1, -1);
+       {
+               char netpath[MAX_OSPATH];
+               snprintf(netpath, sizeof(netpath), "%s/%s", search->filename, filename);
+               return FS_OpenRead(netpath, -1, -1);
+       }
 
        // So, we found it in a package...
        packfile = &search->pack->files[i];
@@ -1167,7 +1170,7 @@ qfile_t *FS_FOpenFile (const char *filename, qboolean quiet)
        // No Zlib DLL = no compressed files
        if (!zlib_dll && (packfile->flags & FILE_FLAG_DEFLATED))
        {
-               Con_Printf ("WARNING: can't open the compressed file %s\n"
+               Con_Printf("WARNING: can't open the compressed file %s\n"
                                        "You need the Zlib DLL to use compressed files\n",
                                        filename);
                fs_filesize = -1;
@@ -1341,12 +1344,13 @@ size_t FS_Read (qfile_t* file, void* buffer, size_t buffersize)
                // If "input" is also empty, we need to fill it
                if (ztk->in_ind == ztk->in_max)
                {
-                       size_t remain = file->length - ztk->in_position;
+                       size_t remain;
 
                        // If we are at the end of the file
-                       if (!remain)
+                       if (ztk->out_position == ztk->real_length)
                                return nb;
 
+                       remain = file->length - ztk->in_position;
                        count = (remain > sizeof (ztk->input)) ? sizeof (ztk->input) : remain;
                        fread (ztk->input, 1, count, file->stream);
 
@@ -1427,6 +1431,18 @@ int FS_Flush (qfile_t* file)
 }
 
 
+/*
+====================
+FS_Print
+
+Print a string into a file
+====================
+*/
+int FS_Print(qfile_t* file, const char *msg)
+{
+       return FS_Write(file, msg, strlen(msg));
+}
+
 /*
 ====================
 FS_Printf
@@ -1434,7 +1450,7 @@ FS_Printf
 Print a string into a file
 ====================
 */
-int FS_Printf (qfile_t* file, const char* format, ...)
+int FS_Printf(qfile_t* file, const char* format, ...)
 {
        int result;
        va_list args;
@@ -1703,7 +1719,7 @@ Filename are relative to the quake directory.
 Always appends a 0 byte.
 ============
 */
-qbyte *FS_LoadFile (const char *path, qboolean quiet)
+qbyte *FS_LoadFile (const char *path, mempool_t *pool, qboolean quiet)
 {
        qfile_t *h;
        qbyte *buf;
@@ -1713,7 +1729,7 @@ qbyte *FS_LoadFile (const char *path, qboolean quiet)
        if (!h)
                return NULL;
 
-       buf = Mem_Alloc(tempmempool, fs_filesize+1);
+       buf = Mem_Alloc(pool, fs_filesize+1);
        if (!buf)
                Sys_Error ("FS_LoadFile: not enough available memory for %s (size %i)", path, fs_filesize);
 
@@ -1740,11 +1756,11 @@ qboolean FS_WriteFile (const char *filename, void *data, int len)
        handle = FS_Open (filename, "wb", false);
        if (!handle)
        {
-               Con_Printf ("FS_WriteFile: failed on %s\n", filename);
+               Con_Printf("FS_WriteFile: failed on %s\n", filename);
                return false;
        }
 
-       Con_DPrintf ("FS_WriteFile: %s\n", filename);
+       Con_DPrintf("FS_WriteFile: %s\n", filename);
        FS_Write (handle, data, len);
        FS_Close (handle);
        return true;
@@ -2086,18 +2102,18 @@ int FS_ListDirectory(const char *pattern, int oneperline)
 
 static void FS_ListDirectoryCmd (const char* cmdname, int oneperline)
 {
-       char pattern[MAX_OSPATH];
+       const char *pattern;
        if (Cmd_Argc() > 3)
        {
                Con_Printf("usage:\n%s [path/pattern]\n", cmdname);
                return;
        }
        if (Cmd_Argc() == 2)
-               snprintf(pattern, sizeof(pattern), "%s", Cmd_Argv(1));
+               pattern = Cmd_Argv(1);
        else
-               strcpy(pattern, "*");
+               pattern = "*";
        if (!FS_ListDirectory(pattern, oneperline))
-               Con_Printf("No files found.\n");
+               Con_Print("No files found.\n");
 }
 
 void FS_Dir_f(void)