xref: /reactos/sdk/lib/atl/atlpath.h (revision 31876ba8)
1 // PROJECT:        ReactOS ATL CPathT
2 // LICENSE:        Public Domain
3 // PURPOSE:        Provides compatibility to Microsoft ATL
4 // PROGRAMMERS:    Benedikt Freisen
5 
6 #ifndef __ATLPATH_H__
7 #define __ATLPATH_H__
8 
9 #pragma once
10 
11 #include <atlcore.h>
12 #include <atlstr.h>
13 #include <shlwapi.h>
14 
15 namespace ATL
16 {
17 
18 template<typename StringType>
19 class CPathT
20 {
21     // const
22     inline BOOL PathFileExistsX(LPCSTR pszPath) const { return PathFileExistsA(pszPath); }
23     inline BOOL PathFileExistsX(LPCWSTR pszPath) const { return PathFileExistsW(pszPath); }
24     inline LPCSTR PathFindExtensionX(LPCSTR pszPath) const { return PathFindExtensionA(pszPath); }
25     inline LPCWSTR PathFindExtensionX(LPCWSTR pszPath) const { return PathFindExtensionW(pszPath); }
26     inline LPCSTR PathFindFileNameX(LPCSTR pszPath) const { return PathFindFileNameA(pszPath); }
27     inline LPCWSTR PathFindFileNameX(LPCWSTR pszPath) const { return PathFindFileNameW(pszPath); }
28     inline int PathGetDriveNumberX(LPCSTR pszPath) const { return PathGetDriveNumberA(pszPath); }
29     inline int PathGetDriveNumberX(LPCWSTR pszPath) const { return PathGetDriveNumberW(pszPath); }
30     inline BOOL PathIsDirectoryX(LPCSTR pszPath) const { return PathIsDirectoryA(pszPath); }
31     inline BOOL PathIsDirectoryX(LPCWSTR pszPath) const { return PathIsDirectoryW(pszPath); }
32     inline BOOL PathIsFileSpecX(LPCSTR pszPath) const { return PathIsFileSpecA(pszPath); }
33     inline BOOL PathIsFileSpecX(LPCWSTR pszPath) const { return PathIsFileSpecW(pszPath); }
34     inline BOOL PathIsPrefixX(LPCSTR pszPath, LPCSTR pszPrefix) { return PathIsPrefixA(pszPath, pszPrefix); }
35     inline BOOL PathIsPrefixX(LPCWSTR pszPath, LPCWSTR pszPrefix) { return PathIsPrefixW(pszPath, pszPrefix); }
36     inline BOOL PathIsRelativeX(LPCSTR pszPath) const { return PathIsRelativeA(pszPath); }
37     inline BOOL PathIsRelativeX(LPCWSTR pszPath) const { return PathIsRelativeW(pszPath); }
38     inline BOOL PathIsRootX(LPCSTR pszPath) const { return PathIsRootA(pszPath); }
39     inline BOOL PathIsRootX(LPCWSTR pszPath) const { return PathIsRootW(pszPath); }
40     inline BOOL PathIsSameRootX(LPCSTR pszPath, LPCSTR pszOther) const { return PathIsSameRootA(pszPath, pszOther); }
41     inline BOOL PathIsSameRootX(LPCWSTR pszPath, LPCWSTR pszOther) const { return PathIsSameRootW(pszPath, pszOther); }
42     inline BOOL PathIsUNCX(LPCSTR pszPath) { return PathIsUNCA(pszPath); }
43     inline BOOL PathIsUNCX(LPCWSTR pszPath) { return PathIsUNCW(pszPath); }
44     inline BOOL PathIsUNCServerX(LPCSTR pszPath) { return PathIsUNCServerA(pszPath); }
45     inline BOOL PathIsUNCServerX(LPCWSTR pszPath) { return PathIsUNCServerW(pszPath); }
46     inline BOOL PathIsUNCServerShareX(LPCSTR pszPath) { return PathIsUNCServerShareA(pszPath); }
47     inline BOOL PathIsUNCServerShareX(LPCWSTR pszPath) { return PathIsUNCServerShareW(pszPath); }
48     inline BOOL PathMatchSpecX(LPCSTR pszPath, LPCSTR pszSpec) const { return PathMatchSpecA(pszPath, pszSpec); }
49     inline BOOL PathMatchSpecX(LPCWSTR pszPath, LPCWSTR pszSpec) const { return PathMatchSpecW(pszPath, pszSpec); }
50     inline LPCSTR PathSkipRootX(LPCSTR pszPath) const { return PathSkipRootA(pszPath); }
51     inline LPCWSTR PathSkipRootX(LPCWSTR pszPath) const { return PathSkipRootW(pszPath); }
52 
53     // non-const
54     inline void PathAddBackslashX(LPSTR pszPath) { PathAddBackslashA(pszPath); }
55     inline void PathAddBackslashX(LPWSTR pszPath) { PathAddBackslashW(pszPath); }
56     inline BOOL PathAddExtensionX(LPSTR pszPath, LPCSTR pszExt) { return PathAddExtensionA(pszPath, pszExt); }
57     inline BOOL PathAddExtensionX(LPWSTR pszPath, LPCWSTR pszExt) { return PathAddExtensionW(pszPath, pszExt); }
58     inline BOOL PathAppendX(LPSTR pszPath, LPCSTR pszMore) { return PathAppendA(pszPath, pszMore); }
59     inline BOOL PathAppendX(LPWSTR pszPath, LPCWSTR pszMore) { return PathAppendW(pszPath, pszMore); }
60     inline void PathBuildRootX(LPSTR pszRoot, int iDrive) { PathBuildRootA(pszRoot, iDrive); }
61     inline void PathBuildRootX(LPWSTR pszRoot, int iDrive) { PathBuildRootW(pszRoot, iDrive); }
62     inline void PathCanonicalizeX(LPSTR pszDst, LPCSTR pszSrc) { PathCanonicalizeA(pszDst, pszSrc); }
63     inline void PathCanonicalizeX(LPWSTR pszDst, LPCWSTR pszSrc) { PathCanonicalizeW(pszDst, pszSrc); }
64     inline void PathCombineX(LPSTR pszPathOut, LPCSTR pszPathIn, LPCSTR pszMore) { PathCombineA(pszPathOut, pszPathIn, pszMore); }
65     inline void PathCombineX(LPWSTR pszPathOut, LPCWSTR pszPathIn, LPCWSTR pszMore) { PathCombineW(pszPathOut, pszPathIn, pszMore); }
66     inline CPathT<StringType> PathCommonPrefixX(LPCSTR pszFile1, LPCSTR pszFile2, LPSTR pszPath) { return PathCommonPrefixA(pszFile1, pszFile2, pszPath); }
67     inline CPathT<StringType> PathCommonPrefixX(LPCWSTR pszFile1, LPCWSTR pszFile2, LPWSTR pszPath) { return PathCommonPrefixW(pszFile1, pszFile2, pszPath); }
68     inline BOOL PathCompactPathX(HDC hDC, LPSTR pszPath, UINT dx) { return PathCompactPathA(hDC, pszPath, dx); }
69     inline BOOL PathCompactPathX(HDC hDC, LPWSTR pszPath, UINT dx) { return PathCompactPathW(hDC, pszPath, dx); }
70     inline BOOL PathCompactPathExX(LPSTR pszOut, LPCSTR pszSrc, UINT cchMax, DWORD dwFlags) { return PathCompactPathExA(pszOut, pszSrc, cchMax, dwFlags); }
71     inline BOOL PathCompactPathExX(LPWSTR pszOut, LPCWSTR pszSrc, UINT cchMax, DWORD dwFlags) { return PathCompactPathExW(pszOut, pszSrc, cchMax, dwFlags); }
72     inline BOOL PathMakePrettyX(LPSTR pszPath) { return PathMakePrettyA(pszPath); }
73     inline BOOL PathMakePrettyX(LPWSTR pszPath) { return PathMakePrettyW(pszPath); }
74     inline void PathQuoteSpacesX(LPSTR pszPath) { PathQuoteSpacesA(pszPath); }
75     inline void PathQuoteSpacesX(LPWSTR pszPath) { PathQuoteSpacesW(pszPath); }
76     inline BOOL PathRelativePathToX(LPSTR pszPath, LPCSTR pszFrom, DWORD dwAttrFrom, LPCSTR pszTo, DWORD dwAttrTo) { return PathRelativePathToA(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo); }
77     inline BOOL PathRelativePathToX(LPWSTR pszPath, LPCWSTR pszFrom, DWORD dwAttrFrom, LPCWSTR pszTo, DWORD dwAttrTo) { return PathRelativePathToW(pszPath, pszFrom, dwAttrFrom, pszTo, dwAttrTo); }
78     inline void PathRemoveArgsX(LPSTR pszPath) { PathRemoveArgsA(pszPath); }
79     inline void PathRemoveArgsX(LPWSTR pszPath) { PathRemoveArgsW(pszPath); }
80     inline void PathRemoveBackslashX(LPSTR pszPath) { PathRemoveBackslashA(pszPath); }
81     inline void PathRemoveBackslashX(LPWSTR pszPath) { PathRemoveBackslashW(pszPath); }
82     inline void PathRemoveBlanksX(LPSTR pszPath) { PathRemoveBlanksA(pszPath); }
83     inline void PathRemoveBlanksX(LPWSTR pszPath) { PathRemoveBlanksW(pszPath); }
84     inline void PathRemoveExtensionX(LPSTR pszPath) { PathRemoveExtensionA(pszPath); }
85     inline void PathRemoveExtensionX(LPWSTR pszPath) { PathRemoveExtensionW(pszPath); }
86     inline BOOL PathRemoveFileSpecX(LPSTR pszPath) { return PathRemoveFileSpecA(pszPath); }
87     inline BOOL PathRemoveFileSpecX(LPWSTR pszPath) { return PathRemoveFileSpecW(pszPath); }
88     inline BOOL PathRenameExtensionX(LPSTR pszPath, LPCSTR pszExt) { return PathRenameExtensionA(pszPath, pszExt); }
89     inline BOOL PathRenameExtensionX(LPWSTR pszPath, LPCWSTR pszExt) { return PathRenameExtensionW(pszPath, pszExt); }
90     inline void PathStripPathX(LPSTR pszPath) { PathStripPathA(pszPath); }
91     inline void PathStripPathX(LPWSTR pszPath) { PathStripPathW(pszPath); }
92     inline BOOL PathStripToRootX(LPSTR pszPath) { return PathStripToRootA(pszPath); }
93     inline BOOL PathStripToRootX(LPWSTR pszPath) { return PathStripToRootW(pszPath); }
94     inline void PathUnquoteSpacesX(LPSTR pszPath) { PathUnquoteSpacesA(pszPath); }
95     inline void PathUnquoteSpacesX(LPWSTR pszPath) { PathUnquoteSpacesW(pszPath); }
96 
97 public:
98     typedef typename StringType::PCXSTR PCXSTR;
99     typedef typename StringType::PXSTR PXSTR;
100     typedef typename StringType::XCHAR XCHAR;
101 
102     StringType m_strPath;
103 
104     CPathT(PCXSTR pszPath)
105     {
106         m_strPath = StringType(pszPath);
107     }
108 
109     CPathT(const CPathT<StringType>& path)
110     {
111         m_strPath = path.m_strPath;
112     }
113 
114     CPathT() noexcept
115     {
116         // do nothing, m_strPath initializes itself
117     }
118 
119     void AddBackslash()
120     {
121         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
122         PathAddBackslashX(str);
123         m_strPath.ReleaseBuffer();
124     }
125 
126     BOOL AddExtension(PCXSTR pszExtension)
127     {
128         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
129         BOOL result = PathAddExtensionX(str, pszExtension);
130         m_strPath.ReleaseBuffer();
131         return result;
132     }
133 
134     BOOL Append(PCXSTR pszMore)
135     {
136         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
137         BOOL result = PathAppendX(str, pszMore);
138         m_strPath.ReleaseBuffer();
139         return result;
140     }
141 
142     void BuildRoot(int iDrive)
143     {
144         PXSTR str = m_strPath.GetBuffer(4);
145         PathBuildRootX(str, iDrive);
146         m_strPath.ReleaseBuffer();
147     }
148 
149     void Canonicalize()
150     {
151         StringType strTemp;
152         PXSTR str = strTemp.GetBuffer(MAX_PATH);
153         PathCanonicalizeX(str, m_strPath);
154         strTemp.ReleaseBuffer();
155         m_strPath = strTemp;
156     }
157 
158     void Combine(PCXSTR pszDir, PCXSTR pszFile)
159     {
160         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
161         PathCombineX(str, pszDir, pszFile);
162         m_strPath.ReleaseBuffer();
163     }
164 
165     CPathT<StringType> CommonPrefix(PCXSTR pszOther)
166     {
167         StringType result;
168         result.SetString(m_strPath, PathCommonPrefixX(m_strPath, pszOther, NULL));
169         return result;
170     }
171 
172     BOOL CompactPath(HDC hDC, UINT nWidth)
173     {
174         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
175         BOOL result = PathCompactPathX(hDC, str, nWidth);
176         m_strPath.ReleaseBuffer();
177         return result;
178     }
179 
180     BOOL CompactPathEx(UINT nMaxChars, DWORD dwFlags = 0)
181     {
182         StringType strTemp;
183         PXSTR str = strTemp.GetBuffer(nMaxChars);
184         BOOL result = PathCompactPathExX(str, m_strPath, nMaxChars, dwFlags);
185         strTemp.ReleaseBuffer();
186         m_strPath = strTemp;
187         return result;
188     }
189 
190     BOOL FileExists() const
191     {
192         return PathFileExistsX(m_strPath);
193     }
194 
195     int FindExtension() const
196     {
197         PCXSTR extension = PathFindExtensionX(m_strPath);
198         if (*extension == '\0')
199             return -1;
200 
201         PCXSTR str = m_strPath;
202         return (int)(extension - str);
203     }
204 
205     int FindFileName() const
206     {
207         PCXSTR filename = PathFindFileNameX(m_strPath);
208         if (*filename == '\0')
209             return -1;
210 
211         PCXSTR str = m_strPath;
212         return (int)(filename - str);
213     }
214 
215     int GetDriveNumber() const
216     {
217         return PathGetDriveNumberX(m_strPath);
218     }
219 
220     StringType GetExtension() const
221     {
222         return StringType(PathFindExtensionX(m_strPath));
223     }
224 
225     BOOL IsDirectory() const
226     {
227         return PathIsDirectoryX(m_strPath);
228     }
229 
230     BOOL IsFileSpec() const
231     {
232         return PathIsFileSpecX(m_strPath);
233     }
234 
235     BOOL IsPrefix(PCXSTR pszPrefix) const
236     {
237         return PathIsPrefixX(m_strPath);
238     }
239 
240     BOOL IsRelative() const
241     {
242         return PathIsRelativeX(m_strPath);
243     }
244 
245     BOOL IsRoot() const
246     {
247         return PathIsRootX(m_strPath);
248     }
249 
250     BOOL IsSameRoot(PCXSTR pszOther) const
251     {
252         return PathIsSameRootX(m_strPath, pszOther);
253     }
254 
255     BOOL IsUNC() const
256     {
257         return PathIsUNCX(m_strPath);
258     }
259 
260     BOOL IsUNCServer() const
261     {
262         return PathIsUNCServerX(m_strPath);
263     }
264 
265     BOOL IsUNCServerShare() const
266     {
267         return PathIsUNCServerShareX(m_strPath);
268     }
269 
270     BOOL MakePretty()
271     {
272         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
273         BOOL result = PathMakePrettyX(str);
274         m_strPath.ReleaseBuffer();
275         return result;
276     }
277 
278     BOOL MatchSpec(PCXSTR pszSpec) const
279     {
280         return PathMatchSpecX(m_strPath, pszSpec);
281     }
282 
283     void QuoteSpaces()
284     {
285         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
286         PathQuoteSpacesX(str);
287         m_strPath.ReleaseBuffer();
288     }
289 
290     BOOL RelativePathTo(PCXSTR pszFrom, DWORD dwAttrFrom, PCXSTR pszTo, DWORD dwAttrTo)
291     {
292         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
293         BOOL result = PathRelativePathToX(str, pszFrom, dwAttrFrom, pszTo, dwAttrTo);
294         m_strPath.ReleaseBuffer();
295         return result;
296     }
297 
298     void RemoveArgs()
299     {
300         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
301         PathRemoveArgsX(str);
302         m_strPath.ReleaseBuffer();
303     }
304 
305     void RemoveBackslash()
306     {
307         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
308         PathRemoveBackslashX(str);
309         m_strPath.ReleaseBuffer();
310     }
311 
312     void RemoveBlanks()
313     {
314         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
315         PathRemoveBlanksX(str);
316         m_strPath.ReleaseBuffer();
317     }
318 
319     void RemoveExtension()
320     {
321         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
322         PathRemoveExtensionX(str);
323         m_strPath.ReleaseBuffer();
324     }
325 
326     BOOL RemoveFileSpec()
327     {
328         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
329         BOOL result = PathRemoveFileSpecX(str);
330         m_strPath.ReleaseBuffer();
331         return result;
332     }
333 
334     BOOL RenameExtension(PCXSTR pszExtension)
335     {
336         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
337         BOOL result = PathRenameExtensionX(str, pszExtension);
338         m_strPath.ReleaseBuffer();
339         return result;
340     }
341 
342     int SkipRoot() const
343     {
344         PCXSTR str = m_strPath;
345         return (int)(PathSkipRootX(m_strPath) - str);
346     }
347 
348     void StripPath()
349     {
350         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
351         PathStripPathX(str);
352         m_strPath.ReleaseBuffer();
353     }
354 
355     BOOL StripToRoot()
356     {
357         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
358         BOOL result = PathStripToRootX(str);
359         m_strPath.ReleaseBuffer();
360         return result;
361     }
362 
363     void UnquoteSpaces()
364     {
365         PXSTR str = m_strPath.GetBuffer(MAX_PATH);
366         PathUnquoteSpacesX(str);
367         m_strPath.ReleaseBuffer();
368     }
369 
370     operator const StringType&() const noexcept
371     {
372         return m_strPath;
373     }
374 
375     operator PCXSTR() const noexcept
376     {
377         return m_strPath;
378     }
379 
380     operator StringType&() noexcept
381     {
382         return m_strPath;
383     }
384 
385     CPathT<StringType>& operator+=(PCXSTR pszMore)
386     {
387         Append(pszMore);
388         return *this;
389     }
390 
391 };
392 
393 typedef CPathT<CString> CPath;
394 typedef CPathT<CStringA> CPathA;
395 typedef CPathT<CStringW> CPathW;
396 
397 }  // namespace ATL
398 
399 #endif
400