My Project
Classes | Static Public Member Functions | Static Public Attributes | List of all members
ParaEngine::CFileUtils Class Reference

file platform related API and helper functions More...

#include <FileUtils.h>

Static Public Member Functions

static void TrimString (std::string &str)
 
static bool FileExistRaw (const char *filename)
 
static bool AddDiskSearchPath (const std::string &sFile, bool nFront=false)
 this is low level, use with care. More...
 
static bool FileExist (const char *filename)
 check if file exist, this function is cross platform. More...
 
static bool FileExist2 (const char *filename, std::string *pDiskFile=NULL)
 check if file exist, this function is cross platform. More...
 
static void MakeFileNameFromRelativePath (char *output, const char *filename, const char *relativePath)
 make a standard filename. More...
 
static bool MoveFile (const char *src, const char *dest)
 The MoveFile function will move (rename) either a file or a directory (including its children) either in the same directory or across directories. More...
 
static bool CopyFile (const char *src, const char *dest, bool bOverride)
 The CopyFile function copies an existing file to a new file. More...
 
static std::string GetParentDirectoryFromPath (const std::string &sfilename, int nParentCounts=0)
 trim the sFile by nParentCounts number of parent directories. More...
 
static bool MakeDirectoryFromFilePath (const char *filename)
 make directory More...
 
static bool SaveBufferToFile (const std::string &filename, bool bReplace, char *buffer, DWORD nBufSize)
 extract this para file to disk More...
 
static bool DeleteFile (const char *filename)
 delete a given file
 
static int DeleteDirectory (const char *dirname)
 delete a given directory, regardless of whether it's empty or not
 
static int DeleteFiles (const std::string &sFilePattern, bool bSecureFolderOnly=true, int nSubFolderCount=10)
 delete a given file. More...
 
static std::string GetWritableFullPathForFilename (const std::string &filename)
 if already absolute path, return it, if not we will prepend current writable path. More...
 
static std::string GetFileName (const std::string &sFilePath)
 get file name
 
static FileData GetDataFromFile (const char *filename)
 Creates binary data from a file. More...
 
static std::string GetStringFromFile (const std::string &filename)
 
static bool GetFileInfo (const char *filename, CParaFileInfo &fileInfo)
 get file size of a disk file in bytes. More...
 
static FileData GetResDataFromFile (const std::string &filename)
 data need not be released, since it is from the resource file. More...
 
static bool DoesResFileExist (const std::string &filename)
 whether the resource file exist
 
static void AddEmbeddedResource (const char *name, const char *buffer, size_t nSize)
 add an embedded resource, the resource is usually from extern static const char* of the executable. More...
 
static std::string GetFullPathForFilename (const std::string &filename)
 get fullPath for filename
 
static bool IsAbsolutePath (const std::string &path)
 Checks whether the path is an absolute path. More...
 
static int GetFileSize (const char *sFilePath)
 get file size of a disk file in bytes. More...
 
static FileHandle OpenFile (const char *sFilePath, bool bRead=false, bool bWrite=true)
 open file and return the file handle. More...
 
static bool SetFilePointer (FileHandle &fileHandle, int lDistanceToMove, int dwMoveMethod)
 The SetFilePointer function moves the file pointer of an open file. More...
 
static int GetFilePosition (FileHandle &fileHandle)
 get the current file pointer position. More...
 
static bool SetEndOfFile (FileHandle &fileHandle)
 The SetEndOfFile function moves the end-of-file (EOF) position for the specified file to the current position of the file pointer.This function can be used to truncate or extend a file. More...
 
static int WriteBytes (FileHandle &fileHandle, const void *src, int bytes)
 Write byte stream to file the file must be opened with write access.
 
static int ReadBytes (FileHandle &fileHandle, void *dest, int bytes)
 read byte stream from file the file must be opened with read access.
 
static void CloseFile (FileHandle &fileHandle)
 close the given file
 
static std::string GetInitialDirectory ()
 get current directory
 
static const std::string & GetWritablePath ()
 in win32, this is the root directory, Gets the writable path. More...
 
static void SetWritablePath (const std::string &writable_path)
 not thread safe, only set at startup when there is just one thread running. More...
 
static void FindDiskFiles (CSearchResult &result, const std::string &sRootPath, const std::string &sFilePattern, int nSubLevel)
 this is a recursive function. More...
 

Static Public Attributes

static std::string s_writepath
 

Detailed Description

file platform related API and helper functions

Member Function Documentation

§ AddDiskSearchPath()

bool ParaEngine::CFileUtils::AddDiskSearchPath ( const std::string &  sFile,
bool  nFront = false 
)
static

this is low level, use with care.

Once added, can not be removed.

§ AddEmbeddedResource()

void ParaEngine::CFileUtils::AddEmbeddedResource ( const char *  name,
const char *  buffer,
size_t  nSize 
)
static

add an embedded resource, the resource is usually from extern static const char* of the executable.

Please note the data is never released. we can obtain it by name with GetResDataFromFile

Parameters
nameshould begin with ":" to designate it is a resource file

§ CopyFile()

bool ParaEngine::CFileUtils::CopyFile ( const char *  src,
const char *  dest,
bool  bOverride 
)
static

The CopyFile function copies an existing file to a new file.

Parameters
srcspecifies the name of an existing file
destspecifies the name of the new file
bOverride[in] If this parameter is false and the new file specified by src already exists, the function fails. If this parameter is true and the new file already exists, the function overwrites the existing file and succeeds.
Returns
true if succeeds

§ DeleteFiles()

int ParaEngine::CFileUtils::DeleteFiles ( const std::string &  sFilePattern,
bool  bSecureFolderOnly = true,
int  nSubFolderCount = 10 
)
static

delete a given file.

It will reject any system files outside the application(or writable directory) directory. after all, this function is of high security level.

Parameters
sFilePatternsuch as "*.dds", "temp/ *.*", etc
bSecureFolderOnlyif true, we only allow user to delete allowed folders.
Returns
: the number of files deleted.

§ FileExist()

bool ParaEngine::CFileUtils::FileExist ( const char *  filename)
static

check if file exist, this function is cross platform.

§ FileExist2()

bool ParaEngine::CFileUtils::FileExist2 ( const char *  filename,
std::string *  pDiskFile = NULL 
)
static

check if file exist, this function is cross platform.

Parameters
pDiskFileoutput the actual file name. This is useful when development or search path is used.

§ FindDiskFiles()

void ParaEngine::CFileUtils::FindDiskFiles ( CSearchResult result,
const std::string &  sRootPath,
const std::string &  sFilePattern,
int  nSubLevel 
)
static

this is a recursive function.

See also
SearchFiles

§ GetDataFromFile()

ParaEngine::FileData ParaEngine::CFileUtils::GetDataFromFile ( const char *  filename)
static

Creates binary data from a file.

Returns
A data object.

load from disk

§ GetFileInfo()

bool ParaEngine::CFileUtils::GetFileInfo ( const char *  filename,
CParaFileInfo fileInfo 
)
static

get file size of a disk file in bytes.

§ GetFilePosition()

int ParaEngine::CFileUtils::GetFilePosition ( FileHandle fileHandle)
static

get the current file pointer position.

§ GetFileSize()

int ParaEngine::CFileUtils::GetFileSize ( const char *  sFilePath)
static

get file size of a disk file in bytes.

§ GetParentDirectoryFromPath()

std::string ParaEngine::CFileUtils::GetParentDirectoryFromPath ( const std::string &  sfilename,
int  nParentCounts = 0 
)
static

trim the sFile by nParentCounts number of parent directories.

Parameters
nParentCountsnumber of parent directory to remove. 0 means the current directory,which is also the default value
Returns
: return "" if the input directory does not have that many parent directories. e.g. "C:/abc/" = GetDirectoryFromPath("C:/abc/aaa",0); "C:/" = GetDirectoryFromPath("C:/abc/",1);

§ GetResDataFromFile()

ParaEngine::FileData ParaEngine::CFileUtils::GetResDataFromFile ( const std::string &  filename)
static

data need not be released, since it is from the resource file.

resources may be embedded with rc file under win32 and added using AddEmbeddedResource API.

Returns
Note one must call FileData.ReleaseOwnership().

§ GetWritableFullPathForFilename()

std::string ParaEngine::CFileUtils::GetWritableFullPathForFilename ( const std::string &  filename)
static

if already absolute path, return it, if not we will prepend current writable path.

§ GetWritablePath()

const std::string & ParaEngine::CFileUtils::GetWritablePath ( )
static

in win32, this is the root directory, Gets the writable path.

Returns
The path that can be write/read a file in

§ IsAbsolutePath()

bool ParaEngine::CFileUtils::IsAbsolutePath ( const std::string &  path)
static

Checks whether the path is an absolute path.

Note
On Android, if the parameter passed in is relative to "assets/", this method will treat it as an absolute path. Also on Blackberry, path starts with "app/native/Resources/" is treated as an absolute path.
Parameters
strPathThe path that needs to be checked.
Returns
true if it's an absolute path, otherwise it will return false.

§ MakeDirectoryFromFilePath()

bool ParaEngine::CFileUtils::MakeDirectoryFromFilePath ( const char *  filename)
static

make directory

Parameters
filenamefile name should be a Canonical File Path. Use ,ToCanonicalFilePath
Returns
: true if the directory is made or already exists

§ MakeFileNameFromRelativePath()

void ParaEngine::CFileUtils::MakeFileNameFromRelativePath ( char *  output,
const char *  filename,
const char *  relativePath 
)
static

make a standard filename.

Parameters
outputthe output string buffer. such as: char output[MAX_PATH]
filenamethe relative file name. if NULL, the parent directory is in output.
relativePaththe directory name. the last '/' or '\' is used as parent directory. e.g. "temp/abc.txt" is same as "temp/". if NULL, the filename is copied to output.

§ MoveFile()

bool ParaEngine::CFileUtils::MoveFile ( const char *  src,
const char *  dest 
)
static

The MoveFile function will move (rename) either a file or a directory (including its children) either in the same directory or across directories.

Parameters
srcspecifies the name of an existing file
destspecifies the name of the new file
Returns
true if succeeds

§ OpenFile()

ParaEngine::FileHandle ParaEngine::CFileUtils::OpenFile ( const char *  sFilePath,
bool  bRead = false,
bool  bWrite = true 
)
static

open file and return the file handle.

default input is create a file for shared writing.

§ SaveBufferToFile()

bool ParaEngine::CFileUtils::SaveBufferToFile ( const std::string &  filename,
bool  bReplace,
char *  buffer,
DWORD  nBufSize 
)
static

extract this para file to disk

Parameters
filenameDestdestination file name.
bReplacereplace file if the file already exists.

§ SetEndOfFile()

bool ParaEngine::CFileUtils::SetEndOfFile ( FileHandle fileHandle)
static

The SetEndOfFile function moves the end-of-file (EOF) position for the specified file to the current position of the file pointer.This function can be used to truncate or extend a file.

If the file is extended, the contents of the file between the old EOF position and the new position are not defined.

Returns

§ SetFilePointer()

bool ParaEngine::CFileUtils::SetFilePointer ( FileHandle fileHandle,
int  lDistanceToMove,
int  dwMoveMethod 
)
static

The SetFilePointer function moves the file pointer of an open file.

this function only works when the ParaFile object is an actual windows file, instead of a virtual file. for virtual file, use the seek and seekRelative function.

Parameters
lDistanceToMove
dwMoveMethod0: FILE_BEGIN The starting point is 0 (zero) or the beginning of the file. 1: FILE_CURRENT The starting point is the current value of the file pointer. 2: FILE_END The starting point is the current end-of-file position.

§ SetWritablePath()

void ParaEngine::CFileUtils::SetWritablePath ( const std::string &  writable_path)
static

not thread safe, only set at startup when there is just one thread running.


The documentation for this class was generated from the following files: