Skip to content

Commit

Permalink
FS: Added utility for appending a slash to path
Browse files Browse the repository at this point in the history
There was a common pattern of checking if the last character
of a path was a slash and appending one if it was missing.
A proper function for doing this was added to fs_util.
  • Loading branch information
skyjake committed Jan 3, 2012
1 parent e7c324a commit be21ede
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 38 deletions.
12 changes: 12 additions & 0 deletions doomsday/engine/portable/include/fs_util.h
Expand Up @@ -66,6 +66,18 @@ boolean F_MakePath(const char* path);
*/
boolean F_FixSlashes(ddstring_t* dst, const ddstring_t* src);

/**
* Appends a slash at the end of @a pathStr if there isn't one.
* @return @c true if a slash was appended, @c false otherwise.
*/
boolean F_AppendMissingSlash(ddstring_t* pathStr);

/**
* Appends a slash at the end of @a path if there isn't one.
* @return @c true if a slash was appended, @c false otherwise.
*/
boolean F_AppendMissingSlashCString(char* path, size_t maxLen);

/**
* Converts directory slashes to tha used by the host file system.
* @return @c true iff the path was modified.
Expand Down
6 changes: 2 additions & 4 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -1336,16 +1336,14 @@ int DD_EarlyInit(void)
Str_Strip(&dataPath);
F_FixSlashes(&dataPath, &dataPath);
F_ExpandBasePath(&dataPath, &dataPath);
if(Str_RAt(&dataPath, 0) != '/')
Str_AppendChar(&dataPath, '/');
F_AppendMissingSlash(&dataPath);

Str_Init(&defsPath);
Str_Set(&defsPath, DD_BASEPATH_DEFS);
Str_Strip(&defsPath);
F_FixSlashes(&defsPath, &defsPath);
F_ExpandBasePath(&defsPath, &defsPath);
if(Str_RAt(&defsPath, 0) != '/')
Str_AppendChar(&defsPath, '/');
F_AppendMissingSlash(&defsPath);

theGame = nullGame = Game_New("null-game", &dataPath, &defsPath, "doomsday", 0, 0);

Expand Down
3 changes: 1 addition & 2 deletions doomsday/engine/portable/src/filedirectory.c
Expand Up @@ -222,8 +222,7 @@ static void resolveAndAddSearchPathsToDirectory(FileDirectory* fd,

// Let's try to make it a relative path.
F_RemoveBasePath(searchPath, searchPath);
if(Str_RAt(searchPath, 0) != '/')
Str_AppendChar(searchPath, '/');
F_AppendMissingSlash(searchPath);

addPaths(fd, (const ddstring_t**)&searchPath, 1, callback, paramaters);
Str_Delete(searchPath);
Expand Down
15 changes: 7 additions & 8 deletions doomsday/engine/portable/src/fs_main.c
Expand Up @@ -1060,8 +1060,10 @@ static foundentry_t* collectLocalPaths(const ddstring_t* searchPath, int* retCou
Str_Init(&found[count].path);
Str_Set(&found[count].path, fd.name);
F_FixSlashes(&found[count].path, &found[count].path);
if((fd.attrib & A_SUBDIR) && '/' != Str_RAt(&found[count].path, 0))
Str_AppendChar(&found[count].path, '/');
if(fd.attrib & A_SUBDIR)
{
F_AppendMissingSlash(&found[count].path);
}
found[count].attrib = fd.attrib;
++count;
}
Expand Down Expand Up @@ -1988,8 +1990,7 @@ void F_AddVirtualDirectoryMapping(const char* source, const char* destination)
Str_Init(&src); Str_Set(&src, source);
Str_Strip(&src);
F_FixSlashes(&src, &src);
if(Str_RAt(&src, 0) != '/')
Str_AppendChar(&src, '/');
F_AppendMissingSlash(&src);
F_ExpandBasePath(&src, &src);
F_PrependWorkPath(&src, &src);

Expand Down Expand Up @@ -2020,8 +2021,7 @@ void F_AddVirtualDirectoryMapping(const char* source, const char* destination)
Str_Set(&vdm->destination, destination);
Str_Strip(&vdm->destination);
F_FixSlashes(&vdm->destination, &vdm->destination);
if(Str_RAt(&vdm->destination, 0) != '/')
Str_AppendChar(&vdm->destination, '/');
F_AppendMissingSlash(&vdm->destination);
F_ExpandBasePath(&vdm->destination, &vdm->destination);
F_PrependWorkPath(&vdm->destination, &vdm->destination);

Expand Down Expand Up @@ -2076,8 +2076,7 @@ static void printVFDirectory(const ddstring_t* path)
Str_Init(&dir); Str_Set(&dir, Str_Text(path));
Str_Strip(&dir);
// Make sure it ends in a directory separator character.
if(Str_RAt(&dir, 0) != '/')
Str_AppendChar(&dir, '/');
F_AppendMissingSlash(&dir);
if(!F_ExpandBasePath(&dir, &dir))
F_PrependBasePath(&dir, &dir);

Expand Down
26 changes: 22 additions & 4 deletions doomsday/engine/portable/src/fs_util.c
Expand Up @@ -198,6 +198,26 @@ boolean F_FixSlashes(ddstring_t* dstStr, const ddstring_t* srcStr)
return result;
}

boolean F_AppendMissingSlash(ddstring_t* pathStr)
{
if(Str_RAt(pathStr, 0) != '/')
{
Str_AppendChar(pathStr, '/');
return true;
}
return false;
}

boolean F_AppendMissingSlashCString(char* path, size_t maxLen)
{
if(path[strlen(path) - 1] != '/')
{
strncat(path, "/", maxLen);
return true;
}
return false;
}

boolean F_ToNativeSlashes(ddstring_t* dstStr, const ddstring_t* srcStr)
{
boolean result = false;
Expand Down Expand Up @@ -453,8 +473,7 @@ boolean F_ExpandBasePath(ddstring_t* dst, const ddstring_t* src)

Str_Set(&homeStr, getenv("HOME"));
F_FixSlashes(&buf, &homeStr);
if(Str_RAt(&buf, 0) != '/')
Str_AppendChar(&buf, '/');
F_AppendMissingSlash(&buf);

// Append the rest of the original path.
Str_PartAppend(&buf, Str_Text(src), 2, Str_Length(src)-2);
Expand Down Expand Up @@ -483,8 +502,7 @@ boolean F_ExpandBasePath(ddstring_t* dst, const ddstring_t* src)
Str_Init(&pwStr);
Str_Set(&pwStr, pw->pw_dir);
F_FixSlashes(&buf, &pwStr);
if(Str_RAt(&buf, 0) != '/')
Str_AppendChar(&buf, '/');
F_AppendMissingSlash(&buf);
result = true;
Str_Free(&pwStr);
}
Expand Down
12 changes: 4 additions & 8 deletions doomsday/engine/portable/src/game.c
Expand Up @@ -88,16 +88,14 @@ Game* Game_New(const char* identityKey, const ddstring_t* dataPath,
Str_Appendf(&g->mainConfig, "configs/%s", configDir);
Str_Strip(&g->mainConfig);
F_FixSlashes(&g->mainConfig, &g->mainConfig);
if(Str_RAt(&g->mainConfig, 0) != '/')
Str_AppendChar(&g->mainConfig, '/');
F_AppendMissingSlash(&g->mainConfig);
Str_Append(&g->mainConfig, "game.cfg");

Str_Init(&g->bindingConfig);
Str_Appendf(&g->bindingConfig, "configs/%s", configDir);
Str_Strip(&g->bindingConfig);
F_FixSlashes(&g->bindingConfig, &g->bindingConfig);
if(Str_RAt(&g->bindingConfig, 0) != '/')
Str_AppendChar(&g->bindingConfig, '/');
F_AppendMissingSlash(&g->bindingConfig);
Str_Append(&g->bindingConfig, "player/bindings.cfg");

Str_Init(&g->title);
Expand Down Expand Up @@ -259,15 +257,13 @@ Game* Game_FromDef(const GameDef* def)
Str_Strip(&dataPath);
F_FixSlashes(&dataPath, &dataPath);
F_ExpandBasePath(&dataPath, &dataPath);
if(Str_RAt(&dataPath, 0) != '/')
Str_AppendChar(&dataPath, '/');
F_AppendMissingSlash(&dataPath);

Str_Init(&defsPath); Str_Set(&defsPath, def->defsPath);
Str_Strip(&defsPath);
F_FixSlashes(&defsPath, &defsPath);
F_ExpandBasePath(&defsPath, &defsPath);
if(Str_RAt(&defsPath, 0) != '/')
Str_AppendChar(&defsPath, '/');
F_AppendMissingSlash(&defsPath);

game = Game_New(def->identityKey, &dataPath, &defsPath, def->configDir,
def->defaultTitle, def->defaultAuthor);
Expand Down
6 changes: 2 additions & 4 deletions doomsday/engine/portable/src/sys_reslocator.c
Expand Up @@ -598,8 +598,7 @@ static void createPackagesResourceNamespace(void)
char last = Str_RAt(path, 0); \
ddstring_t* pathCopy = Str_New(); \
F_FixSlashes(pathCopy, path); \
if(Str_RAt(pathCopy, 0) != '/') \
Str_AppendChar(pathCopy, '/'); \
F_AppendMissingSlash(pathCopy); \
doomWadPaths = realloc(doomWadPaths, sizeof(*doomWadPaths) * ++doomWadPathsCount); \
doomWadPaths[doomWadPathsCount-1] = pathCopy; \
} \
Expand Down Expand Up @@ -645,8 +644,7 @@ static void createPackagesResourceNamespace(void)
}
else
{
if(Str_RAt(doomWadDir, 0) != '/')
Str_AppendChar(doomWadDir, '/');
F_AppendMissingSlash(doomWadDir);
}
}

Expand Down
4 changes: 2 additions & 2 deletions doomsday/engine/unix/src/dd_uinit.c
Expand Up @@ -50,6 +50,7 @@
#include "de_network.h"
#include "de_misc.h"

#include "fs_util.h"
#include "dd_uinit.h"

// MACROS ------------------------------------------------------------------
Expand Down Expand Up @@ -259,8 +260,7 @@ static void determineGlobalPaths(application_t* app)
strncpy(runtimePath, ArgNext(), FILENAME_T_MAXLEN);
Dir_CleanPath(runtimePath, FILENAME_T_MAXLEN);
// Ensure the path is closed with a directory separator.
if(runtimePath[strlen(runtimePath)-1] != '/')
strncat(runtimePath, "/", FILENAME_T_MAXLEN);
F_AppendMissingSlashCString(runtimePath, FILENAME_T_MAXLEN);

temp = Dir_New(runtimePath);
app->usingUserDir = Dir_SetCurrent(Dir_Path(temp));
Expand Down
10 changes: 4 additions & 6 deletions doomsday/engine/win32/src/dd_winit.c
Expand Up @@ -54,6 +54,7 @@
#include "de_misc.h"
#include "de_ui.h"

#include "fs_util.h"
#include "dd_winit.h"

// MACROS ------------------------------------------------------------------
Expand Down Expand Up @@ -337,8 +338,7 @@ static void determineGlobalPaths(application_t* app)

dd_snprintf(path, FILENAME_T_MAXLEN, "%s", DENG_LIBRARY_DIR);
// Ensure it ends with a directory separator.
if(path[strlen(path)-1] != '/')
strncat(path, "/", FILENAME_T_MAXLEN);
F_AppendMissingSlashCString(path, FILENAME_T_MAXLEN);
Dir_MakeAbsolutePath(path);
temp = Dir_ConstructFromPathDir(path);
strncpy(ddBinPath, Str_Text(temp), FILENAME_T_MAXLEN);
Expand Down Expand Up @@ -370,8 +370,7 @@ static void determineGlobalPaths(application_t* app)
strncpy(runtimePath, ArgNext(), FILENAME_T_MAXLEN);
Dir_CleanPath(runtimePath, FILENAME_T_MAXLEN);
// Ensure the path is closed with a directory separator.
if(runtimePath[strlen(runtimePath)-1] != '/')
strncat(runtimePath, "/", FILENAME_T_MAXLEN);
F_AppendMissingSlashCString(runtimePath, FILENAME_T_MAXLEN);

temp = Dir_New(runtimePath);
app->usingUserDir = Dir_SetCurrent(Dir_Path(temp));
Expand Down Expand Up @@ -403,8 +402,7 @@ static void determineGlobalPaths(application_t* app)
Dir_CleanPath(ddBasePath, FILENAME_T_MAXLEN);
Dir_MakeAbsolutePath(ddBasePath, FILENAME_T_MAXLEN);
// Ensure it ends with a directory separator.
if(ddBasePath[strlen(ddBasePath)-1] != '/')
strncat(ddBasePath, "/", FILENAME_T_MAXLEN);
F_AppendMissingSlashCString(ddBasePath, FILENAME_T_MAXLEN);
}

static BOOL createMainWindow(int lnCmdShow)
Expand Down

0 comments on commit be21ede

Please sign in to comment.