Skip to content

Commit

Permalink
Refactor: Switched our internal directory delimiter to '/' (was native)
Browse files Browse the repository at this point in the history
Doomsday now represents all paths using a forward slash for hierarchy
delimiters internally. All paths are now normalized before being used
for building PathDirectorys, indexing files from containers, etc...

The only times a path is now converted to OS-native delimiters is when
dealing with real files on the local system or when being displayed to
the user (e.g., console and log).

These changes meant that the kludge implemented in de::Uri, which would
automatically convert delimiters to native ones has now been removed.
  • Loading branch information
danij-deng committed Nov 27, 2011
1 parent 2669777 commit 404940e
Show file tree
Hide file tree
Showing 26 changed files with 311 additions and 228 deletions.
3 changes: 1 addition & 2 deletions doomsday/engine/portable/include/filedirectory.h
Expand Up @@ -30,8 +30,7 @@

#include "pathdirectory.h"

// Default path delimiter is the platform-specific directory separator.
#define FILEDIRECTORY_DELIMITER DIR_SEP_CHAR
#define FILEDIRECTORY_DELIMITER '/'

/**
* Callback function type for FileDirectory::Iterate
Expand Down
8 changes: 7 additions & 1 deletion doomsday/engine/portable/include/fs_util.h
Expand Up @@ -61,11 +61,17 @@ uint F_LastModified(const char* path);
boolean F_MakePath(const char* path);

/**
* Converts directory slashes to the correct type of slash.
* Converts directory slashes to our internal '/'.
* @return @c true iff the path was modified.
*/
boolean F_FixSlashes(ddstring_t* dst, const ddstring_t* src);

/**
* Converts directory slashes to tha used by the host file system.
* @return @c true iff the path was modified.
*/
boolean F_ToNativeSlashes(ddstring_t* dst, const ddstring_t* src);

/**
* Convert the symbolic path into a real path.
* \todo dj: This seems rather redundant; refactor callers.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/include/r_things.h
Expand Up @@ -44,7 +44,7 @@
// for all views.

typedef struct {
boolean rotate; // If false use 0 for any position
byte rotate; // 0= no rotations, 1= only front, 2= more...
material_t* mats[8]; // Material to use for view angles 0-7
byte flip[8]; // Flip (1 = flip) to use for view angles 0-7
} spriteframe_t;
Expand Down
7 changes: 6 additions & 1 deletion doomsday/engine/portable/include/sys_direc.h
Expand Up @@ -78,7 +78,7 @@ void Dir_CleanPath(char* path, size_t len);

/**
* @return Absolute path to the current working directory for the default drive.
* Always ends with a @c DIR_SEP_CHAR. Path must be released with free()
* Always ends with a '/'. Path must be released with free()
* @c NULL if we are out of memory.
*/
char* Dir_CurrentPath(void);
Expand All @@ -93,6 +93,11 @@ void Dir_FileName(char* name, const char* path, size_t len);
*/
void Dir_ToNativeSeparators(char* path, size_t len);

/**
* Convert directory separators in @a path to our internal '/' form.
*/
void Dir_FixSeparators(char* path, size_t len);

/// @return @c true if @a path is absolute.
int Dir_IsAbsolutePath(const char* path);

Expand Down
37 changes: 25 additions & 12 deletions doomsday/engine/portable/src/con_config.c
Expand Up @@ -155,11 +155,17 @@ static __inline void writeAliasesToFile(FILE* file)

static boolean writeConsoleState(const char* fileName)
{
if(!fileName || !fileName[0])
return false;
ddstring_t nativePath;
FILE* file;
if(!fileName || !fileName[0]) return false;

{ FILE* file;
if((file = fopen(fileName, "wt")))
Str_Init(&nativePath);
Str_Set(&nativePath, fileName);
F_ToNativeSlashes(&nativePath, &nativePath);

file = fopen(Str_Text(&nativePath), "wt");
Str_Free(&nativePath);
if(file)
{
writeHeaderComment(file);
fprintf(file, "#\n# CONSOLE VARIABLES\n#\n\n");
Expand All @@ -169,24 +175,31 @@ static boolean writeConsoleState(const char* fileName)
writeAliasesToFile(file);
fclose(file);
return true;
}}
Con_Message("writeConsoleState: Can't open %s for writing.\n", fileName);
}
Con_Message("Warning:writeConsoleState: Failed opening \"%s\" for writing.\n", F_PrettyPath(fileName));
return false;
}

static boolean writeBindingsState(const char* fileName)
{
if(!fileName || !fileName[0])
return false;
{ FILE* file;
if((file = fopen(fileName, "wt")))
ddstring_t nativePath;
FILE* file;
if(!fileName || !fileName[0]) return false;

Str_Init(&nativePath);
Str_Set(&nativePath, fileName);
F_ToNativeSlashes(&nativePath, &nativePath);

file = fopen(Str_Text(&nativePath), "wt");
Str_Free(&nativePath);
if(file)
{
writeHeaderComment(file);
B_WriteToFile(file);
fclose(file);
return true;
}}
Con_Message("Con_WriteState: Can't open %s for writing.\n", fileName);
}
Con_Message("Warning:writeBindingsState: Failed opening \"%s\" for writing.\n", F_PrettyPath(fileName));
return false;
}

Expand Down
3 changes: 1 addition & 2 deletions doomsday/engine/portable/src/dam_main.c
Expand Up @@ -404,7 +404,7 @@ ddstring_t* DAM_ComposeCacheDir(const char* sourcePath)
ddstring_t mapFileName;
ddstring_t* path;

if(NULL == sourcePath || !sourcePath[0]) return NULL;
if(!sourcePath || !sourcePath[0]) return NULL;

gameIdentityKey = GameInfo_IdentityKey(DD_GameInfo());
mapPathIdentifier = calculateIdentifierForMapPath(sourcePath);
Expand All @@ -415,7 +415,6 @@ ddstring_t* DAM_ComposeCacheDir(const char* sourcePath)
path = Str_New();
Str_Appendf(path, "%s%s/%s-%04X/", mapCacheDir, Str_Text(gameIdentityKey),
Str_Text(&mapFileName), mapPathIdentifier);
F_FixSlashes(path, path);
F_ExpandBasePath(path, path);

Str_Free(&mapFileName);
Expand Down
5 changes: 1 addition & 4 deletions doomsday/engine/portable/src/dd_help.c
Expand Up @@ -291,8 +291,7 @@ void DD_InitHelp(void)
{
float starttime;

if(helpInited)
return; // Already inited.
if(helpInited) return; // Already inited.

VERBOSE( Con_Message("Initializing Help subsystem...\n") )
starttime = (verbose >= 2? Sys_GetSeconds() : 0);
Expand All @@ -303,7 +302,6 @@ void DD_InitHelp(void)
// Parse the control panel help file.
{ ddstring_t helpFileName; Str_Init(&helpFileName);
Str_Set(&helpFileName, DD_BASEPATH_DATA"cphelp.txt");
F_FixSlashes(&helpFileName, &helpFileName);
F_ExpandBasePath(&helpFileName, &helpFileName);

DH_ReadStrings(Str_Text(&helpFileName));
Expand All @@ -329,7 +327,6 @@ void DD_ReadGameHelp(void)

Str_Init(&helpFileName);
Str_Appendf(&helpFileName, "%sconhelp.txt", Str_Text(GameInfo_DataPath(DD_GameInfo())));
F_FixSlashes(&helpFileName, &helpFileName);
F_ExpandBasePath(&helpFileName, &helpFileName);
DH_ReadStrings(Str_Text(&helpFileName));
Str_Free(&helpFileName);
Expand Down
35 changes: 18 additions & 17 deletions doomsday/engine/portable/src/dd_main.c
Expand Up @@ -200,17 +200,15 @@ static gameinfo_t* findGameInfoForCmdlineFlag(const char* cmdlineFlag)

static void addToPathList(ddstring_t*** list, size_t* listSize, const char* rawPath)
{
assert(list && listSize && rawPath && rawPath[0]);
{
ddstring_t* newPath = Str_New();
assert(list && listSize && rawPath && rawPath[0]);

Str_Set(newPath, rawPath);
F_FixSlashes(newPath, newPath);
F_ExpandBasePath(newPath, newPath);

*list = realloc(*list, sizeof(**list) * ++(*listSize)); /// \fixme This is never freed!
(*list)[(*listSize)-1] = newPath;
}
}

static void parseStartupFilePathsAndAddFiles(const char* pathString)
Expand Down Expand Up @@ -407,16 +405,16 @@ gameid_t DD_AddGame(const char* identityKey, const char* _dataPath, const char*
Str_Strip(&dataPath);
F_FixSlashes(&dataPath, &dataPath);
F_ExpandBasePath(&dataPath, &dataPath);
if(Str_RAt(&dataPath, 0) != DIR_SEP_CHAR)
Str_AppendChar(&dataPath, DIR_SEP_CHAR);
if(Str_RAt(&dataPath, 0) != '/')
Str_AppendChar(&dataPath, '/');

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

Str_Init(&cmdlineFlag);
Str_Init(&cmdlineFlag2);
Expand Down Expand Up @@ -807,7 +805,7 @@ static int addFilesFromAutoData(boolean loadFiles)
for(i = 0; extensions[i]; ++i)
{
Str_Clear(&pattern);
Str_Appendf(&pattern, "%sauto"DIR_SEP_STR"*.%s", Str_Text(GameInfo_DataPath(DD_GameInfo())), extensions[i]);
Str_Appendf(&pattern, "%sauto/*.%s", Str_Text(GameInfo_DataPath(DD_GameInfo())), extensions[i]);
F_AllResourcePaths2(Str_Text(&pattern), autoDataAdder, (void*)&data);
}}
Str_Free(&pattern);
Expand Down Expand Up @@ -1349,16 +1347,16 @@ int DD_EarlyInit(void)
Str_Strip(&dataPath);
F_FixSlashes(&dataPath, &dataPath);
F_ExpandBasePath(&dataPath, &dataPath);
if(Str_RAt(&dataPath, 0) != DIR_SEP_CHAR)
Str_AppendChar(&dataPath, DIR_SEP_CHAR);
if(Str_RAt(&dataPath, 0) != '/')
Str_AppendChar(&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) != DIR_SEP_CHAR)
Str_AppendChar(&defsPath, DIR_SEP_CHAR);
if(Str_RAt(&defsPath, 0) != '/')
Str_AppendChar(&defsPath, '/');

currentGameInfoIndex = gameInfoIndex(addGameInfoRecord(0, "null-game", &dataPath, &defsPath, "doomsday.cfg", 0, 0, 0, 0));

Expand Down Expand Up @@ -2336,6 +2334,7 @@ D_CMD(Load)
{
boolean didLoadGame = false, didLoadResource = false;
ddstring_t foundPath, searchPath;
gameinfo_t* info;
int arg = 1;

Str_Init(&searchPath);
Expand All @@ -2346,18 +2345,19 @@ D_CMD(Load)
Str_Free(&searchPath);
return false;
}
F_FixSlashes(&searchPath, &searchPath);

// Ignore attempts to load directories.
if(Str_RAt(&searchPath, 0) == DIR_SEP_CHAR || Str_RAt(&searchPath, 0) == DIR_WRONG_SEP_CHAR)
if(Str_RAt(&searchPath, 0) == '/')
{
Con_Message("Directories cannot be \"loaded\" (only files and/or known games).\n");
Str_Free(&searchPath);
return true;
}

// Are we loading a game?
{ gameinfo_t* info = findGameInfoForIdentityKey(Str_Text(&searchPath));
if(NULL != info)
info = findGameInfoForIdentityKey(Str_Text(&searchPath));
if(info)
{
if(!allGameResourcesFound(info))
{
Expand All @@ -2374,7 +2374,7 @@ D_CMD(Load)
}
didLoadGame = true;
++arg;
}}
}

// Try the resource locator.
Str_Init(&foundPath);
Expand Down Expand Up @@ -2420,9 +2420,10 @@ D_CMD(Unload)
Str_Free(&searchPath);
return false;
}
F_FixSlashes(&searchPath, &searchPath);

// Ignore attempts to unload directories.
if(Str_RAt(&searchPath, 0) == DIR_SEP_CHAR || Str_RAt(&searchPath, 0) == DIR_WRONG_SEP_CHAR)
if(Str_RAt(&searchPath, 0) == '/')
{
Con_Message("Directories cannot be \"unloaded\" (only files and/or known games).\n");
Str_Free(&searchPath);
Expand Down
9 changes: 7 additions & 2 deletions doomsday/engine/portable/src/dd_pinit.c
Expand Up @@ -189,10 +189,15 @@ void DD_InitCommandLine(const char* cmdLine)
void DD_ConsoleInit(void)
{
const char* outFileName = "doomsday.out";
ddstring_t nativePath;

// We'll redirect stdout to a log file.
DD_CheckArg("-out", &outFileName);
outFile = fopen(outFileName, "w");
Str_Init(&nativePath); Str_Set(&nativePath, outFileName);
F_ToNativeSlashes(&nativePath, &nativePath);

// We'll redirect stdout to a log file.
outFile = fopen(Str_Text(&nativePath), "w");
Str_Free(&nativePath);
if(!outFile)
{
DD_ErrorBox(false, "Couldn't open message output file.");
Expand Down
10 changes: 5 additions & 5 deletions doomsday/engine/portable/src/edit_bias.c
Expand Up @@ -391,26 +391,26 @@ static boolean SBE_Save(const char* name)
FILE* file;

Str_Init(&fileName);
if(NULL == name || !name[0])
if(!name || !name[0])
{
Str_Appendf(&fileName, "%s.ded", P_MapUri(map));
}
else
{
Str_Set(&fileName, name);
F_FixSlashes(&fileName, &fileName);
F_ExpandBasePath(&fileName, &fileName);
// Do we need to append an extension?
if(NULL == F_FindFileExtension(Str_Text(&fileName)))
if(!F_FindFileExtension(Str_Text(&fileName)))
{
Str_Append(&fileName, ".ded");
}
}

F_ToNativeSlashes(&fileName, &fileName);
file = fopen(Str_Text(&fileName), "wt");
if(NULL == file)
if(!file)
{
Con_Message("Warning failed to open \"%s\" for writing. Bias Lights not saved.\n", Str_Text(&fileName));
Con_Message("Warning failed to open \"%s\" for writing. Bias Lights not saved.\n", F_PrettyPath(Str_Text(&fileName)));
Str_Free(&fileName);
return false;
}
Expand Down

0 comments on commit 404940e

Please sign in to comment.