Normalize slashes in paths unless they are direct user input (XDG_DATA_HOME/APPDATA, specifically)

This commit is contained in:
Bart van Strien
2014-07-15 13:16:37 +02:00
parent a658c7df76
commit 59ab602111
+26 -15
View File
@@ -21,6 +21,7 @@
#include "common/config.h" #include "common/config.h"
#include <iostream> #include <iostream>
#include <sstream>
#include "common/utf8.h" #include "common/utf8.h"
#include "common/b64.h" #include "common/b64.h"
@@ -47,6 +48,21 @@ namespace
{ {
return input.substr(getDriveDelim(input)+1); return input.substr(getDriveDelim(input)+1);
} }
std::string normalize(const std::string &input)
{
std::stringstream out;
bool seenSep = false, isSep = false;
for (size_t i = 0; i < input.size(); ++i)
{
isSep = (input[i] == LOVE_PATH_SEPARATOR[0]);
if (!isSep || !seenSep)
out << input[i];
seenSep = isSep;
}
return out.str();
}
} }
namespace love namespace love
@@ -116,6 +132,8 @@ bool Filesystem::setIdentity(const char *ident, bool appendToPath)
else else
save_path_full += save_path_relative; save_path_full += save_path_relative;
save_path_full = normalize(save_path_full);
// We now have something like: // We now have something like:
// save_identity: game // save_identity: game
// save_path_relative: ./LOVE/game // save_path_relative: ./LOVE/game
@@ -354,40 +372,33 @@ const char *Filesystem::getWorkingDirectory()
std::string Filesystem::getUserDirectory() std::string Filesystem::getUserDirectory()
{ {
return std::string(PHYSFS_getUserDir()); static std::string userDir = normalize(PHYSFS_getUserDir());
return userDir;
} }
std::string Filesystem::getAppdataDirectory() std::string Filesystem::getAppdataDirectory()
{ {
#ifdef LOVE_WINDOWS
if (appdata.empty()) if (appdata.empty())
{ {
#ifdef LOVE_WINDOWS
wchar_t *w_appdata = _wgetenv(L"APPDATA"); wchar_t *w_appdata = _wgetenv(L"APPDATA");
appdata = to_utf8(w_appdata); appdata = to_utf8(w_appdata);
replace_char(appdata, '\\', '/'); replace_char(appdata, '\\', '/');
}
return appdata;
#elif defined(LOVE_MACOSX) #elif defined(LOVE_MACOSX)
if (appdata.empty())
{
std::string udir = getUserDirectory(); std::string udir = getUserDirectory();
udir.append("/Library/Application Support"); udir.append("/Library/Application Support");
appdata = udir; appdata = normalize(udir);
}
return appdata;
#elif defined(LOVE_LINUX) #elif defined(LOVE_LINUX)
if (appdata.empty())
{
char *xdgdatahome = getenv("XDG_DATA_HOME"); char *xdgdatahome = getenv("XDG_DATA_HOME");
if (!xdgdatahome) if (!xdgdatahome)
appdata = std::string(getUserDirectory()) + "/.local/share/"; appdata = normalize(std::string(getUserDirectory()) + "/.local/share/");
else else
appdata = xdgdatahome; appdata = xdgdatahome;
#else
appdata = getUserDirectory();
#endif
} }
return appdata; return appdata;
#else
return getUserDirectory();
#endif
} }