From cbfa57a9559840d44190925dd376c444fcefd1ce Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 1 Feb 2024 17:45:20 +0000 Subject: [PATCH] Add helper mg_fs_ls() --- mongoose.c | 20 ++++++++++++++++++++ mongoose.h | 1 + src/fs.c | 20 ++++++++++++++++++++ src/fs.h | 1 + 4 files changed, 42 insertions(+) diff --git a/mongoose.c b/mongoose.c index 18aa613841..15ade247d1 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1666,6 +1666,26 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) { return result; } +// This helper function allows to scan a filesystem in a sequential way, +// without using callback function: +// char buf[100] = ""; +// while (mg_fs_ls(&mg_fs_posix, "./", buf, sizeof(buf))) { +// ... +static void mg_fs_ls_fn(const char *filename, void *param) { + struct mg_str *s = (struct mg_str *) param; + if (s->ptr[0] == '\0') { + mg_snprintf((char *) s->ptr, s->len, "%s", filename); + } else if (strcmp(s->ptr, filename) == 0) { + ((char *) s->ptr)[0] = '\0'; // Fetch next file + } +} + +bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len) { + struct mg_str s = {buf, len}; + fs->ls(path, mg_fs_ls_fn, &s); + return buf[0] != '\0'; +} + #ifdef MG_ENABLE_LINES #line 1 "src/fs_fat.c" #endif diff --git a/mongoose.h b/mongoose.h index f40c0023b2..7e708e371f 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1025,6 +1025,7 @@ struct mg_fd { struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags); void mg_fs_close(struct mg_fd *fd); +bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len); char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size); bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t); bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...); diff --git a/src/fs.c b/src/fs.c index 00eeb0edac..19681079a9 100644 --- a/src/fs.c +++ b/src/fs.c @@ -72,3 +72,23 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) { free(data); return result; } + +// This helper function allows to scan a filesystem in a sequential way, +// without using callback function: +// char buf[100] = ""; +// while (mg_fs_ls(&mg_fs_posix, "./", buf, sizeof(buf))) { +// ... +static void mg_fs_ls_fn(const char *filename, void *param) { + struct mg_str *s = (struct mg_str *) param; + if (s->ptr[0] == '\0') { + mg_snprintf((char *) s->ptr, s->len, "%s", filename); + } else if (strcmp(s->ptr, filename) == 0) { + ((char *) s->ptr)[0] = '\0'; // Fetch next file + } +} + +bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len) { + struct mg_str s = {buf, len}; + fs->ls(path, mg_fs_ls_fn, &s); + return buf[0] != '\0'; +} diff --git a/src/fs.h b/src/fs.h index a5fa31d906..b09e9bdaae 100644 --- a/src/fs.h +++ b/src/fs.h @@ -39,6 +39,7 @@ struct mg_fd { struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags); void mg_fs_close(struct mg_fd *fd); +bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len); char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size); bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t); bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);