1 /* GStreamer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
20 /**
21 * SECTION:gstfilter
22 * @short_description: A utility function to filter GLists.
23 *
24 * <example>
25 * <title>Filtering a list</title>
26 * <programlisting>
27 * GList *node;
28 * GstObject *result = NULL;
29 *
30 * node = gst_filter_run (list, (GstFilterFunc) my_filter, TRUE, NULL);
31 * if (node) {
32 * result = GST_OBJECT (node->data);
33 * gst_object_ref (result);
34 * g_list_free (node);
35 * }
36 * </programlisting>
37 * </example>
38 */
39 #include "gst_private.h"
40 #include <gst/gstfilter.h>
42 /**
43 * gst_filter_run:
44 * @list: a linked list
45 * @func: (scope call): the function to execute for each item
46 * @first: flag to stop execution after a successful item
47 * @user_data: (closure): user data
48 *
49 * Iterates over the elements in @list, calling @func with the
50 * list item data for each item. If @func returns TRUE, @data is
51 * prepended to the list of results returned. If @first is true,
52 * the search is halted after the first result is found.
53 *
54 * Since gst_filter_run() knows nothing about the type of @data, no
55 * reference will be taken (if @data refers to an object) and no copy of
56 * @data wil be made in any other way when prepending @data to the list of
57 * results.
58 *
59 * Returns: (transfer container): the list of results. Free with g_list_free()
60 * when no longer needed (the data contained in the list is a flat copy
61 * and does need to be unreferenced or freed).
62 */
63 #ifndef GST_REMOVE_DEPRECATED
64 #ifdef GST_DISABLE_DEPRECATED
65 typedef gboolean (*GstFilterFunc) (gpointer obj, gpointer user_data);
66 GList *gst_filter_run (const GList * list, GstFilterFunc func, gboolean first,
67 gpointer user_data);
68 #endif
69 GList *
70 gst_filter_run (const GList * list, GstFilterFunc func, gboolean first,
71 gpointer user_data)
72 {
73 const GList *walk = list;
74 GList *result = NULL;
76 while (walk) {
77 gboolean res = TRUE;
78 gpointer data = walk->data;
80 walk = g_list_next (walk);
82 if (func)
83 res = func (data, user_data);
85 if (res) {
86 result = g_list_prepend (result, data);
88 if (first)
89 break;
90 }
91 }
93 return result;
94 }
95 #endif /* GST_REMOVE_DEPRECATED */