ZFFramework
 
Loading...
Searching...
No Matches
ZFFile_file.h File Reference

file utility More...

#include "ZFFile_fwd.h"

Go to the source code of this file.

Functions

zfbool ZFFileIsExist (const zfchar *path)
 return true if file specified by path is exist
 
zfbool ZFFileIsDir (const zfchar *path)
 return true if file specified by path is a directory
 
zfbool ZFPathCreate (const zfchar *path, zfbool autoMakeParent=(_ZFT_t_zftrue))
 make directory
 
zfbool ZFFileCopy (const zfchar *srcPath, const zfchar *dstPath, zfbool isRecursive=(_ZFT_t_zftrue), zfbool isForce=(_ZFT_t_zftrue))
 copy a file or directory from srcPath to dstPath
 
zfbool ZFFileMove (const zfchar *srcPath, const zfchar *dstPath, zfbool isForce=(_ZFT_t_zftrue))
 move a file or directory from srcPath to dstPath
 
zfbool ZFFileRemove (const zfchar *path, zfbool isRecursive=(_ZFT_t_zftrue), zfbool isForce=(_ZFT_t_zftrue))
 delete a file or directory from srcPath to dstPath
 
zfbool ZFFileFindFirst (ZFFileFindData &fd, const zfchar *path)
 find file or directory, similar to FindFirstFile under Windows
 
zfbool ZFFileFindNext (ZFFileFindData &fd)
 
void ZFFileFindClose (ZFFileFindData &fd)
 
void * ZFFileOpen (const zfchar *filePath, ZFFileOpenOptionFlags flag=(v_ZFFileOpenOption::e_Read), zfbool autoCreateParent=(_ZFT_t_zftrue))
 open a file for read or write
 
zfbool ZFFileClose (void *token)
 close and save the file if need, return false if save failed
 
zfindex ZFFileTell (void *token)
 get current file's position or zfindexMax() if error
 
zfbool ZFFileSeek (void *token, zfindex byteSize, ZFSeekPos position=(ZFSeekPosBegin))
 similar to fseek, return false if seek out of range
 
zfindex ZFFileRead (void *token, void *buf, zfindex maxByteSize)
 read file
 
zfindex ZFFileWrite (void *token, const void *src, zfindex maxByteSize=(((zfindex) -1)))
 write file, see ZFFileRead
 
void ZFFileFlush (void *token)
 flush the file, useful only for files opened for write
 
zfbool ZFFileIsEof (void *token)
 see ZFFileRead
 
zfbool ZFFileIsError (void *token)
 see ZFFileRead
 
zfindex ZFFileSize (void *token)
 util method to get file's total size (not left size)
 

Detailed Description

file utility

Function Documentation

◆ ZFFileIsExist()

zfbool ZFFileIsExist ( const zfchar * path)
extern

return true if file specified by path is exist

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileIsDir()

zfbool ZFFileIsDir ( const zfchar * path)
extern

return true if file specified by path is a directory

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFPathCreate()

zfbool ZFPathCreate ( const zfchar * path,
zfbool autoMakeParent = (_ZFT_t_zftrue) )
extern

make directory

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileCopy()

zfbool ZFFileCopy ( const zfchar * srcPath,
const zfchar * dstPath,
zfbool isRecursive = (_ZFT_t_zftrue),
zfbool isForce = (_ZFT_t_zftrue) )
extern

copy a file or directory from srcPath to dstPath

if both src and dst exist, but one is file and another is dir, return zffalse
merge directory if dst is an existing dir (if isForce not set, would return false if dst has a child file with the same path in src)
override file if dst is an existing file and isForce is zftrue

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileMove()

zfbool ZFFileMove ( const zfchar * srcPath,
const zfchar * dstPath,
zfbool isForce = (_ZFT_t_zftrue) )
extern

move a file or directory from srcPath to dstPath

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileRemove()

zfbool ZFFileRemove ( const zfchar * path,
zfbool isRecursive = (_ZFT_t_zftrue),
zfbool isForce = (_ZFT_t_zftrue) )
extern

delete a file or directory from srcPath to dstPath

fail if isRecursive is zffalse and dst is a dir

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileFindFirst()

zfbool ZFFileFindFirst ( ZFFileFindData & fd,
const zfchar * path )
extern

find file or directory, similar to FindFirstFile under Windows

path supports path only, without wildcard support, e.g. "/path/" or "/path", "." and ".." won't be included
typical usage:

if(ZFFileFindFirst(fd, path)) {
do {
// do something
} while(ZFFileFindNext(fd));
}
void ZFFileFindClose(ZFFileFindData &fd)
zfbool ZFFileFindFirst(ZFFileFindData &fd, const zfchar *path)
find file or directory, similar to FindFirstFile under Windows
zfbool ZFFileFindNext(ZFFileFindData &fd)
data used by ZFFile when finding files
Definition ZFFile_fwd.h:53
Returns
false if error or nothing found
Warning
strings in ZFFileFindData would be deleted after each findXXX call, you must save it if need future use
See also
ZFFileFindData, ZFFileFindFirst, ZFFileFindNext, ZFFileFindClose
Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileFindNext()

zfbool ZFFileFindNext ( ZFFileFindData & fd)
extern

◆ ZFFileFindClose()

void ZFFileFindClose ( ZFFileFindData & fd)
extern

◆ ZFFileOpen()

void * ZFFileOpen ( const zfchar * filePath,
ZFFileOpenOptionFlags flag = (v_ZFFileOpenOption::e_Read),
zfbool autoCreateParent = (_ZFT_t_zftrue) )
extern

open a file for read or write

Note
path must be well formed, use ZFPathFormat if necessary

◆ ZFFileRead()

zfindex ZFFileRead ( void * token,
void * buf,
zfindex maxByteSize )
extern

read file

return size read, even if error occurred (whether error occurred or eof, could be accessed by ZFFileIsError and ZFFileIsEof)
typical usage:

zfindex sizeRead = 0;
while((sizeRead = ZFFileRead(token, buf)) > 0
&& !ZFFileIsError(token)
) {
// do something with buf
}
if(ZFFileIsError(token)) {
// exit because of error occurred, read fail
}
else if(ZFFileIsEof(token)) {
// exit because of eof, read success
}
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
zfbool ZFFileIsEof(void *token)
see ZFFileRead
zfindex ZFFileRead(void *token, void *buf, zfindex maxByteSize)
read file
zfbool ZFFileIsError(void *token)
see ZFFileRead

note that the tail '\0' won't be written to buf

Warning
ZFFile always read files in binary mode, for text files, you must make sure the file is UTF8 encoded without BOM

◆ ZFFileWrite()

zfindex ZFFileWrite ( void * token,
const void * src,
zfindex maxByteSize = (((zfindex) -1)) )
extern

write file, see ZFFileRead

return size written, even if error occurred (whether error occurred or not, could be accessed by ZFFileIsError)
typical usage:

zfindex sizeWritten = ZFFileWrite(token, src, size);
if(ZFFileIsError(token)) {
// write fail
}
if(sizeWritten != size) { // you can also check by sizeWritten
// write fail
}
zfindex ZFFileWrite(void *token, const void *src, zfindex maxByteSize=(((zfindex) -1)))
write file, see ZFFileRead


maxByteSize could be zfindexMax(), which means ZFFileWrite should be stopped when reached 0x00 in src, usually to output a UTF8 string

◆ ZFFileSize()

zfindex ZFFileSize ( void * token)
extern

util method to get file's total size (not left size)

ZFFileSeek to end, ZFFileTell, then ZFFileSeek to restore, return zfindexMax() if error
note that result is not ensured if file is opened in append mode