mirror of
https://github.com/Lurkki14/tuxclocker.git
synced 2024-11-24 09:10:16 -06:00
lib: add tc_fs_file_exists
This commit is contained in:
parent
4780726d69
commit
42421e11e6
@ -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
|
||||
|
33
src/lib/posix/filesystem.c
Normal file
33
src/lib/posix/filesystem.c
Normal 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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user