hxdir.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 928 行 · 第 1/2 页

CPP
928
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: hxdir.cpp,v 1.4.36.3 2004/07/09 01:44:14 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
 * 
 * The contents of this file, and the files included with this file,
 * are subject to the current version of the RealNetworks Public
 * Source License (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (the "RCSL") available at
 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
 * will apply. You may also obtain the license terms directly from
 * RealNetworks.  You may not use this file except in compliance with
 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
 * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
 * the rights, obligations and limitations governing use of the
 * contents of the file.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the
 * portions it created.
 * 
 * This file, and the files included with this file, is distributed
 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 * 
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 * 
 * Contributor(s):
 * 
 * ***** END LICENSE BLOCK ***** */

#include "hxdir.h"
#include "HX_MoreProcesses.h"
#include "pn_morefiles.h"
#include "fullpathname.h"
#include "hxstrutl.h"

#ifndef _CARBON
#include "morefilesextras.h"
#include "filecopy.h"
#else
#include "MoreFilesX.h"
#include "filespecutils.h"
#endif

#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE		
static const char HX_THIS_FILE[] = __FILE__;
#endif


// Mac paths:   full pathnames:  "Hard Disk:"  "Hard Disk:dev folder:"
//               partial paths:  ":dev folder:"  "myfile"
//
// The ending colon for paths to folders is strongly recommended.
// A word without a colon is a file name; a word followed by a colon
// is a drive name (and the full path of the root directory of the drive.)

static Boolean IsFullMacPath(const CHXString& strPath)
{
	// a full Mac pathname has a colon, but not as the first character
	// (it's also non-empty)
	
	return (strPath.Find(OS_SEPARATOR_CHAR) > 0
		&& strPath.GetAt(0) != OS_SEPARATOR_CHAR);
}

static Boolean IsPartialMacPath(const CHXString& strPath)
{
	// a partial Mac pathname is either a name without colons
	// or else starts with a colon
	return (strPath.Find(OS_SEPARATOR_CHAR) == -1
		|| strPath.GetAt(0) == OS_SEPARATOR_CHAR);
}

CHXDirectory::CHXDirectory() : m_pCurrentFileName(NULL), m_pNextFileName(NULL), m_pPattern(NULL),
			       m_nDefaultVRefNum(0), m_nDirItems(0)
{
	// call SetPath to initialize both the path string and the path FSSpec
	// to the "current directory"
	SetPath("");
}

CHXDirectory::~CHXDirectory()
{
    if (m_pCurrentFileName)
    {
	delete [] m_pCurrentFileName;
	m_pCurrentFileName = 0;
    }

    if (m_pNextFileName)
    {
	delete [] m_pNextFileName;
	m_pNextFileName = 0;
    }
    if (m_pPattern)
    {
	delete [] m_pPattern;
	m_pPattern = 0;
    }
}

void
CHXDirectory::SetPath(const char* szPath)
{
	if (szPath)
	{
		
		// coerce path to a CHXString, then to a FSSpec,
		// and store in our object
		
		CHXString	pathStr(szPath);
		// make sure path ends with a ':' 
		if ((pathStr.GetLength() > 0) && (pathStr[pathStr.GetLength()-1] != ':'))
			pathStr += ':';

		// it is ok for path not to be valid yet

		m_dirSpec = pathStr;
	}
	
	XHXDirectory::SetPath(szPath);
}

/* folderType is actually a Mac FindFolder type - 
   if != kExtensionFolderType, kChewableItemsFolderType (or kTemporaryFolderType) is used 
   kExtensionFolderType is used when we need a temp folder to load DLLs from.
 */
BOOL
CHXDirectory::SetTempPath(HXXHANDLE folderType, const char* szRelPath)
{
    CHXString tempStr;
    
	short	tempVRefNum;
	long	tempDirID;
	FSSpec	tempSpec;
	OSErr	err;
	FSSpec	appSpec;
	
#ifndef USE_APP_DIR
	if (kExtensionFolderType == folderType)
	{
	    err = FindFolder(kOnSystemDisk, kExtensionFolderType, kCreateFolder, &tempVRefNum, &tempDirID);
	}
	else if (kInstallerLogsFolderType == folderType)
	{
	    // if >= Sys 8.5, use Installer Logs folder
	    long	sysVersion;
	    char*	bytes=(char*)&sysVersion;
	    ::Gestalt(gestaltSystemVersion,&sysVersion);
	    if (bytes[2]>8 || ((bytes[2]==8) && (bytes[3] >= 0x50)))
	    	err = FindFolder(kOnSystemDisk, kInstallerLogsFolderType, kCreateFolder, &tempVRefNum, &tempDirID);
	    else
	    {
		GetCurrentAppSpec(&appSpec);
		tempVRefNum = appSpec.vRefNum;
		tempDirID = appSpec.parID;
		err = noErr;
	    }
	}
	else
	{
 	    err = FindFolder(kOnSystemDisk, kChewableItemsFolderType, kCreateFolder, &tempVRefNum, &tempDirID);
	}
	if (err != noErr)
	{
		err = FindFolder(kOnSystemDisk, kTemporaryFolderType, kCreateFolder, &tempVRefNum, &tempDirID);
	}

#else

	// use the app's folder rather than the temporary items folder
	
	if (GetCurrentAppSpec(&appSpec))
	{
		tempVRefNum = appSpec.vRefNum;
		tempDirID = appSpec.parID;
		err = noErr;
	}
	else
	{
		err = fnfErr;
	}
#endif

	if (err != noErr)
	{
		tempVRefNum = -1;	// boot disk
		tempDirID = fsRtDirID;	// root directory
	}

	// we have the temporary folder's dirID and vRefNum
	//
	// make a FSSpec for the folder
	
	err = FSMakeFSSpec(tempVRefNum, tempDirID, "\p", &tempSpec);
	check_noerr(err);
	
	CHXString	tempPath;
	
	tempPath = tempSpec;	// coerce to a full pathname
	tempPath += szRelPath;
	
	SetPath(tempPath);
	
	return true;
	
}

/* Creates directory. */
BOOL 
CHXDirectory::Create()
{
	OSErr	err;
	long	newDirID;
	
	if (EnsureValidPathSpec())
	{
		err = FSpDirCreate(&m_dirSpec, smSystemScript, &newDirID);
	}
	else
	{
		err = fnfErr;
	}
	return (err == noErr);
}

/* Checks if directory exists. */    
BOOL 
CHXDirectory::IsValid()
{
    FSSpec		dirSpec;
    CInfoPBRec	cinfo;
    OSErr		err;
    Boolean		dirFlag;
    
    dirFlag = false;
    err = noErr;
    
	if (EnsureValidPathSpec())
	{
		// make a copy of the filespec so that PBGetCatInfo doesn't munge 
		// the name field (paranoid, since that shouldn't happen to a 
		// canonical FSSpec like this)
		
		dirSpec = m_dirSpec;
	
		// check that it's a valid folder or volume
		
		cinfo.dirInfo.ioNamePtr = dirSpec.name;
		cinfo.dirInfo.ioVRefNum = dirSpec.vRefNum;
		cinfo.dirInfo.ioDrDirID = dirSpec.parID;
		cinfo.dirInfo.ioFDirIndex = 0;	// use name and dirID
		
		err = PBGetCatInfoSync(&cinfo);
		
		if (noErr == err)
		{
			// check the folder/volume bit
		    dirFlag = ((cinfo.dirInfo.ioFlAttrib & ioDirMask) != 0);
		}
    }
    
    return ((err == noErr) && dirFlag);
}


/* Deletes empty directory */
BOOL 
CHXDirectory::DeleteDirectory()
{
	OSErr		err;

	if (EnsureValidPathSpec())
	{
		err = FSpDelete(&m_dirSpec);
	}
	else
	{
		err = fnfErr;
	}
	return (err == noErr);
}	

/* EnsureValidPathSpec

   If the path was so speculative when it was set that
   we couldn't make a FSSpec then, make one now.
   
   Note that this *doesn't* guarantee that the object
   exists, just that the m_dirSpec matches the path. 
   
   Returns true if the FSSpec is valid, false if no
   FSSpec can be made for the current path. 
   
   Call this at the start of any routine that expects
   a valid FSSpec to have been already set. */
   
BOOL 
CHXDirectory::EnsureValidPathSpec()
{
	// to avoid the cost of conversion again,
	// generate the FSSpec only if it's not 
	// currently valid
	
	if (m_dirSpec.vRefNum == 0) 
	{
		m_dirSpec = m_strPath;
	}
	return (m_dirSpec.vRefNum != 0);
}	

/* Destroys directory */
BOOL 
CHXDirectory::Destroy(BOOL bRemoveContents)
{
    OSErr err;

	if (EnsureValidPathSpec())
	{
		if (bRemoveContents)
	    {
		    	// use MoreFilesExtras' routine for this
				err = ::DeleteDirectory(m_dirSpec.vRefNum, m_dirSpec.parID, m_dirSpec.name);
				return (err == noErr);
	    }
	    else
	    {
	    	return DeleteDirectory();
	    }
	}
	else
	{
		// invalid path
		return false;
	}
}	

/* Starts enumeration process. */
CHXDirectory::FSOBJ 
CHXDirectory::FindFirst(const char* szPattern, char* szPath, UINT16 nSize)
{
    FSOBJ RetVal = FSOBJ_NOTVALID;
    int len;

    OSErr err = noErr;
    CHXString tempStr;
    	
    if (err == noErr)
    {
	m_nIndex = 0;
	BOOL bContinue;
	FSSpec nextFSpec;
	char* patternStart;

	if (!m_pPattern)
	{
	    m_pPattern	    = new char[31];
	    *m_pPattern = 0;
	}
	SafeStrCpy(m_pPattern, szPattern, 31);
	
    	tempStr = m_strPath;
    	if (m_strPath.GetLength())
    	{
    	    if (tempStr[tempStr.GetLength()-1] != ':')
    	    	tempStr += ":";
    	}
    	tempStr += szPattern;
    	(void) FSSpecFromPathName(tempStr,&nextFSpec);
	if (IsFolder(&nextFSpec))
	{
	    SafeStrCpy(szPath, (const char*)tempStr, nSize);
	    ++m_nIndex;
	    return FSOBJ_DIRECTORY;
	}

    	(void) FSSpecFromPathName(m_strPath,&m_fileSpec);

	CInfoPBRec		pb;
	OSErr			err=noErr;

	memset(&pb,0,sizeof(pb));
				
	pb.hFileInfo.ioVRefNum=m_fileSpec.vRefNum;
	pb.hFileInfo.ioFDirIndex=0;
	pb.hFileInfo.ioNamePtr=m_fileSpec.name;
	pb.hFileInfo.ioDirID=m_fileSpec.parID;

	err = PBGetCatInfoSync(&pb);
	
	// if m_fileSpec doesn't point to a directory
	// check if szPath + szPattern (nextFSpec) points to a valid file
	if (noErr != err)
	{
	    if (noErr == ::FSMakeFSSpec(nextFSpec.vRefNum, nextFSpec.parID, nextFSpec.name, &nextFSpec))
	    {
	    	tempStr = nextFSpec;
	    	SafeStrCpy(szPath, (const char*)tempStr, nSize);
	    	return FSOBJ_FILE;
	    }
	}
	
	m_nDirItems = pb.dirInfo.ioDrNmFls;
    	
	while (::FSpIterateDirectory(&m_fileSpec, ++m_nIndex, &nextFSpec))
	{
	    bContinue = TRUE;
	    if (!m_pNextFileName)
	    {
		m_pNextFileName	    = new char[_MAX_PATH];
		*m_pNextFileName = 0;
	    }
	    
	    if (!m_pCurrentFileName)
	    {
		m_pCurrentFileName	    = new char[_MAX_PATH];
		*m_pCurrentFileName = 0;
	    }
	    
	    // matches pattern(extension)? (eg. DLL)
	    len = nextFSpec.name[0];
	    if (szPattern)
	    {
	    	if ( 0 == strcmp("*.*", szPattern) ||
	    	    	((szPattern[0] == '*') && (strlen(szPattern) == 1)) )
	    	    bContinue = TRUE;
	    	else
	    	{
	    	    INT16 patlen = strlen(szPattern);
	    	    if (szPattern[0] == '*')
	    	    {
	    	    	if (0 != strncasecmp((char*)&nextFSpec.name[len-2], &szPattern[strlen(szPattern)-3], 3))
	    	    	    bContinue = FALSE;
	    	    }
	    	    else if ((patternStart = strchr(m_pPattern, '*')) != 0) //eg. pncrt*.dll
	    	    {
	    	    	INT16 patternOffset = patternStart-m_pPattern;
	    	    	if (0 != strncasecmp((char*)&nextFSpec.name[1], m_pPattern, patternOffset))
	    	    	    bContinue = FALSE;
	    	    	if (bContinue && (patternOffset < patlen))
	    	    	{
	    	    	    int nCompareLen = (patlen > nextFSpec.name[0]) ? nextFSpec.name[0] - patternOffset
	    	    	    						   : patlen - patternOffset - 1;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?