pathstr.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 6k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. //+-------------------------------------------------------------------------
  2. //
  3. //  Microsoft Windows
  4. //
  5. //  Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. //  File:       pathstr.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include "pch.h"
  11. #pragma hdrstop
  12. #include "pathstr.h"
  13. CPath::CPath(
  14.     LPCTSTR pszRoot, 
  15.     LPCTSTR pszDir, 
  16.     LPCTSTR pszFile, 
  17.     LPCTSTR pszExt
  18.     )
  19. {
  20.     if (pszDir)
  21.         SetPath(pszDir);
  22.     if (pszRoot)
  23.         SetRoot(pszRoot);
  24.     if (pszFile)
  25.         SetFileSpec(pszFile);
  26.     if (pszExt)
  27.         SetExtension(pszExt);
  28. }
  29. CPath::CPath(
  30.     const CPath& rhs
  31.     ) : CString(rhs)
  32. {
  33. }
  34.     
  35. CPath& 
  36. CPath::operator = (
  37.     const CPath& rhs
  38.     )
  39. {
  40.     if (this != &rhs)
  41.     {
  42.         CString::operator = (rhs);
  43.     }
  44.     return *this;
  45. }
  46. CPath& 
  47. CPath::operator = (
  48.     LPCTSTR rhs
  49.     )
  50. {
  51.     CString::operator = (rhs);
  52.     return *this;
  53. }
  54. void
  55. CPath::AddBackslash(
  56.     void
  57.     )
  58. {
  59.     ::PathAddBackslash(GetBuffer(MAX(MAX_PATH, Length() + 2)));
  60.     ReleaseBuffer();
  61. }
  62. void
  63. CPath::RemoveBackslash(
  64.     void
  65.     )
  66. {
  67.     ::PathRemoveBackslash(GetBuffer());
  68.     ReleaseBuffer();
  69. }
  70. bool 
  71. CPath::GetRoot(
  72.     CPath *pOut
  73.     ) const
  74. {
  75.     CPath temp(*this);
  76.     temp.StripToRoot();
  77.     *pOut = temp;
  78.     return 0 < pOut->Length();
  79. }
  80. bool 
  81. CPath::GetPath(
  82.     CPath *pOut
  83.     ) const
  84. {
  85.     CPath temp(*this);
  86.     temp.RemoveFileSpec();
  87.     *pOut = temp;
  88.     return 0 < pOut->Length();
  89. }
  90. bool
  91. CPath::GetDirectory(
  92.     CPath *pOut
  93.     ) const
  94. {
  95.     if (GetPath(pOut))
  96.         pOut->RemoveRoot();
  97.     return 0 < pOut->Length();
  98. }
  99. bool 
  100. CPath::GetExtension(
  101.     CPath *pOut
  102.     ) const
  103. {
  104.     *pOut = ::PathFindExtension(*this);
  105.     return 0 < pOut->Length();
  106. }
  107. bool 
  108. CPath::GetFileSpec(
  109.     CPath *pOut
  110.     ) const
  111. {
  112.     *pOut = ::PathFindFileName(*this);
  113.     return 0 < pOut->Length();
  114. }
  115.     
  116. bool 
  117. CPath::Append(
  118.     LPCTSTR psz
  119.     )
  120. {
  121.     bool bResult = boolify(::PathAppend(GetBuffer(MAX(MAX_PATH, Length() + lstrlen(psz) + 3)), psz));
  122.     ReleaseBuffer();
  123.     return bResult;
  124. }
  125. bool 
  126. CPath::BuildRoot(
  127.     int iDrive
  128.     )
  129. {
  130.     Empty();
  131.     bool bResult = NULL != ::PathBuildRoot(GetBuffer(5), iDrive);
  132.     ReleaseBuffer();
  133.     return bResult;
  134. }
  135. bool 
  136. CPath::Canonicalize(
  137.     void
  138.     )
  139. {
  140.     CString strTemp(*this);
  141.     bool bResult = boolify(::PathCanonicalize(GetBuffer(MAX(MAX_PATH, Size())), strTemp));
  142.     ReleaseBuffer();
  143.     return bResult;
  144. }
  145. bool 
  146. CPath::Compact(
  147.     HDC hdc, 
  148.     int cxPixels
  149.     )
  150. {
  151.     bool bResult = boolify(::PathCompactPath(hdc, GetBuffer(), cxPixels));
  152.     ReleaseBuffer();
  153.     return bResult;
  154. }
  155. bool 
  156. CPath::CommonPrefix(
  157.     LPCTSTR pszPath1, 
  158.     LPCTSTR pszPath2
  159.     )
  160. {
  161.     Empty();
  162.     ::PathCommonPrefix(pszPath1, 
  163.                        pszPath2, 
  164.                        GetBuffer(MAX(MAX_PATH, (MAX(lstrlen(pszPath1), lstrlen(pszPath2)) + 1))));
  165.     ReleaseBuffer();
  166.     return 0 < Length();
  167. }
  168. void
  169. CPath::QuoteSpaces(
  170.     void
  171.     )
  172. {
  173.     ::PathQuoteSpaces(GetBuffer(MAX(MAX_PATH, Length() + 3)));
  174.     ReleaseBuffer();
  175. }
  176. void 
  177. CPath::UnquoteSpaces(
  178.     void
  179.     )
  180. {
  181.     ::PathUnquoteSpaces(GetBuffer());
  182.     ReleaseBuffer();
  183. }
  184. void 
  185. CPath::RemoveBlanks(
  186.     void
  187.     )
  188. {
  189.     ::PathRemoveBlanks(GetBuffer());
  190.     ReleaseBuffer();
  191. }
  192. void
  193. CPath::RemoveExtension(
  194.     void
  195.     )
  196. {
  197.     PathRemoveExtension(GetBuffer());
  198.     ReleaseBuffer();
  199. }
  200. void
  201. CPath::RemoveFileSpec(
  202.     void
  203.     )
  204. {
  205.     ::PathRemoveFileSpec(GetBuffer());
  206.     ReleaseBuffer();
  207. }
  208. void
  209. CPath::RemoveRoot(
  210.     void
  211.     )
  212. {
  213.     LPTSTR psz = ::PathSkipRoot(*this);
  214.     if (psz)
  215.     {
  216.         CPath temp(psz);
  217.         *this = temp; 
  218.     }
  219. }
  220. void
  221. CPath::RemovePath(
  222.     void
  223.     )
  224. {
  225.     CPath temp;
  226.     GetFileSpec(&temp);
  227.     *this = temp;
  228. }
  229. void
  230. CPath::StripToRoot(
  231.     void
  232.     )
  233. {
  234.     ::PathStripToRoot(GetBuffer());
  235.     ReleaseBuffer();
  236. }
  237. void 
  238. CPath::SetRoot(
  239.     LPCTSTR pszRoot
  240.     )
  241. {
  242.     CPath strTemp(*this);
  243.     strTemp.RemoveRoot();
  244.     *this = pszRoot;
  245.     Append(strTemp);
  246. }
  247. void
  248. CPath::SetPath(
  249.     LPCTSTR pszPath
  250.     )
  251. {
  252.     CPath strTemp(*this);
  253.     *this = pszPath;
  254.     strTemp.RemovePath();
  255.     Append(strTemp);
  256. }
  257. void
  258. CPath::SetDirectory(
  259.     LPCTSTR pszDir
  260.     )
  261. {
  262.     CPath path;
  263.     GetPath(&path);
  264.     path.StripToRoot();
  265.     path.AddBackslash();
  266.     path.Append(pszDir);
  267.     SetPath(path);
  268. }
  269. void
  270. CPath::SetFileSpec(
  271.     LPCTSTR pszFileSpec
  272.     )
  273. {
  274.     RemoveFileSpec();
  275.     Append(pszFileSpec);
  276. }
  277. void
  278. CPath::SetExtension(
  279.     LPCTSTR pszExt
  280.     )
  281. {
  282.     ::PathRenameExtension(GetBuffer(MAX(MAX_PATH, Length() + lstrlen(pszExt) + 2)), pszExt);
  283.     ReleaseBuffer();
  284. }
  285. CPathIter::CPathIter(
  286.     const CPath& path
  287.     ) : m_path(path),
  288.         m_pszCurrent((LPTSTR)m_path.Cstr())
  289. {
  290.     //
  291.     // Skip over leading whitespace and backslashes.
  292.     //
  293.     while(*m_pszCurrent &&
  294.           (TEXT('\') == *m_pszCurrent ||
  295.            TEXT(' ') == *m_pszCurrent  ||
  296.            TEXT('t') == *m_pszCurrent ||
  297.            TEXT('n') == *m_pszCurrent))
  298.     {
  299.         m_pszCurrent++;
  300.     }
  301. }
  302. bool
  303. CPathIter::Next(
  304.     CPath *pOut
  305.     )
  306. {
  307.     DBGASSERT((NULL != pOut));
  308.     LPCTSTR pszStart = m_pszCurrent;
  309.     if (NULL == pszStart || TEXT('') == *pszStart)
  310.         return false;
  311.     TCHAR chTemp = TEXT('');
  312.     m_pszCurrent = ::PathFindNextComponent(pszStart);
  313.     if (NULL != m_pszCurrent && *m_pszCurrent)
  314.         SWAP(*(m_pszCurrent - 1), chTemp);
  315.     *pOut = pszStart;
  316.     if (TEXT('') != chTemp)
  317.         SWAP(*(m_pszCurrent - 1), chTemp);
  318.     return true;
  319. }
  320. void
  321. CPathIter::Reset(
  322.     void
  323.     )
  324. {
  325.     m_pszCurrent = (LPTSTR)m_path.Cstr();
  326. }