chxavfileutil.cpp

来自「著名的 helix realplayer 基于手机 symbian 系统的 播放」· C++ 代码 · 共 1,149 行 · 第 1/3 页

CPP
1,149
字号
// "bar"              -> "bar"
// "\"                -> ""
//
// other stuff (e.g., "\\\\") undefined
//
TPtrC GetNakedPathNode(const TDesC& path)
{
    TPtrC name(path);

    TInt idxLastPathSep = path.LocateReverse(KPathDelimiter);
    if( KErrNotFound != idxLastPathSep )
    {
	HX_ASSERT(path.Length() > 0);
	if( idxLastPathSep == path.Length() - 1)
	{
	    // node is a folder

	    // make path look like a file  and recurse
	    TPtrC newPath = path.Left(path.Length() - 1);
	    name.Set(GetNakedPathNode(newPath));
	}
	else
	{
	    // node is a file
	    name.Set(path.Mid(idxLastPathSep + 1));
	}

    }
    return name;

}

////////////////////////////////////
// same as GetNakedPathNode(), but
// also trims of off file extension
//
// "folder/foo.rm" ->       "foo"
// "folder/child/" ->       "child"
// "folder/child.rm/ ->     "child" <- oops?
//
TPtrC GetNakedName(const TDesC& path)
{
    TPtrC ptrName = GetNakedPathNode(path);
    TInt idx = ptrName.LocateReverse(KExtDelimiter);
    if( KErrNotFound != idx )
    {
	ptrName.Set(ptrName.Ptr(), idx);
    }
    return ptrName;
}

/////////////////////////////////////
// get extension only (with dot)
//
// "folder/foo.rm" -> ".rm"
//
TPtrC GetExtension(const TDesC& path)
{
    TPtrC ptrExt = GetNakedPathNode(path);
    TInt idx = ptrExt.LocateReverse(KExtDelimiter);
    if(KErrNotFound != idx)
    {
	ptrExt.Set(ptrExt.Mid(idx));
    }
    else
    {
	ptrExt.Set(KNullDesC);
    }
    return ptrExt;
}

/////////////////////////////////////
// Trim folder suffix if present and return path
//
// c:\myfolder\music\   =>  c:\myfolder\music
// \myfolder            =>  \myfolder
// c:\myfolder\file.rm  =>  c:\myfolder\file.rm
//

TPtrC GetEnsureNoFolderSuffix(const TDesC& path)
{
    TPtrC name(path);

    if( HasFolderSuffix(path) )
    {
        name.Set(path.Left(path.Length() - 1));
    }
    return name;
}

////////////////////////////////////////////////////////////
// make sure we have enough disk space to make the copy
// 
// copy goes to flash or mmc
//
TInt CheckDiskSpaceForCopyL(const TDesC& fullPathSource, TInt idxDriveDest)
{
    CCoeEnv* pEnv = CCoeEnv::Static();
    RFs& fs = pEnv->FsSession();

    TEntry entry;
    TInt err = fs.Entry(fullPathSource, entry);
    if( KErrNone == err )
    {
        if( EDriveE == idxDriveDest )
        {
            // assume drive E is mmc
            if( SysUtil::MMCSpaceBelowCriticalLevelL( &fs, entry.iSize) ) 
	    {
	        err = KErrDiskFull;
	    }
        }
        else
        {
            // assume everything else is flash
            if( SysUtil::FFSSpaceBelowCriticalLevelL( &fs, entry.iSize) ) 
            {
                err = KErrDiskFull;
            }
        }
    }
    return err;
}

////////////////////////////////////////////////////
// create list of folders under the parent path
// 
// useful for populating a folder list (e.g., 'move to', 'save to')
// 
CDesCArrayFlat* AllocFolderListL(const TDesC& parentPath, bool bSorted)
{
    CDesCArrayFlat* pItems = new (ELeave) CDesCArrayFlat(20 /*granularity*/);
    AUTO_PUSH_POP(pItems); // out

    // read directory contents under parent path path...
    CHXAvDirectoryReader reader;
    if( reader.SetToPath(parentPath) )
    {
	const CDir* pFolders = reader.GetDirs();
	if( pFolders )
	{
	    TInt count = pFolders->Count();
	    for( TInt idx = 0; idx < count; ++idx )
	    {
		const TEntry& entry = (*pFolders)[idx];
		HX_ASSERT(entry.IsDir()); 

                if( !CHXAvUtil::ShouldHideFromUser(entry) )
                {
                    // add this folder
		    pItems->AppendL(entry.iName);
                }
	    }
	}

        // just in case; underlying api should sort directory list
        if( bSorted )
        {
	    pItems->Sort(ECmpCollated);	
        }
    }
    else
    {
        HXSYM_LEAVE(reader.GetLastError());
    }
     
    return pItems;
}

////////////////////////////////////////
// additional filename validity checking beyond just checking
// for illegal characters
//
//      name        - filename-only part of path (i.e., not a path)
//
FileNameValidity GetFileNameValidity(const TDesC& name)
{
    FileNameValidity validity = nvValid;

    //
    // check special cases first; RFs::IsValidName() doesn't catch everything
    //

    if(name.Length() > KMaxFileName )
    {
        // name is too long for sure (RFs::IsValidName() will simply return 'false')
        validity = nvTooLong;
    }
    else if(0 == name.Right(1).Compare(CHXAvUtil::KDot) )
    {
        // name can never end in dot
        validity = nvInvalidDots;
    }
    else if(0 == name.Compare(CHXAvUtil::KDot) || 0 == name.Compare(CHXAvUtil::KDoubleDot))
    {
        // name cannot be '.' or '..'
        validity = nvInvalidDots;
    }
    else
    {
        RFs& fs = CCoeEnv::Static()->FsSession();
        if(!fs.IsValidName(name))
        {
            validity = nvInvalidIllegalChars;
        }
    }

    return validity;
}

////////////////////////////////////////////////////////////
// return file size in bytes (-1 if can't determine)
TInt GetFileSize(const TDesC& fileName)
{
    CCoeEnv* pEnv = CCoeEnv::Static();
    RFs& fs = pEnv->FsSession();

    TEntry entry;
    TInt err = fs.Entry(fileName, entry);
    if(KErrNone != err)
    {
        entry.iSize = -1;
    }

    return entry.iSize;

}

// Unset read-only attribute for file (not folder)
TInt EnsureClearReadOnlyAttributeForFile(const TDesC& fileName)
{
    HX_ASSERT(!HasFolderSuffix(fileName));
    CCoeEnv* pEnv = CCoeEnv::Static();
    RFs& fs = pEnv->FsSession();
    //KEntryAttVolume
    //KEntryAttDir
    //KEntryAttArchive
    //KEntryAttSystem
    //KEntryAttHidden
    //KEntryAttReadOnly
    //KEntryAttNormal
    TUint attr = 0;
    TInt err = fs.Att(fileName, attr);
    if( KErrNone == err )
    {
        //DPRINTF(SYMP_FILE, ("EnsureClearReadOnlyAttributeForFile(): att for '%s' = 0x%08x\n", dbg::CharPtr(fileName)(), attr));
        if( attr & KEntryAttReadOnly )
        {
            DPRINTF(SYMP_FILE, ("EnsureClearReadOnlyAttributeForFile(): unsetting read-only attribute\n"));
            err = fs.SetAtt(fileName, 0, KEntryAttReadOnly /*remove*/);
        }
    }
    return err;
}

//
// Unset read-only attribute for given file. If fileName is a folder (indicated
// by trailing slash), all folder children will have read-only attributes removed.
//
TInt EnsureClearReadOnlyAttributeL(const TDesC& fileName)
{
    RFs& fs = CCoeEnv::Static()->FsSession();

    TInt err = KErrNone;
    if( HasFolderSuffix(fileName) )
    {
        DPRINTF(SYMP_FILE, ("EnsureClearReadOnlyAttributeL(): unsetting read-only attribute for folder\n"));

        // recursively unset attributes for folder 
        CFileMan* pFileMan = CFileMan::NewL(fs);
        AUTO_PUSH_POP_DEL(pFileMan);
   
        TTime tm; // ignored
        const TUint setMask = 0;
        const TUint clearMask = KEntryAttReadOnly;



        // trim folder suffix
        TPtrC ptrName = CHXAvFile::GetEnsureNoFolderSuffix(fileName);

        // note: this does not work well for filenames (even with flag set 0)?!? (in emulator file becomes inaccessible)
        err = pFileMan->Attribs(ptrName, setMask, clearMask, tm, CFileMan::ERecurse); 
    }
    else
    {
        err = EnsureClearReadOnlyAttributeForFile(fileName);
    }

    DPRINTF(SYMP_FILE, ("EnsureClearReadOnlyAttributeL(): result = %d\n", err));

    return err;
}




////////////////////////////////////////////////
// return 'true' if path is absolute, i.e., begins with:
//
//  a) a drive letter colon and slash (c:\)
//  b) a slash (\)
//
bool HasDriveOrRootPrefix(const TDesC& path)
{
    bool bHasIt = false;
    TInt idx = GetDriveIndex(path);
    if( idx != -1 )
    {
        bHasIt = false;
    }
    else
    {
        bHasIt = HasFolderPrefix(path);
    }
    return bHasIt;
}
////////////////////////////////////////////////
// prefix given relative path with full player app folder path
//
TFileName* AllocAppFolderPathL(const TDesC& path)
{
    // also see:
    // #include <aknutils.h>
    // CompleteWithAppPath(fullPathLibRoot);

    HX_ASSERT(!HasDriveOrRootPrefix(path));

    TFileName* pOut = new (ELeave) TFileName;
    AUTO_PUSH_POP(pOut);  

    // get our app directory base path
    CEikAppUi* pAppUI = reinterpret_cast<CEikAppUi*>(CCoeEnv::Static()->AppUi());
    HX_ASSERT(pAppUI);
    TFileName dllName = pAppUI->Application()->DllName();
    TPtrC appRootPath = GetFolderPath(dllName);

    pOut->Copy(appRootPath);

    AppendPath(*pOut, path, CHXAvFile::ntUnspecified);
  
    return pOut;

}

////////////////////////////////////////////////////////
// Allocate path from preferences. If path is not fully 
// specified (with drive letter), assume it is relative
// to app folder
//
// return 0 if pref key does not exist
//
TFileName* AllocFullPrefPathL(IHXPreferences* pPrefs, const char* prefKey)
{
    TFileName* pFullPath = 0;
    
    // get path from prefs
    CHXString strPath = prefs::GetString(pPrefs, prefKey);
    if(!strPath.IsEmpty())
    {
        CHXAvCleanString descPath(strPath);
        if(!CHXAvFile::HasDriveOrRootPrefix(descPath()))
        {
            pFullPath = CHXAvFile::AllocAppFolderPathL(descPath());
        }
        else
        {
            pFullPath = new (ELeave) TFileName();
            HXSYM_LEAVE_IF_FALSE(descPath().Length() <= KMaxFileName);
            pFullPath->Copy(descPath());
        }
    }

    return pFullPath;
}


} //ns CHXAvFile




⌨️ 快捷键说明

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