hxdir.cpp

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

CPP
608
字号
        return FALSE;

#if !defined(WIN32_PLATFORM_PSPC)
    UINT nDriveType = 0;
#ifdef _WIN32
    nDriveType = GetDriveType(m_strPath);
    if(nDriveType == DRIVE_CDROM)
       return TRUE;
#else
    nDriveType = GetDriveType(toupper(m_strPath.GetAt(0)) - 'A');
#endif
    if(nDriveType == DRIVE_REMOVABLE)
       return TRUE;
#endif /* defined(WIN32_PLATFORM_PSPC) */

    return FALSE;
}

/* Deletes empty directory. */
BOOL 
CHXDirectory::DeleteDirectory()
{
#ifdef _WIN32
    if(m_hFindFile != INVALID_HANDLE_VALUE)
    {
        FindClose(m_hFindFile);
        m_hFindFile = INVALID_HANDLE_VALUE;
    }
#endif

    if(RemoveDirectory(OS_STRING(m_strPath)))
        return TRUE;
    return FALSE;
}

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

    CHXString strMask = m_strPath;
    const char* pLastChar = HXGetPrevChar(m_strPath, (const char*)m_strPath + m_strPath.GetLength());
    if(pLastChar + 1 != (const char*)m_strPath + m_strPath.GetLength() || *pLastChar != '\\')
	strMask += "\\";
    strMask += szPattern;

#ifdef _WIN32
    if(m_hFindFile != INVALID_HANDLE_VALUE)
    {
        FindClose(m_hFindFile);
        m_hFindFile = INVALID_HANDLE_VALUE;
    }

    WIN32_FIND_DATA findData;
    m_hFindFile =  FindFirstFile(OS_STRING(strMask), &findData);
    if(m_hFindFile != INVALID_HANDLE_VALUE)
    {
        if(m_strPath.GetLength() + 1 + strlen(OS_STRING(findData.cFileName)) < nSize)
	{
	    SafeStrCpy(szPath, (const char*)m_strPath, nSize);
	    pLastChar = HXGetPrevChar(szPath, szPath + strlen(szPath));
	    if(pLastChar + 1 < szPath + strlen(szPath) || *pLastChar != '\\')
		SafeStrCat(szPath,  "\\", nSize);
            SafeStrCat(szPath,  OS_STRING(findData.cFileName), nSize);
	}

	if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
	    RetVal = FSOBJ_DIRECTORY;
        else
            RetVal = FSOBJ_FILE;
    }
#else
    if(!_dos_findfirst(strMask, _A_SUBDIR | _A_HIDDEN, &m_FindFileInfo))
    {   
        if(m_strPath.GetLength() + 1 + strlen(m_FindFileInfo.name) < nSize)
	{
	    SafeStrCpy(szPath, (const char*)m_strPath, nSize);
	    if(m_strPath.GetLength() && m_strPath.GetAt(m_strPath.GetLength() - 1) != '\\')
		SafeStrCat(szPath,  "\\", nSize);
            SafeStrCat(szPath,  m_FindFileInfo.name, nSize);
	}

	if (m_FindFileInfo.attrib & _A_SUBDIR)
            RetVal = FSOBJ_DIRECTORY;
        else
            RetVal = FSOBJ_FILE;
    }
#endif

    while(RetVal != FSOBJ_NOTVALID &&
          !IsValidFileDirName(szPath))
        RetVal = FindNext(szPath, nSize);

    return RetVal;
}

/* Continues enumeration process. */
CHXDirectory::FSOBJ 
CHXDirectory::FindNext(char* szPath, UINT16 nSize)
{
    FSOBJ RetVal = FSOBJ_NOTVALID;

#ifdef _WIN32
    if(m_hFindFile != INVALID_HANDLE_VALUE)
    {
        WIN32_FIND_DATA findData;
        if(FindNextFile(m_hFindFile, &findData))
        {
            if(m_strPath.GetLength() + 1 + strlen(OS_STRING(findData.cFileName)) < nSize)
	    {
		SafeStrCpy(szPath, (const char*)m_strPath, nSize);
		const char* pLastChar = HXGetPrevChar(szPath, szPath + strlen(szPath));
		if(pLastChar + 1 < szPath + strlen(szPath) || *pLastChar != '\\')
		    SafeStrCat(szPath,  "\\", nSize);
		SafeStrCat(szPath,  OS_STRING(findData.cFileName), nSize);
	    }

	    if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
	        RetVal = FSOBJ_DIRECTORY;
            else
                RetVal = FSOBJ_FILE;
        }
    }
#else
    if(!_dos_findnext(&m_FindFileInfo))
    {   
        if(m_strPath.GetLength() + 1 + strlen(m_FindFileInfo.name) < nSize)
	{
	    SafeStrCpy(szPath, (const char*)m_strPath, nSize);
	    if(m_strPath.GetLength() && m_strPath.GetAt(m_strPath.GetLength() - 1) != '\\')
		SafeStrCat(szPath,  "\\", nSize);
            SafeStrCat(szPath,  m_FindFileInfo.name, nSize);
	}

	if (m_FindFileInfo.attrib & _A_SUBDIR)
            RetVal = FSOBJ_DIRECTORY;
        else
            RetVal = FSOBJ_FILE;
    }
#endif

    while(RetVal != FSOBJ_NOTVALID &&
          !IsValidFileDirName(szPath))
        RetVal = FindNext(szPath, nSize);

    return RetVal;
}

BOOL 
CHXDirectory::IsValidFileDirName(const char* szPath)
{
    const char* pSlash = HXReverseFindChar(szPath, '\\');
    if(!pSlash)
        return FALSE;

    CHXString strEnding = pSlash + 1;
    if(!strEnding.Compare(".") || !strEnding.Compare(".."))
        return FALSE;

    return TRUE;
}

BOOL 
CHXDirectory::DeleteFile(const char* szRelPath)
{
    if(!szRelPath)
        return FALSE;

    CHXString strPath;
    if(strlen(szRelPath) > 1 && szRelPath[1] == ':')
    {
        strPath = szRelPath;
    }
    else
    {
	strPath = m_strPath;
	const char* pLastChar = HXGetPrevChar(m_strPath, (const char*)m_strPath + m_strPath.GetLength());
	if(pLastChar + 1 < (const char*)m_strPath + m_strPath.GetLength() || *pLastChar != '\\')
	    strPath += "\\";
	strPath += szRelPath;
    }

    BOOL RetVal = FALSE;

    //_chmod(strPath, S_IREAD | S_IWRITE);
    DWORD attrib = GetFileAttributes(OS_STRING(strPath)); 
    attrib &= ~FILE_ATTRIBUTE_READONLY;
    SetFileAttributes(OS_STRING(strPath), attrib); 

    //if(!unlink(strPath) || errno != EACCES)
    if (::DeleteFile(OS_STRING(strPath)))
        RetVal = TRUE;

    return RetVal;
}

/* Sets itself to current directory. */
BOOL 
CHXDirectory::SetCurrentDir()
{
    BOOL bRetVal = TRUE;

#if defined(WIN32_PLATFORM_PSPC)
    // WinCE doesn't have a concept of a working directory.
    // Assert here and look at the calling code to see
    // if we really need to worry about this
    HX_ASSERT(!"CHXDirectory::SetCurrentDir()");
#else /* defined(WIN32_PLATFORM_PSPC) */
    if(!getcwd(m_strPath.GetBuffer(_MAX_PATH + 1), _MAX_PATH))
	bRetVal = FALSE;
    m_strPath.ReleaseBuffer();
#endif /* defined(WIN32_PLATFORM_PSPC) */

    return bRetVal;
}

/* Makes itself a current directory. */
BOOL 
CHXDirectory::MakeCurrentDir()
{

#ifdef _WIN16
    // In win16, you have to change the current drive seperately from changing
    // to the new directory, if the new directory is on a different drive than
    // the current drive.
    int retval = 0;
    int curdrive = _getdrive();
    int drive = (toupper(m_strPath.GetAt(0)) - 'A') + 1;
    if (curdrive != drive)
	if ((retval = _chdrive(drive)) != 0)
	    return FALSE;
#endif

#if defined(WIN32_PLATFORM_PSPC)
    // WinCE does not have the concept of a current directory.
    // Assert here and look at the calling code to see if
    // we need to worry about it
    HX_ASSERT(!"CHXDirectory::MakeCurrentDir()");
#else /* defined(WIN32_PLATFORM_PSPC) */
    if(!chdir(m_strPath))
        return TRUE;
#endif /* defined(WIN32_PLATFORM_PSPC) */

    return FALSE;
}


UINT32 
CHXDirectory::Rename(const char* szOldName, const char* szNewName)
{
    if ((szOldName == NULL) || (szNewName == NULL))
    {
        HX_ASSERT(FALSE);
	return HXR_FAIL;
    }

    UINT32 theError = HXR_FAIL;


    CHXDirectory DestinationDir;
    DestinationDir.SetPath( szNewName );

    if ( DestinationDir.IsValid() )
    {
        //Destination directory exist.
	if(!::RemoveDirectory(OS_STRING(szNewName)) )
	{
	    //Failed to delete destination directory.
	    //Check if read only attribute is set.
	    DWORD attrib = GetFileAttributes(OS_STRING(szNewName));
	    if ( attrib & FILE_ATTRIBUTE_READONLY )
	    {
		//Directory is read only.
		//Remove read only attribute.
		attrib &= ~FILE_ATTRIBUTE_READONLY;
		SetFileAttributes(OS_STRING(szNewName), attrib); 
		
		if(!::RemoveDirectory(OS_STRING(szNewName)))
		{
		    //Still failed to delete.
		    //So put the read only attribute back.
    		    attrib |= FILE_ATTRIBUTE_READONLY;
		    SetFileAttributes(OS_STRING(szNewName), attrib); 

		    return HXR_FAIL;
		}
	    }
	    else
	    {	
		//Failed to delete & directory is not readonly.
		return HXR_FAIL;
	    }
	}
    }

    if(MoveFile(OS_STRING(szOldName), OS_STRING(szNewName)))
	theError = HXR_OK;


    return theError;
}

⌨️ 快捷键说明

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