2018-08-21 02:37:40 +01:00
|
|
|
/*
|
2024-03-27 07:00:53 +00:00
|
|
|
* Copyright (c) 2018-2024 CTCaer
|
2018-08-21 02:37:40 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-10-09 13:14:44 +01:00
|
|
|
#include "dirlist.h"
|
2020-06-14 14:45:45 +01:00
|
|
|
#include <libs/fatfs/ff.h>
|
|
|
|
#include <mem/heap.h>
|
|
|
|
#include <utils/types.h>
|
2018-08-21 02:37:40 +01:00
|
|
|
|
2024-10-09 13:14:44 +01:00
|
|
|
dirlist_t *dirlist(const char *directory, const char *pattern, bool includeHiddenFiles, bool parse_dirs)
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
|
|
|
int res = 0;
|
2024-10-09 13:14:44 +01:00
|
|
|
u32 k = 0;
|
2018-08-21 02:37:40 +01:00
|
|
|
DIR dir;
|
2019-06-30 01:40:37 +01:00
|
|
|
FILINFO fno;
|
2019-10-18 16:02:06 +01:00
|
|
|
|
2024-10-09 13:14:44 +01:00
|
|
|
dirlist_t *dir_entries = (dirlist_t *)malloc(sizeof(dirlist_t));
|
|
|
|
|
|
|
|
// Setup pointer tree.
|
|
|
|
for (u32 i = 0; i < DIR_MAX_ENTRIES; i++)
|
|
|
|
dir_entries->name[i] = &dir_entries->data[i * 256];
|
2018-08-21 02:37:40 +01:00
|
|
|
|
2018-09-24 21:47:35 +01:00
|
|
|
if (!pattern && !f_opendir(&dir, directory))
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
res = f_readdir(&dir, &fno);
|
|
|
|
if (res || !fno.fname[0])
|
|
|
|
break;
|
2020-06-13 16:16:29 +01:00
|
|
|
|
|
|
|
bool curr_parse = parse_dirs ? (fno.fattrib & AM_DIR) : !(fno.fattrib & AM_DIR);
|
|
|
|
|
|
|
|
if (curr_parse)
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
2020-06-13 16:16:29 +01:00
|
|
|
if ((fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID)))
|
|
|
|
{
|
2024-10-09 13:14:44 +01:00
|
|
|
strcpy(&dir_entries->data[k * 256], fno.fname);
|
|
|
|
if (++k >= DIR_MAX_ENTRIES)
|
2020-06-13 16:16:29 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f_closedir(&dir);
|
2018-09-24 21:47:35 +01:00
|
|
|
}
|
|
|
|
else if (pattern && !f_findfirst(&dir, &fno, directory, pattern) && fno.fname[0])
|
|
|
|
{
|
|
|
|
do
|
2018-09-18 21:47:37 +01:00
|
|
|
{
|
2018-11-09 00:09:14 +00:00
|
|
|
if (!(fno.fattrib & AM_DIR) && (fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID)))
|
2018-09-24 21:47:35 +01:00
|
|
|
{
|
2024-10-09 13:14:44 +01:00
|
|
|
strcpy(&dir_entries->data[k * 256], fno.fname);
|
|
|
|
if (++k >= DIR_MAX_ENTRIES)
|
2018-09-24 21:47:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
res = f_findnext(&dir, &fno);
|
|
|
|
} while (fno.fname[0] && !res);
|
|
|
|
f_closedir(&dir);
|
2018-08-21 02:37:40 +01:00
|
|
|
}
|
2018-09-24 21:47:35 +01:00
|
|
|
|
|
|
|
if (!k)
|
2018-08-23 02:37:02 +01:00
|
|
|
{
|
|
|
|
free(dir_entries);
|
2018-09-24 21:47:35 +01:00
|
|
|
|
2018-08-21 02:37:40 +01:00
|
|
|
return NULL;
|
2018-08-23 02:37:02 +01:00
|
|
|
}
|
2018-08-21 02:37:40 +01:00
|
|
|
|
2024-10-09 13:14:44 +01:00
|
|
|
// Terminate name list.
|
|
|
|
dir_entries->name[k] = NULL;
|
|
|
|
|
2019-03-30 17:10:49 +00:00
|
|
|
// Reorder ini files by ASCII ordering.
|
2024-10-09 13:14:44 +01:00
|
|
|
for (u32 i = 0; i < k - 1 ; i++)
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
2024-10-09 13:14:44 +01:00
|
|
|
for (u32 j = i + 1; j < k; j++)
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
2024-10-09 13:14:44 +01:00
|
|
|
if (strcmp(dir_entries->name[i], dir_entries->name[j]) > 0)
|
2018-08-21 02:37:40 +01:00
|
|
|
{
|
2024-10-09 13:14:44 +01:00
|
|
|
char *tmp = dir_entries->name[i];
|
|
|
|
dir_entries->name[i] = dir_entries->name[j];
|
|
|
|
dir_entries->name[j] = tmp;
|
2018-08-21 02:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dir_entries;
|
|
|
|
}
|