Skip to content

Commit

Permalink
Sort folders after files. Closes: #843.
Browse files Browse the repository at this point in the history
  • Loading branch information
jlindgren90 committed Nov 11, 2018
1 parent d14281a commit 275b050
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
13 changes: 9 additions & 4 deletions src/libaudcore/adder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ static void add_folder (const char * filename, Playlist::FilterFunc filter,

String error;
Index<String> files = VFSFile::read_folder (filename, error);
Index<String> folders;

if (error)
aud_ui_show_error (str_printf (_("Error reading %s:\n%s"), filename, (const char *) error));
Expand All @@ -327,7 +328,7 @@ static void add_folder (const char * filename, Playlist::FilterFunc filter,
// sort file list in natural order (must come after add_cuesheets)
files.sort (str_compare_encoded);

for (const char * file : files)
for (const String & file : files)
{
if (filter && ! filter (file, user))
{
Expand All @@ -340,16 +341,20 @@ static void add_folder (const char * filename, Playlist::FilterFunc filter,
VFSFileTest (VFS_IS_REGULAR | VFS_IS_SYMLINK | VFS_IS_DIR), error);

if (error)
AUDERR ("%s: %s\n", file, (const char *) error);
AUDERR ("%s: %s\n", (const char *) file, (const char *) error);

if (mode & VFS_IS_SYMLINK)
continue;

if (mode & VFS_IS_REGULAR)
add_file ({String (file)}, filter, user, result, true);
add_file ({file}, filter, user, result, true);
else if ((mode & VFS_IS_DIR) && aud_get_bool (nullptr, "recurse_folders"))
add_folder (file, filter, user, result, false);
folders.append (file);
}

// add folders after files
for (const String & folder : folders)
add_folder (folder, filter, user, result, false);
}

static void add_generic (PlaylistAddItem && item, Playlist::FilterFunc filter,
Expand Down
15 changes: 14 additions & 1 deletion src/libaudcore/playlist-utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ static const char * get_basename (const char * filename)
return slash ? slash + 1 : filename;
}

static int filename_compare_path (const char * a, const char * b)
{
int dirlen_a = get_basename (a) - a;
int dirlen_b = get_basename (b) - b;

// if one folder is a subfolder of the other, sort it last
if (dirlen_a != dirlen_b && memcmp (a, b, aud::min (dirlen_a, dirlen_b)) == 0)
return dirlen_a - dirlen_b;

// in all other cases, compare the entire paths
return str_compare_encoded (a, b);
}

static int filename_compare_basename (const char * a, const char * b)
{
return str_compare_encoded (get_basename (a), get_basename (b));
Expand Down Expand Up @@ -88,7 +101,7 @@ static int tuple_compare_comment (const Tuple & a, const Tuple & b)
{ return tuple_compare_string (a, b, Tuple::Comment); }

static const Playlist::StringCompareFunc filename_comparisons[] = {
str_compare_encoded, // path
filename_compare_path, // path
filename_compare_basename, // filename
nullptr, // title
nullptr, // album
Expand Down

0 comments on commit 275b050

Please sign in to comment.