Skip to content
Browse files

URIUtils: add 'CanonicalizePath' function

  • Loading branch information...
1 parent 0af940a commit 206e1f10e9c7564061a920959195972ecb9d2a85 @Karlson2k Karlson2k committed Oct 17, 2013
Showing with 45 additions and 0 deletions.
  1. +33 −0 xbmc/utils/URIUtils.cpp
  2. +12 −0 xbmc/utils/URIUtils.h
View
33 xbmc/utils/URIUtils.cpp
@@ -1059,6 +1059,39 @@ std::string URIUtils::FixSlashesAndDups(const std::string& path, const char slas
}
+std::string URIUtils::CanonicalizePath(const std::string& path, const char slashCharacter /*= '\\'*/)
+{
+ assert(slashCharacter == '\\' || slashCharacter == '/');
+
+ if (path.empty())
+ return path;
+
+ const std::string slashStr(1, slashCharacter);
+ vector<std::string> pathVec, resultVec;
+ StringUtils::Tokenize(path, pathVec, slashStr);
+
+ for (vector<std::string>::const_iterator it = pathVec.begin(); it != pathVec.end(); ++it)
+ {
+ if (*it == ".")
+ { /* skip - do nothing */ }
+ else if (*it == ".." && !resultVec.empty() && resultVec.back() != "..")
+ resultVec.pop_back();
+ else
+ resultVec.push_back(*it);
+ }
+
+ std::string result;
+ if (path[0] == slashCharacter)
+ result.push_back(slashCharacter); // add slash at the begin
+
+ result += StringUtils::Join(resultVec, slashStr);
+
+ if (path[path.length() - 1] == slashCharacter && !result.empty() && result[result.length() - 1] != slashCharacter)
+ result.push_back(slashCharacter); // add slash at the end if result isn't empty and result isn't "/"
+
+ return result;
+}
+
CStdString URIUtils::AddFileToFolder(const CStdString& strFolder,
const CStdString& strFile)
{
View
12 xbmc/utils/URIUtils.h
@@ -132,6 +132,18 @@ class URIUtils
static void RemoveSlashAtEnd(std::string& strFolder);
static bool CompareWithoutSlashAtEnd(const CStdString& strPath1, const CStdString& strPath2);
static std::string FixSlashesAndDups(const std::string& path, const char slashCharacter = '/', const size_t startFrom = 0);
+ /**
+ * Convert path to form without duplicated slashes and without relative directories
+ * Strip duplicated slashes
+ * Resolve and remove relative directories ("/../" and "/./")
+ * Will ignore slashes with other direction than specified
+ * Will not resolve path starting from relative directory
+ * @warning Don't use with "protocol://path"-style URLs
+ * @param path string to process
+ * @param slashCharacter character to use as directory delimiter
+ * @return transformed path
+ */
+ static std::string CanonicalizePath(const std::string& path, const char slashCharacter = '\\');
static void CreateArchivePath(CStdString& strUrlPath,
const CStdString& strType,

0 comments on commit 206e1f1

Please sign in to comment.
Something went wrong with that request. Please try again.