Skip to content

Commit

Permalink
Refactor: PathDirectory and PathDirectoryNode use '/' as the default …
Browse files Browse the repository at this point in the history
…delimiter
  • Loading branch information
danij-deng committed Aug 11, 2012
1 parent b8ab818 commit 68c4938
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 106 deletions.
18 changes: 9 additions & 9 deletions doomsday/engine/portable/include/filedirectory.h
Expand Up @@ -68,7 +68,7 @@ class FileDirectory : private PathDirectory
*
* @return @c true iff successful.
*/
bool find(pathdirectorynode_type_t type, const char* searchPath, char searchDelimiter='/',
bool find(PathDirectoryNodeType type, const char* searchPath, char searchDelimiter='/',
ddstring_t* foundPath=NULL, char foundDelimiter='/');

/**
Expand Down Expand Up @@ -99,14 +99,14 @@ class FileDirectory : private PathDirectory
/**
* Collate all paths in the directory into a list.
*
* @param count Total number of collated paths is written back here.
* @param flags @ref pathComparisonFlags
* @param delimiter Fragments of the path will be delimited by this character.
* @param count Total number of collated paths is written back here.
*
* @return The allocated list; it is the responsibility of the caller to Str_Free()
* each string in the list and free() the list itself.
*/
ddstring_t* collectPaths(int flags, char delimiter, size_t* count);
ddstring_t* collectPaths(size_t* count, int flags, char delimiter='/');

#if _DEBUG
static void debugPrint(FileDirectory* inst);
Expand All @@ -133,7 +133,7 @@ class FileDirectory : private PathDirectory
* @return Non-zero if the current iteration should stop else @c 0.
*/
int addPathNodesAndMaybeDescendBranch(bool descendBranches, const ddstring_t* filePath,
pathdirectorynode_type_t nodeType,
PathDirectoryNodeType nodeType,
int flags,
int (*callback) (struct pathdirectorynode_s* node, void* parameters),
void* parameters);
Expand Down Expand Up @@ -175,7 +175,7 @@ void FileDirectory_AddPathList2(FileDirectory* fd, int flags, const char* pathLi
int (*callback) (PathDirectoryNode* node, void* parameters)); /*parameters=NULL*/
void FileDirectory_AddPathList(FileDirectory* fd, int flags, const char* pathList); /*callback=NULL*/

boolean FileDirectory_Find(FileDirectory* fd, pathdirectorynode_type_t type,
boolean FileDirectory_Find(FileDirectory* fd, PathDirectoryNodeType type,
const char* searchPath, char searchDelimiter, ddstring_t* foundPath, char foundDelimiter);

/**
Expand All @@ -202,14 +202,14 @@ typedef pathdirectory_iterateconstcallback_t filedirectory_iterateconstcallback_
*
* @return @c 0 iff iteration completed wholly.
*/
int FileDirectory_Iterate2(FileDirectory* fd, pathdirectorynode_type_t type, PathDirectoryNode* parent, ushort hash,
int FileDirectory_Iterate2(FileDirectory* fd, PathDirectoryNodeType type, PathDirectoryNode* parent, ushort hash,
filedirectory_iteratecallback_t callback, void* parameters);
int FileDirectory_Iterate(FileDirectory* fd, pathdirectorynode_type_t type, PathDirectoryNode* parent, ushort hash,
int FileDirectory_Iterate(FileDirectory* fd, PathDirectoryNodeType type, PathDirectoryNode* parent, ushort hash,
filedirectory_iteratecallback_t callback); /*parameters=NULL*/

int FileDirectory_Iterate2_Const(const FileDirectory* fd, pathdirectorynode_type_t type, const PathDirectoryNode* parent, ushort hash,
int FileDirectory_Iterate2_Const(const FileDirectory* fd, PathDirectoryNodeType type, const PathDirectoryNode* parent, ushort hash,
filedirectory_iterateconstcallback_t callback, void* parameters);
int FileDirectory_Iterate_Const(const FileDirectory* fd, pathdirectorynode_type_t type, const PathDirectoryNode* parent, ushort hash,
int FileDirectory_Iterate_Const(const FileDirectory* fd, PathDirectoryNodeType type, const PathDirectoryNode* parent, ushort hash,
filedirectory_iterateconstcallback_t callback); /*parameters=NULL*/

#if _DEBUG
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/include/fs_main.h
Expand Up @@ -306,10 +306,10 @@ void F_CacheChangeTag(abstractfile_t* file, int lumpIdx, int tag);
* @param flags @see searchPathFlags
*/
int F_AllResourcePaths2(const char* searchPath, int flags,
int (*callback) (const ddstring_t* path, pathdirectorynode_type_t type, void* paramaters),
int (*callback) (const ddstring_t* path, PathDirectoryNodeType type, void* paramaters),
void* paramaters);
int F_AllResourcePaths(const char* searchPath, int flags,
int (*callback) (const ddstring_t* path, pathdirectorynode_type_t type, void* paramaters));
int (*callback) (const ddstring_t* path, PathDirectoryNodeType type, void* paramaters));

#ifdef __cplusplus
} // extern "C"
Expand Down
53 changes: 31 additions & 22 deletions doomsday/engine/portable/include/pathdirectory.h
Expand Up @@ -56,7 +56,7 @@ typedef enum {
PT_BRANCH = PATHDIRECTORYNODE_TYPE_FIRST,
PT_LEAF,
PATHDIRECTORYNODE_TYPE_COUNT
} pathdirectorynode_type_t;
} PathDirectoryNodeType;

// Helper macro for determining if the value v can be interpreted as a valid node type.
#define VALID_PATHDIRECTORYNODE_TYPE(v) (\
Expand All @@ -78,9 +78,13 @@ class PathDirectory;

class PathDirectoryNode
{
public:
/// @return Print-ready name for node @a type.
static const ddstring_t* typeName(PathDirectoryNodeType type);

public:
/// @todo ctor/dtor should be private or made callable only by de::PathDirectory
PathDirectoryNode(PathDirectory& directory, pathdirectorynode_type_t type,
PathDirectoryNode(PathDirectory& directory, PathDirectoryNodeType type,
StringPoolId internId, PathDirectoryNode* parent=NULL,
void* userData=NULL);
~PathDirectoryNode();
Expand All @@ -92,7 +96,7 @@ class PathDirectoryNode
PathDirectoryNode* parent() const;

/// @return Type of this directory node.
pathdirectorynode_type_t type() const;
PathDirectoryNodeType type() const;

/// @return Hash for this directory node path fragment.
ushort hash() const;
Expand All @@ -108,13 +112,10 @@ class PathDirectoryNode
/**
* @param flags @see pathComparisonFlags
* @param searchPattern Fragment mapped search pattern (path).
* @return @c true iff the directory matched this.
* @return Non-zero iff the directory matched this.
*/
int matchDirectory(int flags, PathMap* candidatePath);

/// @return Print-ready name for node @a type.
static const ddstring_t* typeName(pathdirectorynode_type_t type);

/// @fixme should be private:
StringPoolId internId() const;

Expand Down Expand Up @@ -197,7 +198,7 @@ class PathDirectory
* "c:/somewhere/something" where @a delimiter @c= '/' the resultant node
* is that for the path fragment "something".
*/
PathDirectoryNode* insert(const char* path, char delimiter, void* userData=NULL);
PathDirectoryNode* insert(const char* path, char delimiter='/', void* userData=NULL);

/**
* Find a node in the directory.
Expand All @@ -219,7 +220,7 @@ class PathDirectory
*
* @return Found node else @c NULL.
*/
PathDirectoryNode* find(int flags, const char* path, char delimiter);
PathDirectoryNode* find(int flags, const char* path, char delimiter='/');

/**
* Composes and/or calculates the composed-length of the relative path for a node.
Expand All @@ -231,35 +232,35 @@ class PathDirectory
* @return The composed path pointer specified with @a path, for caller's convenience.
*/
ddstring_t* composePath(const PathDirectoryNode* node, ddstring_t* path,
int* length, char delimiter);
int* length, char delimiter='/');

/// @return The path fragment which @a node represents.
const ddstring_t* pathFragment(const PathDirectoryNode* node);

/**
* Collate all paths in the directory into a list.
*
* @param count Number of visited paths is written back here.
* @param flags @ref pathComparisonFlags
* @param delimiter Fragments of the path will be delimited by this character.
* @param count Number of visited paths is written back here.
*
* @return The allocated list; it is the responsibility of the caller to Str_Free()
* each string in the list and free() the list itself.
*/
ddstring_t* collectPaths(int flags, char delimiter, size_t* count);
ddstring_t* collectPaths(size_t* count, int flags, char delimiter='/');

/**
* Provides access to the PathDirectoryNode hash for efficent traversals.
*/
/*const*/ NodeHash* nodeHash(pathdirectorynode_type_t type) const;
/*const*/ NodeHash* nodeHash(PathDirectoryNodeType type) const;

/**
* This is a hash function. It uses the path fragment string to generate
* a somewhat-random number between @c 0 and @c PATHDIRECTORY_PATHHASH_SIZE
*
* @return The generated hash key.
*/
static ushort hashPathFragment(const char* fragment, size_t len, char delimiter);
static ushort hashPathFragment(const char* fragment, size_t len, char delimiter='/');

#if _DEBUG
static void debugPrint(PathDirectory* pd, char delimiter='/');
Expand Down Expand Up @@ -292,8 +293,9 @@ void PathDirectory_Delete(PathDirectory* pd);
uint PathDirectory_Size(PathDirectory* pd);
void PathDirectory_Clear(PathDirectory* pd);

PathDirectoryNode* PathDirectory_Insert2(PathDirectory* pd, const char* path, char delimiter, void* userData);
PathDirectoryNode* PathDirectory_Insert(PathDirectory* pd, const char* path, char delimiter); /*userData = NULL*/
PathDirectoryNode* PathDirectory_Insert3(PathDirectory* pd, const char* path, char delimiter, void* userData);
PathDirectoryNode* PathDirectory_Insert2(PathDirectory* pd, const char* path, char delimiter); /*userData=NULL*/
PathDirectoryNode* PathDirectory_Insert(PathDirectory* pd, const char* path); /*delimiter='/'*/

/**
* Callback function type for PathDirectory::Iterate
Expand Down Expand Up @@ -340,7 +342,8 @@ typedef int (*pathdirectory_searchcallback_t) (PathDirectoryNode* node, int flag
PathDirectoryNode* PathDirectory_Search2(PathDirectory* pd, int flags, PathMap* mappedSearchPath, pathdirectory_searchcallback_t callback, void* parameters);
PathDirectoryNode* PathDirectory_Search (PathDirectory* pd, int flags, PathMap* mappedSearchPath, pathdirectory_searchcallback_t callback); /*parameters=NULL*/

PathDirectoryNode* PathDirectory_Find(PathDirectory* pd, int flags, const char* path, char delimiter);
PathDirectoryNode* PathDirectory_Find2(PathDirectory* pd, int flags, const char* path, char delimiter);
PathDirectoryNode* PathDirectory_Find(PathDirectory* pd, int flags, const char* path); /* delimiter='/' */

/**
* Iterate over nodes in the directory making a callback for each.
Expand All @@ -361,11 +364,16 @@ int PathDirectory_Iterate (PathDirectory* pd, int flags, PathDirectoryNode* pare
int PathDirectory_Iterate2_Const(const PathDirectory* pd, int flags, const PathDirectoryNode* parent, ushort hash, pathdirectory_iterateconstcallback_t callback, void* parameters);
int PathDirectory_Iterate_Const (const PathDirectory* pd, int flags, const PathDirectoryNode* parent, ushort hash, pathdirectory_iterateconstcallback_t callback); /*parameters=NULL*/

ddstring_t* PathDirectory_ComposePath(PathDirectory* pd, const PathDirectoryNode* node, ddstring_t* path, int* length, char delimiter);
ddstring_t* PathDirectory_ComposePath2(PathDirectory* pd, const PathDirectoryNode* node, ddstring_t* path, int* length, char delimiter);
ddstring_t* PathDirectory_ComposePath(PathDirectory* pd, const PathDirectoryNode* node, ddstring_t* path, int* length); /*delimiter='/'*/

const ddstring_t* PathDirectory_GetFragment(PathDirectory* pd, const PathDirectoryNode* node);
ddstring_t* PathDirectory_CollectPaths(PathDirectory* pd, int flags, char delimiter, size_t* count);

ushort PathDirectory_HashPathFragment(const char* path, size_t len, char delimiter);
ddstring_t* PathDirectory_CollectPaths2(PathDirectory* pd, size_t* count, int flags, char delimiter);
ddstring_t* PathDirectory_CollectPaths(PathDirectory* pd, size_t* count, int flags); /* delimiter='/' */

ushort PathDirectory_HashPathFragment2(const char* path, size_t len, char delimiter);
ushort PathDirectory_HashPathFragment(const char* path, size_t len);/* delimiter='/' */

#if _DEBUG
void PathDirectory_DebugPrint(PathDirectory* pd, char delimiter);
Expand All @@ -378,12 +386,13 @@ void PathDirectory_DebugPrintHashDistribution(PathDirectory* pd);

PathDirectory* PathDirectoryNode_Directory(const PathDirectoryNode* node);
PathDirectoryNode* PathDirectoryNode_Parent(const PathDirectoryNode* node);
pathdirectorynode_type_t PathDirectoryNode_Type(const PathDirectoryNode* node);
PathDirectoryNodeType PathDirectoryNode_Type(const PathDirectoryNode* node);
ushort PathDirectoryNode_Hash(const PathDirectoryNode* node);
int PathDirectoryNode_MatchDirectory(PathDirectoryNode* node, int flags, PathMap* candidatePath, void* parameters);
void* PathDirectoryNode_UserData(const PathDirectoryNode* node);
void PathDirectoryNode_SetUserData(PathDirectoryNode* node, void* data);
const ddstring_t* PathDirectoryNode_TypeName(pathdirectorynode_type_t type);

const ddstring_t* PathDirectoryNodeType_Name(PathDirectoryNodeType type);

#ifdef __cplusplus
} // extern "C"
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/portable/src/con_data.c
Expand Up @@ -193,7 +193,7 @@ static void clearVariables(void)
/// Construct a new variable from the specified template and add it to the database.
static cvar_t* addVariable(const cvartemplate_t* tpl)
{
PathDirectoryNode* node = PathDirectory_Insert(cvarDirectory, tpl->path, CVARDIRECTORY_DELIMITER);
PathDirectoryNode* node = PathDirectory_Insert2(cvarDirectory, tpl->path, CVARDIRECTORY_DELIMITER);
cvar_t* newVar;

if(PathDirectoryNode_UserData(node))
Expand Down Expand Up @@ -479,7 +479,7 @@ int CVar_Flags(const cvar_t* var)
ddstring_t* CVar_ComposePath(const cvar_t* var)
{
assert(var);
return PathDirectory_ComposePath(PathDirectoryNode_Directory(var->directoryNode), var->directoryNode, Str_New(), NULL, CVARDIRECTORY_DELIMITER);
return PathDirectory_ComposePath2(PathDirectoryNode_Directory(var->directoryNode), var->directoryNode, Str_New(), NULL, CVARDIRECTORY_DELIMITER);
}

void CVar_SetUri2(cvar_t* var, const Uri* uri, int svFlags)
Expand Down Expand Up @@ -826,7 +826,7 @@ cvar_t* Con_FindVariable(const char* path)
{
PathDirectoryNode* node;
assert(inited);
node = PathDirectory_Find(cvarDirectory, PCF_NO_BRANCH|PCF_MATCH_FULL, path, CVARDIRECTORY_DELIMITER);
node = PathDirectory_Find2(cvarDirectory, PCF_NO_BRANCH|PCF_MATCH_FULL, path, CVARDIRECTORY_DELIMITER);
if(!node) return NULL;
return (cvar_t*) PathDirectoryNode_UserData(node);
}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/dd_main.c
Expand Up @@ -744,7 +744,7 @@ void DD_PrintGame(Game* game, int flags)
/**
* (f_allresourcepaths_callback_t)
*/
static int autoDataAdder(const ddstring_t* fileName, pathdirectorynode_type_t type, void* paramaters)
static int autoDataAdder(const ddstring_t* fileName, PathDirectoryNodeType type, void* paramaters)
{
assert(fileName && paramaters);
// We are only interested in files.
Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/portable/src/def_main.c
Expand Up @@ -705,7 +705,7 @@ static void Def_InitTextDef(ddtext_t* txt, char* str)
/**
* Callback for DD_ReadProcessDED.
*/
int Def_ReadDEDFile(const char* fn, pathdirectorynode_type_t type, void* parm)
int Def_ReadDEDFile(const char* fn, PathDirectoryNodeType type, void* parm)
{
// Skip directories.
if(type == PT_BRANCH)
Expand Down Expand Up @@ -819,7 +819,7 @@ static __inline void readDefinitionFile(const char* fileName)
/**
* (f_allresourcepaths_callback_t)
*/
static int autoDefsReader(const ddstring_t* fileName, pathdirectorynode_type_t type, void* paramaters)
static int autoDefsReader(const ddstring_t* fileName, PathDirectoryNodeType type, void* paramaters)
{
// Ignore directories.
if(type != PT_BRANCH)
Expand Down

0 comments on commit 68c4938

Please sign in to comment.