![]() |
Defines | |
#define | MAX_EXTENSION_LENGTH (8) |
Functions | |
bool | qFileLock (int fd) |
Lock file. | |
bool | qFileUnlock (int fd) |
Unlock file which is locked by qFileLock(). | |
bool | qFileExist (const char *filepath) |
Check file existence. | |
void * | qFileLoad (const char *filepath, size_t *nbytes) |
Load file into memory. | |
void * | qFileRead (FILE *fp, size_t *nbytes) |
Load file stream which has unknown size into memory. | |
ssize_t | qFileSave (const char *filepath, const void *buf, size_t size, bool append) |
Save data into file. | |
char * | qFileReadLine (FILE *fp) |
Read one line from file. | |
bool | qFileMkdir (const char *dirpath, mode_t mode, bool recursive) |
Attempts to create a directory recursively. | |
bool | qFileCheckPath (const char *path) |
Check path string contains invalid characters. | |
char * | qFileGetName (const char *filepath) |
Get filename from filepath. | |
char * | qFileGetDir (const char *filepath) |
Get directory suffix from filepath. | |
char * | qFileGetExt (const char *filepath) |
Get extension from filepath. | |
off_t | qFileGetSize (const char *filepath) |
Get file size. | |
char * | qFileCorrectPath (char *path) |
Correct path string. | |
char * | qFileGetAbsPath (char *buf, size_t bufsize, const char *path) |
Make full absolute system path string. |
bool qFileLock | ( | int | fd | ) |
Lock file.
fd | file descriptor |
// for file descriptor int fd = open(...); if(qFileLock(fd) == true) { (...atomic file access...) qFileUnlock(fd); } // for FILE stream object FILE *fp = fopen(...); int fd = fileno(fp); if(qFileLock(fd) == true) { (...atomic file access...) qFileUnlock(fd); }
bool qFileUnlock | ( | int | fd | ) |
Unlock file which is locked by qFileLock().
fd | file descriptor |
bool qFileExist | ( | const char * | filepath | ) |
Check file existence.
filepath | file or directory path |
void* qFileLoad | ( | const char * | filepath, | |
size_t * | nbytes | |||
) |
Load file into memory.
filepath | file path | |
nbytes | has two purpost, one is to set how many bytes are readed. the other is actual the number loaded bytes will be stored. nbytes must be point 0 or NULL to read entire file. |
// loading text file char *text = (char *)qFileLoad("/tmp/text.txt", NULL); // loading binary file int binlen = 0; char *bin = (char *)qFileLoad("/tmp/binary.bin", &binlen); // loading partial int binlen = 10; char *bin = (char *)qFileLoad("/tmp/binary.bin", &binlen);
void* qFileRead | ( | FILE * | fp, | |
size_t * | nbytes | |||
) |
Load file stream which has unknown size into memory.
fp | FILE pointer | |
nbytes | has two purpost, one is to set how many bytes are readed. the other is actual the number loaded bytes will be stored. nbytes must be point 0 or NULL to read end of stream. |
ssize_t qFileSave | ( | const char * | filepath, | |
const void * | buf, | |||
size_t | size, | |||
bool | append | |||
) |
Save data into file.
filepath | file path | |
buf | data | |
size | the number of bytes to save | |
append | false for new(if exists truncate), true for appending |
char* qFileReadLine | ( | FILE * | fp | ) |
Read one line from file.
fp | FILE pointer |
bool qFileMkdir | ( | const char * | dirpath, | |
mode_t | mode, | |||
bool | recursive | |||
) |
Attempts to create a directory recursively.
dirpath | directory path | |
mode | permissions to use | |
recursive | whether or not to create parent directories automatically |
bool qFileCheckPath | ( | const char * | path | ) |
Check path string contains invalid characters.
path | path string |
char* qFileGetName | ( | const char * | filepath | ) |
Get filename from filepath.
filepath | file or directory path |
char* qFileGetDir | ( | const char * | filepath | ) |
Get directory suffix from filepath.
filepath | file or directory path |
char* qFileGetExt | ( | const char * | filepath | ) |
Get extension from filepath.
filepath | file or directory path |
off_t qFileGetSize | ( | const char * | filepath | ) |
Get file size.
filepath | file or directory path |
char* qFileCorrectPath | ( | char * | path | ) |
Correct path string.
path | path string |
"/hello//my/../world" => "/hello/world" "././././hello/./world" => "./hello/world" "/../hello//world" => "/hello/world"
char* qFileGetAbsPath | ( | char * | buf, | |
size_t | bufsize, | |||
const char * | path | |||
) |
Make full absolute system path string.
buf | buffer string pointer | |
bufsize | buffer size | |
path | path string |
char buf[PATH_MAX]; chdir("/usr/local"); qFileGetAbsPathFullPathGetAbs(buf, sizeof(buf), "."); // returns "/usr/local" qFileGetAbsPathFullPathGetAbs(buf, sizeof(buf), ".."); // returns "/usr" qFileGetAbsPathFullPathGetAbs(buf, sizeof(buf), "etc"); // returns "/usr/local/etc" qFileGetAbsPathFullPathGetAbs(buf, sizeof(buf), "/etc"); // returns "/etc"