4 * @brief Path handling routines
13 #pragma warning (push) // prevent "warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared"
14 #pragma warning (disable:4091) // VC bug when using XP enabled toolsets.
18 #include "PathContext.h"
19 #include "coretools.h"
25 static bool IsSlash(const String& pszStart, size_t nPos);
26 static bool GetDirName(const String& sDir, String& sName);
29 * @brief Checks if char in string is slash.
30 * @param [in] pszStart String to check.
31 * @param [in] nPos of char in string to check (0-based index).
32 * @return true if char is slash.
34 static bool IsSlash(const String& pszStart, size_t nPos)
36 return pszStart[nPos]=='/' ||
40 // Avoid 0x5C (ASCII backslash) byte occurring as trail byte in MBCS
41 (pszStart[nPos]=='\\' && !_ismbstrail((unsigned char *)pszStart.c_str(), (unsigned char *)pszStart.c_str() + nPos));
46 * @brief Checks if string ends with slash.
47 * This function checks if given string ends with slash. In many places,
48 * especially in GUI, we assume folder paths end with slash.
49 * @param [in] s String to check.
50 * @return true if last char in string is slash.
52 bool EndsWithSlash(const String& s)
54 if (size_t len = s.length())
55 return IsSlash(s, (int)len - 1);
60 * @brief Checks if path exists and if it points to folder or file.
61 * This function first checks if path exists. If path exists
62 * then function checks if path points to folder or file.
63 * @param [in] szPath Path to check.
65 * - DOES_NOT_EXIST : path does not exists
66 * - IS_EXISTING_DIR : path points to existing folder
67 * - IS_EXISTING_FILE : path points to existing file
69 PATH_EXISTENCE DoesPathExist(const String& szPath, bool (*IsArchiveFile)(const String&) /*= nullptr*/)
72 return DOES_NOT_EXIST;
74 // Expand environment variables:
75 // Convert "%userprofile%\My Documents" to "C:\Documents and Settings\username\My Documents"
76 const TCHAR *lpcszPath = szPath.c_str();
77 TCHAR expandedPath[MAX_PATH_FULL];
79 if (_tcschr(lpcszPath, '%') != nullptr)
81 DWORD dwLen = ExpandEnvironmentStrings(lpcszPath, expandedPath, MAX_PATH_FULL);
82 if (dwLen > 0 && dwLen < MAX_PATH_FULL)
83 lpcszPath = expandedPath;
86 DWORD attr = GetFileAttributes(TFile(String(lpcszPath)).wpath().c_str());
88 if (attr == ((DWORD) -1))
90 if (IsArchiveFile && IsArchiveFile(szPath))
91 return IS_EXISTING_DIR;
92 return DOES_NOT_EXIST;
94 else if (attr & FILE_ATTRIBUTE_DIRECTORY)
95 return IS_EXISTING_DIR;
98 if (IsArchiveFile && IsArchiveFile(szPath))
99 return IS_EXISTING_DIR;
100 return IS_EXISTING_FILE;
105 * @brief Like shlwapi's PathFindFileName(), but works with both \ and /.
109 String FindFileName(const String& path)
111 const TCHAR *filename = path.c_str();
112 while (const TCHAR *slash = _tcspbrk(filename, _T("\\/")))
114 if (*(slash + 1) == '\0')
116 filename = slash + 1;
122 * @brief Like shlwapi's PathFindFileName(), but works with both \ and /.
126 String FindExtension(const String& path)
128 return ::PathFindExtension(path.c_str());
132 * @brief Strip trailing slas.
133 * This function strips trailing slash from given path. Root paths are special
134 * case and they are left intact. Since C:\ is a valid path but C: is not.
135 * @param [in,out] sPath Path to strip.
137 void normalize(String & sPath)
139 size_t len = sPath.length();
143 // prefix root with current drive
144 sPath = GetLongPath(sPath);
146 // Do not remove trailing slash from root directories
147 if (len == 3 && sPath[1] == ':')
150 // remove any trailing slash
151 if (EndsWithSlash(sPath))
152 sPath.resize(sPath.length() - 1);
156 * @brief Get canonical name of folder.
157 * @param [in] sDir Folder to handle.
158 * @param [out] sName Canonicalized folder name.
159 * @return true if canonical name exists.
160 * @todo Should we return empty string as sName when returning false?
162 static bool GetDirName(const String& sDir, String& sName)
164 // FindFirstFile doesn't work for root:
165 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/findfirstfile.asp
166 // You cannot use root directories as the lpFileName input string for FindFirstFile - with or without a trailing backslash.
167 if (sDir[0] && sDir[1] == ':' && sDir[2] == '\0')
169 // I don't know if this work for empty root directories
170 // because my first return value is not a dot directory, as I would have expected
173 StringCchPrintf(sPath, sizeof(sPath)/sizeof(sPath[0]), _T("%s\\*"), sDir.c_str());
174 HANDLE h = FindFirstFile(sPath, &ffd);
175 if (h == INVALID_HANDLE_VALUE)
181 // (Couldn't get info for just the directory from CFindFile)
184 HANDLE h = FindFirstFile(TFile(sDir).wpath().c_str(), &ffd);
185 if (h == INVALID_HANDLE_VALUE)
187 sName = ffd.cFileName;
193 * Convert path to canonical long path.
194 * This function converts given path to canonical long form. For example
195 * foldenames with ~ short names are expanded. Also environment strings are
196 * expanded if @p bExpandEnvs is true. If path does not exist, make canonical
197 * the part that does exist, and leave the rest as is. Result, if a directory,
198 * usually does not have a trailing backslash.
199 * @param [in] sPath Path to convert.
200 * @param [in] bExpandEnvs If true environment variables are expanded.
201 * @return Converted path.
203 String GetLongPath(const String& szPath, bool bExpandEnvs)
205 String sPath = szPath;
206 size_t len = sPath.length();
210 TCHAR fullPath[MAX_PATH_FULL] = {0};
211 TCHAR *pFullPath = &fullPath[0];
214 // GetFullPathName GetLongPathName
215 // Convert to fully qualified form Yes No
217 // Convert /, //, \/, ... to \ Yes No
218 // Handle ., .., ..\..\.. Yes No
219 // Convert 8.3 names to long names No Yes
220 // Fail when file/directory does not exist No Yes
222 // Fully qualify/normalize name using GetFullPathName.
224 // Expand environment variables:
225 // Convert "%userprofile%\My Documents" to "C:\Documents and Settings\username\My Documents"
226 TCHAR expandedPath[MAX_PATH_FULL];
227 const TCHAR *lpcszPath = sPath.c_str();
228 if (bExpandEnvs && _tcschr(lpcszPath, '%') != nullptr)
230 DWORD dwLen = ExpandEnvironmentStrings(lpcszPath, expandedPath, MAX_PATH_FULL);
231 if (dwLen > 0 && dwLen < MAX_PATH_FULL)
232 lpcszPath = expandedPath;
235 String tPath = TFile(String(lpcszPath)).wpath();
236 DWORD dwLen = GetFullPathName(tPath.c_str(), MAX_PATH_FULL, pFullPath, &lpPart);
237 if (dwLen == 0 || dwLen >= MAX_PATH_FULL)
238 _tcscpy_s(pFullPath, MAX_PATH_FULL, tPath.c_str());
240 // We are done if this is not a short name.
241 if (_tcschr(pFullPath, _T('~')) == nullptr)
244 // We have to do it the hard way because GetLongPathName is not
245 // available on Win9x and some WinNT 4
247 // The file/directory does not exist, use as much long name as we can
248 // and leave the invalid stuff at the end.
250 TCHAR *ptr = pFullPath;
251 TCHAR *end = nullptr;
253 // Skip to \ position d:\abcd or \\host\share\abcd
254 // indicated by ^ ^ ^
255 if (_tcslen(ptr) > 2)
256 end = _tcschr(pFullPath+2, _T('\\'));
257 if (end != nullptr && !_tcsncmp(pFullPath, _T("\\\\"),2))
258 end = _tcschr(end+1, _T('\\'));
267 // now walk down each directory and do short to long name conversion
268 while (ptr != nullptr)
270 end = _tcschr(ptr, '\\');
271 // zero-terminate current component
272 // (if we're at end, its already zero-terminated)
280 // advance to next component (or set ptr=`nullptr` to flag end)
281 ptr = (end!=nullptr ? end+1 : nullptr);
283 // (Couldn't get info for just the directory from CFindFile)
285 HANDLE h = FindFirstFile(TFile(sTemp).wpath().c_str(), &ffd);
286 if (h == INVALID_HANDLE_VALUE)
297 sLong += ffd.cFileName;
304 * @brief Check if the path exist and create the folder if needed.
305 * This function checks if path exists. If path does not yet exist
306 * function created needed folder structure. So this function is the
307 * easy way to create a needed folder structure. Environment strings are
308 * expanded when handling paths.
309 * @param [in] sPath Path to check/create.
310 * @return true if path exists or if we successfully created it.
312 bool CreateIfNeeded(const String& szPath)
318 if (GetDirName(szPath, sTemp))
321 if (szPath.length() >= MAX_PATH_FULL)
324 // Expand environment variables:
325 // Convert "%userprofile%\My Documents" to "C:\Documents and Settings\username\My Documents"
326 TCHAR fullPath[MAX_PATH_FULL];
328 if (_tcschr(szPath.c_str(), '%') != nullptr)
330 DWORD dwLen = ExpandEnvironmentStrings(szPath.c_str(), fullPath, MAX_PATH_FULL);
331 if (dwLen == 0 || dwLen >= MAX_PATH_FULL)
332 _tcscpy_safe(fullPath, szPath.c_str());
335 _tcscpy_safe(fullPath, szPath.c_str());
336 // Now fullPath holds our desired path
338 TCHAR *ptr = fullPath;
339 TCHAR *end = nullptr;
341 // Skip to \ position d:\abcd or \\host\share\abcd
342 // indicated by ^ ^ ^
343 if (_tcslen(ptr) > 2)
344 end = _tcschr(fullPath+2, _T('\\'));
345 if (end != nullptr && !_tcsncmp(fullPath, _T("\\\\"),2))
346 end = _tcschr(end+1, _T('\\'));
348 if (end == nullptr) return false;
350 // check that first component exists
352 if (!GetDirName(fullPath, sTemp))
358 while (ptr != nullptr)
360 end = _tcschr(ptr, '\\');
361 // zero-terminate current component
362 // (if we're at end, its already zero-terminated)
366 // advance to next component (or set ptr=`nullptr` to flag end)
367 ptr = (end != nullptr ? end+1 : nullptr);
370 if (!GetDirName(fullPath, sNextName))
372 // try to create directory, and then double-check its existence
373 if (!CreateDirectory(fullPath, 0) ||
374 !GetDirName(fullPath, sNextName))
379 // if not finished, restore directory string we're working in
387 * @brief Check if paths are both folders or files.
388 * This function checks if paths are "compatible" as in many places we need
389 * to have two folders or two files.
390 * @param [in] paths Left and right paths.
392 * - IS_EXISTING_DIR : both are directories & exist
393 * - IS_EXISTING_FILE : both are files & exist
394 * - DOES_NOT_EXIST : in all other cases
396 PATH_EXISTENCE GetPairComparability(const PathContext & paths, bool (*IsArchiveFile)(const String&) /*= nullptr*/)
398 // fail if not both specified
399 if (paths.GetSize() < 2 || paths[0].empty() || paths[1].empty())
400 return DOES_NOT_EXIST;
401 PATH_EXISTENCE p1 = DoesPathExist(paths[0], IsArchiveFile);
402 // short circuit testing right if left doesn't exist
403 if (p1 == DOES_NOT_EXIST)
404 return DOES_NOT_EXIST;
405 PATH_EXISTENCE p2 = DoesPathExist(paths[1], IsArchiveFile);
408 p1 = DoesPathExist(paths[0]);
409 p2 = DoesPathExist(paths[1]);
411 return DOES_NOT_EXIST;
413 if (paths.GetSize() < 3) return p1;
414 PATH_EXISTENCE p3 = DoesPathExist(paths[2], IsArchiveFile);
417 p1 = DoesPathExist(paths[0]);
418 p2 = DoesPathExist(paths[1]);
419 p3 = DoesPathExist(paths[2]);
420 if (p1 != p2 || p2 != p3)
421 return DOES_NOT_EXIST;
427 * @brief Check if the given path points to shotcut.
428 * Windows considers paths having a filename with extension ".lnk" as
429 * shortcuts. This function checks if the given path is shortcut.
430 * We usually want to expand shortcuts with ExpandShortcut().
431 * @param [in] inPath Path to check;
432 * @return true if the path points to shortcut, false otherwise.
434 bool IsShortcut(const String& inPath)
436 const TCHAR ShortcutExt[] = _T(".lnk");
437 TCHAR ext[_MAX_EXT] = {0};
438 _tsplitpath_s(inPath.c_str(), nullptr, 0, nullptr, 0, nullptr, 0, ext, _MAX_EXT);
439 if (_tcsicmp(ext, ShortcutExt) == 0)
445 bool IsDirectory(const String &path)
447 return !!PathIsDirectory(path.c_str());
450 //////////////////////////////////////////////////////////////////
451 // use IShellLink to expand the shortcut
452 // returns the expanded file, or "" on error
454 // original code was part of CShortcut
455 // 1996 by Rob Warner
456 // rhwarner@southeast.net
457 // http://users.southeast.net/~rhwarner
460 * @brief Expand given shortcut to full path.
461 * @param [in] inFile Shortcut to expand.
462 * @return Full path or empty string if error happened.
464 String ExpandShortcut(const String &inFile)
466 assert(inFile != _T(""));
468 // No path, nothing to return
469 if (inFile == _T(""))
476 // Create instance for shell link
477 hres = ::CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
478 IID_IShellLink, (LPVOID*) &psl);
481 // Get a pointer to the persist file interface
483 hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf);
486 WCHAR wsz[MAX_PATH_FULL];
488 _tcscpy_safe(wsz, inFile.c_str());
490 ::MultiByteToWideChar(CP_ACP, 0, inFile.c_str(), -1, wsz, MAX_PATH_FULL);
494 hres = ppf->Load(wsz, STGM_READ);
498 // find the path from that
499 TCHAR buf[MAX_PATH_FULL] = {0};
500 psl->GetPath(buf, MAX_PATH_FULL, nullptr, SLGP_UNCPRIORITY);
508 // if this fails, outFile == ""
513 * @brief Append subpath to path.
514 * This function appends subpath to given path. Function ensures there
515 * is only one backslash between path parts.
516 * @param [in] path "Base" path where other part is appended.
517 * @param [in] subpath Path part to append to base part.
518 * @return Formatted path. If one of arguments is empty then returns
519 * non-empty argument. If both argumets are empty empty string is returned.
521 String ConcatPath(const String & path, const String & subpath)
527 if (EndsWithSlash(path))
529 return String(path).append(subpath.c_str() + (IsSlash(subpath, 0) ? 1 : 0));
533 if (IsSlash(subpath, 0))
535 return path + subpath;
539 return path + _T("\\") + subpath;
545 * @brief Get parent path.
546 * This function returns parent path for given path. For example for
547 * path "c:\folder\subfolder" we return "c:\folder".
548 * @param [in] path Path to get parent path for.
549 * @return Parent path.
551 String GetParentPath(const String& path)
553 String parentPath(path);
554 size_t len = parentPath.length();
556 // Remove last '\' from paths
557 if (parentPath[len - 1] == '\\')
559 parentPath.resize(len - 1);
563 // Remove last part of path
564 size_t pos = parentPath.rfind('\\');
566 if (pos != parentPath.npos)
568 // Do not remove trailing slash from root directories
569 parentPath.resize(pos == 2 ? pos + 1 : pos);
575 * @brief Get last subdirectory of path.
577 * Returns last subdirectory name (if one exists) from given path.
579 * - C:\work\myproject returns \myproject
580 * @param [in] path Original path.
581 * @return Last subdirectory in path.
583 String GetLastSubdir(const String & path)
585 String parentPath(path);
586 size_t len = parentPath.length();
588 // Remove last '\' from paths
589 if (parentPath[len - 1] == '\\')
591 parentPath.erase(len - 1, 1);
595 // Find last part of path
596 size_t pos = parentPath.find_last_of('\\');
597 if (pos >= 2 && pos != String::npos)
598 parentPath.erase(0, pos);
603 * @brief Checks if path is an absolute path.
604 * @param [in] path Path to check.
605 * @return true if given path is absolute path.
607 bool IsPathAbsolute(const String &path)
609 if (path.length() < 3)
612 size_t pos = path.find_last_of('\\');
614 // Absolute path must have "\" and cannot start with it.
615 // Also "\\blahblah" is invalid.
616 if (pos < 2 || pos == String::npos)
619 // Maybe "X:\blahblah"?
620 if (path[1] == ':' && path[2] == '\\')
624 if (path[0] == '\\' && path[1] == '\\' && pos > 2)
631 * @brief Checks if folder exists and creates it if needed.
632 * This function checks if folder exists and creates it if not.
634 * @return Path if it exists or were created successfully. If
635 * path points to file or folder failed to create returns empty
638 String EnsurePathExist(const String & sPath)
640 int rtn = DoesPathExist(sPath);
641 if (rtn == IS_EXISTING_DIR)
643 if (rtn == IS_EXISTING_FILE)
645 if (!CreateIfNeeded(sPath))
647 // Check creating folder succeeded
648 if (DoesPathExist(sPath) == IS_EXISTING_DIR)
655 * @brief Return true if *pszChar is a slash (either direction) or a colon
657 * begin points to start of string, in case multibyte trail test is needed
659 bool IsSlashOrColon(const TCHAR *pszChar, const TCHAR *begin)
662 return (*pszChar == '/' || *pszChar == ':' || *pszChar == '\\');
664 // Avoid 0x5C (ASCII backslash) byte occurring as trail byte in MBCS
665 return (*pszChar == '/' || *pszChar == ':'
666 || (*pszChar == '\\' && !_ismbstrail((unsigned char *)begin, (unsigned char *)pszChar)));
671 * @brief Extract path name components from given full path.
672 * @param [in] pathLeft Original path.
673 * @param [out] pPath Folder name component of full path, excluding
675 * @param [out] pFile File name part, excluding extension.
676 * @param [out] pExt Filename extension part, excluding leading dot.
678 void SplitFilename(const String& pathLeft, String* pPath, String* pFile, String* pExt)
680 const TCHAR *pszChar = pathLeft.c_str() + pathLeft.length();
681 const TCHAR *pend = pszChar;
682 const TCHAR *extptr = 0;
685 while (pathLeft.c_str() < --pszChar)
693 (*pExt) = pszChar + 1;
695 ext = true; // extension is only after last period
699 else if (IsSlashOrColon(pszChar, pathLeft.c_str()))
701 // Ok, found last slash, so we collect any info desired
704 if (pPath != nullptr)
706 // Grab directory (omit trailing slash)
707 size_t len = pszChar - pathLeft.c_str();
709 ++len; // Keep trailing colon ( eg, C:filename.txt)
711 pPath->erase(len); // Cut rest of path
714 if (pFile != nullptr)
717 *pFile = pszChar + 1;
724 // Never found a delimiter
725 if (pFile != nullptr)
731 // if both filename & extension requested, remove extension from filename
733 if (pFile != nullptr && pExt != nullptr && extptr != nullptr)
735 size_t extlen = pend - extptr;
736 pFile->erase(pFile->length() - extlen);
741 * @brief Return path component from full path.
742 * @param [in] fullpath Full path to split.
743 * @return Path without filename.
745 String GetPathOnly(const String& fullpath)
747 if (fullpath.empty()) return _T("");
749 SplitFilename(fullpath, &spath, 0, 0);
753 bool IsURLorCLSID(const String& path)
755 return (path.find(_T("://")) != String::npos || path.find(_T("::{")) != String::npos);
758 bool IsDecendant(const String& path, const String& ancestor)
760 return path.length() > ancestor.length() &&
761 strutils::compare_nocase(String(path.c_str(), path.c_str() + ancestor.length()), ancestor) == 0;
764 static void replace_char(TCHAR *s, int target, int repl)
767 for (p=s; *p != _T('\0'); p = _tcsinc(p))
772 String ToWindowsPath(const String& path)
774 String winpath = path;
775 replace_char(&*winpath.begin(), '/', '\\');
779 String ToUnixPath(const String& path)
781 String unixpath = path;
782 replace_char(&*unixpath.begin(), '\\', '/');