filespec.cpp
Upload User: dangjiwu
Upload Date: 2013-07-19
Package Size: 42019k
Code Size: 17k
Category:

Symbian

Development Platform:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: filespec.cpp,v 1.1.32.3 2004/07/09 01:44:17 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. #include "hxstring.h"
  50. #include "filespec.h"
  51. #define MAX_PATH_COMPONENTS 256
  52. //////////////////////////////////////////////////////////
  53. //
  54. // Utility base class -- CHXPathParser
  55. //
  56. //////////////////////////////////////////////////////////
  57. CHXPathParser::CHXPathParser()
  58. : m_bEmpty(TRUE)
  59. , m_bIsValid(FALSE)
  60. {
  61. }
  62. CHXPathParser &CHXPathParser::operator=(const CHXPathParser &other)
  63. {
  64.     if(!other.IsSet())
  65.     {
  66. m_bEmpty = TRUE;
  67. m_path = "";
  68.     }
  69.     else
  70. ParsePath(other.m_path);
  71.     return *this;
  72. }
  73. BOOL CHXPathParser::operator==(const CHXPathParser &other)
  74. {
  75.     // for now, just returns if the paths are identical
  76.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  77.     return m_path.CompareNoCase(other.m_path) == 0;
  78. }
  79. BOOL CHXPathParser::operator!=(const CHXPathParser &other)
  80. {
  81.     // for now, just returns if the paths are not identical
  82.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  83.     return !(*this == other);
  84. }
  85. CHXString CHXPathParser::GetPathName() const
  86. {
  87.     return m_path;
  88. }
  89. BOOL CHXPathParser::IsSet() const
  90. {
  91.     return !m_bEmpty;
  92. }
  93. void CHXPathParser::UnSet()
  94. {
  95. m_path = "";
  96. m_bEmpty = TRUE;
  97. }
  98. void CHXPathParser::ParsePath(const char *psz)
  99. {
  100.     m_path = psz;
  101.     int i, nStart, nLength;
  102.     int nPathLength = m_path.GetLength();
  103.     m_bIsValid = TRUE; // possibly set to false later in this method
  104.     m_bCannotBeFile = FALSE;
  105.     m_bAbsolute = TRUE;
  106.     m_bEmpty = FALSE;
  107.     m_nVolumeLength = 0;
  108.     m_nParentLength = 0;
  109.     m_nSeparatorLength = 0;
  110.     m_nNameLength = 0;
  111.     m_nExtensionSeparatorLength = 0;
  112.     m_nExtensionLength = 0;
  113.     if(psz == NULL || m_path.IsEmpty())
  114.     {
  115. m_bIsValid = FALSE;
  116. m_bCannotBeFile = TRUE;
  117. m_bEmpty = TRUE;
  118. return;
  119.     }
  120.     // first, parse the local disk name, if present
  121.     if(nPathLength >= 2 && m_path[1] == ':')
  122.     {
  123. // local disk/volume
  124. m_nVolumeLength = 2;
  125. nStart = 2;
  126.     }
  127.     else
  128.     {
  129. m_nVolumeLength = 0;
  130. nStart = 0;
  131.     }
  132.     // now, break the rest of the path up into components based on slashes
  133.     // Here is an example of how the path will be broken up.  The carats point
  134.     // to the start of each component.  The separator length will be the
  135.     // number of slashes following the carat, and the name will be the remaining
  136.     // characters in that component.
  137.     //
  138.     // \hostnameblablablafile.txt
  139.     // ^         ^   ^   ^   ^ 
  140.     //
  141.     // In the case of a trailing slash:
  142.     //
  143.     // blabla
  144.     // ^   ^   ^
  145.     //
  146.     int anStart[MAX_PATH_COMPONENTS];
  147.     int anSeparatorLen[MAX_PATH_COMPONENTS];
  148.     int anNameLen[MAX_PATH_COMPONENTS];
  149.     int nNumComponents = 0;
  150.     while(nStart < nPathLength && nNumComponents < MAX_PATH_COMPONENTS)
  151.     {
  152. int nNumSep, nNameLen;
  153. // count the number of separators starting at position nStart
  154. for(nNumSep = 0; 
  155. nStart + nNumSep < nPathLength && 
  156. (m_path[nStart + nNumSep] == '\' || 
  157.  m_path[nStart + nNumSep] == '/');
  158. nNumSep++);
  159. // count the number of non-separators starting at position nStart + nNumSep
  160. for(nNameLen = 0; 
  161. nStart + nNumSep + nNameLen < nPathLength && 
  162. (m_path[nStart + nNumSep + nNameLen] != '\' &&
  163.  m_path[nStart + nNumSep + nNameLen] != '/');
  164. nNameLen++);
  165. anStart[nNumComponents] = nStart;
  166. anSeparatorLen[nNumComponents] = nNumSep;
  167. anNameLen[nNumComponents] = nNameLen;
  168. nNumComponents++;
  169. nStart += nNumSep + nNameLen;
  170.     }
  171.     // now we are ready to interpret the results
  172.     if(nNumComponents == 0)
  173.     {
  174. // no components at all.  If there was a volume, then this is a relative
  175. // path on the volume [and cannot be a file].  If there was no volume, then
  176. // this is invalid
  177. if(m_nVolumeLength > 0)
  178. {
  179.     m_bAbsolute = FALSE;
  180.     m_bCannotBeFile = TRUE;
  181. }
  182. else
  183.     m_bIsValid = FALSE;
  184.     }
  185.     else
  186.     {
  187. // we will now start using nStart as the component number of the start of the actual path [excluding volume]
  188. nStart = 0;
  189. // here, we look for a possible \hostname type of volume
  190. if(anSeparatorLen[0] > 1)
  191. {
  192.     if(m_nVolumeLength > 0)
  193.     {
  194. // this catches these invalid paths: C:\\temp, C:\hostnamebla
  195. // the problem with the second one is that it has both a local and network volume
  196. // the problem with the first one is it has too many slashes to be an absolute path
  197. m_bIsValid = FALSE;
  198. // however, we continue parsing the path, as if this was ok
  199. nStart = 0;
  200.     }
  201.     else if(anSeparatorLen[0] > 2)
  202.     {
  203. // this catches these invalid paths: \\hostnamebla
  204. m_bIsValid = FALSE;
  205. // however, we continue parsing the path, as if this was ok,
  206. // and if it was not a hostname
  207. nStart = 0;
  208.     }
  209.     else
  210.     {
  211. // this catches these *valid* paths: \hostnamebla
  212. m_nVolumeLength = anSeparatorLen[0] + anNameLen[0];
  213. nStart = 1;
  214.     }
  215. }
  216. // now that we have dealt with all possible volumes, nStart points to the first component in the
  217. // actual path.  There are still several possibilities:
  218. //
  219. // "" [i.e. no more components]
  220. // 
  221. // bla
  222. // bla.txt
  223. // blabla.txt
  224. // bla
  225. // bla.txt
  226. // blabla.txt
  227. // these cannot be files: "", "bla", "blabla"
  228. if(nNumComponents == nStart || anNameLen[nNumComponents - 1] == 0)
  229.     m_bCannotBeFile = TRUE;
  230. // these are not valid: "\\file.txt",
  231. if(nNumComponents > nStart && anSeparatorLen[nStart] > 1)
  232.     m_bIsValid = FALSE;
  233. // these are absolute: "blafile.txt"
  234. // there is one special case: the full path "\hostname" is absolute
  235. if((nNumComponents > nStart && anSeparatorLen[nStart] >= 1) || m_path[0] == '\' || m_path[0] == '/')
  236.     m_bAbsolute = TRUE;
  237. else
  238.     m_bAbsolute = FALSE;
  239. if(nNumComponents == nStart + 1 && anNameLen[nStart] == 0)
  240. {
  241.     // the path is simply: ""
  242.     m_nParentLength = anSeparatorLen[nStart];
  243.     nStart = nPathLength;
  244.     nLength = 0;
  245. }
  246. else if(nNumComponents > nStart)
  247. {
  248.     // remove the last component if it is just a slash
  249.     if(nNumComponents > nStart && anNameLen[nNumComponents-1] == 0)
  250. nNumComponents--;
  251.     if(nNumComponents == nStart + 1)
  252.     {
  253. // just one component
  254. m_nParentLength = anSeparatorLen[nStart];
  255. // back to using nStart as an index into the path.
  256. // note that we must set nLength first, since it
  257. // uses nStart as the index
  258. nLength = anNameLen[nStart];
  259. nStart = anStart[nStart] + anSeparatorLen[nStart];
  260.     }
  261.     else
  262.     {
  263. // more than one component
  264. m_nParentLength = anStart[nNumComponents - 1] - m_nVolumeLength;
  265. m_nSeparatorLength = anSeparatorLen[nNumComponents - 1];
  266. // back to using nStart as an index into the path
  267. nLength = anNameLen[nNumComponents - 1];
  268. nStart = anStart[nNumComponents - 1] + anSeparatorLen[nNumComponents - 1];
  269.     }
  270. }
  271. else
  272. {
  273.     // no more components
  274.     nStart = nPathLength;
  275.     nLength = 0;
  276. }
  277. // now, use nStart and nLength as the portion of the path to look at for the name
  278. // now we break it up into name, extension separator, and extension
  279. for(i = nStart + nLength - 1; i >= nStart; i--)
  280.     if(m_path[i] == '.')
  281. break;
  282. if(i < nStart)
  283.     m_nNameLength = nLength;
  284. else
  285. {
  286.     m_nNameLength = i - nStart;
  287.     m_nExtensionSeparatorLength = 1;
  288.     m_nExtensionLength = nStart + nLength - i - 1;
  289. }
  290.     }
  291. }
  292. CHXString CHXPathParser::GetSubstring(int nStart, int nLength) const
  293. {
  294.     if(nLength == -1)
  295. nLength = m_path.GetLength() - nStart;
  296.     return m_path.Mid(nStart, nLength);
  297. }
  298. CHXString CHXPathParser::GetPersistentString() const
  299. {
  300.     return m_path;
  301. }
  302. void CHXPathParser::SetFromPersistentString(const char *pBuffer)
  303. {
  304.     ParsePath(pBuffer);
  305. }
  306. //////////////////////////////////////////////////////////
  307. //
  308. // CHXFileSpecifier
  309. //
  310. //////////////////////////////////////////////////////////
  311. CHXFileSpecifier::CHXFileSpecifier()
  312. {
  313. }
  314. CHXFileSpecifier::CHXFileSpecifier(const char* psz)
  315. {
  316.     m_parser.ParsePath(psz);
  317. }
  318. CHXFileSpecifier::CHXFileSpecifier(const CHXString &str)
  319. {
  320.     m_parser.ParsePath(str);
  321. }
  322. CHXFileSpecifier::CHXFileSpecifier(const CHXFileSpecifier &other)
  323. {
  324.     *this = other;
  325. }
  326. CHXFileSpecifier::~CHXFileSpecifier()
  327. {
  328. }
  329. CHXFileSpecifier &CHXFileSpecifier::operator=(const CHXFileSpecifier &other)
  330. {
  331.     m_parser = other.m_parser;
  332.     return *this;
  333. }
  334. BOOL CHXFileSpecifier::operator==(const CHXFileSpecifier &other)
  335. {
  336.     // for now, just returns if the paths are identical
  337.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  338.     return m_parser == other.m_parser;
  339. }
  340. BOOL CHXFileSpecifier::operator!=(const CHXFileSpecifier &other)
  341. {
  342.     // for now, just returns if the paths are not identical
  343.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  344.     return m_parser != other.m_parser;
  345. }
  346. CHXString CHXFileSpecifier::GetPathName() const
  347. {
  348.     return m_parser.GetPathName();
  349. }
  350. CHXString CHXFileSpecifier::GetURL() const
  351. {
  352.     const BOOL kReplaceAll = TRUE;
  353.     
  354.     CHXString strPath, strURL;
  355.     
  356.     strPath = m_parser.GetPathName();
  357.     if (strPath.GetLength() > 0)
  358.     {
  359.         strPath.FindAndReplace("\", "/", kReplaceAll); // replace path separators
  360.         strURL = "file://";
  361.     strURL += strPath;
  362.     }
  363.     
  364.     return strURL;
  365. }
  366. BOOL CHXFileSpecifier::IsSet() const
  367. {
  368.     return m_parser.IsSet();
  369. }
  370. CHXDirSpecifier CHXFileSpecifier::GetParentDirectory() const
  371. {
  372.     if(IsSet())
  373. return CHXDirSpecifier(m_parser.GetSubstring(0, m_parser.m_nVolumeLength + m_parser.m_nParentLength));
  374.     else
  375. return CHXDirSpecifier();
  376. }
  377. CHXDirSpecifier CHXFileSpecifier::GetVolume() const
  378. {
  379.     if(IsSet())
  380. return CHXDirSpecifier(m_parser.GetSubstring(0, m_parser.m_nVolumeLength));
  381.     else
  382. return CHXDirSpecifier();
  383. }
  384. CHXString CHXFileSpecifier::GetName() const
  385. {
  386.     if(IsSet())
  387. return m_parser.GetSubstring(m_parser.m_nVolumeLength +
  388.     m_parser.m_nParentLength + m_parser.m_nSeparatorLength,
  389.     m_parser.m_nNameLength + m_parser.m_nExtensionSeparatorLength +
  390.     m_parser.m_nExtensionLength);
  391.     else
  392. return "";
  393. }
  394. CHXString CHXFileSpecifier::GetExtension() const
  395. {
  396.     if(IsSet())
  397. return m_parser.GetSubstring(m_parser.m_nVolumeLength +
  398.     m_parser.m_nParentLength + m_parser.m_nSeparatorLength +
  399.     m_parser.m_nNameLength + m_parser.m_nExtensionSeparatorLength,-1);
  400.     else
  401. return "";
  402. }
  403. CHXString CHXFileSpecifier::GetPersistentString() const
  404. {
  405.     return m_parser.GetPersistentString();
  406. }
  407. HX_RESULT CHXFileSpecifier::SetFromPersistentString(const char *pBuffer)
  408. {
  409.     m_parser.SetFromPersistentString(pBuffer);
  410. return HXR_OK;
  411. }
  412. void CHXFileSpecifier::Unset()
  413. {
  414. m_parser.UnSet();
  415. }
  416. HX_RESULT CHXFileSpecifier::SetFromURL(const char *pBuffer)
  417. {
  418. CHXString  strURL, strChoppedURL;
  419. int nChop;
  420. int nLength;
  421. Unset();
  422. strURL = pBuffer;
  423. nLength = strURL.GetLength();
  424. nChop = 0;
  425. if (strURL.Left(8) == "file:///") nChop = 8;
  426. else if (strURL.Left(7) == "file://") nChop = 7;
  427. else if (strURL.Left(5) == "file:") nChop = 5;
  428. if (nChop > 0)
  429. {
  430. strChoppedURL = strURL.Right(nLength - nChop);
  431.     m_parser.ParsePath(strChoppedURL);
  432. }
  433. return IsSet() ? HXR_OK : HXR_FAILED;
  434. }
  435. //////////////////////////////////////////////////////////
  436. //
  437. // CHXDirSpecifier
  438. //
  439. //////////////////////////////////////////////////////////
  440. CHXDirSpecifier::CHXDirSpecifier()
  441. {
  442. }
  443. CHXDirSpecifier::CHXDirSpecifier(const char* psz)
  444. {
  445.     m_parser.ParsePath(psz);
  446. }
  447. CHXDirSpecifier::CHXDirSpecifier(const CHXString &str)
  448. {
  449.     m_parser.ParsePath(str);
  450. }
  451. CHXDirSpecifier::CHXDirSpecifier(const CHXDirSpecifier &other)
  452. {
  453.     *this = other;
  454. }
  455. CHXDirSpecifier::~CHXDirSpecifier()
  456. {
  457. }
  458. CHXDirSpecifier &CHXDirSpecifier::operator=(const CHXDirSpecifier &other)
  459. {
  460.     m_parser = other.m_parser;
  461.     return *this;
  462. }
  463. BOOL CHXDirSpecifier::operator==(const CHXDirSpecifier &other)
  464. {
  465.     // for now, just returns if the paths are identical
  466.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  467.     return m_parser == other.m_parser;
  468. }
  469. BOOL CHXDirSpecifier::operator!=(const CHXDirSpecifier &other)
  470. {
  471.     // for now, just returns if the paths are not identical
  472.     // in the future, should normalize paths (i.e. remove duplicate slashes, resolve ...... type issues
  473.     return m_parser != other.m_parser;
  474. }
  475. CHXString CHXDirSpecifier::GetPathName() const
  476. {
  477.     return m_parser.GetPathName();
  478. }
  479. BOOL CHXDirSpecifier::IsSet() const
  480. {
  481.     return m_parser.IsSet();
  482. }
  483. BOOL CHXDirSpecifier::IsVolume() const
  484. {
  485.     return m_parser.m_path.GetLength() == m_parser.m_nVolumeLength;
  486. }
  487. CHXString CHXDirSpecifier::GetName() const
  488. {
  489.     if(IsSet())
  490. return m_parser.GetSubstring(m_parser.m_nVolumeLength + m_parser.m_nParentLength + m_parser.m_nSeparatorLength,
  491.     m_parser.m_nNameLength + m_parser.m_nExtensionSeparatorLength + m_parser.m_nExtensionLength);
  492.     else
  493. return "";
  494. }
  495. CHXDirSpecifier CHXDirSpecifier::GetParentDirectory() const
  496. {
  497.     if(IsSet())
  498. return CHXDirSpecifier(m_parser.GetSubstring(0, m_parser.m_nVolumeLength + m_parser.m_nParentLength));
  499.     else
  500. return CHXDirSpecifier();
  501. }
  502. CHXDirSpecifier CHXDirSpecifier::GetVolume() const
  503. {
  504.     if(IsSet())
  505. return CHXDirSpecifier(m_parser.GetSubstring(0, m_parser.m_nVolumeLength));
  506.     else
  507. return CHXDirSpecifier();
  508. }
  509. CHXFileSpecifier CHXDirSpecifier::SpecifyChildFile(const char *child) const
  510. {
  511.     if(IsSet())
  512.     {
  513. char last_ch = m_parser.m_path[m_parser.m_path.GetLength() - 1];
  514. if(last_ch == '\' || last_ch == '/')
  515.     return CHXFileSpecifier( m_parser.m_path + child );
  516. else
  517.     return CHXFileSpecifier( m_parser.m_path + '\' + child );
  518.     }
  519.     else
  520. return child;
  521. }
  522. CHXDirSpecifier CHXDirSpecifier::SpecifyChildDirectory(const char *child) const
  523. {
  524.     if(IsSet())
  525.     {
  526. char last_ch = m_parser.m_path[m_parser.m_path.GetLength() - 1];
  527. if(last_ch == '\' || last_ch == '/')
  528.     return CHXDirSpecifier( m_parser.m_path + child );
  529. else
  530.     return CHXDirSpecifier( m_parser.m_path + '\' + child );
  531.     }
  532.     else
  533. return child;
  534. }
  535. CHXString CHXDirSpecifier::GetPersistentString() const
  536. {
  537.     return m_parser.GetPersistentString();
  538. }
  539. HX_RESULT CHXDirSpecifier::SetFromPersistentString(const char *pBuffer)
  540. {
  541.     m_parser.SetFromPersistentString(pBuffer);
  542. return HXR_OK;
  543. }
  544. #if 0
  545. Utility class:
  546. might be things like
  547. IsLocal() is this on a server or a local volume
  548. Rename()
  549. GetFilesInDirectory()  (gets a list into a buffer; much better than an iterator)
  550. GetFileType()
  551. GetFileModificationDate()
  552. GetFileSize()
  553. #endif // 0