qLibc
qfile.c File Reference

File handling APIs. More...

Go to the source code of this file.

Macros

#define MAX_EXTENSION_LENGTH   (8)
 

Functions

bool qfile_lock (int fd)
 Lock file.
 
bool qfile_unlock (int fd)
 Unlock file which is locked by qfile_lock()
 
bool qfile_exist (const char *filepath)
 Check file existence.
 
void * qfile_load (const char *filepath, size_t *nbytes)
 Load file into memory.
 
void * qfile_read (FILE *fp, size_t *nbytes)
 Read data from a file stream.
 
ssize_t qfile_save (const char *filepath, const void *buf, size_t size, bool append)
 Save data into file.
 
bool qfile_mkdir (const char *dirpath, mode_t mode, bool recursive)
 Attempts to create a directory recursively.
 
bool qfile_check_path (const char *path)
 Check path string contains invalid characters.
 
char * qfile_get_name (const char *filepath)
 Get filename from filepath.
 
char * qfile_get_dir (const char *filepath)
 Get directory suffix from filepath.
 
char * qfile_get_ext (const char *filepath)
 Get extension from filepath.
 
off_t qfile_get_size (const char *filepath)
 Get file size.
 
char * qfile_correct_path (char *path)
 Correct path string.
 
char * qfile_abspath (char *buf, size_t bufsize, const char *path)
 Make full absolute system path string.
 

Detailed Description

File handling APIs.

Definition in file qfile.c.

Function Documentation

◆ qfile_lock()

bool qfile_lock ( int  fd)

Lock file.

Parameters
fdfile descriptor
Returns
true if successful, otherwise returns false.
// for file descriptor
int fd = open(...);
if(qfile_lock(fd) == true) {
(...atomic file access...)
}
// for FILE stream object
FILE *fp = fopen(...);
int fd = fileno(fp);
if(qfile_lock(fd) == true) {
(...atomic file access...)
}
bool qfile_unlock(int fd)
Unlock file which is locked by qfile_lock()
Definition qfile.c:98
bool qfile_lock(int fd)
Lock file.
Definition qfile.c:74

Definition at line 74 of file qfile.c.

◆ qfile_unlock()

bool qfile_unlock ( int  fd)

Unlock file which is locked by qfile_lock()

Parameters
fdfile descriptor
Returns
true if successful, otherwise returns false.

Definition at line 98 of file qfile.c.

◆ qfile_exist()

bool qfile_exist ( const char *  filepath)

Check file existence.

Parameters
filepathfile or directory path
Returns
true if exists, otherwise returns false.

Definition at line 122 of file qfile.c.

◆ qfile_load()

void * qfile_load ( const char *  filepath,
size_t *  nbytes 
)

Load file into memory.

Parameters
filepathfile path
nbyteshas 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.
Returns
allocated memory pointer if successful, otherwise returns NULL.
// loading text file
char *text = (char *)qfile_load("/tmp/text.txt", NULL);
// loading binary file
int binlen = 0;
char *bin = (char *)qfile_load("/tmp/binary.bin", &binlen);
// loading partial
int binlen = 10;
char *bin = (char *)qfile_load("/tmp/binary.bin", &binlen);
void * qfile_load(const char *filepath, size_t *nbytes)
Load file into memory.
Definition qfile.c:159
Note
This method actually allocates memory more than 1 bytes than filesize then append NULL character at the end. For example, when the file size is 10 bytes long, 10+1 bytes will allocated and the last byte is always NULL character. So you can load text file and use without appending NULL character. By the way, the actual file size 10 will be returned at nbytes variable.

Definition at line 159 of file qfile.c.

◆ qfile_read()

void * qfile_read ( FILE *  fp,
size_t *  nbytes 
)

Read data from a file stream.

Parameters
fpFILE pointer
nbyteshas two purpose, one is to set bytes to read. the other is to return actual number of bytes loaded. 0 or NULL can be set to read file until the end.
Returns
allocated memory pointer if successful, otherwise returns NULL.
int binlen = 0;
char *bin = (char *)qfile_read(fp, &binlen);
void * qfile_read(FILE *fp, size_t *nbytes)
Read data from a file stream.
Definition qfile.c:214
Note
This method append NULL character at the end of stream. but nbytes only counts actual readed bytes.

Definition at line 214 of file qfile.c.

◆ qfile_save()

ssize_t qfile_save ( const char *  filepath,
const void *  buf,
size_t  size,
bool  append 
)

Save data into file.

Parameters
filepathfile path
bufdata
sizethe number of bytes to save
appendfalse for new(if exists truncate), true for appending
Returns
the number of bytes written if successful, otherwise returns -1.
// save text
char *text = "hello";
qfile_save("/tmp/text.txt", (void*)text, strlen(text), false);
// save binary
int integer1 = 75;
qfile_save("/tmp/integer.bin, (void*)&integer, sizeof(int));
ssize_t qfile_save(const char *filepath, const void *buf, size_t size, bool append)
Save data into file.
Definition qfile.c:283

Definition at line 283 of file qfile.c.

◆ qfile_mkdir()

bool qfile_mkdir ( const char *  dirpath,
mode_t  mode,
bool  recursive 
)

Attempts to create a directory recursively.

Parameters
dirpathdirectory path
modepermissions to use
recursivewhether or not to create parent directories automatically
Returns
true if successful, otherwise returns false.

Definition at line 312 of file qfile.c.

◆ qfile_check_path()

bool qfile_check_path ( const char *  path)

Check path string contains invalid characters.

Parameters
pathpath string
Returns
true if ok, otherwise returns false.

Definition at line 337 of file qfile.c.

◆ qfile_get_name()

char * qfile_get_name ( const char *  filepath)

Get filename from filepath.

Parameters
filepathfile or directory path
Returns
malloced filename string

Definition at line 356 of file qfile.c.

◆ qfile_get_dir()

char * qfile_get_dir ( const char *  filepath)

Get directory suffix from filepath.

Parameters
filepathfile or directory path
Returns
malloced filepath string

Definition at line 371 of file qfile.c.

◆ qfile_get_ext()

char * qfile_get_ext ( const char *  filepath)

Get extension from filepath.

Parameters
filepathfile or directory path
Returns
malloced extension string which is converted to lower case.

Definition at line 386 of file qfile.c.

◆ qfile_get_size()

off_t qfile_get_size ( const char *  filepath)

Get file size.

Parameters
filepathfile or directory path
Returns
the file size if exists, otherwise returns -1.

Definition at line 410 of file qfile.c.

◆ qfile_correct_path()

char * qfile_correct_path ( char *  path)

Correct path string.

Parameters
pathpath string
Returns
path string pointer
Note
This modify path argument itself.
"/hello//my/../world" => "/hello/world"
"././././hello/./world" => "./hello/world"
"/../hello//world" => "/hello/world"
"/../hello//world/" => "/hello/world"

Definition at line 434 of file qfile.c.

◆ qfile_abspath()

char * qfile_abspath ( char *  buf,
size_t  bufsize,
const char *  path 
)

Make full absolute system path string.

Parameters
bufbuffer string pointer
bufsizebuffer size
pathpath string
Returns
buffer pointer if successful, otherwise returns NULL.
char buf[PATH_MAX];
chdir("/usr/local");
qfile_abspath(buf, sizeof(buf), "."); // returns "/usr/local"
qfile_abspath(buf, sizeof(buf), ".."); // returns "/usr"
qfile_abspath(buf, sizeof(buf), "etc"); // returns "/usr/local/etc"
qfile_abspath(buf, sizeof(buf), "/etc"); // returns "/etc"
qfile_abspath(buf, sizeof(buf), "/etc/"); // returns "/etc/"
char * qfile_abspath(char *buf, size_t bufsize, const char *path)
Make full absolute system path string.
Definition qfile.c:530

Definition at line 530 of file qfile.c.