Skip to content

Commit

Permalink
Refactor|PathTree: PathTree now returns a stack-allocated de::String …
Browse files Browse the repository at this point in the history
…composed path
  • Loading branch information
danij-deng committed Nov 7, 2012
1 parent af3c5ee commit 2bf7f48
Show file tree
Hide file tree
Showing 34 changed files with 211 additions and 205 deletions.
2 changes: 1 addition & 1 deletion doomsday/engine/api/dd_wad.h
Expand Up @@ -48,7 +48,7 @@ uint W_LumpLastModified(lumpnum_t lumpNum);

/// @return Name of the WAD file where the data associated with @a lumpNum resides.
/// Always returns a valid filename (or an empty string).
char const* W_LumpSourceFile(lumpnum_t lumpNum);
AutoStr* W_LumpSourceFile(lumpnum_t lumpNum);

/// @return @c true iff the data associated with @a lumpNum does not originate from the current game.
boolean W_LumpIsCustom(lumpnum_t lumpNum);
Expand Down
6 changes: 3 additions & 3 deletions doomsday/engine/api/doomsday.h
Expand Up @@ -486,9 +486,9 @@ void P_Impulse(int playerNum, int control);
/// @addtogroup map
///@{
// Play: Setup.
boolean P_MapExists(const char* uri);
boolean P_MapIsCustom(const char* uri);
const char* P_MapSourceFile(const char* uri);
boolean P_MapExists(char const* uri);
boolean P_MapIsCustom(char const* uri);
AutoStr* P_MapSourceFile(char const* uri);

boolean P_LoadMap(const char* uri);
///@}
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/file.h
Expand Up @@ -89,7 +89,7 @@ class File1
*
* @return String containing the absolute path.
*/
virtual AutoStr* composePath(char delimiter = '/') const;
virtual String composePath(char delimiter = '/') const;

/// @return @c true iff this file is contained by another.
bool isContained() const;
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/fs_main.h
Expand Up @@ -79,7 +79,7 @@ namespace de
String path;
int attrib;

PathListItem(QString const& _path, int _attrib = 0)
PathListItem(String const& _path, int _attrib = 0)
: path(_path), attrib(_attrib)
{}

Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/p_mapdata.h
Expand Up @@ -259,7 +259,7 @@ boolean P_MapIsCustom(const char* uri);
* @param Uri identifying the map to be searched for.
* @return Fully qualified (i.e., absolute) path to the source file.
*/
const char* P_MapSourceFile(const char* uri);
AutoStr* P_MapSourceFile(char const* uri);

/**
* Begin the process of loading a new map.
Expand Down
19 changes: 7 additions & 12 deletions doomsday/engine/portable/include/pathtree.h
Expand Up @@ -166,21 +166,16 @@ namespace de
int comparePath(PathMap& candidatePath, int flags) const;

/**
* Composes and/or calculates the length of the composed path for this node.
* 'Composing' the path of a node is to downwardly reconstruct the whole path
* for a given node toward the root of the hierarchy.
* Composes the full path for this node. 'Composing' the path of a node is
* to upwardly reconstruct the whole path toward the root of the hierarchy.
*
* @param path If not @c NULL the composed path is written here.
* Previous contents are discarded.
* @param length If not @c NULL the length of the composed path is
* written here.
* @param delimiter Names in the composed path hierarchy will be delimited
* with this character. Paths to branches always include
* a terminating delimiter.
* @param delimiter Names in the composed path hierarchy will be delimited
* with this character. Paths to branches always include
* a terminating delimiter.
*
* @return Same as @a path for caller's convenience.
* @return The composed path.
*/
ddstring_t* composePath(ddstring_t* path, int* length, char delimiter = '/') const;
String composePath(char delimiter = '/') const;

/**
* Sets the user-specified custom pointer.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/wad.h
Expand Up @@ -89,7 +89,7 @@ class Wad : public File1
*
* @return String containing the absolute path.
*/
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');
String composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Retrieve a lump contained by this file.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/zip.h
Expand Up @@ -91,7 +91,7 @@ class Zip : public File1
*
* @return String containing the absolute path.
*/
AutoStr* composeLumpPath(int lumpIdx, char delimiter = '/');
String composeLumpPath(int lumpIdx, char delimiter = '/');

/**
* Retrieve a lump contained by this file.
Expand Down
3 changes: 2 additions & 1 deletion doomsday/engine/portable/src/con_data.cpp
Expand Up @@ -443,7 +443,8 @@ int CVar_Flags(const cvar_t* var)
AutoStr* CVar_ComposePath(cvar_t const* var)
{
DENG_ASSERT(var);
return reinterpret_cast<CVarDirectory::Node*>(var->directoryNode)->composePath(AutoStr_NewStd(), NULL, CVARDIRECTORY_DELIMITER);
QByteArray path = reinterpret_cast<CVarDirectory::Node*>(var->directoryNode)->composePath(CVARDIRECTORY_DELIMITER).toUtf8();
return AutoStr_FromTextStd(path.constData());
}

void CVar_SetUri2(cvar_t* var, Uri const* uri, int svFlags)
Expand Down
10 changes: 6 additions & 4 deletions doomsday/engine/portable/src/dd_main.cpp
Expand Up @@ -2359,7 +2359,8 @@ static bool tryLoadFile(char const* path, size_t baseOffset, de::File1** file)
{
de::FileHandle& hndl = App_FileSystem()->openFile(path, "rb", baseOffset, false /* no duplicates */);

VERBOSE( Con_Message("Loading \"%s\"...\n", F_PrettyPath(Str_Text(hndl.file().composePath()))) )
QByteArray pathUtf8 = hndl.file().composePath().prettyNativePath().toUtf8();
VERBOSE( Con_Message("Loading \"%s\"...\n", pathUtf8.constData()) )
App_FileSystem()->index(hndl.file());

if(file) *file = &hndl.file();
Expand All @@ -2382,20 +2383,21 @@ static bool tryUnloadFile(char const* path)
try
{
de::File1& file = App_FileSystem()->find(path);
QByteArray pathUtf8 = file.composePath().prettyNativePath().toUtf8();

// Do not attempt to unload a resource required by the current game.
if(games->currentGame().isRequiredFile(file))
{
Con_Message("\"%s\" is required by the current game.\n"
"Required game files cannot be unloaded in isolation.\n",
F_PrettyPath(Str_Text(file.composePath())));
pathUtf8.constData());
return false;
}

VERBOSE2( Con_Message("Unloading \"%s\"...\n", F_PrettyPath(Str_Text(file.composePath()))) )
VERBOSE2( Con_Message("Unloading \"%s\"...\n", pathUtf8.constData()) )
App_FileSystem()->deindex(file);
delete &file;
VERBOSE2( Con_Message("Done unloading \"%s\".\n", F_PrettyPath(Str_Text(file.composePath()))) )
VERBOSE2( Con_Message("Done unloading \"%s\".\n", pathUtf8.constData()) )
return true;
}
catch(FS1::NotFoundError const&)
Expand Down
7 changes: 4 additions & 3 deletions doomsday/engine/portable/src/dd_wad.cpp
Expand Up @@ -81,19 +81,20 @@ uint W_LumpLastModified(lumpnum_t absoluteLumpNum)
return 0;
}

char const* W_LumpSourceFile(lumpnum_t absoluteLumpNum)
AutoStr* W_LumpSourceFile(lumpnum_t absoluteLumpNum)
{
try
{
lumpnum_t lumpNum = absoluteLumpNum;
de::File1 const& lump = App_FileSystem()->nameIndexForLump(lumpNum).lump(lumpNum);
return Str_Text(lump.container().composePath());
QByteArray path = lump.container().composePath().toUtf8();
return AutoStr_FromText(path.constData());
}
catch(LumpIndex::NotFoundError const&)
{
W_Error("W_LumpSourceFile: Invalid lumpnum %i.", absoluteLumpNum);
}
return "";
return AutoStr_NewStd();
}

boolean W_LumpIsCustom(lumpnum_t absoluteLumpNum)
Expand Down
3 changes: 2 additions & 1 deletion doomsday/engine/portable/src/def_main.cpp
Expand Up @@ -760,7 +760,8 @@ void Def_ReadLumpDefs(void)

if(!DED_ReadLump(&defs, lump.info().lumpIdx))
{
Con_Error("DD_ReadLumpDefs: Parse error reading \"%s:DD_DEFNS\".\n", Str_Text(lump.container().composePath()));
QByteArray path = lump.container().composePath().toUtf8();
Con_Error("DD_ReadLumpDefs: Parse error reading \"%s:DD_DEFNS\".\n", path.constData());
}
}

Expand Down
8 changes: 4 additions & 4 deletions doomsday/engine/portable/src/file.cpp
Expand Up @@ -59,7 +59,7 @@ bool File1::isContained() const

File1& File1::container() const
{
if(!container_) throw NotContainedError("File1::container", String(Str_Text(composePath())) + " is not contained");
if(!container_) throw NotContainedError("File1::container", "File \"" + composePath() + " is not contained");
return *container_;
}

Expand All @@ -68,12 +68,12 @@ de::FileHandle& File1::handle()
return *handle_;
}

AutoStr* File1::composePath(char delimiter) const
String File1::composePath(char delimiter) const
{
AutoStr* path = Str_Copy(AutoStr_NewStd(), &path_);
String result = String(Str_Text(&path_));
if(delimiter != '/')
throw de::Error("File1::composePath", "Non '/' delimiter not yet implemented");
return path;
return result;
}

uint File1::loadOrderIndex() const
Expand Down
7 changes: 2 additions & 5 deletions doomsday/engine/portable/src/filehandle.cpp
Expand Up @@ -138,11 +138,8 @@ FileHandle* FileHandleBuilder::fromFileLump(File1& container, int lumpIdx, bool
Con_Error("FileHandleBuilder::fromFileLump: Failed on allocation of %lu bytes for data buffer.",
(unsigned long) hndl->d->size);
#if _DEBUG
VERBOSE2(
Con_Printf("FileHandle [%p] buffering \"%s:%s\"...\n", (void*)hndl,
F_PrettyPath(Str_Text(container.composePath())),
F_PrettyPath(Str_Text(file.composePath())));
)
LOG_VERBOSE("FileHandle [%p] buffering \"%s:%s\"...")
<< de::dintptr(hndl) << container.composePath().prettyNativePath() << file.composePath();
#endif
file.read((uint8_t*)hndl->d->data, 0, file.size());
}
Expand Down
29 changes: 16 additions & 13 deletions doomsday/engine/portable/src/fonts.cpp
Expand Up @@ -143,8 +143,8 @@ static inline fontnamespaceid_t namespaceIdForDirectoryNode(FontRepository::Node
static de::Uri composeUriForDirectoryNode(FontRepository::Node const& node)
{
Str const* namespaceName = Fonts_NamespaceName(namespaceIdForDirectoryNode(node));
AutoStr* path = node.composePath(AutoStr_NewStd(), NULL, FONTS_PATH_DELIMITER);
return de::Uri(Str_Text(path), RC_NULL).setScheme(Str_Text(namespaceName));
QByteArray path = node.composePath(FONTS_PATH_DELIMITER).toUtf8();
return de::Uri(path.constData(), RC_NULL).setScheme(Str_Text(namespaceName));
}

/// @pre fontIdMap has been initialized and is large enough!
Expand Down Expand Up @@ -1081,7 +1081,8 @@ AutoStr* Fonts_ComposePath(fontid_t id)
#endif
return AutoStr_NewStd();
}
return node->composePath(AutoStr_NewStd(), NULL, FONTS_PATH_DELIMITER);
QByteArray path = node->composePath(FONTS_PATH_DELIMITER).toUtf8();
return AutoStr_FromTextStd(path.constData());
}

Uri* Fonts_ComposeUri(fontid_t id)
Expand Down Expand Up @@ -1278,7 +1279,7 @@ static void printFontOverview(FontRepository::Node& node, bool printNamespace)
*/
typedef struct {
char delimiter;
const char* like;
de::String like;
int idx;
FontRepository::Node** storage;
} collectdirectorynodeworker_params_t;
Expand All @@ -1287,11 +1288,10 @@ static int collectDirectoryNodeWorker(FontRepository::Node& node, void* paramete
{
collectdirectorynodeworker_params_t* p = (collectdirectorynodeworker_params_t*)parameters;

if(p->like && p->like[0])
if(!p->like.isEmpty())
{
AutoStr* path = node.composePath(AutoStr_NewStd(), NULL, p->delimiter);
int delta = strnicmp(Str_Text(path), p->like, strlen(p->like));
if(delta) return 0; // Continue iteration.
de::String path = node.composePath(p->delimiter);
if(!path.beginsWith(p->like, Qt::CaseInsensitive)) return 0; // Continue iteration.
}

if(p->storage)
Expand All @@ -1307,7 +1307,7 @@ static int collectDirectoryNodeWorker(FontRepository::Node& node, void* paramete
}

static FontRepository::Node** collectDirectoryNodes(fontnamespaceid_t namespaceId,
char const * like, uint* count, FontRepository::Node** storage)
de::String like, uint* count, FontRepository::Node** storage)
{
fontnamespaceid_t fromId, toId;
if(VALID_FONTNAMESPACEID(namespaceId))
Expand Down Expand Up @@ -1360,8 +1360,10 @@ static int composeAndCompareDirectoryNodePaths(void const* a, void const* b)
// Decode paths before determining a lexicographical delta.
FontRepository::Node const& nodeA = **(FontRepository::Node const**)a;
FontRepository::Node const& nodeB = **(FontRepository::Node const**)b;
AutoStr* pathA = Str_PercentDecode(nodeA.composePath(AutoStr_NewStd(), NULL, FONTS_PATH_DELIMITER));
AutoStr* pathB = Str_PercentDecode(nodeB.composePath(AutoStr_NewStd(), NULL, FONTS_PATH_DELIMITER));
QByteArray pathAUtf8 = nodeA.composePath(FONTS_PATH_DELIMITER).toUtf8();
QByteArray pathBUtf8 = nodeB.composePath(FONTS_PATH_DELIMITER).toUtf8();
AutoStr* pathA = Str_PercentDecode(AutoStr_FromTextStd(pathAUtf8.constData()));
AutoStr* pathB = Str_PercentDecode(AutoStr_FromTextStd(pathBUtf8.constData()));
return stricmp(Str_Text(pathA), Str_Text(pathB));
}

Expand Down Expand Up @@ -1524,7 +1526,7 @@ ddstring_t** Fonts_CollectNames(int* rCount)
uint count = 0;
ddstring_t** list = NULL;

FontRepository::Node** foundFonts = collectDirectoryNodes(FN_ANY, NULL, &count, 0);
FontRepository::Node** foundFonts = collectDirectoryNodes(FN_ANY, "", &count, 0);
if(foundFonts)
{
qsort(foundFonts, (size_t)count, sizeof *foundFonts, composeAndCompareDirectoryNodePaths);
Expand All @@ -1537,7 +1539,8 @@ ddstring_t** Fonts_CollectNames(int* rCount)
for(FontRepository::Node** iter = foundFonts; *iter; ++iter)
{
FontRepository::Node& node = **iter;
list[idx++] = Str_FromAutoStr(node.composePath(AutoStr_NewStd(), NULL, FONTS_PATH_DELIMITER));
QByteArray path = node.composePath(FONTS_PATH_DELIMITER).toUtf8();
list[idx++] = Str_Set(Str_NewStd(), path.constData());
}
list[idx] = NULL; // Terminate.
}
Expand Down

0 comments on commit 2bf7f48

Please sign in to comment.