Skip to content

Commit

Permalink
When adding a directory to the playlist, group the files by their own…
Browse files Browse the repository at this point in the history
…ing directory, and sort each group of files alphabetically (strcasecmp).
  • Loading branch information
mywave82 committed Jul 6, 2024
1 parent 88730b1 commit 9f19a19
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
53 changes: 53 additions & 0 deletions filesel/modlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,66 @@ static int mlecmp (const void *a, const void *b)
return strcasecmp(n1, n2);
}

static int mlecmp_filesonly_groupdir (const void *a, const void *b)
{
int _1 = *(int *)a;
int _2 = *(int *)b;
const struct modlistentry *e1 = &sorting->files[_1];
const struct modlistentry *e2 = &sorting->files[_2];

int i1 = mlecmp_score (e1);
int i2 = mlecmp_score (e2);

const char *n1, *n2;

int retval;

if (i1 != i2)
{
return i2 - i1;
}

if (e1->flags & MODLIST_FLAG_DRV)
{
return 0;
}
if (!e1->file->parent)
{
return 0;
}

retval = (int)((int32_t)e1->file->parent->dirdb_ref - (int32_t)e2->file->parent->dirdb_ref);
if (retval)
{
return retval;
}

dirdbGetName_internalstr (e1->file->dirdb_ref, &n1);
dirdbGetName_internalstr (e2->file->dirdb_ref, &n2);

return strcasecmp(n1, n2);
}

void modlist_sort (struct modlist *modlist)
{
sorting = modlist; /* dirty HACK that is not thread-safe / reentrant what so ever */
qsort(modlist->sortindex, modlist->num, sizeof(modlist->sortindex[0]), mlecmp);
sorting = 0;
}

void modlist_subsort_filesonly_groupdir (struct modlist *modlist, unsigned int pos, unsigned int length)
{
if ((pos >= modlist->num) ||
(length > modlist->num) ||
((pos + length) > modlist->num))
{
return;
}
sorting = modlist; /* dirty HACK that is not thread-safe / reentrant what so ever */
qsort(modlist->sortindex + pos, length, sizeof(modlist->sortindex[0]), mlecmp_filesonly_groupdir);
sorting = 0;
}

struct modlist *modlist_create (void)
{
/* TODO ARCS */
Expand Down
1 change: 1 addition & 0 deletions filesel/modlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct dmDrive;
struct modlist *modlist_create(void);
void modlist_free(struct modlist *modlist);
void modlist_sort(struct modlist *modlist);
void modlist_subsort_filesonly_groupdir (struct modlist *modlist, unsigned int pos, unsigned int length); /* sorts a slice of the list */
void modlist_append(struct modlist *modlist, struct modlistentry *entry);
void modlist_append_dir (struct modlist *modlist, struct ocpdir_t *dir);
void modlist_append_dotdot (struct modlist *modlist, struct ocpdir_t *dir);
Expand Down
16 changes: 14 additions & 2 deletions filesel/pfilesel.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ int fsReadDir (struct modlist *ml, struct ocpdir_t *dir, const char *mask, unsig
{
struct fsReadDir_token_t token;
ocpdirhandle_pt dh;
unsigned int prenum = 0;

if (opt & RD_SUBSORT)
{
prenum = ml->num;
}

if (opt & RD_PUTDRIVES)
{
Expand All @@ -335,7 +341,7 @@ int fsReadDir (struct modlist *ml, struct ocpdir_t *dir, const char *mask, unsig
#else
token.mask = (char *)mask;
#endif
token.opt = opt;
token.opt = opt & ~(RD_SUBSORT);

if ((opt & RD_PUTRSUBS) && dir->readflatdir_start)
{
Expand Down Expand Up @@ -371,6 +377,12 @@ int fsReadDir (struct modlist *ml, struct ocpdir_t *dir, const char *mask, unsig
#ifndef FNM_CASEFOLD
free (token.mask);
#endif

if (opt & RD_SUBSORT)
{
modlist_subsort_filesonly_groupdir (ml, prenum, ml->num - prenum);
}

return 1;
}

Expand Down Expand Up @@ -3726,7 +3738,7 @@ signed int fsFileSelect(void)
} else {
if (m->dir)
{
if (!(fsReadDir (playlist, m->dir, curmask, RD_PUTRSUBS | RD_ISMODONLY)))
if (!(fsReadDir (playlist, m->dir, curmask, RD_PUTRSUBS | RD_ISMODONLY | RD_SUBSORT)))
{
return -1;
}
Expand Down
1 change: 1 addition & 0 deletions filesel/pfilesel.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void plFindInterface(struct moduletype modtype, const struct interfacestruct **i
#define RD_PUTDRIVES 8
#define RD_PUTRSUBS 16
#define RD_ISMODONLY 32
#define RD_SUBSORT 64

int fsMatchFileName12(const char *a, const char *b);

Expand Down

0 comments on commit 9f19a19

Please sign in to comment.