1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
3 /* eel-glib-extensions.c - implementation of new functions that conceptually
4                                 belong in glib. Perhaps some of these will be
5                                 actually rolled into glib someday.
6 
7    Copyright (C) 2000 Eazel, Inc.
8 
9    The Gnome Library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Library General Public License as
11    published by the Free Software Foundation; either version 2 of the
12    License, or (at your option) any later version.
13 
14    The Gnome Library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Library General Public License for more details.
18 
19    You should have received a copy of the GNU Library General Public
20    License along with the Gnome Library; see the file COPYING.LIB.  If not,
21    write to the Free Software Foundation, Inc., 51 Franklin Street - Suite 500,
22    Boston, MA 02110-1335, USA.
23 
24    Authors: John Sullivan <sullivan@eazel.com>
25 */
26 
27 #include <config.h>
28 #include "eel-glib-extensions.h"
29 
30 #include "eel-debug.h"
31 #include "eel-lib-self-check-functions.h"
32 #include "eel-string.h"
33 #include <glib-object.h>
34 #include <math.h>
35 #include <stdlib.h>
36 
37 /**
38  * eel_g_str_list_equal
39  *
40  * Compares two lists of C strings to see if they are equal.
41  * @list_a: First list.
42  * @list_b: Second list.
43  *
44  * Return value: TRUE if the lists contain the same strings.
45  **/
46 gboolean
eel_g_str_list_equal(GList * list_a,GList * list_b)47 eel_g_str_list_equal (GList *list_a, GList *list_b)
48 {
49 	GList *p, *q;
50 
51 	for (p = list_a, q = list_b; p != NULL && q != NULL; p = p->next, q = q->next) {
52 		if (g_strcmp0 (p->data, q->data) != 0) {
53 			return FALSE;
54 		}
55 	}
56 	return p == NULL && q == NULL;
57 }
58 
59 /**
60  * eel_g_str_list_copy
61  *
62  * @list: List of strings and/or NULLs to copy.
63  * Return value: Deep copy of @list.
64  **/
65 GList *
eel_g_str_list_copy(GList * list)66 eel_g_str_list_copy (GList *list)
67 {
68 	return g_list_copy_deep (list, (GCopyFunc) g_strdup, NULL);
69 }
70 
71 gboolean
eel_g_strv_equal(char ** a,char ** b)72 eel_g_strv_equal (char **a, char **b)
73 {
74 	int i;
75 
76 	if (g_strv_length (a) != g_strv_length (b)) {
77 		return FALSE;
78 	}
79 
80 	for (i = 0; a[i] != NULL; i++) {
81 		if (strcmp (a[i], b[i]) != 0) {
82 			return FALSE;
83 		}
84 	}
85 	return TRUE;
86 }
87 
88 static int
compare_pointers(gconstpointer pointer_1,gconstpointer pointer_2)89 compare_pointers (gconstpointer pointer_1, gconstpointer pointer_2)
90 {
91 	if ((const char *) pointer_1 < (const char *) pointer_2) {
92 		return -1;
93 	}
94 	if ((const char *) pointer_1 > (const char *) pointer_2) {
95 		return +1;
96 	}
97 	return 0;
98 }
99 
100 gboolean
eel_g_lists_sort_and_check_for_intersection(GList ** list_1,GList ** list_2)101 eel_g_lists_sort_and_check_for_intersection (GList **list_1,
102 					     GList **list_2)
103 
104 {
105 	GList *node_1, *node_2;
106 	int compare_result;
107 
108 	*list_1 = g_list_sort (*list_1, compare_pointers);
109 	*list_2 = g_list_sort (*list_2, compare_pointers);
110 
111 	node_1 = *list_1;
112 	node_2 = *list_2;
113 
114 	while (node_1 != NULL && node_2 != NULL) {
115 		compare_result = compare_pointers (node_1->data, node_2->data);
116 		if (compare_result == 0) {
117 			return TRUE;
118 		}
119 		if (compare_result <= 0) {
120 			node_1 = node_1->next;
121 		}
122 		if (compare_result >= 0) {
123 			node_2 = node_2->next;
124 		}
125 	}
126 
127 	return FALSE;
128 }
129 
130 
131 /**
132  * eel_g_list_partition
133  *
134  * Parition a list into two parts depending on whether the data
135  * elements satisfy a provided predicate. Order is preserved in both
136  * of the resulting lists, and the original list is consumed. A list
137  * of the items that satisfy the predicate is returned, and the list
138  * of items not satisfying the predicate is returned via the failed
139  * out argument.
140  *
141  * @list: List to partition.
142  * @predicate: Function to call on each element.
143  * @user_data: Data to pass to function.
144  * @failed: The GList * variable pointed to by this argument will be
145  * set to the list of elements for which the predicate returned
146  * false. */
147 
148 GList *
eel_g_list_partition(GList * list,EelPredicateFunction predicate,gpointer user_data,GList ** failed)149 eel_g_list_partition (GList *list,
150 			   EelPredicateFunction  predicate,
151 			   gpointer user_data,
152 			   GList **failed)
153 {
154 	GList *predicate_true;
155 	GList *predicate_false;
156 	GList *reverse;
157 	GList *p;
158 	GList *next;
159 
160 	predicate_true = NULL;
161 	predicate_false = NULL;
162 
163 	reverse = g_list_reverse (list);
164 
165 	for (p = reverse; p != NULL; p = next) {
166 		next = p->next;
167 
168 		if (next != NULL) {
169 			next->prev = NULL;
170 		}
171 
172 		if (predicate (p->data, user_data)) {
173 			p->next = predicate_true;
174  			if (predicate_true != NULL) {
175 				predicate_true->prev = p;
176 			}
177 			predicate_true = p;
178 		} else {
179 			p->next = predicate_false;
180  			if (predicate_false != NULL) {
181 				predicate_false->prev = p;
182 			}
183 			predicate_false = p;
184 		}
185 	}
186 
187 	*failed = predicate_false;
188 	return predicate_true;
189 }
190 
191 typedef struct {
192 	GList *keys;
193 	GList *values;
194 } FlattenedHashTable;
195 
196 static void
flatten_hash_table_element(gpointer key,gpointer value,gpointer callback_data)197 flatten_hash_table_element (gpointer key, gpointer value, gpointer callback_data)
198 {
199 	FlattenedHashTable *flattened_table;
200 
201 	flattened_table = callback_data;
202 	flattened_table->keys = g_list_prepend
203 		(flattened_table->keys, key);
204 	flattened_table->values = g_list_prepend
205 		(flattened_table->values, value);
206 }
207 
208 void
eel_g_hash_table_safe_for_each(GHashTable * hash_table,GHFunc callback,gpointer callback_data)209 eel_g_hash_table_safe_for_each (GHashTable *hash_table,
210 				GHFunc callback,
211 				gpointer callback_data)
212 {
213 	FlattenedHashTable flattened;
214 	GList *p, *q;
215 
216 	flattened.keys = NULL;
217 	flattened.values = NULL;
218 
219 	g_hash_table_foreach (hash_table,
220 			      flatten_hash_table_element,
221 			      &flattened);
222 
223 	for (p = flattened.keys, q = flattened.values;
224 	     p != NULL;
225 	     p = p->next, q = q->next) {
226 		(* callback) (p->data, q->data, callback_data);
227 	}
228 
229 	g_list_free (flattened.keys);
230 	g_list_free (flattened.values);
231 }
232 
233 /**
234  * eel_g_object_list_copy
235  *
236  * Copy the list of objects, ref'ing each one.
237  * @list: GList of objects.
238  **/
239 GList *
eel_g_object_list_copy(GList * list)240 eel_g_object_list_copy (GList *list)
241 {
242 	g_list_foreach (list, (GFunc) g_object_ref, NULL);
243 	return g_list_copy (list);
244 }
245 
246 #if !defined (EEL_OMIT_SELF_CHECK)
247 
248 static gboolean
eel_test_predicate(gpointer data,gpointer callback_data)249 eel_test_predicate (gpointer data,
250 		    gpointer callback_data)
251 {
252 	return g_ascii_strcasecmp (data, callback_data) <= 0;
253 }
254 
255 void
eel_self_check_glib_extensions(void)256 eel_self_check_glib_extensions (void)
257 {
258 	GList *compare_list_1;
259 	GList *compare_list_2;
260 	GList *compare_list_3;
261 	GList *compare_list_4;
262 	GList *compare_list_5;
263 	GList *list_to_partition;
264 	GList *expected_passed;
265 	GList *expected_failed;
266 	GList *actual_passed;
267 	GList *actual_failed;
268 
269 	/* eel_g_str_list_equal */
270 
271 	/* We g_strdup because identical string constants can be shared. */
272 
273 	compare_list_1 = NULL;
274 	compare_list_1 = g_list_append (compare_list_1, g_strdup ("Apple"));
275 	compare_list_1 = g_list_append (compare_list_1, g_strdup ("zebra"));
276 	compare_list_1 = g_list_append (compare_list_1, g_strdup ("!@#!@$#@$!"));
277 
278 	compare_list_2 = NULL;
279 	compare_list_2 = g_list_append (compare_list_2, g_strdup ("Apple"));
280 	compare_list_2 = g_list_append (compare_list_2, g_strdup ("zebra"));
281 	compare_list_2 = g_list_append (compare_list_2, g_strdup ("!@#!@$#@$!"));
282 
283 	compare_list_3 = NULL;
284 	compare_list_3 = g_list_append (compare_list_3, g_strdup ("Apple"));
285 	compare_list_3 = g_list_append (compare_list_3, g_strdup ("zebra"));
286 
287 	compare_list_4 = NULL;
288 	compare_list_4 = g_list_append (compare_list_4, g_strdup ("Apple"));
289 	compare_list_4 = g_list_append (compare_list_4, g_strdup ("zebra"));
290 	compare_list_4 = g_list_append (compare_list_4, g_strdup ("!@#!@$#@$!"));
291 	compare_list_4 = g_list_append (compare_list_4, g_strdup ("foobar"));
292 
293 	compare_list_5 = NULL;
294 	compare_list_5 = g_list_append (compare_list_5, g_strdup ("Apple"));
295 	compare_list_5 = g_list_append (compare_list_5, g_strdup ("zzzzzebraaaaaa"));
296 	compare_list_5 = g_list_append (compare_list_5, g_strdup ("!@#!@$#@$!"));
297 
298 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (compare_list_1, compare_list_2), TRUE);
299 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (compare_list_1, compare_list_3), FALSE);
300 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (compare_list_1, compare_list_4), FALSE);
301 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (compare_list_1, compare_list_5), FALSE);
302 
303 	g_list_free_full (compare_list_1, g_free);
304 	g_list_free_full (compare_list_2, g_free);
305 	g_list_free_full (compare_list_3, g_free);
306 	g_list_free_full (compare_list_4, g_free);
307 	g_list_free_full (compare_list_5, g_free);
308 
309 	/* eel_g_list_partition */
310 
311 	list_to_partition = NULL;
312 	list_to_partition = g_list_append (list_to_partition, (gpointer) "Cadillac");
313 	list_to_partition = g_list_append (list_to_partition, (gpointer) "Pontiac");
314 	list_to_partition = g_list_append (list_to_partition, (gpointer) "Ford");
315 	list_to_partition = g_list_append (list_to_partition, (gpointer) "Range Rover");
316 
317 	expected_passed = NULL;
318 	expected_passed = g_list_append (expected_passed, (gpointer) "Cadillac");
319 	expected_passed = g_list_append (expected_passed, (gpointer) "Ford");
320 
321 	expected_failed = NULL;
322 	expected_failed = g_list_append (expected_failed, (gpointer) "Pontiac");
323 	expected_failed = g_list_append (expected_failed, (gpointer) "Range Rover");
324 
325 	actual_passed = eel_g_list_partition (list_to_partition,
326 						   eel_test_predicate,
327 						   (gpointer) "m",
328 						   &actual_failed);
329 
330 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (expected_passed, actual_passed), TRUE);
331 	EEL_CHECK_BOOLEAN_RESULT (eel_g_str_list_equal (expected_failed, actual_failed), TRUE);
332 
333 	/* Don't free "list_to_partition", since it is consumed
334 	 * by eel_g_list_partition.
335 	 */
336 
337 	g_list_free (expected_passed);
338 	g_list_free (actual_passed);
339 	g_list_free (expected_failed);
340 	g_list_free (actual_failed);
341 }
342 
343 #endif /* !EEL_OMIT_SELF_CHECK */
344