[vlc-devel] [PATCH] 'meta_strcasecamp_title' function modified to compare two numericals across the resp. 'title' strings, not lexically but based on their values.

Sandesh C sandeshc13 at gmail.com
Fri May 8 14:10:51 CEST 2015


Tokenized 'title' strings. Comparision between the strings was implemented token by token.
Numerical value used for comparison incase of both tokens from resp strings are numerics, else lexicographic ordering using 'strcasecmp'.
Fixes bug #14548.
---
 src/playlist/sort.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 83 insertions(+), 1 deletion(-)

diff --git a/src/playlist/sort.c b/src/playlist/sort.c
index 2b91a90..9150f0f 100644
--- a/src/playlist/sort.c
+++ b/src/playlist/sort.c
@@ -33,6 +33,42 @@
 #include "playlist_internal.h"
 
 
+/**
+ * Tokenize a character array using delimiters
+ * Works similar to 'strtok' function from 'cstdlib'
+ * Ignores all sub-strings which can be generated from characters in 'delim'
+ * @param psz_title: charac. array to be tokenized
+ * @param psz_token_head: pointer to head of the current token
+ * @param psz_delim: list of delimiter ascii characters
+ * @retrun (next token) pointer to a dynamically allocated cstring
+ */
+char * meta_strtok( const char *psz_title, char **psz_token_head, const char *psz_delim )
+{
+    char *psz_end, *psz_ret;
+
+    if( psz_title )
+        *psz_token_head = psz_title;
+    else
+    {
+        while( **psz_token_head != '\0' && strchr( psz_delim, **psz_token_head ) == NULL )
+            *psz_token_head = *psz_token_head + 1;
+        while( **psz_token_head != '\0' && strchr( psz_delim, **psz_token_head ) != NULL )
+            *psz_token_head = *psz_token_head + 1;
+    }
+
+    if( **psz_token_head == '\0' )
+        return NULL;
+
+    psz_end = *psz_token_head;
+    while( *psz_end != '\0' && strchr( psz_delim, *psz_end ) == NULL )
+        psz_end++;
+
+    psz_ret = (char *) malloc( (psz_end - *psz_token_head + 1) * sizeof( char ) );
+    strncpy( psz_ret, *psz_token_head, psz_end - *psz_token_head );
+    psz_ret[ psz_end - *psz_token_head ] = '\0';
+    return psz_ret;
+}
+
 /* General comparison functions */
 /**
  * Compare two items using their title or name
@@ -47,8 +83,54 @@ static inline int meta_strcasecmp_title( const playlist_item_t *first,
     char *psz_first = input_item_GetTitleFbName( first->p_input );
     char *psz_second = input_item_GetTitleFbName( second->p_input );
 
+    char *psz_head_first = NULL, *psz_head_second = NULL;
+
     if( psz_first && psz_second )
-        i_ret = strcasecmp( psz_first, psz_second );
+    {
+        /* Get the first token for each of the 'title' */
+        char *psz_token_first = meta_strtok( psz_first, &psz_head_first, " ,/-" );
+        char *psz_token_second = meta_strtok( psz_second, &psz_head_second, " ,/-" );
+
+        do
+        {
+            if( !psz_token_first && psz_token_second )
+                i_ret = 1;
+            else if( psz_token_first && !psz_token_second )
+                i_ret = -1;
+            else
+            {
+                i_ret = strcasecmp( psz_token_first, psz_token_second );
+                if ( !i_ret )
+                {
+                    free( psz_token_first );
+                    free( psz_token_second );
+
+                    /* Get the next token(s) */
+                    psz_token_first = meta_strtok( NULL, &psz_head_first, " ,/-" );
+                    psz_token_second = meta_strtok( NULL, &psz_head_second, " ,/-" );
+                }
+                else
+                {
+                    /* Check if both the tokens are 'numeric' */
+                    char psz_buffer_first[10], psz_buffer_second[10];
+                    sprintf( psz_buffer_first, "%d", atoi( psz_token_first ) );
+                    sprintf( psz_buffer_second, "%d", atoi( psz_token_second ) );
+
+                    if( strcmp( psz_buffer_first, psz_token_first ) == 0 && \
+                            strcmp( psz_buffer_second, psz_token_second ) == 0 )
+                    {
+                        /* Both tokens are numeric */
+                        i_ret = atoi( psz_token_first ) - atoi( psz_token_second );
+                    }
+
+                    free( psz_token_first );
+                    free( psz_token_second );
+                    break;
+                }
+            }
+        }
+        while( psz_token_first && psz_token_second );
+    }
     else if( !psz_first && psz_second )
         i_ret = 1;
     else if( psz_first && !psz_second )
-- 
1.9.1




More information about the vlc-devel mailing list