libtcoddocumentation

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)

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

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

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

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

ParameterDescription
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, ...)

ParameterDescription
filenamethe 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)

ParameterDescription
filenamethe file name
bufa buffer to be allocated and filled with the file content
sizethe 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)

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

TCODSystem::writeFile("myfile.dat",buf,size));

TCOD_sys_write_file("myfile.dat",buf,size));