Skip to content

Commit

Permalink
File Sys: Improved utility routines for working with relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Feb 22, 2012
1 parent d573039 commit 71ea346
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
10 changes: 7 additions & 3 deletions doomsday/engine/portable/include/fs_util.h
Expand Up @@ -99,27 +99,31 @@ boolean F_IsAbsolute(const ddstring_t* path);
/**
* @return @c true iff the path can be made into a relative path.
*/
boolean F_IsRelativeToBasePath(const char* path);
boolean F_IsRelativeToBase(const char* path, const char* base);

/**
* Attempt to remove the base path if found at the beginning of the path.
*
* @param dst Potential base-relative path written here.
* @param src Possibly absolute path.
* @param base Base to attempt to remove from @a src.
*
* @return @c true iff the base path was found and removed.
*/
boolean F_RemoveBasePath(ddstring_t* dst, const ddstring_t* src);
boolean F_RemoveBasePath2(ddstring_t* dst, const ddstring_t* src, const ddstring_t* base);
boolean F_RemoveBasePath(ddstring_t* dst, const ddstring_t* src /*, const ddstring_t* base=ddBasePath*/);

/**
* Attempt to prepend the base path. If @a src is already absolute do nothing.
*
* @param dst Absolute path written here.
* @param src Original path.
* @param base Base to attempt to prepend to @a src.
*
* @return @c true iff the path was prepended.
*/
boolean F_PrependBasePath(ddstring_t* dst, const ddstring_t* src);
boolean F_PrependBasePath2(ddstring_t* dst, const ddstring_t* src, const ddstring_t* base);
boolean F_PrependBasePath(ddstring_t* dst, const ddstring_t* src /*, const ddstring_t* base=ddBasePath*/);

/**
* Attempt to prepend the current work path. If @a src is already absolute do nothing.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/fs_main.c
Expand Up @@ -2179,7 +2179,7 @@ int printResourcePath(const ddstring_t* fileNameStr, pathdirectorynode_type_t ty
{
//assert(fileNameStr && VALID_PATHDIRECTORYNODE_TYPE(type));
const char* fileName = Str_Text(fileNameStr);
boolean makePretty = F_IsRelativeToBasePath(fileName);
boolean makePretty = F_IsRelativeToBase(fileName, ddBasePath);
Con_Printf(" %s\n", makePretty? F_PrettyPath(fileName) : fileName);
return 0; // Continue the listing.
}
Expand Down
38 changes: 26 additions & 12 deletions doomsday/engine/portable/src/fs_util.c
Expand Up @@ -347,31 +347,31 @@ void F_ResolveSymbolicPath(ddstring_t* dst, const ddstring_t* src)
Str_Appendf(dst, "%s%s", ddRuntimePath, Str_Text(src));
}

boolean F_IsRelativeToBasePath(const char* path)
boolean F_IsRelativeToBase(const char* path, const char* base)
{
assert(path);
return !strnicmp(path, ddBasePath, strlen(ddBasePath));
assert(path && base);
return !strnicmp(path, base, strlen(base));
}

boolean F_RemoveBasePath(ddstring_t* dst, const ddstring_t* absPath)
boolean F_RemoveBasePath2(ddstring_t* dst, const ddstring_t* absPath, const ddstring_t* basePath)
{
assert(dst && absPath);
assert(dst && absPath && basePath);

if(F_IsRelativeToBasePath(Str_Text(absPath)))
if(F_IsRelativeToBase(Str_Text(absPath), Str_Text(basePath)))
{
boolean mustCopy = (dst == absPath);
if(mustCopy)
{
ddstring_t buf;
Str_Init(&buf);
Str_PartAppend(&buf, Str_Text(absPath), (int)strlen(ddBasePath), Str_Length(absPath) - (int)strlen(ddBasePath));
Str_PartAppend(&buf, Str_Text(absPath), Str_Length(basePath), Str_Length(absPath) - Str_Length(basePath));
Str_Set(dst, Str_Text(&buf));
Str_Free(&buf);
return true;
}

Str_Clear(dst);
Str_PartAppend(dst, Str_Text(absPath), (int)strlen(ddBasePath), Str_Length(absPath) - (int)strlen(ddBasePath));
Str_PartAppend(dst, Str_Text(absPath), Str_Length(basePath), Str_Length(absPath) - Str_Length(basePath));
return true;
}

Expand All @@ -383,6 +383,13 @@ boolean F_RemoveBasePath(ddstring_t* dst, const ddstring_t* absPath)
return false;
}

boolean F_RemoveBasePath(ddstring_t* dst, const ddstring_t* absPath)
{
ddstring_t base;
Str_InitStatic(&base, ddBasePath);
return F_RemoveBasePath2(dst, absPath, &base);
}

boolean F_IsAbsolute(const ddstring_t* str)
{
if(!str)
Expand All @@ -397,15 +404,15 @@ boolean F_IsAbsolute(const ddstring_t* str)
return false;
}

boolean F_PrependBasePath(ddstring_t* dst, const ddstring_t* src)
boolean F_PrependBasePath2(ddstring_t* dst, const ddstring_t* src, const ddstring_t* base)
{
assert(dst && src);
assert(dst && src && base);

if(!F_IsAbsolute(src))
{
if(dst != src)
Str_Set(dst, Str_Text(src));
Str_Prepend(dst, ddBasePath);
Str_Prepend(dst, Str_Text(base));
return true;
}

Expand All @@ -416,6 +423,13 @@ boolean F_PrependBasePath(ddstring_t* dst, const ddstring_t* src)
return false; // Not done.
}

boolean F_PrependBasePath(ddstring_t* dst, const ddstring_t* src)
{
ddstring_t base;
Str_InitStatic(&base, ddBasePath);
return F_PrependBasePath2(dst, src, &base);
}

boolean F_PrependWorkPath(ddstring_t* dst, const ddstring_t* src)
{
assert(dst && src);
Expand Down Expand Up @@ -572,7 +586,7 @@ const char* F_PrettyPath(const char* path)
}

// If within our the base directory cut out the base path.
if(F_IsRelativeToBasePath(path))
if(F_IsRelativeToBase(path, ddBasePath))
{
if(!buf)
{
Expand Down

0 comments on commit 71ea346

Please sign in to comment.