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)

pathDirectory path. The immediate father directory (/..) must exist and be writable.

Delete an empty directory

static bool TCODSystem::deleteDirectory(const char *path)

bool TCOD_sys_delete_directory(const char *path)

pathdirectory 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)

pathFile 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)

patha 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)

patha directory
patternIf 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, ...)

filenamethe file name, using printf-like formatting
...optional arguments for filename formatting

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)

filenamethe file name
bufa buffer to be allocated and filled with the file content
sizethe size of the allocated buffer.

unsigned char *buf;
(TCODSystem::readFile("myfile.dat",&buf,&size)) {
    // do something with buf

if (TCOD_sys_read_file("myfile.dat",&buf,&size)) {
    // do something with 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)

filenamethe file name
bufa buffer containing the data to write
sizethe number of bytes to write.