Skip to content

Commit

Permalink
Refactor directory listing to greatly optimize sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Jun 23, 2012
1 parent 031e37d commit 14f7f64
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 125 deletions.
33 changes: 18 additions & 15 deletions driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,28 +484,31 @@ static void deinit_filter(void)
#endif

#ifdef HAVE_XML
static void deinit_shader_dir(void)
{
// It handles NULL, no worries :D
dir_list_free(g_extern.shader_dir.list);
g_extern.shader_dir.list = NULL;
g_extern.shader_dir.ptr = 0;
}

static void init_shader_dir(void)
{
if (!*g_settings.video.shader_dir)
return;

g_extern.shader_dir.elems = dir_list_new(g_settings.video.shader_dir, "shader", false);
g_extern.shader_dir.size = dir_list_size(g_extern.shader_dir.elems);
g_extern.shader_dir.ptr = 0;

dir_list_sort(g_extern.shader_dir.elems, false);
g_extern.shader_dir.list = dir_list_new(g_settings.video.shader_dir, "shader", false);
if (g_extern.shader_dir.list->size == 0)
{
deinit_shader_dir();
return;
}

for (unsigned i = 0; i < g_extern.shader_dir.size; i++)
RARCH_LOG("Found shader \"%s\"\n", g_extern.shader_dir.elems[i]);
}
g_extern.shader_dir.ptr = 0;
dir_list_sort(g_extern.shader_dir.list, false);

static void deinit_shader_dir(void)
{
// It handles NULL, no worries :D
dir_list_free(g_extern.shader_dir.elems);
g_extern.shader_dir.elems = NULL;
g_extern.shader_dir.size = 0;
g_extern.shader_dir.ptr = 0;
for (unsigned i = 0; i < g_extern.shader_dir.list->size; i++)
RARCH_LOG("Found shader \"%s\"\n", g_extern.shader_dir.list->elems[i].data);
}
#endif

Expand Down
31 changes: 24 additions & 7 deletions file.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,30 @@ void save_ram_file(const char *path, int type);

bool init_rom_file(enum rarch_game_type type);

// Returns a NULL-terminated list of files in a directory with full paths.
// If ext is NULL, any file will be picked.
// If non-NULL, only files with extension ext are added.
char **dir_list_new(const char *dir, const char *ext, bool include_dirs);
size_t dir_list_size(char * const *dir_list);
void dir_list_sort(char **dir_list, bool dir_first);
void dir_list_free(char **dir_list);
// Yep, this is C alright ;)
union string_list_elem_attr
{
bool b;
int i;
void *p;
};

struct string_list_elem
{
char *data;
union string_list_elem_attr attr;
};

struct string_list
{
struct string_list_elem *elems;
size_t size;
size_t cap;
};

struct string_list *dir_list_new(const char *dir, const char *ext, bool include_dirs);
void dir_list_sort(struct string_list *list, bool dir_first);
void dir_list_free(struct string_list *list);

bool path_is_directory(const char *path);
bool path_file_exists(const char *path);
Expand Down
Loading

0 comments on commit 14f7f64

Please sign in to comment.