* common.c (COM_FileExists): changed return type to qboolean.

(COM_filelength): new procedure.
(COM_FindFile): adjustments to avoid multiple file open/close operations just a
little.
* common.h (COM_FileExists): adjusted prototype.
* bgmusic.c (BGM_PlayCDtrack): adjusted for COM_FileExists() return type change.
* sys_sdl_win.c, sys_sdl_unix.c (Sys_filelength): changed return type to long.


git-svn-id: svn+ssh://svn.code.sf.net/p/quakespasm/code/trunk@408 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
sezero 2011-02-10 17:25:43 +00:00
parent 92bc5e8b9e
commit 8d3ba3ac02
5 changed files with 61 additions and 44 deletions

View file

@ -304,7 +304,7 @@ void BGM_PlayCDtrack (byte track, qboolean looping)
goto _next; goto _next;
q_snprintf(tmp, sizeof(tmp), "%s/track%02d%s", q_snprintf(tmp, sizeof(tmp), "%s/track%02d%s",
MUSIC_DIRNAME, (int)track, handler->ext); MUSIC_DIRNAME, (int)track, handler->ext);
if (COM_FileExists(tmp, &path_id) == -1) if (! COM_FileExists(tmp, &path_id))
goto _next; goto _next;
if (path_id > prev_id) if (path_id > prev_id)
{ {

View file

@ -1445,6 +1445,23 @@ void COM_CreatePath (char *path)
} }
} }
/*
================
COM_filelength
================
*/
long COM_filelength (FILE *f)
{
long pos, end;
pos = ftell (f);
fseek (f, 0, SEEK_END);
end = ftell (f);
fseek (f, pos, SEEK_SET);
return end;
}
/* /*
=========== ===========
COM_FindFile COM_FindFile
@ -1474,66 +1491,68 @@ static int COM_FindFile (const char *filename, int *handle, FILE **file,
// //
for (search = com_searchpaths; search; search = search->next) for (search = com_searchpaths; search; search = search->next)
{ {
// is the element a pak file? if (search->pack) /* look through all the pak file elements */
if (search->pack)
{ {
// look through all the pak file elements
pak = search->pack; pak = search->pack;
for (i=0 ; i<pak->numfiles ; i++) for (i=0 ; i<pak->numfiles ; i++)
if (!strcmp (pak->files[i].name, filename)) {
{ // found it! if (strcmp(pak->files[i].name, filename) != 0)
// Sys_Printf ("PackFile: %s : %s\n",pak->filename, filename); continue;
file_from_pak = 1; // found it!
com_filesize = pak->files[i].filelen; com_filesize = pak->files[i].filelen;
if (path_id) file_from_pak = 1;
*path_id = search->path_id; if (path_id)
if (handle) *path_id = search->path_id;
{ if (handle)
*handle = pak->handle; {
Sys_FileSeek (pak->handle, pak->files[i].filepos); *handle = pak->handle;
} Sys_FileSeek (pak->handle, pak->files[i].filepos);
else if (file)
{ // open a new file on the pakfile
*file = fopen (pak->filename, "rb");
if (*file)
fseek (*file, pak->files[i].filepos, SEEK_SET);
}
return com_filesize; return com_filesize;
} }
else if (file)
{ /* open a new file on the pakfile */
*file = fopen (pak->filename, "rb");
if (*file)
fseek (*file, pak->files[i].filepos, SEEK_SET);
return com_filesize;
}
else /* for COM_FileExists() */
{
return com_filesize;
}
}
} }
else else /* check a file in the directory tree */
{ {
// check a file in the directory tree
if (!static_registered) if (!static_registered)
{ // if not a registered version, don't ever go beyond base { /* if not a registered version, don't ever go beyond base */
if ( strchr (filename, '/') || strchr (filename,'\\')) if ( strchr (filename, '/') || strchr (filename,'\\'))
continue; continue;
} }
sprintf (netpath, "%s/%s",search->filename, filename); sprintf (netpath, "%s/%s",search->filename, filename);
findtime = Sys_FileTime (netpath); findtime = Sys_FileTime (netpath);
if (findtime == -1) if (findtime == -1)
continue; continue;
// Sys_Printf ("FindFile: %s\n",netpath);
com_filesize = Sys_FileOpenRead (netpath, &i);
if (path_id) if (path_id)
*path_id = search->path_id; *path_id = search->path_id;
if (handle) if (handle)
{ {
com_filesize = Sys_FileOpenRead (netpath, &i);
*handle = i; *handle = i;
return com_filesize;
} }
else if (file) else if (file)
{ {
Sys_FileClose (i);
*file = fopen (netpath, "rb"); *file = fopen (netpath, "rb");
com_filesize = (*file == NULL) ? -1 : COM_filelength (*file);
return com_filesize;
} }
else else
{ {
Sys_FileClose (i); return 0; /* dummy valid value for COM_FileExists() */
} }
return com_filesize;
} }
} }
@ -1542,10 +1561,10 @@ static int COM_FindFile (const char *filename, int *handle, FILE **file,
if (handle) if (handle)
*handle = -1; *handle = -1;
else if (file) if (file)
*file = NULL; *file = NULL;
com_filesize = -1; com_filesize = -1;
return -1; return com_filesize;
} }
@ -1553,13 +1572,13 @@ static int COM_FindFile (const char *filename, int *handle, FILE **file,
=========== ===========
COM_FileExists COM_FileExists
Returns com_filesize if file is found in the quake filesystem, Returns whether the file is found in the quake filesystem.
-1 if not. Closes the files it opens, if any.
=========== ===========
*/ */
int COM_FileExists (const char *filename, unsigned int *path_id) qboolean COM_FileExists (const char *filename, unsigned int *path_id)
{ {
return COM_FindFile (filename, NULL, NULL, path_id); int ret = COM_FindFile (filename, NULL, NULL, path_id);
return (ret == -1) ? false : true;
} }
/* /*

View file

@ -196,7 +196,7 @@ extern int file_from_pak; // global indicating that file came from a pak
void COM_WriteFile (const char *filename, const void *data, int len); void COM_WriteFile (const char *filename, const void *data, int len);
int COM_OpenFile (const char *filename, int *handle, unsigned int *path_id); int COM_OpenFile (const char *filename, int *handle, unsigned int *path_id);
int COM_FOpenFile (const char *filename, FILE **file, unsigned int *path_id); int COM_FOpenFile (const char *filename, FILE **file, unsigned int *path_id);
int COM_FileExists (const char *filename, unsigned int *path_id); qboolean COM_FileExists (const char *filename, unsigned int *path_id);
void COM_CloseFile (int h); void COM_CloseFile (int h);
// these procedures open a file using COM_FindFile and loads it into a proper // these procedures open a file using COM_FindFile and loads it into a proper

View file

@ -52,10 +52,9 @@ static int findhandle (void)
return -1; return -1;
} }
int Sys_filelength (FILE *f) long Sys_filelength (FILE *f)
{ {
int pos; long pos, end;
int end;
pos = ftell (f); pos = ftell (f);
fseek (f, 0, SEEK_END); fseek (f, 0, SEEK_END);

View file

@ -57,10 +57,9 @@ static int findhandle (void)
return -1; return -1;
} }
int Sys_filelength (FILE *f) long Sys_filelength (FILE *f)
{ {
int pos; long pos, end;
int end;
pos = ftell (f); pos = ftell (f);
fseek (f, 0, SEEK_END); fseek (f, 0, SEEK_END);