File handling APIs.
More...
Go to the source code of this file.
|
#define | MAX_EXTENSION_LENGTH (8) |
|
|
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.
|
|
File handling APIs.
Definition in file qfile.c.
◆ qfile_lock()
bool qfile_lock |
( |
int |
fd | ) |
|
Lock file.
- Parameters
-
- Returns
- true if successful, otherwise returns false.
int fd = open(...);
(...atomic file access...)
}
FILE *fp = fopen(...);
int fd = fileno(fp);
(...atomic file access...)
}
bool qfile_unlock(int fd)
Unlock file which is locked by qfile_lock()
bool qfile_lock(int fd)
Lock file.
Definition at line 74 of file qfile.c.
◆ qfile_unlock()
bool qfile_unlock |
( |
int |
fd | ) |
|
Unlock file which is locked by qfile_lock()
- Parameters
-
- 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
-
filepath | file 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
-
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. |
- Returns
- allocated memory pointer if successful, otherwise returns NULL.
char *text = (
char *)
qfile_load(
"/tmp/text.txt", NULL);
int binlen = 0;
char *bin = (
char *)
qfile_load(
"/tmp/binary.bin", &binlen);
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.
- 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
-
fp | FILE pointer |
nbytes | has 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;
void * qfile_read(FILE *fp, size_t *nbytes)
Read data from a file stream.
- 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
-
filepath | file path |
buf | data |
size | the number of bytes to save |
append | false for new(if exists truncate), true for appending |
- Returns
- the number of bytes written if successful, otherwise returns -1.
char *text = "hello";
qfile_save(
"/tmp/text.txt", (
void*)text, strlen(text),
false);
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 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
-
dirpath | directory path |
mode | permissions to use |
recursive | whether 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
-
- 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
-
filepath | file 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
-
filepath | file 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
-
filepath | file 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
-
filepath | file 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
-
- 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
-
buf | buffer string pointer |
bufsize | buffer size |
path | path string |
- Returns
- buffer pointer if successful, otherwise returns NULL.
char buf[PATH_MAX];
chdir("/usr/local");
char * qfile_abspath(char *buf, size_t bufsize, const char *path)
Make full absolute system path string.
Definition at line 530 of file qfile.c.