macff.cpp
Upload User: zhongxx05
Upload Date: 2007-06-06
Package Size: 33641k
Code Size: 7k
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 <string.h>
  36. #include "platform/mac/macff.h"
  37. #include "pn_morefiles.h"
  38. #include "hxstrutl.h"
  39. #include "hxstring.h"
  40. #include "fullpathname.h"
  41. #ifndef _CARBON
  42. #include "morefilesextras.h" // for FSpGetDirectoryID, which is in pn_morefiles under Carbon
  43. #endif
  44. #ifndef _MAX_PATH
  45. #define _MAX_PATH 255
  46. #endif
  47. CMacFindFile::CMacFindFile ( const char *path,
  48. const char *delimiter,
  49. const char *pattern) :
  50.     CFindFile (path, delimiter, pattern)
  51. {
  52.     m_pCurrentDirectory     = NULL;
  53.     m_pCurrentFileName     = new char[_MAX_PATH];
  54.     *m_pCurrentFileName = 0;
  55.     m_pNextFileName     = new char[_MAX_PATH];
  56.     *m_pNextFileName = 0;
  57.     m_nIndex = 0;
  58.     m_VRefNum = 0;
  59.     m_DirID = 0;
  60. }
  61. CMacFindFile::~CMacFindFile()
  62. {
  63.     if (m_pCurrentDirectory)
  64.     {
  65. delete [] m_pCurrentDirectory;
  66. m_pCurrentDirectory = 0;
  67.     }
  68.     if (m_pCurrentFileName)
  69.     {
  70. delete [] m_pCurrentFileName;
  71. m_pCurrentFileName = 0;
  72.     }
  73.     if (m_pNextFileName)
  74.     {
  75. delete [] m_pNextFileName;
  76. m_pNextFileName = 0;
  77.     }
  78. }
  79. //
  80. // Open the directory; initialize the directory handle.
  81. // Return FALSE if the directory couldn't be opened.
  82. //
  83. BOOL CMacFindFile::OS_OpenDirectory (const char *dirname)
  84. {
  85.     BOOL bReturn = FALSE;
  86.     
  87.     if (m_pCurrentDirectory)
  88.     {
  89. delete [] m_pCurrentDirectory;
  90. m_pCurrentDirectory = 0;
  91.     }
  92.     UINT16 ulPatLen = 0;
  93.     /* 1 for  and 1 for NULL at end */
  94.     m_pCurrentDirectory = new char[strlen(dirname) + ulPatLen + 2];
  95.     if (!m_pCurrentDirectory)
  96. return FALSE;
  97.     strcpy(m_pCurrentDirectory, dirname); /* Flawfinder: ignore */
  98.     FSSpec dirSpec;
  99.     OSErr err = FSSpecFromPathName (dirname, &dirSpec); 
  100.     if (err == noErr)
  101.     {
  102. Boolean isDir;
  103. m_VRefNum = dirSpec.vRefNum;
  104. err = FSpGetDirectoryID(&dirSpec, &m_DirID, &isDir);
  105.     }
  106.     
  107.     if (err == noErr)
  108.     {
  109. m_nIndex = 0;
  110. bReturn = TRUE;
  111. BOOL bContinue;
  112. FSSpec nextFSpec;
  113. while (noErr == FSpGetNthDirectoryItem(m_VRefNum, m_DirID, ++m_nIndex, &nextFSpec))
  114. {
  115.     bContinue = TRUE;
  116.     if (!m_pNextFileName)
  117.     {
  118. m_pNextFileName     = new char[_MAX_PATH];
  119. if (!m_pNextFileName)
  120. {
  121.     return FALSE;
  122. }
  123. *m_pNextFileName = 0;
  124.     }
  125.     
  126.     if (!m_pCurrentFileName)
  127.     {
  128. m_pCurrentFileName     = new char[_MAX_PATH];
  129. if (!m_pCurrentFileName)
  130. {
  131.     return FALSE;
  132. }
  133. *m_pCurrentFileName = 0;
  134.     }
  135.     // matches pattern?
  136.     int len = nextFSpec.name[0];
  137.     INT16 patternLen = strlen(m_pattern)-1;
  138.     if (0 == strcmp("*.*", m_pattern))
  139.      bContinue = TRUE;
  140.     else
  141.     {
  142.     if (m_pattern[0] == '*')
  143.     {
  144.     if (0 != strncasecmp((char*)&nextFSpec.name[len-patternLen+1], &m_pattern[1], patternLen))
  145.      bContinue = FALSE;
  146.     }
  147.     else if (m_pattern[patternLen-1] == '*')
  148.     {
  149.          if (0 != strncasecmp((char*)&nextFSpec.name[1], &m_pattern[0], patternLen))
  150.           bContinue = FALSE;
  151.     }
  152.     else if (strncasecmp((char*)&nextFSpec.name[1], &m_pattern[0], patternLen))
  153.      bContinue = FALSE;
  154.     }
  155.     
  156.     if (bContinue)
  157.     {
  158.      CHXString tempStr;
  159.      tempStr = nextFSpec; // copy full path
  160.      SafeStrCpy(m_pNextFileName, (const char*)tempStr, _MAX_PATH);
  161.      return TRUE;
  162.     }
  163. }
  164.     }
  165.     // if we haven't returned yet, return TRUE if dir is valid & no pattern supplied
  166.     if (bReturn)
  167.     {
  168.      if (m_pattern)
  169.          bReturn = FALSE;
  170.     }
  171.     return bReturn;
  172. }
  173. //
  174. // Get the next file in the directory. Filters according to pattern
  175. //
  176. char* CMacFindFile::OS_GetNextFile()
  177. {
  178.     if (m_pNextFileName)
  179.     {
  180. SafeStrCpy(m_pCurrentFileName, m_pNextFileName, _MAX_PATH);
  181.     }
  182.     else
  183.     {
  184. if (m_pCurrentFileName)
  185. {
  186.     delete [] m_pCurrentFileName;
  187.     m_pCurrentFileName = NULL;
  188. }
  189.     }
  190.     
  191.     if (m_pNextFileName)
  192.     {
  193. FSSpec nextFSpec;
  194. BOOL bContinue;
  195. BOOL bFound = FALSE;
  196. INT16 len = 0;
  197. while (noErr == FSpGetNthDirectoryItem(m_VRefNum, m_DirID, ++m_nIndex, &nextFSpec))
  198. {
  199.     bContinue = TRUE;
  200.     // matches pattern(extension)? (eg. DLL)
  201.     len = nextFSpec.name[0]; // pascal str
  202.     if (m_pattern)
  203.     {
  204. INT16 patternLen = strlen(m_pattern)-1;
  205.      if (0 == strcmp("*.*", m_pattern))
  206.          bContinue = TRUE;
  207.      else
  208.      {
  209. if (m_pattern[0] == '*')
  210. {
  211.     if (0 != strncasecmp((char*)&nextFSpec.name[len-patternLen+1], &m_pattern[1], patternLen))
  212.      bContinue = FALSE;
  213. }
  214. else if (m_pattern[patternLen-1] == '*')
  215. {
  216.          if (0 != strncasecmp((char*)&nextFSpec.name[1], &m_pattern[0], patternLen))
  217.           bContinue = FALSE;
  218.      }
  219.      }
  220.     }
  221.     if (bContinue)
  222.     {
  223.      CHXString tempStr;
  224.      tempStr = nextFSpec; // copy full path
  225.         SafeStrCpy(m_pNextFileName, (const char*)tempStr, _MAX_PATH);
  226.      bFound = TRUE;
  227.      break;
  228.     }
  229. }
  230. if (!bFound)
  231. {
  232.     delete [] m_pNextFileName;
  233.     m_pNextFileName = 0;
  234. }
  235.     }
  236. #ifdef _CARBON
  237. //xxxbobclark I think this is crashing because on the final iteration
  238. // it's an empty string and is thus returning nil somehow.
  239. static char* sEmptyString = "";
  240. char* curFName = m_pCurrentFileName;
  241. if (!curFName)
  242. {
  243. curFName = sEmptyString;
  244. }
  245.     char* fName = strrchr(curFName,':');
  246. #else
  247.     char* fName = strrchr(m_pCurrentFileName,':');
  248. #endif
  249.     if (fName) 
  250.      ++fName;
  251.     else
  252.      fName = m_pCurrentFileName;
  253.     return fName;
  254. }
  255. //
  256. // release the directory
  257. //
  258. void CMacFindFile::OS_CloseDirectory ()
  259. {
  260.     return;
  261. }
  262. BOOL CMacFindFile::OS_InitPattern ()
  263. {
  264.     return TRUE;
  265. }
  266. BOOL CMacFindFile::OS_FileMatchesPattern (const char * fname)
  267. {
  268.     return TRUE;
  269. }
  270. void CMacFindFile::OS_FreePattern ()
  271. {
  272.     return;
  273. }
  274. // ------------------------------------------------------------------------------------