Skip to content

Commit

Permalink
Refactor|FS1: Removed the now redundant allResourcePaths() iteration …
Browse files Browse the repository at this point in the history
…mechanism
  • Loading branch information
danij-deng committed Oct 15, 2012
1 parent b01707d commit 957253c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 133 deletions.
12 changes: 0 additions & 12 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -315,15 +315,6 @@ namespace de
*/
int findAllPaths(char const* searchPattern, int flags, PathList& found);

/**
* Parm is passed on to the callback, which is called for each file
* matching the filespec. Absolute path names are given to the callback.
* Zip directory, DD_DIREC and the real files are scanned.
*
* @param flags @see searchPathFlags
*/
int allResourcePaths(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters), void* parameters = 0);

/**
* Print contents of the specified directory of the virtual file system.
*/
Expand Down Expand Up @@ -479,9 +470,6 @@ void F_UnlockLump(struct abstractfile_s* file, int lumpIdx);
*/
void F_ComposeFileList(filetype_t type, boolean markedCustom, char* outBuf, size_t outBufSize, const char* delimiter);

int F_AllResourcePaths2(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters), void* parameters);
int F_AllResourcePaths(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters)/*, parameters = 0 */);

uint F_CRCNumber(void);

lumpnum_t F_OpenAuxiliary2(char const* fileName, size_t baseOffset);
Expand Down
56 changes: 21 additions & 35 deletions doomsday/engine/portable/src/dd_main.cpp
Expand Up @@ -289,8 +289,8 @@ static int addFilesFromAutoData(void)
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;

QByteArray foundPathUtf8 = i->path.toUtf8();
if(F_AddFile(foundPathUtf8.constData()))
QByteArray foundPath = i->path.toUtf8();
if(F_AddFile(foundPath.constData()))
{
count += 1;
}
Expand All @@ -300,28 +300,6 @@ static int addFilesFromAutoData(void)
return count;
}

typedef struct {
ddstring_t*** list;
size_t* listSize;
int count; /// Number of files loaded successfully.
} listfilesfromautodata_params_t;

/**
* (f_allresourcepaths_callback_t)
*/
static int listFilesWorker(char const* fileName, PathDirectoryNodeType type, void* parameters)
{
DENG_ASSERT(fileName && parameters);
// We are only interested in files.
if(type == PT_LEAF)
{
listfilesfromautodata_params_t* data = (listfilesfromautodata_params_t*)parameters;
addToPathList(data->list, data->listSize, fileName);
data->count += 1;
}
return 0; // Continue searching.
}

/**
* Files with the extensions wad, lmp, pk3, zip and deh in the automatical data
* directory are added to the specified file list.
Expand All @@ -338,25 +316,33 @@ static int listFilesFromAutoData(ddstring_t*** list, size_t* listSize)
0
};

listfilesfromautodata_params_t data;
ddstring_t pattern;
uint i;

if(!list || !listSize) return 0;

data.list = list;
data.listSize = listSize;
data.count = 0;
ddstring_t pattern; Str_InitStd(&pattern);

Str_Init(&pattern);
for(i = 0; extensions[i]; ++i)
uint numFilesAdded = 0;
for(uint i = 0; extensions[i]; ++i)
{
Str_Clear(&pattern);
Str_Appendf(&pattern, "%sauto/*.%s", Str_Text(&games->currentGame().dataPath()), extensions[i]);
F_AllResourcePaths2(Str_Text(&pattern), 0, listFilesWorker, (void*)&data);

FS1::PathList found;
if(App_FileSystem()->findAllPaths(Str_Text(&pattern), 0, found))
{
DENG2_FOR_EACH(i, found, FS1::PathList::const_iterator)
{
// Ignore directories.
if(i->attrib & A_SUBDIR) continue;

QByteArray foundPath = i->path.toUtf8();
addToPathList(list, listSize, foundPath.constData());
numFilesAdded += 1;
}
}
}
Str_Free(&pattern);
return data.count;

return numFilesAdded;
}

static boolean exchangeEntryPoints(pluginid_t pluginId)
Expand Down
131 changes: 45 additions & 86 deletions doomsday/engine/portable/src/fs_main.cpp
Expand Up @@ -760,24 +760,10 @@ int FS1::findAll(bool (*predicate)(de::DFile* hndl, void* parameters), void* par
return numFound;
}

static int findPathsWorker(char const* path, PathDirectoryNodeType type, void* parameters)
int FS1::findAllPaths(char const* rawSearchPattern, int flags, FS1::PathList& found)
{
FS1::PathList* found = (FS1::PathList*)parameters;
found->push_back(FS1::PathListItem(path, type == PT_BRANCH? A_SUBDIR : 0));
return 0; // Continue iteration.
}

int FS1::findAllPaths(char const* searchPattern, int flags, FS1::PathList& found)
{
int numFoundSoFar = found.count();
allResourcePaths(searchPattern, flags, findPathsWorker, (void*)&found);
return found.count() - numFoundSoFar;
}
int const numFoundSoFar = found.count();

int FS1::allResourcePaths(char const* rawSearchPattern, int flags,
int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters),
void* parameters)
{
// First normalize the raw search pattern into one we can process.
AutoStr* searchPattern = AutoStr_NewStd();
Str_Set(searchPattern, rawSearchPattern);
Expand All @@ -792,9 +778,9 @@ int FS1::allResourcePaths(char const* rawSearchPattern, int flags,
PathMap patternMap;
PathMap_Initialize(&patternMap, PathDirectory::hashPathFragment, Str_Text(searchPattern));

// Check the Zip directory.
{
int result = 0;
/*
* Check the Zip directory.
*/
DENG2_FOR_EACH(i, d->zipLumpIndex.lumps(), LumpIndex::Lumps::const_iterator)
{
LumpInfo const* lumpInfo = *i;
Expand All @@ -820,46 +806,39 @@ int FS1::allResourcePaths(char const* rawSearchPattern, int flags,

if(!patternMatched) continue;

result = callback(Str_Text(filePath), PT_LEAF, parameters);
if(result) break;
found.push_back(PathListItem(Str_Text(filePath), node.type() == PT_BRANCH? A_SUBDIR : 0));
}

PathMap_Destroy(&patternMap);
if(result) return result;
}

// Check the dir/WAD direcs.
/*
* Check the dir/WAD direcs.
*/
if(!d->lumpMappings.empty())
{
DENG2_FOR_EACH(i, d->lumpMappings, LumpMappings::const_iterator)
{
LumpMapping const& found = *i;
QByteArray foundPathUtf8 = found.first.toUtf8();
bool patternMatched = F_MatchFileName(foundPathUtf8.constData(), Str_Text(searchPattern));

if(!patternMatched) continue;
if(!F_MatchFileName(i->first.toUtf8().constData(), Str_Text(searchPattern))) continue;

int result = callback(foundPathUtf8.constData(), PT_LEAF, parameters);
if(result) return result;
found.push_back(PathListItem(i->first, 0 /*only filepaths (i.e., leaves) can be mapped to lumps*/));
}

/// @todo Shouldn't these be sorted? -ds
}

/**
* Check real files on the search path.
* Our existing normalized search pattern cannot be used as-is due to the
* interface of the search algorithm requiring that the name and directory of
* the pattern be specified separately.
/*
* Check native paths.
*/

// Extract the directory path.
AutoStr* searchDirectory = AutoStr_NewStd();
F_FileDir(searchDirectory, searchPattern);

if(!Str_IsEmpty(searchDirectory))
{
PathList foundPaths;
PathList nativePaths;
AutoStr* wildPath = AutoStr_NewStd();
finddata_t fd;

for(int i = -1; i < (int)d->pathMappings.count(); ++i)
{
if(i == -1)
Expand All @@ -885,30 +864,23 @@ int FS1::allResourcePaths(char const* rawSearchPattern, int flags,
if(Str_Compare(&fd.name, ".") && Str_Compare(&fd.name, ".."))
{
QString foundPath = QString("%1%2").arg(Str_Text(searchDirectory)).arg(Str_Text(&fd.name));
foundPaths.push_back(PathListItem(foundPath, fd.attrib));
if(!F_MatchFileName(foundPath.toUtf8().constData(), Str_Text(searchPattern))) continue;

nativePaths.push_back(PathListItem(foundPath, fd.attrib));
}
} while(!myfindnext(&fd));
}
myfindend(&fd);
}

// Sort all the found paths.
qSort(foundPaths.begin(), foundPaths.end());

DENG2_FOR_EACH(i, foundPaths, PathList::const_iterator)
{
PathListItem const& found = *i;
QByteArray foundPathUtf8 = found.path.toUtf8();
bool patternMatched = F_MatchFileName(foundPathUtf8.constData(), Str_Text(searchPattern));

if(!patternMatched) continue;
// Sort the native paths.
qSort(nativePaths.begin(), nativePaths.end());

int result = callback(foundPathUtf8.constData(), (found.attrib & A_SUBDIR)? PT_BRANCH : PT_LEAF, parameters);
if(result) return result;
}
// Add the native paths to the found results.
found.append(nativePaths);
}

return 0; // Not found.
return found.count() - numFoundSoFar;
}

FILE* FS1::findRealFile(char const* path, char const* mymode, ddstring_t** foundPath)
Expand Down Expand Up @@ -1567,37 +1539,34 @@ void FS1::initPathMap()
}
}

/**
* Prints the resource path to the console.
* This is a f_allresourcepaths_callback_t.
*/
static int printResourcePath(char const* fileName, PathDirectoryNodeType /*type*/,
void* /*parameters*/)
{
bool makePretty = CPP_BOOL( F_IsRelativeToBase(fileName, ddBasePath) );
Con_Printf(" %s\n", makePretty? F_PrettyPath(fileName) : fileName);
return 0; // Continue the listing.
}

void FS1::printDirectory(ddstring_t const* path)
{
ddstring_t dir; Str_InitStd(&dir);
AutoStr* searchPattern = Str_Set(AutoStr_NewStd(), Str_Text(path));

Str_Set(&dir, Str_Text(path));
Str_Strip(&dir);
F_FixSlashes(&dir, &dir);
// Make sure it ends in a directory separator character.
F_AppendMissingSlash(&dir);
if(!F_ExpandBasePath(&dir, &dir))
F_PrependBasePath(&dir, &dir);
Str_Strip(searchPattern);
F_FixSlashes(searchPattern, searchPattern);
// Ensure it ends in a directory separator character.
F_AppendMissingSlash(searchPattern);
if(!F_ExpandBasePath(searchPattern, searchPattern))
F_PrependBasePath(searchPattern, searchPattern);

Con_Printf("Directory: %s\n", F_PrettyPath(Str_Text(&dir)));
Con_Printf("Directory: %s\n", F_PrettyPath(Str_Text(searchPattern)));

// Make the pattern.
Str_AppendChar(&dir, '*');
allResourcePaths(Str_Text(&dir), 0, printResourcePath);
Str_AppendChar(searchPattern, '*');

PathList found;
if(findAllPaths(Str_Text(searchPattern), 0, found))
{
DENG2_FOR_EACH(i, found, PathList::const_iterator)
{
QByteArray foundPath = i->path.toUtf8();
bool const makePretty = CPP_BOOL( F_IsRelativeToBase(foundPath, ddBasePath) );

Str_Free(&dir);
Con_Printf(" %s\n", makePretty? F_PrettyPath(foundPath.constData()) : foundPath.constData());
}
}
}

static void printLumpIndex(de::LumpIndex const& index)
Expand Down Expand Up @@ -2180,16 +2149,6 @@ void F_ComposeFileList(filetype_t type, boolean markedCustom, char* outBuf, size
Str_Delete(str);
}

int F_AllResourcePaths2(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters), void* parameters)
{
return App_FileSystem()->allResourcePaths(searchPath, flags, callback, parameters);
}

int F_AllResourcePaths(char const* searchPath, int flags, int (*callback) (char const* path, PathDirectoryNodeType type, void* parameters)/*, parameters = 0 */)
{
return App_FileSystem()->allResourcePaths(searchPath, flags, callback);
}

uint F_CRCNumber(void)
{
return App_FileSystem()->loadedFilesCRC();
Expand Down

0 comments on commit 957253c

Please sign in to comment.