lib: add tc_fs_file_exists

This commit is contained in:
jussi 2019-10-22 21:28:41 +03:00
parent 4780726d69
commit 42421e11e6
2 changed files with 43 additions and 3 deletions

View File

@ -1,14 +1,21 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <sys/stat.h>
// Contains functions and definitions for dealing with files
#ifdef __cplusplus
extern "C" {
#endif
// Get filename list from a folder
char **tc_fs_folder_filenames();
// Get filename list from a directory. Return value needs to be freed.
char **tc_fs_dir_filenames(const char *dir_name, uint16_t *file_count);
// Check if a file exists
bool tc_fs_file_exists(const char *path);
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,33 @@
#include <tc_filesystem.h>
#include <tc_common.h>
#include <stdlib.h>
#include <stdint.h>
#include <dirent.h>
char **tc_fs_dir_filenames(const char *dir_name, uint16_t *file_count) {
char *file_names[512];
struct dirent *entry;
DIR *dir = opendir(dir_name);
uint16_t i = 0;
if (dir != NULL) {
while ((entry = readdir(dir)) != NULL) {
file_names[i] = entry->d_name;
i++;
}
closedir(dir);
}
else {
// Couldn't open dir
return NULL;
}
*file_count = i;
return tc_str_arr_dup(i, file_names);
}
bool tc_fs_file_exists(const char *path) {
struct stat buf;
return (stat(path, &buf) == 0);
}