1
1
Fork 0
mirror of https://github.com/FellippeHeitor/InForm.git synced 2025-01-15 11:59:34 +00:00
InForm/examples/TextFetch/direntry.h

67 lines
1.3 KiB
C
Raw Normal View History

2023-07-21 22:53:35 +00:00
// Cross-platform directory iterator
// Original version by Steve McNeill
// Changes and enhancements by a740g (24-June-2023)
2023-07-21 22:53:35 +00:00
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
enum : int8_t
{
QB_TRUE = -1,
QB_FALSE
};
enum : int32_t
{
IS_DIR_FLAG = 1,
IS_FILE_FLAG
};
static DIR *p_dir = nullptr;
2023-07-21 22:53:35 +00:00
int8_t __open_dir(const char *path)
{
2023-07-21 22:53:35 +00:00
if (p_dir)
return QB_FALSE; // return false if a directory is already open
p_dir = opendir(path);
if (!p_dir)
return QB_FALSE;
return QB_TRUE;
}
2023-07-21 22:53:35 +00:00
const char *read_dir(int *flags, int *file_size)
{
2023-07-21 22:53:35 +00:00
static char dir_name[4096]; // 4k static buffer
dir_name[0] = 0; // set to empty string
auto next_entry = readdir(p_dir);
if (!next_entry)
2023-07-21 22:53:35 +00:00
return dir_name; // return an empty string to indicate we have nothing
struct stat entry_info;
stat(next_entry->d_name, &entry_info);
*flags = S_ISDIR(entry_info.st_mode) ? IS_DIR_FLAG : IS_FILE_FLAG;
*file_size = entry_info.st_size;
2023-07-21 22:53:35 +00:00
strncpy(dir_name, next_entry->d_name, sizeof(dir_name));
dir_name[sizeof(dir_name)] = 0; // overflow protection
return dir_name; // QB64-PE does the right thing with this
}
void close_dir()
{
closedir(p_dir);
2023-07-21 22:53:35 +00:00
p_dir = nullptr; // set this to NULL so that subsequent __open_dir() works correctly
}