Skip to content

Commit

Permalink
Refactor|PathTree: Moved PathTreeNodeType into PathTree
Browse files Browse the repository at this point in the history
Plus various minor cleanup refactorings.
  • Loading branch information
danij-deng committed Oct 25, 2012
1 parent ad25c17 commit b67b916
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 196 deletions.
19 changes: 9 additions & 10 deletions doomsday/engine/portable/include/filedirectory.h
Expand Up @@ -68,7 +68,7 @@ class FileDirectory : private PathTree
*
* @return @c true iff successful.
*/
bool find(PathTreeNodeType type, char const* searchPath, char searchDelimiter = '/',
bool find(NodeType type, char const* searchPath, char searchDelimiter = '/',
ddstring_t* foundPath = 0, char foundDelimiter = '/');

/**
Expand All @@ -81,7 +81,7 @@ class FileDirectory : private PathTree
* @param parameters Passed to the callback.
*/
void addPaths(int flags, Uri const* const* searchPaths, uint searchPathsCount,
int (*callback) (PathTree::Node& node, void* parameters) = 0,
int (*callback) (Node& node, void* parameters) = 0,
void* parameters = 0);

/**
Expand All @@ -93,7 +93,7 @@ class FileDirectory : private PathTree
* @param parameters Passed to the callback.
*/
void addPathList(int flags, char const* pathList,
int (*callback) (PathTree::Node& node, void* parameters) = 0, void* parameters = 0);
int (*callback) (Node& node, void* parameters) = 0, void* parameters = 0);

/**
* Collate all paths in the directory into a list.
Expand All @@ -115,10 +115,10 @@ class FileDirectory : private PathTree
private:
void clearNodeInfo();

PathTree::Node* addPathNodes(ddstring_t const* rawPath);
Node* addPathNodes(ddstring_t const* rawPath);

int addChildNodes(PathTree::Node& node, int flags,
int (*callback) (PathTree::Node& node, void* parameters),
int addChildNodes(Node& node, int flags,
int (*callback) (Node& node, void* parameters),
void* parameters);

/**
Expand All @@ -130,17 +130,16 @@ class FileDirectory : private PathTree
*
* @return Non-zero if the current iteration should stop else @c 0.
*/
int addPathNodesAndMaybeDescendBranch(bool descendBranches, ddstring_t const* filePath,
PathTreeNodeType nodeType, int flags,
int (*callback) (PathTree::Node& node, void* parameters),
int addPathNodesAndMaybeDescendBranch(bool descendBranches, ddstring_t const* filePath, bool isDirectory,
int flags, int (*callback) (Node& node, void* parameters),
void* parameters);

private:
/// Used with relative path directories.
ddstring_t* basePath;

/// Used with relative path directories.
PathTree::Node* baseNode;
Node* baseNode;
};

} // namespace de
Expand Down
10 changes: 10 additions & 0 deletions doomsday/engine/portable/include/pathmap.h
Expand Up @@ -35,6 +35,16 @@ typedef struct pathmapfragment_s {
ushort hash;
const char* from, *to;
struct pathmapfragment_s* next;

#ifdef __cplusplus
// Determine length of fragment in characters.
int length() const
{
if(!qstrcmp(to, "") && !qstrcmp(from, ""))
return 0;
return (to - from) + 1;
}
#endif
} PathMapFragment;

/// Size of the fixed-length "short" fragment buffer allocated with the map.
Expand Down
40 changes: 15 additions & 25 deletions doomsday/engine/portable/include/pathtree.h
Expand Up @@ -50,19 +50,6 @@ extern "C" {
(i.e., relative) matches. */
///@}

typedef enum {
PT_ANY = -1,
PATHTREENODE_TYPE_FIRST = 0,
PT_BRANCH = PATHTREENODE_TYPE_FIRST,
PT_LEAF,
PATHTREENODE_TYPE_COUNT
} PathTreeNodeType;

// Helper macro for determining if the value v can be interpreted as a valid node type.
#define VALID_PATHTREENODE_TYPE(v) (\
(v) >= PATHTREENODE_TYPE_FIRST && (v) < PATHTREENODE_TYPE_COUNT)


// Number of buckets in the hash table.
#define PATHTREE_PATHHASH_SIZE 512

Expand Down Expand Up @@ -105,18 +92,24 @@ namespace de {
class PathTree
{
public:
/// Node type identifiers.
enum NodeType
{
Branch,
Leaf
};

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

/**
* Node is the base class for all nodes of a PathTree.
*/
class Node
{
public:
/// @return Print-ready name for node @a type.
static ddstring_t const* typeName(PathTreeNodeType type);

private:
Node();
Node(PathTree& tree, PathTreeNodeType type, StringPoolId internId,
Node(PathTree& tree, NodeType type, StringPoolId internId,
Node* parent = NULL, void* userData = NULL);
virtual ~Node();

Expand All @@ -128,7 +121,7 @@ class PathTree
Node* parent() const;

/// @return Type of this node.
PathTreeNodeType type() const;
NodeType type() const;

/// @return Hash for this node path fragment.
ushort hash() const;
Expand Down Expand Up @@ -266,14 +259,14 @@ class PathTree
/**
* Provides access to the PathTreeNodes for efficent traversals.
*/
Nodes const& nodes(PathTreeNodeType type) const;
Nodes const& nodes(NodeType type) const;

inline Nodes const& leafNodes() const {
return nodes(PT_LEAF);
return nodes(Leaf);
}

inline Nodes const& branchNodes() const {
return nodes(PT_BRANCH);
return nodes(Branch);
}

public:
Expand Down Expand Up @@ -406,7 +399,6 @@ void PathTree_DebugPrintHashDistribution(PathTree* pt);

PathTree* PathTreeNode_Tree(PathTreeNode const* node);
PathTreeNode* PathTreeNode_Parent(PathTreeNode const* node);
PathTreeNodeType PathTreeNode_Type(PathTreeNode const* node);
ushort PathTreeNode_Hash(PathTreeNode const* node);
int PathTreeNode_ComparePath(PathTreeNode* node, int flags, PathMap* candidatePath, void* parameters);
ddstring_t* PathTreeNode_ComposePath2(PathTreeNode const* node, ddstring_t* path, int* length, char delimiter);
Expand All @@ -415,8 +407,6 @@ ddstring_t const* PathTreeNode_PathFragment(PathTreeNode const* node);
void* PathTreeNode_UserData(PathTreeNode const* node);
void PathTreeNode_SetUserData(PathTreeNode* node, void* data);

ddstring_t const* PathTreeNodeType_Name(PathTreeNodeType type);

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
2 changes: 0 additions & 2 deletions doomsday/engine/portable/src/con_data.c
Expand Up @@ -136,8 +136,6 @@ static int clearVariable(PathTreeNode* node, void* parameters)

if(var)
{
assert(PT_LEAF == PathTreeNode_Type(node));

// Detach our user data from this node.
PathTreeNode_SetUserData(node, 0);

Expand Down
30 changes: 6 additions & 24 deletions doomsday/engine/portable/src/def_main.cpp
Expand Up @@ -721,35 +721,17 @@ static void Def_InitTextDef(ddtext_t* txt, char const* str)
txt->text = (char*) M_Realloc(txt->text, strlen(txt->text) + 1);
}

/**
* Callback for DD_ReadProcessDED.
*/
int Def_ReadDEFileHandle(const char* fn, PathTreeNodeType type, void* parm)
void Def_ReadProcessDED(char const* fileName)
{
DENG_UNUSED(parm);
if(!fileName || !fileName[0]) return;

// Skip directories.
if(type == PT_BRANCH)
return true;
if(!App_FileSystem()->checkFileId(fileName))
Con_Message("Warning: Def_ReadProcessDED \"%s\" not found!\n", fileName);

if(App_FileSystem()->checkFileId(fn))
{
if(!DED_Read(&defs, fn))
Con_Error("Def_ReadDEFileHandle: %s\n", dedReadError);
}
else
if(!DED_Read(&defs, fileName))
{
Con_Message("Warning:Def_ReadDEFileHandle \"%s\" not found!\n", fn);
Con_Error("Def_ReadProcessDED: %s\n", dedReadError);
}

// Continue processing files.
return true;
}

void Def_ReadProcessDED(const char* fileName)
{
assert(fileName && fileName[0]);
Def_ReadDEFileHandle(fileName, PT_LEAF, 0);
}

/**
Expand Down

0 comments on commit b67b916

Please sign in to comment.