filespecutils.cpp
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 11k
Category:

Symbian

Development Platform:

C/C++

  1. /* ***** BEGIN LICENSE BLOCK ***** 
  2.  * Version: RCSL 1.0/RPSL 1.0 
  3.  *  
  4.  * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
  5.  *      
  6.  * The contents of this file, and the files included with this file, are 
  7.  * subject to the current version of the RealNetworks Public Source License 
  8.  * Version 1.0 (the "RPSL") available at 
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed 
  10.  * the file under the RealNetworks Community Source License Version 1.0 
  11.  * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
  12.  * in which case the RCSL will apply. You may also obtain the license terms 
  13.  * directly from RealNetworks.  You may not use this file except in 
  14.  * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
  15.  * applicable to this file, the RCSL.  Please see the applicable RPSL or 
  16.  * RCSL for the rights, obligations and limitations governing use of the 
  17.  * contents of the file.  
  18.  *  
  19.  * This file is part of the Helix DNA Technology. RealNetworks is the 
  20.  * developer of the Original Code and owns the copyrights in the portions 
  21.  * it created. 
  22.  *  
  23.  * This file, and the files included with this file, is distributed and made 
  24.  * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  25.  * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  26.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
  27.  * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  28.  * 
  29.  * Technology Compatibility Kit Test Suite(s) Location: 
  30.  *    http://www.helixcommunity.org/content/tck 
  31.  * 
  32.  * Contributor(s): 
  33.  *  
  34.  * ***** END LICENSE BLOCK ***** */ 
  35. #include "hxtypes.h"
  36. #include "hlxclib/sys/stat.h"
  37. #include "hxtick.h"
  38. #include "hxrand.h"
  39. #include "filespecutils.h"
  40. #include "symbihxdataf.h"
  41. HX_RESULT CHXFileSpecUtils::GetFreeSpaceOnDisk(const CHXDirSpecifier& volSpec, INT64& freeSpace)
  42. {
  43.     HX_RESULT retVal = HXR_NOTIMPL;
  44.     
  45.     HX_ASSERT("GetFreeSpaceOnDisk Not written yet" == NULL);
  46.     return retVal;
  47. }
  48. //******************************************************************************
  49. HX_RESULT CHXFileSpecUtils::GetTotalSpaceOnDisk(const CHXDirSpecifier& volSpec, 
  50. INT64& totalSpace)
  51.     HX_RESULT retVal = HXR_NOTIMPL;
  52.     
  53.     HX_ASSERT("GetTotalSpaceOnDisk Not written yet" == NULL);
  54.     return retVal;
  55. }
  56. //******************************************************************************
  57. BOOL CHXFileSpecUtils::IsDiskEjectable(const CHXDirSpecifier& volSpec)
  58. {
  59.     BOOL bRetVal = FALSE;
  60.     
  61.     HX_ASSERT("IsDiskEjectable Not written yet" == NULL);
  62.     return bRetVal;
  63. }
  64. // IsLocal returns TRUE if the file or directory is on a local volume
  65. // (not on a server)
  66. //******************************************************************************
  67. /* Include correct headers to get more fs types below... */
  68. BOOL CHXFileSpecUtils::IsDiskLocal(const CHXDirSpecifier& volSpec)
  69. {
  70.     BOOL bRetVal = FALSE;
  71.     
  72.     HX_ASSERT("IsDiskLocal Not written yet" == NULL);
  73.     return bRetVal;
  74. }
  75. // file/dir utilities
  76. //******************************************************************** **********
  77. HX_RESULT CHXFileSpecUtils::RemoveDir(const CHXDirSpecifier& dirSpec)
  78. {
  79.     HX_RESULT retVal = HXR_NOTIMPL;
  80.     
  81.     HX_ASSERT("RemoveDir Not written yet" == NULL);
  82.     return retVal;
  83. };
  84. //******************************************************************** **********
  85. HX_RESULT CHXFileSpecUtils::RemoveFile(const CHXFileSpecifier& fileSpec)
  86. {
  87.     HX_RESULT retVal = HXR_FAILED;
  88.     CSymbIHXDataFile* pDataFile = new CSymbIHXDataFile();
  89.     if (pDataFile)
  90.     {
  91. pDataFile->AddRef();
  92. pDataFile->Bind(fileSpec.GetPathName());
  93. retVal = pDataFile->Delete();
  94. pDataFile->Close();
  95. pDataFile->Release();
  96.     }
  97.     return retVal;
  98. };
  99. //******************************************************************************
  100. HX_RESULT CHXFileSpecUtils::GetFileSize(const CHXFileSpecifier& fileSpec, INT64& fSize)
  101. {
  102.     HX_RESULT retVal = HXR_FAILED;
  103.     CSymbIHXDataFile* pDataFile = new CSymbIHXDataFile();
  104.     if (pDataFile)
  105.     {
  106. struct stat statBuffer;
  107. pDataFile->AddRef();
  108. pDataFile->Bind(fileSpec.GetPathName());
  109. retVal = pDataFile->Stat(&statBuffer);
  110. pDataFile->Close();
  111. pDataFile->Release();
  112. if (retVal == HXR_OK)
  113. {
  114.     fSize = statBuffer.st_size;
  115. }
  116.     }
  117.     return retVal;
  118. }
  119. //******************************************************************************
  120. HX_RESULT CHXFileSpecUtils::GetDirectorySize(const CHXDirSpecifier& dirSpec, BOOL shouldDescend, INT64& fSize)
  121. {
  122.     HX_RESULT retVal = HXR_NOTIMPL;
  123.     
  124.     HX_ASSERT("GetDirectorySize Not written yet" == NULL);
  125.     return retVal;
  126. }
  127. //******************************************************************************
  128. CHXFileSpecifier CHXFileSpecUtils::GetCurrentApplication(void)
  129. {
  130.     CHXFileSpecifier retSpecifier;
  131.     
  132.     HX_ASSERT("GetCurrentApplication Not written yet" == NULL);
  133.     return retSpecifier;
  134. }
  135. //******************************************************************************
  136. CHXDirSpecifier CHXFileSpecUtils::GetCurrentApplicationDir(void)
  137. {
  138.     CHXDirSpecifier retSpecifier;
  139.     
  140.     HX_ASSERT("GetCurrentApplicationDir Not written yet" == NULL);
  141.     return retSpecifier;
  142. }
  143. //******************************************************************************
  144. BOOL CHXFileSpecUtils::FileExists(const CHXFileSpecifier& fileSpec)
  145. {
  146.     HX_RESULT bRetVal = FALSE;
  147.     CSymbIHXDataFile* pDataFile = new CSymbIHXDataFile();
  148.     if (pDataFile)
  149.     {
  150. struct stat statBuffer;
  151. pDataFile->AddRef();
  152. pDataFile->Bind(fileSpec.GetPathName());
  153. bRetVal = (pDataFile->Stat(&statBuffer) == HXR_OK);
  154. pDataFile->Close();
  155. pDataFile->Release();
  156.     }
  157.     return bRetVal;
  158. }
  159. //******************************************************************************
  160. BOOL CHXFileSpecUtils::DirectoryExists(const CHXDirSpecifier& dirSpec)
  161. {
  162.     BOOL bRetVal = FALSE;
  163.     
  164.     HX_ASSERT("DirectoryExists Not written yet" == NULL);
  165.     return bRetVal;
  166. }
  167. //******************************************************************************
  168. HX_RESULT CHXFileSpecUtils::CreateDir(const CHXDirSpecifier& dirSpec)
  169. {
  170.     HX_RESULT retVal = HXR_NOTIMPL;
  171.     
  172.     HX_ASSERT("CreateDir Not written yet" == NULL);
  173.     return retVal;
  174. }
  175. //******************************************************************************
  176. static CHXFileSpecifier GetUniqueFileSpecInternal(const CHXDirSpecifier& locationSpec, 
  177.   const char *pszNameFirst, const char *pszTemplate, 
  178.   const char *pszWildcard, UINT32 nStartNum);
  179. const UINT32 kNumWrapValue = 9999+1; // limit insertions to 4-digit numbers
  180. CHXFileSpecifier CHXFileSpecUtils::GetUniqueFileSpec(const CHXDirSpecifier& locationSpec, 
  181.      const char *pszNameFirst, const char *pszTemplate, 
  182.      const char *pszWildcard)
  183. {
  184.     return GetUniqueFileSpecInternal(locationSpec, pszNameFirst, pszTemplate, pszWildcard, 0);
  185. }
  186. CHXFileSpecifier CHXFileSpecUtils::GetUniqueTempFileSpec(const CHXDirSpecifier& locationSpec, 
  187.  const char *pszTemplate, const char *pszWildcard)
  188. {
  189.     CMultiplePrimeRandom  rand(HX_GET_TICKCOUNT());
  190.     
  191.     UINT32 num;
  192.     
  193.     num = rand.GetRandomNumber();
  194.     
  195.     num %= kNumWrapValue;
  196.     
  197.     if (num == 0 || num == 1) num = 2;
  198.     
  199.     return GetUniqueFileSpecInternal(locationSpec, NULL, pszTemplate, pszWildcard, num);
  200. }
  201. static CHXFileSpecifier GetUniqueFileSpecInternal(const CHXDirSpecifier& locationSpec, 
  202.                                                   const char *pszNameFirst, const char *pszTemplate, 
  203.                                                   const char *pszWildcard, UINT32 nStartNum)
  204. {
  205.     CHXFileSpecifier  resultFileSpec;
  206.     
  207.     require_return(locationSpec.IsSet(), resultFileSpec);
  208.     require_return(pszTemplate != NULL && pszWildcard != NULL, resultFileSpec);
  209.     require_return(pszNameFirst != NULL || nStartNum != 0, resultFileSpec);
  210.     
  211.     CHXFileSpecifier  testFileSpec;
  212.     CHXDirSpecifier  testDirSpec;
  213.     CHXString strNumber;
  214.     CHXString strName;
  215.     UINT32 nCurrentNum;
  216.     
  217.     nCurrentNum = nStartNum;
  218.     
  219.     while (1) 
  220.     {
  221.         // if the number is non-zero, make a string from the template;
  222.         // if the number is zero, user the initial name string
  223.         if (nCurrentNum == 0)
  224.         {
  225.             // replace the wildcard in the template with the number string
  226.             strName = pszNameFirst;
  227.         }
  228.         else
  229.         {
  230.             // replace the wildcard in the template with the number string
  231.             strNumber.Empty();
  232.             strNumber.AppendULONG(nCurrentNum);
  233.     
  234.             strName = pszTemplate;
  235.             strName.FindAndReplace(pszWildcard, strNumber); // replace first wildcard with number string
  236.         }
  237.         // test if a file or directory exists with that name
  238.         testFileSpec = locationSpec.SpecifyChildFile(strName);
  239.         testDirSpec = locationSpec.SpecifyChildDirectory(strName);
  240.         if (CHXFileSpecUtils::FileExists(testFileSpec)
  241.             || CHXFileSpecUtils::DirectoryExists(testDirSpec))
  242.         {
  243.             // an item already has that name, so increment & wrap the number
  244.             nCurrentNum++;
  245.             nCurrentNum %= kNumWrapValue;
  246.     
  247.             // don't use 0 again, and skip 1 since "MyFile2.txt" logically follows "MyFile.txt"
  248.             if (nCurrentNum == 0 || nCurrentNum == 1) 
  249.             {
  250.                 nCurrentNum = 2; 
  251.             }
  252.     
  253.             // a quick sanity check
  254.             if (nCurrentNum == nStartNum)
  255.             {
  256.                 check(!"GetUniqueFileSpecInternal number wrapped");
  257.                 break;
  258.             }
  259.         }
  260.         else
  261.         {
  262.             // the name is unique
  263.             resultFileSpec = testFileSpec;
  264.             break;
  265.         }
  266.     } // while
  267.     
  268.     return resultFileSpec;
  269. }
  270. //******************************************************************************
  271. CHXDirSpecifier CHXFileSpecUtils::GetSystemTempDirectory()
  272. {
  273.     CHXDirSpecifier retSpecifier;
  274.     
  275.     HX_ASSERT("GetSystemTempDirectory Not written yet" == NULL);
  276.     return retSpecifier;
  277. }
  278. BOOL CHXFileSpecUtils::MakeNameLegal(char *pszName)
  279. {
  280.     BOOL bRetVal = FALSE;
  281.     
  282.     HX_ASSERT("MakeNameLegal Not written yet" == NULL);
  283.     return bRetVal;
  284. }
  285. //******************************************************************************
  286. CHXDirSpecifier 
  287. CHXFileSpecUtils::GetAppDataDir(const char* szAppName)
  288. {
  289.     CHXDirSpecifier retSpecifier;
  290.     
  291.     HX_ASSERT("GetAppDataDir Not written yet" == NULL);
  292.     return retSpecifier;
  293. }