1
0
Fork 0
mirror of https://github.com/CTCaer/hekate.git synced 2024-10-30 07:21:44 +00:00

bdk: utils: improve dirlist

Stop doing unnecessary copies during reordering and use pointers for that.
This commit is contained in:
CTCaer 2024-10-09 15:14:44 +03:00
parent f15af01727
commit d2fc6379c6
3 changed files with 34 additions and 26 deletions

View file

@ -17,21 +17,23 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include "dirlist.h"
#include <libs/fatfs/ff.h> #include <libs/fatfs/ff.h>
#include <mem/heap.h> #include <mem/heap.h>
#include <utils/types.h> #include <utils/types.h>
#define MAX_ENTRIES 64 dirlist_t *dirlist(const char *directory, const char *pattern, bool includeHiddenFiles, bool parse_dirs)
char *dirlist(const char *directory, const char *pattern, bool includeHiddenFiles, bool parse_dirs)
{ {
int res = 0; int res = 0;
u32 i = 0, j = 0, k = 0; u32 k = 0;
DIR dir; DIR dir;
FILINFO fno; FILINFO fno;
char *dir_entries = (char *)zalloc(MAX_ENTRIES * 256); dirlist_t *dir_entries = (dirlist_t *)malloc(sizeof(dirlist_t));
char *temp = (char *)zalloc(256);
// Setup pointer tree.
for (u32 i = 0; i < DIR_MAX_ENTRIES; i++)
dir_entries->name[i] = &dir_entries->data[i * 256];
if (!pattern && !f_opendir(&dir, directory)) if (!pattern && !f_opendir(&dir, directory))
{ {
@ -47,9 +49,8 @@ char *dirlist(const char *directory, const char *pattern, bool includeHiddenFile
{ {
if ((fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID))) if ((fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID)))
{ {
strcpy(dir_entries + (k * 256), fno.fname); strcpy(&dir_entries->data[k * 256], fno.fname);
k++; if (++k >= DIR_MAX_ENTRIES)
if (k > (MAX_ENTRIES - 1))
break; break;
} }
} }
@ -62,9 +63,8 @@ char *dirlist(const char *directory, const char *pattern, bool includeHiddenFile
{ {
if (!(fno.fattrib & AM_DIR) && (fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID))) if (!(fno.fattrib & AM_DIR) && (fno.fname[0] != '.') && (includeHiddenFiles || !(fno.fattrib & AM_HID)))
{ {
strcpy(dir_entries + (k * 256), fno.fname); strcpy(&dir_entries->data[k * 256], fno.fname);
k++; if (++k >= DIR_MAX_ENTRIES)
if (k > (MAX_ENTRIES - 1))
break; break;
} }
res = f_findnext(&dir, &fno); res = f_findnext(&dir, &fno);
@ -74,27 +74,27 @@ char *dirlist(const char *directory, const char *pattern, bool includeHiddenFile
if (!k) if (!k)
{ {
free(temp);
free(dir_entries); free(dir_entries);
return NULL; return NULL;
} }
// Terminate name list.
dir_entries->name[k] = NULL;
// Reorder ini files by ASCII ordering. // Reorder ini files by ASCII ordering.
for (i = 0; i < k - 1 ; i++) for (u32 i = 0; i < k - 1 ; i++)
{ {
for (j = i + 1; j < k; j++) for (u32 j = i + 1; j < k; j++)
{ {
if (strcmp(&dir_entries[i * 256], &dir_entries[j * 256]) > 0) if (strcmp(dir_entries->name[i], dir_entries->name[j]) > 0)
{ {
strcpy(temp, &dir_entries[i * 256]); char *tmp = dir_entries->name[i];
strcpy(&dir_entries[i * 256], &dir_entries[j * 256]); dir_entries->name[i] = dir_entries->name[j];
strcpy(&dir_entries[j * 256], temp); dir_entries->name[j] = tmp;
} }
} }
} }
free(temp);
return dir_entries; return dir_entries;
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018 CTCaer * Copyright (c) 2018-2024 CTCaer
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License, * under the terms and conditions of the GNU General Public License,
@ -16,4 +16,12 @@
#include <utils/types.h> #include <utils/types.h>
char *dirlist(const char *directory, const char *pattern, bool includeHiddenFiles, bool parse_dirs); #define DIR_MAX_ENTRIES 64
typedef struct _dirlist_t
{
char *name[DIR_MAX_ENTRIES];
char data[DIR_MAX_ENTRIES * 256];
} dirlist_t;
dirlist_t *dirlist(const char *directory, const char *pattern, bool includeHiddenFiles, bool parse_dirs);

View file

@ -62,7 +62,7 @@ int ini_parse(link_t *dst, const char *ini_path, bool is_dir)
ini_sec_t *csec = NULL; ini_sec_t *csec = NULL;
char *lbuf = NULL; char *lbuf = NULL;
char *filelist = NULL; dirlist_t *filelist = NULL;
char *filename = (char *)malloc(256); char *filename = (char *)malloc(256);
strcpy(filename, ini_path); strcpy(filename, ini_path);
@ -85,9 +85,9 @@ int ini_parse(link_t *dst, const char *ini_path, bool is_dir)
// Copy ini filename in path string. // Copy ini filename in path string.
if (is_dir) if (is_dir)
{ {
if (filelist[k * 256]) if (filelist->name[k])
{ {
strcpy(filename + pathlen, &filelist[k * 256]); strcpy(filename + pathlen, filelist->name[k]);
k++; k++;
} }
else else