Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

327 lines
4.7 KiB

  1. // FILE: tpath.h
  2. // AUTHOR: BrianAu
  3. // REMARKS:
  4. #include "shtl.h"
  5. #include "tpath.h"
  6. #include <xutility>
  7. using namespace std;
  8. __DATL_INLINE tpath::tpath(
  9. LPCTSTR pszRoot,
  10. LPCTSTR pszDir,
  11. LPCTSTR pszFile,
  12. LPCTSTR pszExt
  13. )
  14. {
  15. if (pszDir)
  16. SetPath(pszDir);
  17. if (pszRoot)
  18. SetRoot(pszRoot);
  19. if (pszFile)
  20. SetFileSpec(pszFile);
  21. if (pszExt)
  22. SetExtension(pszExt);
  23. }
  24. __DATL_INLINE tpath::tpath(
  25. const tpath& rhs
  26. ) : tstring(rhs)
  27. {
  28. }
  29. __DATL_INLINE tpath&
  30. tpath::operator = (
  31. const tpath& rhs
  32. )
  33. {
  34. if (this != &rhs)
  35. {
  36. tstring::operator = (rhs);
  37. }
  38. return *this;
  39. }
  40. __DATL_INLINE tpath&
  41. tpath::operator = (
  42. LPCTSTR rhs
  43. )
  44. {
  45. tstring::operator = (rhs);
  46. return *this;
  47. }
  48. __DATL_INLINE void
  49. tpath::AddBackslash(
  50. void
  51. )
  52. {
  53. ::PathAddBackslash(GetBuffer(max(MAX_PATH, length() + 2)));
  54. ReleaseBuffer();
  55. }
  56. __DATL_INLINE void
  57. tpath::RemoveBackslash(
  58. void
  59. )
  60. {
  61. ::PathRemoveBackslash(GetBuffer());
  62. ReleaseBuffer();
  63. }
  64. __DATL_INLINE bool
  65. tpath::GetRoot(
  66. tpath *pOut
  67. ) const
  68. {
  69. tpath temp(*this);
  70. temp.StripToRoot();
  71. *pOut = temp;
  72. return 0 < pOut->length();
  73. }
  74. __DATL_INLINE bool
  75. tpath::GetPath(
  76. tpath *pOut
  77. ) const
  78. {
  79. tpath temp(*this);
  80. temp.RemoveFileSpec();
  81. *pOut = temp;
  82. return 0 < pOut->length();
  83. }
  84. __DATL_INLINE bool
  85. tpath::GetDirectory(
  86. tpath *pOut
  87. ) const
  88. {
  89. if (GetPath(pOut))
  90. pOut->RemoveRoot();
  91. return 0 < pOut->length();
  92. }
  93. __DATL_INLINE bool
  94. tpath::GetExtension(
  95. tpath *pOut
  96. ) const
  97. {
  98. *pOut = ::PathFindExtension(*this);
  99. return 0 < pOut->length();
  100. }
  101. __DATL_INLINE bool
  102. tpath::GetFileSpec(
  103. tpath *pOut
  104. ) const
  105. {
  106. *pOut = ::PathFindFileName(*this);
  107. return 0 < pOut->length();
  108. }
  109. __DATL_INLINE bool
  110. tpath::Append(
  111. LPCTSTR psz
  112. )
  113. {
  114. bool bResult = boolify(::PathAppend(GetBuffer(max(MAX_PATH, length() + lstrlen(psz) + 3)), psz));
  115. ReleaseBuffer();
  116. return bResult;
  117. }
  118. __DATL_INLINE bool
  119. tpath::BuildRoot(
  120. int iDrive
  121. )
  122. {
  123. empty();
  124. bool bResult = NULL != ::PathBuildRoot(GetBuffer(5), iDrive);
  125. ReleaseBuffer();
  126. return bResult;
  127. }
  128. __DATL_INLINE bool
  129. tpath::Canonicalize(
  130. void
  131. )
  132. {
  133. tstring strTemp(*this);
  134. bool bResult = boolify(::PathCanonicalize(GetBuffer(max(MAX_PATH, length())), strTemp));
  135. ReleaseBuffer();
  136. return bResult;
  137. }
  138. __DATL_INLINE bool
  139. tpath::Compact(
  140. HDC hdc,
  141. int cxPixels
  142. )
  143. {
  144. bool bResult = boolify(::PathCompactPath(hdc, GetBuffer(), cxPixels));
  145. ReleaseBuffer();
  146. return bResult;
  147. }
  148. __DATL_INLINE bool
  149. tpath::CommonPrefix(
  150. LPCTSTR pszPath1,
  151. LPCTSTR pszPath2
  152. )
  153. {
  154. empty();
  155. ::PathCommonPrefix(pszPath1,
  156. pszPath2,
  157. GetBuffer(max(MAX_PATH, (max(lstrlen(pszPath1), lstrlen(pszPath2)) + 1))));
  158. ReleaseBuffer();
  159. return 0 < length();
  160. }
  161. __DATL_INLINE void
  162. tpath::QuoteSpaces(
  163. void
  164. )
  165. {
  166. ::PathQuoteSpaces(GetBuffer(max(MAX_PATH, length() + 3)));
  167. ReleaseBuffer();
  168. }
  169. __DATL_INLINE void
  170. tpath::UnquoteSpaces(
  171. void
  172. )
  173. {
  174. ::PathUnquoteSpaces(GetBuffer());
  175. ReleaseBuffer();
  176. }
  177. __DATL_INLINE void
  178. tpath::RemoveBlanks(
  179. void
  180. )
  181. {
  182. ::PathRemoveBlanks(GetBuffer());
  183. ReleaseBuffer();
  184. }
  185. __DATL_INLINE void
  186. tpath::RemoveExtension(
  187. void
  188. )
  189. {
  190. PathRemoveExtension(GetBuffer());
  191. ReleaseBuffer();
  192. }
  193. __DATL_INLINE void
  194. tpath::RemoveFileSpec(
  195. void
  196. )
  197. {
  198. ::PathRemoveFileSpec(GetBuffer());
  199. ReleaseBuffer();
  200. }
  201. __DATL_INLINE void
  202. tpath::RemoveRoot(
  203. void
  204. )
  205. {
  206. LPTSTR psz = ::PathSkipRoot(*this);
  207. if (psz)
  208. {
  209. tpath temp(psz);
  210. *this = temp;
  211. }
  212. }
  213. __DATL_INLINE void
  214. tpath::RemovePath(
  215. void
  216. )
  217. {
  218. tpath temp;
  219. GetFileSpec(&temp);
  220. *this = temp;
  221. }
  222. __DATL_INLINE void
  223. tpath::StripToRoot(
  224. void
  225. )
  226. {
  227. ::PathStripToRoot(GetBuffer());
  228. ReleaseBuffer();
  229. }
  230. __DATL_INLINE void
  231. tpath::SetRoot(
  232. LPCTSTR pszRoot
  233. )
  234. {
  235. tpath strTemp(*this);
  236. strTemp.RemoveRoot();
  237. *this = pszRoot;
  238. Append(strTemp);
  239. }
  240. __DATL_INLINE void
  241. tpath::SetPath(
  242. LPCTSTR pszPath
  243. )
  244. {
  245. tpath strTemp(*this);
  246. *this = pszPath;
  247. strTemp.RemovePath();
  248. Append(strTemp);
  249. }
  250. __DATL_INLINE void
  251. tpath::SetDirectory(
  252. LPCTSTR pszDir
  253. )
  254. {
  255. tpath path;
  256. GetPath(&path);
  257. path.StripToRoot();
  258. path.AddBackslash();
  259. path.Append(pszDir);
  260. SetPath(path);
  261. }
  262. __DATL_INLINE void
  263. tpath::SetFileSpec(
  264. LPCTSTR pszFileSpec
  265. )
  266. {
  267. RemoveFileSpec();
  268. Append(pszFileSpec);
  269. }
  270. __DATL_INLINE void
  271. tpath::SetExtension(
  272. LPCTSTR pszExt
  273. )
  274. {
  275. ::PathRenameExtension(GetBuffer(max(MAX_PATH, length() + lstrlen(pszExt) + 2)), pszExt);
  276. ReleaseBuffer();
  277. }