]> git.sesse.net Git - vlc/blobdiff - modules/media_library/sql_media_library.c
Revert "mingw32: Look for contribs in /usr/win32 or /usr/win64"
[vlc] / modules / media_library / sql_media_library.c
index 2638f10e446a117ea015262cef78e58f763d9963..2b4a2b08e57f88f09b15eaf6c19ff45e73490112 100644 (file)
@@ -52,51 +52,55 @@ static const char* ppsz_VideoExtensions[] = { EXTENSIONS_VIDEO_CSV, NULL };
 static int load( vlc_object_t* );
 static void unload( vlc_object_t* );
 
-static int CreateInputItemFromMedia( media_library_t *p_ml,
-                              input_item_t **pp_item,
-                              ml_media_t *p_media );
+static int CreateInputItemFromMedia( input_item_t **pp_item,
+                                     ml_media_t *p_media );
 
 
 struct ml_table_elt
 {
     int column_id;
-    char column_name[];
+    const char* column_name;
 };
 
+static int compare_ml_elts( const void *a, const void *b )
+{
+    return strcmp( ( (struct ml_table_elt* )a )->column_name,
+            ( ( struct ml_table_elt* )b )->column_name );
+}
+
 static const struct ml_table_elt ml_table_map[]=
 {
-    { ML_ALBUM_COVER,  "album_cover" },
-    { ML_ALBUM_ID,         "album_id" },
-    { ML_ALBUM,                "album_title" },
-    { ML_COMMENT,          "comment" },
-    { ML_COVER,                "cover" },
-    { ML_DIRECTORY,        "directory_id" },
-    { ML_DIRECTORY,        "directory_id" },
-    { ML_DISC_NUMBER,  "disc" },
-    { ML_DURATION,         "duration" },
-    { ML_EXTRA,                "extra" },
-    { ML_FILESIZE,         "filesize" },
-    { ML_FIRST_PLAYED, "first_played" },
-    { ML_GENRE,                "genre" },
-    { ML_ID,           "id" },
-    { ML_IMPORT_TIME,  "import_time" },
-    { ML_LANGUAGE,         "language" },
-    { ML_LAST_PLAYED,  "last_played" },
-    { ML_LAST_SKIPPED, "last_skipped" },
-    { ML_ORIGINAL_TITLE,"original_title" },
-    { ML_PEOPLE_ID,        "people_id" },
-    { ML_PEOPLE,           "people_name" },
-    { ML_PEOPLE_ROLE,  "people_role" },
-    { ML_PLAYED_COUNT, "played_count" },
-    { ML_PREVIEW,          "preview" },
-    { ML_SCORE,                "score" },
-    { ML_SKIPPED_COUNT,        "skipped_count" },
-    { ML_TITLE,                "title" },
-    { ML_TRACK_NUMBER, "track" },
-    { ML_TYPE,         "type" },
-    { ML_URI,          "uri" },
-    { ML_VOTE,         "vote" },
-    { ML_YEAR,          "year" }
+    { ML_ALBUM_COVER,    "album_cover" },
+    { ML_ALBUM_ID,       "album_id" },
+    { ML_ALBUM,          "album_title" },
+    { ML_COMMENT,        "comment" },
+    { ML_COVER,          "cover" },
+    { ML_DIRECTORY,      "directory_id" },
+    { ML_DISC_NUMBER,    "disc" },
+    { ML_DURATION,       "duration" },
+    { ML_EXTRA,          "extra" },
+    { ML_FILESIZE,       "filesize" },
+    { ML_FIRST_PLAYED,   "first_played" },
+    { ML_GENRE,          "genre" },
+    { ML_ID,             "id" },
+    { ML_IMPORT_TIME,    "import_time" },
+    { ML_LANGUAGE,       "language" },
+    { ML_LAST_PLAYED,    "last_played" },
+    { ML_LAST_SKIPPED,   "last_skipped" },
+    { ML_ORIGINAL_TITLE, "original_title" },
+    { ML_PEOPLE_ID,      "people_id" },
+    { ML_PEOPLE,         "people_name" },
+    { ML_PEOPLE_ROLE,    "people_role" },
+    { ML_PLAYED_COUNT,   "played_count" },
+    { ML_PREVIEW,        "preview" },
+    { ML_SCORE,          "score" },
+    { ML_SKIPPED_COUNT,  "skipped_count" },
+    { ML_TITLE,          "title" },
+    { ML_TRACK_NUMBER,   "track" },
+    { ML_TYPE,           "type" },
+    { ML_URI,            "uri" },
+    { ML_VOTE,           "vote" },
+    { ML_YEAR,           "year" }
 };
 
 /*****************************************************************************
@@ -472,31 +476,23 @@ int SQLToMediaArray( media_library_t *p_ml, vlc_array_t *p_result_array,
     /* Analyze first row */
     int *indexes = ( int* ) calloc( i_cols + 1, sizeof( int ) );
     if( !indexes )
+    {
+        vlc_array_destroy( p_intermediate_array );
         return VLC_ENOMEM;
+    }
 
     const int count = sizeof( ml_table_map )/ sizeof( struct ml_table_elt );
     for( int col = 0; col < i_cols; col++ )
     {
-        //binary search
-        int low = 0, high = count - 1;
-        int answer = -1;
-        while( low <= high ) {
-            int mid = (low + high ) / 2;
-            char* mid_val = ml_table_map[mid].column_name;
-            int cmp = strcmp( mid_val, res( 0, col ) );
-            if( cmp > 0 )
-                low = mid + 1;
-            else if ( cmp < 0 )
-                high = mid - 1;
-            else
-            {
-                answer = mid;  break;
-            }
-        }
-        if( answer == -1 )
+        struct ml_table_elt key, *result = NULL;
+        key.column_name = res( 0, col );
+        result = bsearch( &key, ml_table_map, count,
+                sizeof( struct ml_table_elt ), compare_ml_elts );
+
+        if( !result )
             msg_Warn( p_ml, "unknown column: %s", res( 0, col ) );
         else
-            indexes[col] = ml_table_map[answer].column_id;
+            indexes[col] = result->column_id;
     }
 
     /* Read rows 1 to i_rows */
@@ -509,14 +505,16 @@ int SQLToMediaArray( media_library_t *p_ml, vlc_array_t *p_result_array,
         if( !p_media )
         {
             free( indexes );
-            return VLC_ENOMEM;
+            i_ret = VLC_ENOMEM;
+            goto quit_sqlmediaarray;
         }
         p_result = ( ml_result_t * ) calloc( 1, sizeof( ml_result_t ) );
         if( !p_result )
         {
             ml_gc_decref( p_media );
             free( indexes );
-            return VLC_ENOMEM;
+            i_ret = VLC_ENOMEM;
+            goto quit_sqlmediaarray;
         }
 
         char* psz_append_pname = NULL;
@@ -627,6 +625,7 @@ int SQLToMediaArray( media_library_t *p_ml, vlc_array_t *p_result_array,
         }
         else /* This is a repeat row and the people need to be put together */
         {
+            free( p_result );
             ml_LockMedia( p_append->value.p_media );
             if( psz_append_pname && i_append_pid && psz_append_prole )
                 ml_CreateAppendPersonAdv( &(p_append->value.p_media->p_people),
@@ -1128,7 +1127,7 @@ ml_media_t* GetMedia( media_library_t* p_ml, int id,
         else
         {
             ml_LockMedia( p_media );
-            if( p_media->b_sparse == true && select == ML_MEDIA )
+            if( p_media->b_sparse && select == ML_MEDIA )
                 reload = true;
             /* Utilise ML_MEDIA_EXTRA load? TODO */
             ml_UnlockMedia( p_media );
@@ -1176,7 +1175,7 @@ input_item_t* GetInputItemFromMedia( media_library_t *p_ml, int i_media )
         ml_media_t* p_media = media_New( p_ml, i_media, ML_MEDIA, true );
         if( p_media == NULL )
             return NULL;
-        CreateInputItemFromMedia( p_ml, &p_item, p_media );
+        CreateInputItemFromMedia( &p_item, p_media );
         watch_add_Item( p_ml, p_item, p_media );
         ml_gc_decref( p_media );
     }
@@ -1349,19 +1348,14 @@ void CopyMediaToInputItem( input_item_t *p_item, ml_media_t *p_media )
 
 /**
  * @brief Copy a ml_media_t to an input_item_t
- * @param p_ml The Media Library object
  * @param pp_item A pointer to a new input_item (return value)
  * @param p_media The media to copy as an input item
  * @note This function is threadsafe
  */
-static int CreateInputItemFromMedia( media_library_t *p_ml,
-                              input_item_t **pp_item,
-                              ml_media_t *p_media )
+static int CreateInputItemFromMedia( input_item_t **pp_item,
+                                     ml_media_t *p_media )
 {
-    playlist_t *p_pl = pl_Get( p_ml );
-    *pp_item = input_item_New( VLC_OBJECT( p_pl ),
-                               p_media->psz_uri,
-                               p_media->psz_title );
+    *pp_item = input_item_New( p_media->psz_uri, p_media->psz_title );
                                /* ITEM_TYPE_FILE ); */
     if( !*pp_item )
         return VLC_EGENERIC;