mirror of
https://git.do.srb2.org/KartKrew/Kart-Public.git
synced 2025-03-13 22:32:01 +00:00
Multiple files filesearch and findmultiplefiles
This commit is contained in:
parent
731c6bf56d
commit
60a4b403dd
4 changed files with 82 additions and 34 deletions
|
@ -1023,13 +1023,13 @@ filestatus_t checkfilemd5(char *filename, const UINT8 *wantedmd5sum)
|
|||
// Rewritten by Monster Iestyn to be less stupid
|
||||
// Note: if completepath is true, "filename" is modified, but only if FS_FOUND is going to be returned
|
||||
// (Don't worry about WinCE's version of filesearch, nobody cares about that OS anymore)
|
||||
filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean completepath)
|
||||
filestatus_t findmultiplefiles(int n, filequery_t *files, boolean checkmd5, boolean completepath)
|
||||
{
|
||||
filestatus_t homecheck; // store result of last file search
|
||||
boolean badmd5 = false; // store whether md5 was bad from either of the first two searches (if nothing was found in the third)
|
||||
|
||||
// first, check SRB2's "home" directory
|
||||
homecheck = filesearch(filename, srb2home, wantedmd5sum, completepath, 10);
|
||||
homecheck = filesearch(n, files, srb2home, checkmd5, completepath, 10);
|
||||
|
||||
if (homecheck == FS_FOUND) // we found the file, so return that we have :)
|
||||
return FS_FOUND;
|
||||
|
@ -1038,7 +1038,7 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet
|
|||
// if not found at all, just move on without doing anything
|
||||
|
||||
// next, check SRB2's "path" directory
|
||||
homecheck = filesearch(filename, srb2path, wantedmd5sum, completepath, 10);
|
||||
homecheck = filesearch(n, files, srb2path, checkmd5, completepath, 10);
|
||||
|
||||
if (homecheck == FS_FOUND) // we found the file, so return that we have :)
|
||||
return FS_FOUND;
|
||||
|
@ -1048,9 +1048,9 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet
|
|||
|
||||
// finally check "." directory
|
||||
#ifdef _arch_dreamcast
|
||||
homecheck = filesearch(filename, "/cd", wantedmd5sum, completepath, 10);
|
||||
homecheck = filesearch(n, files, "/cd", checkmd5, completepath, 10);
|
||||
#else
|
||||
homecheck = filesearch(filename, ".", wantedmd5sum, completepath, 10);
|
||||
homecheck = filesearch(n, files, ".", checkmd5, completepath, 10);
|
||||
#endif
|
||||
|
||||
if (homecheck != FS_NOTFOUND) // if not found this time, fall back on the below return statement
|
||||
|
@ -1059,6 +1059,12 @@ filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean complet
|
|||
return (badmd5 ? FS_MD5SUMBAD : FS_NOTFOUND); // md5 sum bad or file not found
|
||||
}
|
||||
|
||||
filestatus_t findfile(char *filename, const UINT8 *wantedmd5sum, boolean completepath)
|
||||
{
|
||||
filequery_t q = { FS_NOTFOUND, wantedmd5sum, filename };
|
||||
return findmultiplefiles(1, &q, ( wantedmd5sum != NULL ), completepath);
|
||||
}
|
||||
|
||||
#ifdef HAVE_CURL
|
||||
size_t curlwrite_data(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
{
|
||||
|
|
|
@ -445,17 +445,31 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut)
|
|||
|
||||
#else
|
||||
|
||||
filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum, boolean completepath, int maxsearchdepth)
|
||||
filestatus_t filesearch(int nfiles, filequery_t *files, const char *startpath, boolean checkmd5, boolean completepath, int maxsearchdepth)
|
||||
{
|
||||
filestatus_t retval = FS_NOTFOUND;
|
||||
const UINT8 *wantedmd5sum = NULL;
|
||||
DIR **dirhandle;
|
||||
struct dirent *dent;
|
||||
struct stat fsstat;
|
||||
int found = 0;
|
||||
char *searchname = strdup(filename);
|
||||
int depthleft = maxsearchdepth;
|
||||
char searchpath[1024];
|
||||
size_t *searchpathindex;
|
||||
int files_left = 0;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < nfiles; ++i)
|
||||
{
|
||||
if (files[i].status != FS_FOUND)
|
||||
{
|
||||
files_left++;
|
||||
}
|
||||
}
|
||||
|
||||
if (! files_left)
|
||||
{
|
||||
return FS_FOUND;
|
||||
}
|
||||
|
||||
dirhandle = (DIR**) malloc(maxsearchdepth * sizeof (DIR*));
|
||||
searchpathindex = (size_t *) malloc(maxsearchdepth * sizeof (size_t));
|
||||
|
@ -467,7 +481,6 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
|||
|
||||
if (dirhandle[depthleft] == NULL)
|
||||
{
|
||||
free(searchname);
|
||||
free(dirhandle);
|
||||
free(searchpathindex);
|
||||
return FS_NOTFOUND;
|
||||
|
@ -481,7 +494,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
|||
else
|
||||
searchpathindex[depthleft]--;
|
||||
|
||||
while ((!found) && (depthleft < maxsearchdepth))
|
||||
while ((files_left) && (depthleft < maxsearchdepth))
|
||||
{
|
||||
searchpath[searchpathindex[depthleft]]=0;
|
||||
dent = readdir(dirhandle[depthleft]);
|
||||
|
@ -520,34 +533,46 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
|
|||
searchpath[searchpathindex[depthleft]-1]='/';
|
||||
searchpath[searchpathindex[depthleft]]=0;
|
||||
}
|
||||
else if (!strcasecmp(searchname, dent->d_name))
|
||||
else
|
||||
{
|
||||
switch (checkfilemd5(searchpath, wantedmd5sum))
|
||||
for (i = 0; i < nfiles; ++i)
|
||||
{
|
||||
case FS_FOUND:
|
||||
if (completepath)
|
||||
strcpy(filename,searchpath);
|
||||
else
|
||||
strcpy(filename,dent->d_name);
|
||||
retval = FS_FOUND;
|
||||
found = 1;
|
||||
break;
|
||||
case FS_MD5SUMBAD:
|
||||
retval = FS_MD5SUMBAD;
|
||||
break;
|
||||
default: // prevent some compiler warnings
|
||||
break;
|
||||
if (checkmd5)
|
||||
{
|
||||
wantedmd5sum = files[i].wantedmd5sum;
|
||||
}
|
||||
|
||||
if (
|
||||
files[i].status != FS_FOUND &&
|
||||
!strcasecmp(files[i].filename, dent->d_name)
|
||||
){
|
||||
switch (checkfilemd5(searchpath, wantedmd5sum))
|
||||
{
|
||||
case FS_FOUND:
|
||||
if (completepath)
|
||||
strcpy(files[i].filename,searchpath);
|
||||
else
|
||||
strcpy(files[i].filename,dent->d_name);
|
||||
files[i].status = FS_FOUND;
|
||||
files_left--;
|
||||
break;
|
||||
case FS_MD5SUMBAD:
|
||||
retval = files[i].status = FS_MD5SUMBAD;
|
||||
break;
|
||||
default: // prevent some compiler warnings
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (; depthleft < maxsearchdepth; closedir(dirhandle[depthleft++]));
|
||||
|
||||
free(searchname);
|
||||
free(searchpathindex);
|
||||
free(dirhandle);
|
||||
|
||||
return retval;
|
||||
return ( (files_left) ? retval : FS_FOUND );
|
||||
}
|
||||
|
||||
char exttable[NUM_EXT_TABLE][7] = { // maximum extension length (currently 4) plus 3 (null terminator, stop, and length including previous two)
|
||||
|
|
|
@ -8,25 +8,38 @@
|
|||
#include "d_netfil.h"
|
||||
#include "m_menu.h" // MAXSTRINGLENGTH
|
||||
|
||||
typedef struct
|
||||
{
|
||||
filestatus_t status;
|
||||
const UINT8 * wantedmd5sum;
|
||||
char * filename;
|
||||
}
|
||||
filequery_t;
|
||||
|
||||
extern consvar_t cv_addons_option, cv_addons_folder, cv_addons_md5, cv_addons_showall, cv_addons_search_case, cv_addons_search_type;
|
||||
|
||||
/** \brief The filesearch function
|
||||
|
||||
This function search files, manly WADs and return back the status of the file
|
||||
|
||||
\param filename the file to look for
|
||||
\param nfiles number of files
|
||||
\param queries the files to look for
|
||||
\param startpath where to start look from
|
||||
\param wantedmd5sum want to check with MD5
|
||||
\param completepath want to return the complete path of the file?
|
||||
\param maxsearchdepth the max depth to search for the file
|
||||
|
||||
\return filestatus_t
|
||||
\return FS_FOUND if all files were found, FS_NOTFOUND or FS_MD5SUMBAD
|
||||
if some files were not found
|
||||
|
||||
|
||||
*/
|
||||
|
||||
filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *wantedmd5sum,
|
||||
boolean completepath, int maxsearchdepth);
|
||||
filestatus_t filesearch(int nfiles, filequery_t *queries, const char *startpath,
|
||||
boolean checkmd5, boolean completepath, int maxsearchdepth);
|
||||
|
||||
/* this is a better place for it tbh (im still too lazy to move the other) */
|
||||
filestatus_t findmultiplefiles(int nfiles, filequery_t *files,
|
||||
boolean checkmd5, boolean completepath);
|
||||
|
||||
#define menudepth 20
|
||||
|
||||
|
|
|
@ -3617,10 +3617,14 @@ static void pathonly(char *s)
|
|||
static const char *searchWad(const char *searchDir)
|
||||
{
|
||||
static char tempsw[256] = "";
|
||||
filequery_t fsquery;
|
||||
filestatus_t fstemp;
|
||||
|
||||
fsquery.filename = tempsw;
|
||||
|
||||
strcpy(tempsw, WADKEYWORD1);
|
||||
fstemp = filesearch(tempsw,searchDir,NULL,true,20);
|
||||
fsquery.status = FS_NOTFOUND;
|
||||
fstemp = filesearch(1,&fsquery,searchDir,false,true,20);
|
||||
if (fstemp == FS_FOUND)
|
||||
{
|
||||
pathonly(tempsw);
|
||||
|
@ -3628,7 +3632,7 @@ static const char *searchWad(const char *searchDir)
|
|||
}
|
||||
|
||||
strcpy(tempsw, WADKEYWORD2);
|
||||
fstemp = filesearch(tempsw, searchDir, NULL, true, 20);
|
||||
fstemp = filesearch(1, &fsquery, searchDir, false, true, 20);
|
||||
if (fstemp == FS_FOUND)
|
||||
{
|
||||
pathonly(tempsw);
|
||||
|
|
Loading…
Reference in a new issue