Skip to content

Commit

Permalink
MTY_StrSearch added, add size to MTY_FileDesc
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisd1100 committed Jan 31, 2024
1 parent a69f8bd commit c933d8d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
16 changes: 13 additions & 3 deletions src/matoya.h
Original file line number Diff line number Diff line change
Expand Up @@ -1732,9 +1732,10 @@ typedef enum {

/// @brief File properties.
typedef struct {
char *path; ///< The base path to the file.
char *name; ///< The file name.
bool dir; ///< The file is a directory.
char *path; ///< The base path to the file.
char *name; ///< The file name.
uint64_t size; ///< The file size in bytes.
bool dir; ///< The file is a directory.
} MTY_FileDesc;

/// @brief A list of files.
Expand Down Expand Up @@ -2400,6 +2401,15 @@ MTY_SprintfD(const char *fmt, ...) MTY_FMT(1, 2);
MTY_EXPORT const char *
MTY_SprintfDL(const char *fmt, ...) MTY_FMT(1, 2);

/// @brief Search a string for a list of substrings.
/// @param a String to be searched.
/// @param b List of substrings delimited by `delim`.
/// @param delim Delimiters used to for `s1`. Each character in this string is treated
/// as a delimiter like MTY_Strtok.
/// @returns Returns true if any of `s1` is found in `s0`, otherwise false.
MTY_EXPORT bool
MTY_StrSearch(const char *s0, const char *s1, const char *delim);

/// @brief Case insensitive string comparison.
/// @details For more information, see `strcasecmp` from the C standard library.
/// @param s0 First string to compare.
Expand Down
24 changes: 24 additions & 0 deletions src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ const char *MTY_SprintfDL(const char *fmt, ...)
return local;
}

bool MTY_StrSearch(const char *s0, const char *s1, const char *delim)
{
char *tmp = MTY_Strdup(s1);
char *ptr = NULL;

char *s = MTY_Strtok(tmp, delim, &ptr);
if (!s)
return true;

bool r = false;

while (s) {
r = strstr(s0, s);
if (r)
break;

s = MTY_Strtok(NULL, delim, &ptr);
}

MTY_Free(tmp);

return r;
}


// Stable qsort

Expand Down
12 changes: 9 additions & 3 deletions src/unix/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ static int32_t file_compare(const void *p1, const void *p2)

MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
{
uint8_t tmp[4 * 1024];
mty_tlocal_set_mem(tmp, sizeof(tmp));
void *tmp = MTY_Alloc(0x1000, 1);
mty_tlocal_set_mem(tmp, 0x1000);

MTY_FileList *fl = MTY_Alloc(1, sizeof(MTY_FileList));
char *pathd = MTY_Strdup(path);
Expand All @@ -223,12 +223,17 @@ MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
bool is_dir = ent->d_type == DT_DIR ||
(ent->d_type == DT_UNKNOWN && (!strcmp(name, "..") || !strcmp(name, ".")));

if (is_dir || strstr(name, filter ? filter : "")) {
if (is_dir || MTY_StrSearch(name, filter ? filter : "", "|")) {
fl->files = MTY_Realloc(fl->files, fl->len + 1, sizeof(MTY_FileDesc));

fl->files[fl->len].dir = is_dir;
fl->files[fl->len].name = MTY_Strdup(name);
fl->files[fl->len].path = MTY_Strdup(MTY_JoinPath(pathd, name));

struct stat st;
if (!is_dir && stat(name, &st) == 0)
fl->files[fl->len].size = st.st_size;

fl->len++;
}

Expand All @@ -245,6 +250,7 @@ MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
MTY_Sort(fl->files, fl->len, sizeof(MTY_FileDesc), file_compare);

mty_tlocal_set_mem(NULL, 0);
MTY_Free(tmp);

return fl;
}
17 changes: 10 additions & 7 deletions src/windows/filew.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ static int32_t file_compare(const void *p1, const void *p2)

MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
{
uint8_t tmp[4 * 1024];
mty_tlocal_set_mem(tmp, sizeof(tmp));
void *tmp = MTY_Alloc(0x1000, 1);
mty_tlocal_set_mem(tmp, 0x1000);

MTY_FileList *fl = MTY_Alloc(1, sizeof(MTY_FileList));
char *pathd = MTY_Strdup(path);
Expand All @@ -246,20 +246,23 @@ MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
HANDLE dir = FindFirstFile(pathw, &ent);
bool ok = dir != INVALID_HANDLE_VALUE;

wchar_t *filterw = filter ? MTY_MultiToWideD(filter) : NULL;

while (ok) {
wchar_t *namew = ent.cFileName;
char *name = MTY_WideToMultiD(namew);

bool is_dir = ent.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;

if (is_dir || wcsstr(namew, filterw ? filterw : L"")) {
if (is_dir || MTY_StrSearch(name, filter ? filter : "", "|")) {
fl->files = MTY_Realloc(fl->files, fl->len + 1, sizeof(MTY_FileDesc));

char *name = MTY_WideToMultiD(namew);
fl->files[fl->len].name = name;
fl->files[fl->len].path = MTY_Strdup(MTY_JoinPath(pathd, name));
fl->files[fl->len].dir = is_dir;
fl->files[fl->len].size = (uint64_t) ent.nFileSizeHigh << 32 | ent.nFileSizeLow;
fl->len++;

} else {
MTY_Free(name);
}

ok = FindNextFile(dir, &ent);
Expand All @@ -268,13 +271,13 @@ MTY_FileList *MTY_GetFileList(const char *path, const char *filter)
FindClose(dir);
}

MTY_Free(filterw);
MTY_Free(pathd);

if (fl->len > 0)
MTY_Sort(fl->files, fl->len, sizeof(MTY_FileDesc), file_compare);

mty_tlocal_set_mem(NULL, 0);
MTY_Free(tmp);

return fl;
}

0 comments on commit c933d8d

Please sign in to comment.