Skip to content

Commit fa5aa30

Browse files
authored
Merge pull request #2596 from cesanta/ls
Add helper mg_fs_ls()
2 parents f8898b0 + cbfa57a commit fa5aa30

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

mongoose.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,26 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) {
16661666
return result;
16671667
}
16681668

1669+
// This helper function allows to scan a filesystem in a sequential way,
1670+
// without using callback function:
1671+
// char buf[100] = "";
1672+
// while (mg_fs_ls(&mg_fs_posix, "./", buf, sizeof(buf))) {
1673+
// ...
1674+
static void mg_fs_ls_fn(const char *filename, void *param) {
1675+
struct mg_str *s = (struct mg_str *) param;
1676+
if (s->ptr[0] == '\0') {
1677+
mg_snprintf((char *) s->ptr, s->len, "%s", filename);
1678+
} else if (strcmp(s->ptr, filename) == 0) {
1679+
((char *) s->ptr)[0] = '\0'; // Fetch next file
1680+
}
1681+
}
1682+
1683+
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len) {
1684+
struct mg_str s = {buf, len};
1685+
fs->ls(path, mg_fs_ls_fn, &s);
1686+
return buf[0] != '\0';
1687+
}
1688+
16691689
#ifdef MG_ENABLE_LINES
16701690
#line 1 "src/fs_fat.c"
16711691
#endif

mongoose.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@ struct mg_fd {
10251025

10261026
struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags);
10271027
void mg_fs_close(struct mg_fd *fd);
1028+
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len);
10281029
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size);
10291030
bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
10301031
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);

src/fs.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,23 @@ bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...) {
7272
free(data);
7373
return result;
7474
}
75+
76+
// This helper function allows to scan a filesystem in a sequential way,
77+
// without using callback function:
78+
// char buf[100] = "";
79+
// while (mg_fs_ls(&mg_fs_posix, "./", buf, sizeof(buf))) {
80+
// ...
81+
static void mg_fs_ls_fn(const char *filename, void *param) {
82+
struct mg_str *s = (struct mg_str *) param;
83+
if (s->ptr[0] == '\0') {
84+
mg_snprintf((char *) s->ptr, s->len, "%s", filename);
85+
} else if (strcmp(s->ptr, filename) == 0) {
86+
((char *) s->ptr)[0] = '\0'; // Fetch next file
87+
}
88+
}
89+
90+
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len) {
91+
struct mg_str s = {buf, len};
92+
fs->ls(path, mg_fs_ls_fn, &s);
93+
return buf[0] != '\0';
94+
}

src/fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct mg_fd {
3939

4040
struct mg_fd *mg_fs_open(struct mg_fs *fs, const char *path, int flags);
4141
void mg_fs_close(struct mg_fd *fd);
42+
bool mg_fs_ls(struct mg_fs *fs, const char *path, char *buf, size_t len);
4243
char *mg_file_read(struct mg_fs *fs, const char *path, size_t *size);
4344
bool mg_file_write(struct mg_fs *fs, const char *path, const void *, size_t);
4445
bool mg_file_printf(struct mg_fs *fs, const char *path, const char *fmt, ...);

0 commit comments

Comments
 (0)