From be21edef36ca52044fa631b8cd3196823937c019 Mon Sep 17 00:00:00 2001 From: skyjake Date: Tue, 3 Jan 2012 08:15:01 +0200 Subject: [PATCH] FS: Added utility for appending a slash to path 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. --- doomsday/engine/portable/include/fs_util.h | 12 +++++++++ doomsday/engine/portable/src/dd_main.c | 6 ++--- doomsday/engine/portable/src/filedirectory.c | 3 +-- doomsday/engine/portable/src/fs_main.c | 15 +++++------ doomsday/engine/portable/src/fs_util.c | 26 ++++++++++++++++--- doomsday/engine/portable/src/game.c | 12 +++------ doomsday/engine/portable/src/sys_reslocator.c | 6 ++--- doomsday/engine/unix/src/dd_uinit.c | 4 +-- doomsday/engine/win32/src/dd_winit.c | 10 +++---- 9 files changed, 56 insertions(+), 38 deletions(-) diff --git a/doomsday/engine/portable/include/fs_util.h b/doomsday/engine/portable/include/fs_util.h index b541c92a6b..a3ea7471a4 100644 --- a/doomsday/engine/portable/include/fs_util.h +++ b/doomsday/engine/portable/include/fs_util.h @@ -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. diff --git a/doomsday/engine/portable/src/dd_main.c b/doomsday/engine/portable/src/dd_main.c index 19850af07d..d1784206c5 100644 --- a/doomsday/engine/portable/src/dd_main.c +++ b/doomsday/engine/portable/src/dd_main.c @@ -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); diff --git a/doomsday/engine/portable/src/filedirectory.c b/doomsday/engine/portable/src/filedirectory.c index e2913501ea..ea6d9aae69 100644 --- a/doomsday/engine/portable/src/filedirectory.c +++ b/doomsday/engine/portable/src/filedirectory.c @@ -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); diff --git a/doomsday/engine/portable/src/fs_main.c b/doomsday/engine/portable/src/fs_main.c index fdf33df97b..698a2a4f73 100644 --- a/doomsday/engine/portable/src/fs_main.c +++ b/doomsday/engine/portable/src/fs_main.c @@ -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; } @@ -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); @@ -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); @@ -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); diff --git a/doomsday/engine/portable/src/fs_util.c b/doomsday/engine/portable/src/fs_util.c index 4253188a11..0584a233e9 100644 --- a/doomsday/engine/portable/src/fs_util.c +++ b/doomsday/engine/portable/src/fs_util.c @@ -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; @@ -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); @@ -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); } diff --git a/doomsday/engine/portable/src/game.c b/doomsday/engine/portable/src/game.c index 13d88e037f..aec9f95d85 100644 --- a/doomsday/engine/portable/src/game.c +++ b/doomsday/engine/portable/src/game.c @@ -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); @@ -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); diff --git a/doomsday/engine/portable/src/sys_reslocator.c b/doomsday/engine/portable/src/sys_reslocator.c index 6ae927c930..90e231faa6 100644 --- a/doomsday/engine/portable/src/sys_reslocator.c +++ b/doomsday/engine/portable/src/sys_reslocator.c @@ -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; \ } \ @@ -645,8 +644,7 @@ static void createPackagesResourceNamespace(void) } else { - if(Str_RAt(doomWadDir, 0) != '/') - Str_AppendChar(doomWadDir, '/'); + F_AppendMissingSlash(doomWadDir); } } diff --git a/doomsday/engine/unix/src/dd_uinit.c b/doomsday/engine/unix/src/dd_uinit.c index 4445b105b1..ca7d15aa79 100644 --- a/doomsday/engine/unix/src/dd_uinit.c +++ b/doomsday/engine/unix/src/dd_uinit.c @@ -50,6 +50,7 @@ #include "de_network.h" #include "de_misc.h" +#include "fs_util.h" #include "dd_uinit.h" // MACROS ------------------------------------------------------------------ @@ -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)); diff --git a/doomsday/engine/win32/src/dd_winit.c b/doomsday/engine/win32/src/dd_winit.c index 74ba969d42..941a168777 100644 --- a/doomsday/engine/win32/src/dd_winit.c +++ b/doomsday/engine/win32/src/dd_winit.c @@ -54,6 +54,7 @@ #include "de_misc.h" #include "de_ui.h" +#include "fs_util.h" #include "dd_winit.h" // MACROS ------------------------------------------------------------------ @@ -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); @@ -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)); @@ -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)