3.3. Filesystem utilities
Those are a few function that cannot be easily implemented in a portable way in C/C++. They have no python wrapper since python provides its own builtin functions. All those functions return false if an error occured.
Create a directory
static bool TCODSystem::createDirectory(const char *path)
bool TCOD_sys_create_directory(const char *path)
Parameter | Description |
---|---|
path | Directory path. The immediate father directory ( |
Delete an empty directory
static bool TCODSystem::deleteDirectory(const char *path)
bool TCOD_sys_delete_directory(const char *path)
Parameter | Description |
---|---|
path | directory path. This directory must exist, be writable and empty |
Delete a file
static bool TCODSystem::deleteFile(const char *path)
bool TCOD_sys_delete_file(const char *path)
Parameter | Description |
---|---|
path | File path. This file must exist and be writable. |
Check if a path is a directory
static bool TCODSystem::isDirectory(const char *path)
bool TCOD_sys_is_directory(const char *path)
Parameter | Description |
---|---|
path | a path to check |
List files in a directory
To get the list of entries in a directory (including sub-directories, except . and ..).
The returned list is allocated by the function and must be deleted by you. All the const char * inside must be also freed with TCODList::clearAndDelete.
static TCODList TCODSystem::getDirectoryContent(const char *path, const char *pattern)
TCOD_list_t TCOD_sys_get_directory_content(const char *path)
Parameter | Description |
---|---|
path | a directory |
pattern | If NULL or empty, returns all directory entries. Else returns only entries matching the pattern. The pattern is NOT a regular expression. It can only handle one '*' wildcard. Examples : *.png, saveGame*, font*.png |
Check if a given file exists
In order to check whether a given file exists in the filesystem. Useful for detecting errors caused by missing files.
static bool TCODSystem::fileExists(const char *filename, ...)
bool TCOD_sys_file_exists(const char * filename, ...)
Parameter | Description |
---|---|
filename | the file name, using printf-like formatting |
... | optional arguments for filename formatting |
Example:
if (!TCODSystem::fileExists("myfile.%s","txt")) {
fprintf(stderr,"no such file!");
}
if (!TCOD_sys_file_exists("myfile.%s","txt")) {
fprintf(stderr,"no such file!");
}
Read the content of a file into memory
This is a portable function to read the content of a file from disk or from the application apk (android).
buf must be freed with free(buf).
static bool TCODSystem::readFile(const char *filename, unsigned char **buf, uint32 *size)
bool TCOD_sys_read_file(const char *filename, unsigned char **buf, uint32 *size)
Parameter | Description |
---|---|
filename | the file name |
buf | a buffer to be allocated and filled with the file content |
size | the size of the allocated buffer. |
Example:
unsigned char *buf;
uint32 size;
if (TCODSystem::readFile("myfile.dat",&buf,&size)) {
// do something with buf
free(buf);
}
if (TCOD_sys_read_file("myfile.dat",&buf,&size)) {
// do something with buf
free(buf);
}
Write the content of a memory buffer to a file
This is a portable function to write some data to a file.
static bool TCODSystem::writeFile(const char *filename, unsigned char *buf, uint32 size)
bool TCOD_sys_write_file(const char *filename, unsigned char *buf, uint32 size)
Parameter | Description |
---|---|
filename | the file name |
buf | a buffer containing the data to write |
size | the number of bytes to write. |
Example:
TCODSystem::writeFile("myfile.dat",buf,size));
TCOD_sys_write_file("myfile.dat",buf,size));