Browse Source

src/playlist: add sorting by file size logic

Add SORT_FILE_SIZE to the core playlist sorting functions.
File sizes are retrieved using vlc_stat and cached in the input item's extra info (category '.stat', name 'size') to improve performance on subsequent sorts.
3.0.x
unichronic 2 months ago
committed by Felix Paul Kühne
parent
commit
d112b45558
  1. 3
      include/vlc_playlist.h
  2. 61
      src/playlist/sort.c

3
include/vlc_playlist.h

@ -181,7 +181,8 @@ struct playlist_t
DEF( SORT_RATING )\
DEF( SORT_URI )\
DEF( SORT_DISC_NUMBER )\
DEF( SORT_DATE )
DEF( SORT_DATE )\
DEF( SORT_FILE_SIZE )
#define DEF( s ) s,
enum

61
src/playlist/sort.c

@ -31,6 +31,9 @@
#define VLC_INTERNAL_PLAYLIST_SORT_FUNCTIONS
#include "vlc_playlist.h"
#include "playlist_internal.h"
#include <vlc_url.h>
#include <vlc_fs.h>
#include <sys/stat.h>
/* General comparison functions */
@ -347,6 +350,64 @@ SORTFN( SORT_URI, first, second )
return i_ret;
}
SORTFN( SORT_FILE_SIZE, first, second )
{
input_item_t *p_input1 = first->p_input;
input_item_t *p_input2 = second->p_input;
int64_t i_size1 = 0;
int64_t i_size2 = 0;
/* we get the file size from metadata. If not present (first time added to playlist) then we store it*/
char *psz_size1 = input_item_GetInfo( p_input1, ".stat", "size" );
if( psz_size1 && *psz_size1 )
{
i_size1 = atoll( psz_size1 );
free( psz_size1 );
}
else
{
free( psz_size1 );
char *psz_url = input_item_GetURI( p_input1 );
char *psz_path = psz_url ? vlc_uri2path( psz_url ) : NULL;
struct stat result;
if( psz_path && vlc_stat( psz_path, &result ) == 0 )
{
i_size1 = result.st_size;
input_item_AddInfo( p_input1, ".stat", "size", "%lld", (long long)i_size1 );
}
free( psz_path );
free( psz_url );
}
char *psz_size2 = input_item_GetInfo( p_input2, ".stat", "size" );
if( psz_size2 && *psz_size2 )
{
i_size2 = atoll( psz_size2 );
free( psz_size2 );
}
else
{
free( psz_size2 );
char *psz_url = input_item_GetURI( p_input2 );
char *psz_path = psz_url ? vlc_uri2path( psz_url ) : NULL;
struct stat result;
if( psz_path && vlc_stat( psz_path, &result ) == 0 )
{
i_size2 = result.st_size;
input_item_AddInfo( p_input2, ".stat", "size", "%lld", (long long)i_size2 );
}
free( psz_path );
free( psz_url );
}
if( i_size1 > i_size2 )
return 1;
else if( i_size1 < i_size2 )
return -1;
else
return 0;
}
#undef SORTFN
/* Generate stubs around the proto_## sorting functions, ascending and

Loading…
Cancel
Save