- moved the file lookup functions to utilities.

# Conflicts:
#	src/common/utility/findfile.cpp
This commit is contained in:
Christoph Oelckers 2020-04-11 18:08:20 +02:00
commit 0e7480b4ed
7 changed files with 288 additions and 273 deletions

View file

@ -34,10 +34,13 @@
#include "findfile.h"
#include "zstring.h"
#include "cmdlib.h"
#include "printf.h"
#include "configfile.h"
#ifndef _WIN32
#include <unistd.h>
#include <fnmatch.h>
#include "cmdlib.h"
@ -126,7 +129,8 @@ int I_FindAttr(findstate_t *const fileinfo)
#else
#include <windows.h>
#include <direct.h>
//==========================================================================
//
// I_FindFirst
@ -186,3 +190,242 @@ const char *I_FindName(findstate_t *fileinfo)
}
#endif
//==========================================================================
//
// D_AddFile
//
//==========================================================================
bool D_AddFile(TArray<FString>& wadfiles, const char* file, bool check, int position, FConfigFile* config)
{
if (file == nullptr || *file == '\0')
{
return false;
}
if (check && !DirEntryExists(file))
{
const char* f = BaseFileSearch(file, ".wad", false, config);
if (f == nullptr)
{
Printf("Can't find '%s'\n", file);
return false;
}
file = f;
}
FString f = file;
FixPathSeperator(f);
if (position == -1) wadfiles.Push(f);
else wadfiles.Insert(position, f);
return true;
}
//==========================================================================
//
// D_AddWildFile
//
//==========================================================================
void D_AddWildFile(TArray<FString>& wadfiles, const char* value, const char *extension, FConfigFile* config)
{
if (value == nullptr || *value == '\0')
{
return;
}
const char* wadfile = BaseFileSearch(value, extension, false, config);
if (wadfile != nullptr)
{
D_AddFile(wadfiles, wadfile, true, -1, config);
}
else
{ // Try pattern matching
findstate_t findstate;
char path[ZPATH_MAX];
char* sep;
void* handle = I_FindFirst(value, &findstate);
strcpy(path, value);
sep = strrchr(path, '/');
if (sep == nullptr)
{
sep = strrchr(path, '\\');
#ifdef _WIN32
if (sep == nullptr && path[1] == ':')
{
sep = path + 1;
}
#endif
}
if (handle != ((void*)-1))
{
do
{
if (!(I_FindAttr(&findstate) & FA_DIREC))
{
if (sep == nullptr)
{
D_AddFile(wadfiles, I_FindName(&findstate), true, -1, config);
}
else
{
strcpy(sep + 1, I_FindName(&findstate));
D_AddFile(wadfiles, path, true, -1, config);
}
}
} while (I_FindNext(handle, &findstate) == 0);
}
I_FindClose(handle);
}
}
//==========================================================================
//
// D_AddConfigWads
//
// Adds all files in the specified config file section.
//
//==========================================================================
void D_AddConfigFiles(TArray<FString>& wadfiles, const char* section, const char* extension, FConfigFile *config)
{
if (config && config->SetSection(section))
{
const char* key;
const char* value;
FConfigFile::Position pos;
while (config->NextInSection(key, value))
{
if (stricmp(key, "Path") == 0)
{
// D_AddWildFile resets config's position, so remember it
config->GetPosition(pos);
D_AddWildFile(wadfiles, ExpandEnvVars(value), extension, config);
// Reset config's position to get next wad
config->SetPosition(pos);
}
}
}
}
//==========================================================================
//
// D_AddDirectory
//
// Add all .wad files in a directory. Does not descend into subdirectories.
//
//==========================================================================
void D_AddDirectory(TArray<FString>& wadfiles, const char* dir, const char *filespec, FConfigFile* config)
{
char curdir[ZPATH_MAX];
if (getcwd(curdir, ZPATH_MAX))
{
char skindir[ZPATH_MAX];
findstate_t findstate;
void* handle;
size_t stuffstart;
stuffstart = strlen(dir);
memcpy(skindir, dir, stuffstart * sizeof(*dir));
skindir[stuffstart] = 0;
if (skindir[stuffstart - 1] == '/')
{
skindir[--stuffstart] = 0;
}
if (!chdir(skindir))
{
skindir[stuffstart++] = '/';
if ((handle = I_FindFirst(filespec, &findstate)) != (void*)-1)
{
do
{
if (!(I_FindAttr(&findstate) & FA_DIREC))
{
strcpy(skindir + stuffstart, I_FindName(&findstate));
D_AddFile(wadfiles, skindir, true, -1, config);
}
} while (I_FindNext(handle, &findstate) == 0);
I_FindClose(handle);
}
}
chdir(curdir);
}
}
//==========================================================================
//
// BaseFileSearch
//
// If a file does not exist at <file>, looks for it in the directories
// specified in the config file. Returns the path to the file, if found,
// or nullptr if it could not be found.
//
//==========================================================================
const char* BaseFileSearch(const char* file, const char* ext, bool lookfirstinprogdir, FConfigFile* config)
{
static char wad[ZPATH_MAX];
if (file == nullptr || *file == '\0')
{
return nullptr;
}
if (lookfirstinprogdir)
{
mysnprintf(wad, countof(wad), "%s%s%s", progdir.GetChars(), progdir.Back() == '/' ? "" : "/", file);
if (DirEntryExists(wad))
{
return wad;
}
}
if (DirEntryExists(file))
{
mysnprintf(wad, countof(wad), "%s", file);
return wad;
}
if (config != nullptr && config->SetSection("FileSearch.Directories"))
{
const char* key;
const char* value;
while (config->NextInSection(key, value))
{
if (stricmp(key, "Path") == 0)
{
FString dir;
dir = NicePath(value);
if (dir.IsNotEmpty())
{
mysnprintf(wad, countof(wad), "%s%s%s", dir.GetChars(), dir.Back() == '/' ? "" : "/", file);
if (DirEntryExists(wad))
{
return wad;
}
}
}
}
}
// Retry, this time with a default extension
if (ext != nullptr)
{
FString tmp = file;
DefaultExtension(tmp, ext);
return BaseFileSearch(tmp, nullptr, lookfirstinprogdir, config);
}
return nullptr;
}