1
// TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016-2018 - TortoiseGit
4 // Copyright (C) 2003-2008, 2013-2014 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 // CSTRING is always available in an MFC build
24 #define CSTRING_AVAILABLE
29 * helper class to handle path strings.
34 CPathUtils() = delete;
35 static BOOL
MakeSureDirectoryPathExists(LPCTSTR path
);
36 static void ConvertToBackslash(LPTSTR dest
, LPCTSTR src
, size_t len
);
38 #ifdef CSTRING_AVAILABLE
39 inline static void ConvertToBackslash(CString
& path
);
42 * returns the filename of a full path
44 static CString
GetFileNameFromPath(CString sPath
);
47 * returns the file extension from a full path
49 static CString
GetFileExtFromPath(const CString
& sPath
);
52 * Returns the long pathname of a path which may be in 8.3 format.
54 static CString
GetLongPathname(const CString
& path
);
57 * Copies a file or a folder from \a srcPath to \a destpath, creating
58 * intermediate folders if necessary. If \a force is TRUE, then files
59 * are overwritten if they already exist.
60 * Folders are just created at the new location, no files in them get
63 static BOOL
FileCopy(CString srcPath
, CString destPath
, BOOL force
= TRUE
);
66 * parses a string for a path or url. If no path or url is found,
67 * an empty string is returned.
68 * \remark if more than one path or url is inside the string, only
69 * the first one is returned.
71 static CString
ParsePathInString(const CString
& Str
);
74 * Returns the path to the installation folder, in our case the TortoiseSVN/bin folder.
75 * \remark the path returned has a trailing backslash
77 static CString
GetAppDirectory(HMODULE hMod
= nullptr);
80 * Returns the path to the installation parent folder, in our case the TortoiseSVN folder.
81 * \remark the path returned has a trailing backslash
83 static CString
GetAppParentDirectory(HMODULE hMod
= nullptr);
85 static CString
GetDocumentsDirectory();
86 static CString
GetProgramsDirectory();
89 * Returns the path to the application data folder, in our case the %APPDATA%TortoiseSVN folder.
90 * \remark the path returned has a trailing backslash
92 static CString
GetAppDataDirectory();
93 static CString
GetLocalAppDataDirectory();
96 * Removes any of the following namespace prefixes from a path, if found: "\??\", "\\?\", "\\?\UNC\".
98 static void DropPathPrefixes(CString
& path
);
100 static int ReadLink(LPCTSTR filename
, CStringA
* target
= nullptr);
103 * Escapes regexp-specific chars.
105 static CString
PathPatternEscape(const CString
& path
);
107 * Unescapes regexp-specific chars.
109 static CString
PathPatternUnEscape(const CString
& path
);
112 * Returns the version string from the VERSION resource of a dll or exe.
113 * \param p_strFilename path to the dll or exe
114 * \return the version string
116 static CString
GetVersionFromFile(const CString
& p_strFilename
);
119 * Ensures that the path ends with a folder separator.
120 * If the delimiter already exists, no additional delimiter will be added.
121 * \param path to ensure
123 static void EnsureTrailingPathDelimiter(CString
& path
);
126 * Returns a path guaranteeing that a valid path delimiter follows.
127 * If the delimiter already exists, no additional delimiter will be added.
128 * \param path to ensure
129 * \return path including path delimiter
131 static CString
BuildPathWithPathDelimiter(const CString
& path
);
134 * Trims a possible included trailing folder separator from the provided path.
135 * \param path to trim
137 static void TrimTrailingPathDelimiter(CString
& path
);
140 * ExpandFileName converts the relative file name into a fully qualified path name.
141 * ExpandFileName does not verify that the resulting fully qualified path name
142 * refers to an existing file, or even that the resulting path exists.
143 * \param path to expand
144 * \return fully qualified path name
146 static CString
CPathUtils::ExpandFileName(const CString
& path
);
149 * This method will make a path comparable to another path.
150 * It will do the following:
151 * 1.) Modify all characters in the path to be lower case
152 * 2.) Account for ..\'s and .\'s that may occur in the middle of the path and remove them
153 * 3.) Expand a path that has DOS 8.3 file/folder names
154 * 4.) Remove the trailing path delimiter at the end
155 * The function does not account for symlinks at this point in time.
156 * \param path to normalize
157 * \return normalized path
159 static CString
CPathUtils::NormalizePath(const CString
& path
);
162 * Compares two paths and returns true if they are logically the same path.
163 * The function does not account for symlinks at this point in time.
164 * \param path1 to compare
165 * \param path2 to compare
166 * \return true if they are the same path
168 static bool CPathUtils::IsSamePath(const CString
& path1
, const CString
& path2
);
171 * Checks if two path strings are equal. No conversion of slashes is done!
172 * \remark for slash-independent comparison, use IsEquivalentTo()
174 static bool ArePathStringsEqual(const CString
& sP1
, const CString
& sP2
);
175 static bool ArePathStringsEqualWithCase(const CString
& sP1
, const CString
& sP2
);
177 static CString
GetCopyrightForSelf();
180 * Sets the last-write-time of the file to the current time
182 static bool Touch(const CString
& path
);