forked from GNOME/rhythmbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrb-util.c
1275 lines (1134 loc) · 31.2 KB
/
rb-util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2003 Colin Walters <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/**
* SECTION:rb-util
* @short_description: assorted utility functions
*
* This is a dumping ground for utility functions that may or may not
* be generally useful in Rhythmbox or elsewhere. Things end up here
* if they're clever or if they're used all over the place.
*/
#include "config.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gobject/gvaluecollector.h>
#include <gio/gio.h>
#include "rb-util.h"
#include "rb-debug.h"
static GPrivate private_is_primary_thread;
/**
* rb_true_function: (skip)
* @dummy: unused
*
* Just returns %TRUE, useful as a callback function.
*
* Return value: %TRUE
*/
gboolean
rb_true_function (gpointer dummy)
{
return TRUE;
}
/**
* rb_false_function: (skip)
* @dummy: unused
*
* Just returns %FALSE, useful as a callback function.
*
* Return value: %FALSE
*/
gboolean
rb_false_function (gpointer dummy)
{
return FALSE;
}
/**
* rb_null_function: (skip)
* @dummy: unused
*
* Just returns NULL. Useful as a callback function.
*
* Return value: NULL
*/
gpointer
rb_null_function (gpointer dummy)
{
return NULL;
}
/**
* rb_copy_function: (skip)
* @data: generic argument
*
* Just returns its first argument. Useful as a callback function.
*
* Return value: @data
*/
gpointer
rb_copy_function (gpointer data)
{
return data;
}
/**
* rb_gvalue_compare: (skip)
* @a: left hand side
* @b: right hand size
*
* Compares @a and @b for sorting. @a and @b must contain the same value
* type for the comparison to be valid. Comparisons for some value types
* are not particularly useful.
*
* Return value: -1 if @a < @b, 0 if @a == @b, 1 if @a > @b
*/
int
rb_gvalue_compare (GValue *a, GValue *b)
{
int retval;
const char *stra, *strb;
if (G_VALUE_TYPE (a) != G_VALUE_TYPE (b))
return -1;
switch (G_VALUE_TYPE (a))
{
case G_TYPE_BOOLEAN:
if (g_value_get_int (a) < g_value_get_int (b))
retval = -1;
else if (g_value_get_int (a) == g_value_get_int (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_CHAR:
if (g_value_get_schar (a) < g_value_get_schar (b))
retval = -1;
else if (g_value_get_schar (a) == g_value_get_schar (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_UCHAR:
if (g_value_get_uchar (a) < g_value_get_uchar (b))
retval = -1;
else if (g_value_get_uchar (a) == g_value_get_uchar (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_INT:
if (g_value_get_int (a) < g_value_get_int (b))
retval = -1;
else if (g_value_get_int (a) == g_value_get_int (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_UINT:
if (g_value_get_uint (a) < g_value_get_uint (b))
retval = -1;
else if (g_value_get_uint (a) == g_value_get_uint (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_LONG:
if (g_value_get_long (a) < g_value_get_long (b))
retval = -1;
else if (g_value_get_long (a) == g_value_get_long (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_ULONG:
if (g_value_get_ulong (a) < g_value_get_ulong (b))
retval = -1;
else if (g_value_get_ulong (a) == g_value_get_ulong (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_INT64:
if (g_value_get_int64 (a) < g_value_get_int64 (b))
retval = -1;
else if (g_value_get_int64 (a) == g_value_get_int64 (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_UINT64:
if (g_value_get_uint64 (a) < g_value_get_uint64 (b))
retval = -1;
else if (g_value_get_uint64 (a) == g_value_get_uint64 (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_ENUM:
/* this is somewhat bogus. */
if (g_value_get_enum (a) < g_value_get_enum (b))
retval = -1;
else if (g_value_get_enum (a) == g_value_get_enum (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_FLAGS:
/* this is even more bogus. */
if (g_value_get_flags (a) < g_value_get_flags (b))
retval = -1;
else if (g_value_get_flags (a) == g_value_get_flags (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_FLOAT:
if (g_value_get_float (a) < g_value_get_float (b))
retval = -1;
else if (g_value_get_float (a) == g_value_get_float (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_DOUBLE:
if (g_value_get_double (a) < g_value_get_double (b))
retval = -1;
else if (g_value_get_double (a) == g_value_get_double (b))
retval = 0;
else
retval = 1;
break;
case G_TYPE_STRING:
stra = g_value_get_string (a);
strb = g_value_get_string (b);
if (stra == NULL) stra = "";
if (strb == NULL) strb = "";
retval = g_utf8_collate (stra, strb);
break;
case G_TYPE_POINTER:
retval = (g_value_get_pointer (a) != g_value_get_pointer (b));
break;
case G_TYPE_BOXED:
retval = (g_value_get_boxed (a) != g_value_get_boxed (b));
break;
case G_TYPE_OBJECT:
retval = (g_value_get_object (a) != g_value_get_object (b));
break;
default:
g_assert_not_reached ();
retval = 0;
break;
}
return retval;
}
/**
* rb_compare_gtimeval:
* @a: left hand side
* @b: right hand size
*
* Compares two #GTimeVal structures for sorting.
*
* Return value: -1 if @a < @b, 0 if @a == @b, 1 if @a > @b
*/
int
rb_compare_gtimeval (GTimeVal *a, GTimeVal *b)
{
if (a->tv_sec == b->tv_sec)
/* It's quite unlikely that microseconds are equal,
* so just ignore that case, we don't need a lot
* of precision.
*/
return a->tv_usec > b->tv_usec ? 1 : -1;
else if (a->tv_sec > b->tv_sec)
return 1;
else
return -1;
}
/* this is obsoleted by g_strcmp0, don't use it */
int
rb_safe_strcmp (const char *a,
const char *b)
{
return (!a && !b) ? 0 : (a && !b) || (!a && b) ? 1 : strcmp (a, b);
}
/**
* rb_is_main_thread:
*
* Checks if currently executing on the main thread.
*
* Return value: %TRUE if on the main thread
*/
gboolean
rb_is_main_thread (void)
{
return GPOINTER_TO_UINT(g_private_get (&private_is_primary_thread)) == 1;
}
static gboolean
purge_useless_threads (gpointer data)
{
g_thread_pool_stop_unused_threads ();
return TRUE;
}
static gboolean mutex_recurses;
/**
* rb_assert_locked: (skip)
* @mutex: a #GMutex
*
* Asserts that @mutex is currently locked. Does not work with all
* mutex implementations.
*/
void
rb_assert_locked (GMutex *mutex)
{
if (!mutex_recurses)
g_assert (!g_mutex_trylock (mutex));
}
/**
* rb_threads_init: (skip)
*
* Initializes various thread helpers. Must be called on startup.
*/
void
rb_threads_init (void)
{
GMutex m;
g_private_set (&private_is_primary_thread, GUINT_TO_POINTER (1));
g_mutex_init (&m);
g_mutex_lock (&m);
mutex_recurses = g_mutex_trylock (&m);
if (mutex_recurses)
g_mutex_unlock (&m);
g_mutex_unlock (&m);
rb_debug ("GMutex %s recursive", mutex_recurses ? "is" : "isn't");
/* purge useless thread-pool threads occasionally */
g_timeout_add_seconds (30, purge_useless_threads, NULL);
}
/**
* rb_string_split_words:
* @string: the string to split
*
* Splits @string on word boundaries using Unicode character definitions.
*
* Return value: (array zero-terminated=1) (transfer full): NULL-terminated array of strings
*/
gchar **
rb_string_split_words (const gchar *string)
{
/*return g_slist_prepend (NULL, g_strdup (string));*/
GSList *words, *current;
gunichar *unicode, *cur_write, *cur_read;
gchar **ret;
gchar *normalized;
gint i, wordcount = 1;
gboolean new_word = TRUE;
g_return_val_if_fail (string != NULL, NULL);
normalized = g_utf8_normalize(string, -1, G_NORMALIZE_DEFAULT);
cur_write = cur_read = unicode = g_utf8_to_ucs4_fast (normalized, -1, NULL);
/* we may fail here, we expect valid utf-8 */
g_return_val_if_fail (unicode != NULL, NULL);
words = g_slist_prepend (NULL, unicode);
/* now normalize this text */
while (*cur_read) {
switch (g_unichar_type (*cur_read)) {
case G_UNICODE_UNASSIGNED:
rb_debug ("unassigned unicode character type found");
/* fall through */
case G_UNICODE_CONTROL:
case G_UNICODE_FORMAT:
case G_UNICODE_PRIVATE_USE:
case G_UNICODE_SURROGATE:
case G_UNICODE_LINE_SEPARATOR:
case G_UNICODE_PARAGRAPH_SEPARATOR:
case G_UNICODE_SPACE_SEPARATOR:
/* remove these and start a new word */
if (!new_word) {
/* end current word if it isn't ended yet */
*cur_write++ = 0;
new_word = TRUE;
}
break;
case G_UNICODE_SPACING_MARK:
case G_UNICODE_ENCLOSING_MARK:
case G_UNICODE_NON_SPACING_MARK:
case G_UNICODE_CONNECT_PUNCTUATION:
case G_UNICODE_DASH_PUNCTUATION:
case G_UNICODE_CLOSE_PUNCTUATION:
case G_UNICODE_FINAL_PUNCTUATION:
case G_UNICODE_INITIAL_PUNCTUATION:
case G_UNICODE_OTHER_PUNCTUATION:
case G_UNICODE_OPEN_PUNCTUATION:
/* remove these */
/*break;*/
case G_UNICODE_LOWERCASE_LETTER:
case G_UNICODE_MODIFIER_LETTER:
case G_UNICODE_OTHER_LETTER:
case G_UNICODE_TITLECASE_LETTER:
case G_UNICODE_UPPERCASE_LETTER:
case G_UNICODE_DECIMAL_NUMBER:
case G_UNICODE_LETTER_NUMBER:
case G_UNICODE_OTHER_NUMBER:
case G_UNICODE_CURRENCY_SYMBOL:
case G_UNICODE_MODIFIER_SYMBOL:
case G_UNICODE_MATH_SYMBOL:
case G_UNICODE_OTHER_SYMBOL:
/* keep these unchanged */
*cur_write = *cur_read;
if (new_word) {
if (cur_write != unicode) {/* first insert has been done above */
words = g_slist_prepend (words, cur_write);
wordcount++;
}
new_word = FALSE;
}
cur_write++;
break;
default:
g_warning ("unknown unicode character type found");
break;
}
cur_read++;
}
if (!new_word) {
*cur_write++ = 0;
}
ret = g_new (gchar *, wordcount + 1);
current = words;
for (i = wordcount - 1; i >= 0; i--) {
ret[i] = g_ucs4_to_utf8 (current->data, -1, NULL, NULL, NULL);
current = g_slist_next (current);
}
ret[wordcount] = NULL;
g_slist_free (words);
g_free (unicode);
g_free (normalized);
return ret;
}
/**
* rb_search_fold:
* @original: the string to fold
*
* Returns a case-folded and punctuation-stripped version of @original, useful
* for performing text searches.
*
* Return value: (transfer full): case-folded string
*/
gchar*
rb_search_fold (const char *original)
{
GString *string;
gchar *normalized;
gunichar *unicode, *cur;
g_return_val_if_fail (original != NULL, NULL);
/* old behaviour is equivalent to: return g_utf8_casefold (original, -1); */
string = g_string_new (NULL);
normalized = g_utf8_normalize(original, -1, G_NORMALIZE_DEFAULT);
unicode = g_utf8_to_ucs4_fast (normalized, -1, NULL);
for (cur = unicode; *cur != 0; cur++) {
switch (g_unichar_type (*cur)) {
case G_UNICODE_SPACING_MARK:
case G_UNICODE_ENCLOSING_MARK:
case G_UNICODE_NON_SPACING_MARK:
case G_UNICODE_CONNECT_PUNCTUATION:
case G_UNICODE_DASH_PUNCTUATION:
case G_UNICODE_CLOSE_PUNCTUATION:
case G_UNICODE_FINAL_PUNCTUATION:
case G_UNICODE_INITIAL_PUNCTUATION:
case G_UNICODE_OTHER_PUNCTUATION:
case G_UNICODE_OPEN_PUNCTUATION:
/* remove these */
break;
case G_UNICODE_LOWERCASE_LETTER:
case G_UNICODE_MODIFIER_LETTER:
case G_UNICODE_OTHER_LETTER:
case G_UNICODE_TITLECASE_LETTER:
case G_UNICODE_UPPERCASE_LETTER:
/* convert to lower case */
*cur = g_unichar_tolower (*cur);
/* ... and fall through */\
case G_UNICODE_DECIMAL_NUMBER:
case G_UNICODE_LETTER_NUMBER:
case G_UNICODE_OTHER_NUMBER:
/* should be keep symbols? */
case G_UNICODE_CURRENCY_SYMBOL:
case G_UNICODE_MODIFIER_SYMBOL:
case G_UNICODE_MATH_SYMBOL:
case G_UNICODE_OTHER_SYMBOL:
g_string_append_unichar (string, *cur);
break;
case G_UNICODE_UNASSIGNED:
rb_debug ("unassigned unicode character type found");
/* fall through */
default:
/* leave these in */
g_string_append_unichar (string, *cur);
}
}
g_free (unicode);
g_free (normalized);
return g_string_free (string, FALSE);
}
/**
* rb_make_time_string:
* @seconds: time in seconds
*
* Constructs a string describing the specified time.
*
* Return value: (transfer full): time string
*/
char *
rb_make_time_string (guint nseconds)
{
int hours, minutes, seconds;
hours = nseconds / (60 * 60);
minutes = (nseconds - (hours * 60 * 60)) / 60;
seconds = nseconds % 60;
if (hours == 0)
return g_strdup_printf (_("%d:%02d"), minutes, seconds);
else
return g_strdup_printf (_("%d:%02d:%02d"), hours, minutes, seconds);
}
/**
* rb_make_duration_string:
* @duration: duration in seconds
*
* Constructs a string describing the specified duration. The string
* describes hours, minutes, and seconds, and its format is localised.
*
* Return value: (transfer full): duration string
*/
char *
rb_make_duration_string (guint duration)
{
if (duration == 0)
return g_strdup (_("Unknown"));
else
return rb_make_time_string (duration);
}
/**
* rb_make_elapsed_time_string:
* @elapsed: elapsed time (in seconds)
* @duration: duration (in seconds)
* @show_remaining: if %TRUE, show the remaining time, otherwise show elapsed time
*
* Constructs a string describing a playback position. The string describes hours,
* minutes, and seconds, and its format is localised. The string can describe either
* the elapsed time or the time remaining.
*
* Return value: (transfer full): elapsed/remaining time string
*/
char *
rb_make_elapsed_time_string (guint elapsed, guint duration, gboolean show_remaining)
{
int seconds = 0, minutes = 0, hours = 0;
int seconds2 = 0, minutes2 = 0, hours2 = 0;
if (duration == 0)
return rb_make_time_string (elapsed);
if (duration > 0) {
hours2 = duration / (60 * 60);
minutes2 = (duration - (hours2 * 60 * 60)) / 60;
seconds2 = duration % 60;
}
if (elapsed > 0) {
hours = elapsed / (60 * 60);
minutes = (elapsed - (hours * 60 * 60)) / 60;
seconds = elapsed % 60;
}
if (show_remaining) {
int remaining = duration - elapsed;
int remaining_hours = remaining / (60 * 60);
int remaining_minutes = (remaining - (remaining_hours * 60 * 60)) / 60;
/* remaining could conceivably be negative. This would
* be a bug, but the elapsed time will display right
* with the abs(). */
int remaining_seconds = abs (remaining % 60);
if (hours2 == 0)
return g_strdup_printf (_("%d:%02d of %d:%02d remaining"),
remaining_minutes, remaining_seconds,
minutes2, seconds2);
else
return g_strdup_printf (_("%d:%02d:%02d of %d:%02d:%02d remaining"),
remaining_hours, remaining_minutes, remaining_seconds,
hours2, minutes2, seconds2);
} else {
if (hours == 0 && hours2 == 0)
return g_strdup_printf (_("%d:%02d of %d:%02d"),
minutes, seconds,
minutes2, seconds2);
else
return g_strdup_printf (_("%d:%02d:%02d of %d:%02d:%02d"),
hours, minutes, seconds,
hours2, minutes2, seconds2);
}
}
/**
* rb_string_list_equal: (skip)
* @a: (element-type utf8): list of strings to compare
* @b: (element-type utf8): other list of strings to compare
*
* Checks if @a and @b contain exactly the same set of strings,
* regardless of order.
*
* Return value: %TRUE if the lists contain all the same strings
*/
gboolean
rb_string_list_equal (GList *a, GList *b)
{
GList *sorted_a_keys;
GList *sorted_b_keys;
GList *a_ptr, *b_ptr;
gboolean ret = TRUE;
if (a == b)
return TRUE;
if (g_list_length (a) != g_list_length (b))
return FALSE;
for (sorted_a_keys = NULL; a; a = a->next) {
sorted_a_keys = g_list_prepend (sorted_a_keys,
g_utf8_collate_key (a->data, -1));
}
for (sorted_b_keys = NULL; b; b = b->next) {
sorted_b_keys = g_list_prepend (sorted_b_keys,
g_utf8_collate_key (b->data, -1));
}
sorted_a_keys = g_list_sort (sorted_a_keys, (GCompareFunc) strcmp);
sorted_b_keys = g_list_sort (sorted_b_keys, (GCompareFunc) strcmp);
for (a_ptr = sorted_a_keys, b_ptr = sorted_b_keys;
a_ptr && b_ptr; a_ptr = a_ptr->next, b_ptr = b_ptr->next) {
if (strcmp (a_ptr->data, b_ptr->data)) {
ret = FALSE;
break;
}
}
g_list_foreach (sorted_a_keys, (GFunc) g_free, NULL);
g_list_foreach (sorted_b_keys, (GFunc) g_free, NULL);
g_list_free (sorted_a_keys);
g_list_free (sorted_b_keys);
return ret;
}
static void
list_copy_cb (const char *s, GList **list)
{
*list = g_list_prepend (*list, g_strdup (s));
}
/**
* rb_string_list_copy: (skip)
* @list: (element-type utf8): list of strings to copy
*
* Creates a deep copy of @list.
*
* Return value: (element-type utf8) (transfer full): copied list
*/
GList *
rb_string_list_copy (GList *list)
{
GList *copy = NULL;
if (list == NULL)
return NULL;
g_list_foreach (list, (GFunc)list_copy_cb, ©);
copy = g_list_reverse (copy);
return copy;
}
/**
* rb_string_list_contains: (skip)
* @list: (element-type utf8): list to check
* @s: string to check for
*
* Checks if @list contains the string @s.
*
* Return value: %TRUE if found
*/
gboolean
rb_string_list_contains (GList *list, const char *s)
{
GList *l;
for (l = list; l != NULL; l = g_list_next (l)) {
if (strcmp ((const char *)l->data, s) == 0)
return TRUE;
}
return FALSE;
}
/**
* rb_list_destroy_free: (skip)
* @list: list to destroy
* @destroyer: function to call to free elements of @list
*
* Calls @destroyer for each element in @list, then frees @list.
*/
void
rb_list_destroy_free (GList *list, GDestroyNotify destroyer)
{
g_list_foreach (list, (GFunc)destroyer, NULL);
g_list_free (list);
}
/**
* rb_list_deep_free: (skip)
* @list: (element-type any) (transfer full): list to free
*
* Frees each element of @list and @list itself.
*/
void
rb_list_deep_free (GList *list)
{
rb_list_destroy_free (list, (GDestroyNotify)g_free);
}
/**
* rb_slist_deep_free: (skip)
* @list: (element-type any) (transfer full): list to free
*
* Frees each element of @list and @list itself.
*/
void
rb_slist_deep_free (GSList *list)
{
g_slist_foreach (list, (GFunc)g_free, NULL);
g_slist_free (list);
}
static void
collate_keys_cb (gpointer key, gpointer value, GList **list)
{
*list = g_list_prepend (*list, key);
}
static void
collate_values_cb (gpointer key, gpointer value, GList **list)
{
*list = g_list_prepend (*list, value);
}
/**
* rb_collate_hash_table_keys: (skip)
* @table: #GHashTable to collate
*
* Returns a #GList containing all keys from @table. The keys are
* not copied.
*
* Return value: (element-type any) (transfer container): #GList of keys
*/
GList*
rb_collate_hash_table_keys (GHashTable *table)
{
GList *list = NULL;
g_hash_table_foreach (table, (GHFunc)collate_keys_cb, &list);
list = g_list_reverse (list);
return list;
}
/**
* rb_collate_hash_table_values: (skip)
* @table: #GHashTable to collate
*
* Returns a #GList containing all values from @table. The values are
* not copied.
*
* Return value: (element-type any) (transfer container): #GList of values
*/
GList*
rb_collate_hash_table_values (GHashTable *table)
{
GList *list = NULL;
g_hash_table_foreach (table, (GHFunc)collate_values_cb, &list);
list = g_list_reverse (list);
return list;
}
/**
* rb_uri_list_parse:
* @uri_list: string containing URIs to parse
*
* Converts a single string containing a list of URIs into
* a #GList of URI strings.
*
* Return value: (element-type utf8) (transfer full): #GList of URI strings
*/
GList *
rb_uri_list_parse (const char *uri_list)
{
const gchar *p, *q;
gchar *retval;
GList *result = NULL;
g_return_val_if_fail (uri_list != NULL, NULL);
p = uri_list;
while (p != NULL) {
while (g_ascii_isspace (*p))
p++;
q = p;
while ((*q != '\0')
&& (*q != '\n')
&& (*q != '\r'))
q++;
if (q > p) {
q--;
while (q > p
&& g_ascii_isspace (*q))
q--;
retval = g_malloc (q - p + 2);
strncpy (retval, p, q - p + 1);
retval[q - p + 1] = '\0';
if (retval != NULL)
result = g_list_prepend (result, retval);
}
p = strchr (p, '\n');
if (p != NULL)
p++;
}
return g_list_reverse (result);
}
/**
* rb_signal_accumulator_object_handled: (skip)
* @hint: a #GSignalInvocationHint
* @return_accu: holds the accumulated return value
* @handler_return: holds the return value to be accumulated
* @dummy: user data (unused)
*
* A #GSignalAccumulator that aborts the signal emission after the
* first handler to return a value, and returns the value returned by
* that handler. This is the opposite behaviour from what you get when
* no accumulator is specified, where the last signal handler wins.
*
* Return value: %FALSE to abort signal emission, %TRUE to continue
*/
gboolean
rb_signal_accumulator_object_handled (GSignalInvocationHint *hint,
GValue *return_accu,
const GValue *handler_return,
gpointer dummy)
{
if (handler_return == NULL ||
!G_VALUE_HOLDS_OBJECT (handler_return) ||
g_value_get_object (handler_return) == NULL)
return TRUE;
g_value_unset (return_accu);
g_value_init (return_accu, G_VALUE_TYPE (handler_return));
g_value_copy (handler_return, return_accu);
return FALSE;
}
/**
* rb_signal_accumulator_value_handled: (skip)
* @hint: a #GSignalInvocationHint
* @return_accu: holds the accumulated return value
* @handler_return: holds the return value to be accumulated
* @dummy: user data (unused)
*
* A #GSignalAccumulator that aborts the signal emission after the
* first handler to return a value, and returns the value returned by
* that handler. This is the opposite behaviour from what you get when
* no accumulator is specified, where the last signal handler wins.
*
* Return value: %FALSE to abort signal emission, %TRUE to continue
*/
gboolean
rb_signal_accumulator_value_handled (GSignalInvocationHint *hint,
GValue *return_accu,
const GValue *handler_return,
gpointer dummy)
{
if (handler_return == NULL ||
!G_VALUE_HOLDS (handler_return, G_TYPE_VALUE) ||
g_value_get_boxed (handler_return) == NULL)
return TRUE;
g_value_unset (return_accu);
g_value_init (return_accu, G_VALUE_TYPE (handler_return));
g_value_copy (handler_return, return_accu);
return FALSE;
}
/**
* rb_signal_accumulator_value_array: (skip)
* @hint: a #GSignalInvocationHint
* @return_accu: holds the accumulated return value
* @handler_return: holds the return value to be accumulated
* @dummy: user data (unused)
*
* A #GSignalAccumulator used to combine all returned values into
* a #GArray of #GValue instances.
*
* Return value: %FALSE to abort signal emission, %TRUE to continue
*/
gboolean
rb_signal_accumulator_value_array (GSignalInvocationHint *hint,
GValue *return_accu,
const GValue *handler_return,
gpointer dummy)
{
GArray *a;
GArray *b;
int i;
if (handler_return == NULL)
return TRUE;
a = g_array_sized_new (FALSE, TRUE, sizeof (GValue), 1);
g_array_set_clear_func (a, (GDestroyNotify) g_value_unset);
if (G_VALUE_HOLDS_BOXED (return_accu)) {
b = g_value_get_boxed (return_accu);
if (b != NULL) {
g_array_append_vals (a, b->data, b->len);
}
}
if (G_VALUE_HOLDS_BOXED (handler_return)) {
b = g_value_get_boxed (handler_return);
for (i=0; i < b->len; i++) {
a = g_array_append_val (a, g_array_index (b, GValue, i));
}
}
g_value_unset (return_accu);
g_value_init (return_accu, G_TYPE_ARRAY);
g_value_set_boxed (return_accu, a);
return TRUE;
}
/**
* rb_signal_accumulator_boolean_or: (skip)