summaryrefslogtreecommitdiff
path: root/libdevkit-power/egg-obj-list.c
blob: 6e3fd25151d93bf42337195df7fc0476e64be4e2 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 of the License, or
 * (at your option) any later 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <glib/gi18n.h>
#include <glib.h>

#include "egg-debug.h"
#include "egg-obj-list.h"

#define EGG_OBJ_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_OBJ_LIST, EggObjListPrivate))

struct EggObjListPrivate
{
	EggObjListNewFunc	 func_new;
	EggObjListCopyFunc	 func_copy;
	EggObjListFreeFunc	 func_free;
	EggObjListCompareFunc	 func_compare;
	EggObjListToStringFunc	 func_to_string;
	EggObjListFromStringFunc func_from_string;
	GPtrArray		*array;
};

G_DEFINE_TYPE (EggObjList, egg_obj_list, G_TYPE_OBJECT)

/**
 * egg_obj_list_set_new:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a creation func
 **/
void
egg_obj_list_set_new (EggObjList *list, EggObjListNewFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_new = func;
}

/**
 * egg_obj_list_set_copy:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a copy func
 **/
void
egg_obj_list_set_copy (EggObjList *list, EggObjListCopyFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_copy = func;
}

/**
 * egg_obj_list_set_free:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a free func
 **/
void
egg_obj_list_set_free (EggObjList *list, EggObjListFreeFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_free = func;
}

/**
 * egg_obj_list_set_compare:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a compare func
 **/
void
egg_obj_list_set_compare (EggObjList *list, EggObjListCompareFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_compare = func;
}

/**
 * egg_obj_list_set_to_string:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a to string func
 **/
void
egg_obj_list_set_to_string (EggObjList *list, EggObjListToStringFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_to_string = func;
}

/**
 * egg_obj_list_set_from_string:
 * @list: a valid #EggObjList instance
 * @func: typedef'd function
 *
 * Adds a from string func
 **/
void
egg_obj_list_set_from_string (EggObjList *list, EggObjListFromStringFunc func)
{
	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	list->priv->func_from_string = func;
}

/**
 * egg_obj_list_clear:
 * @list: a valid #EggObjList instance
 *
 * Clears the package list
 **/
void
egg_obj_list_clear (EggObjList *list)
{
	GPtrArray *array;
	EggObjListFreeFunc func_free;

	g_return_if_fail (EGG_IS_OBJ_LIST (list));

	array = list->priv->array;
	func_free = list->priv->func_free;
	if (func_free != NULL)
		g_ptr_array_foreach (array, (GFunc) func_free, NULL);
	g_ptr_array_remove_range (array, 0, array->len);

	list->len = 0;
}

/**
 * egg_obj_list_print:
 * @list: a valid #EggObjList instance
 *
 * Prints the package list
 **/
void
egg_obj_list_print (EggObjList *list)
{
	guint i;
	gpointer obj;
	GPtrArray *array;
	gchar *text;
	EggObjListToStringFunc func_to_string;

	g_return_if_fail (list->priv->func_to_string != NULL);
	g_return_if_fail (EGG_IS_OBJ_LIST (list));

	array = list->priv->array;
	func_to_string = list->priv->func_to_string;
	for (i=0; i<array->len; i++) {
		obj = g_ptr_array_index (array, i);
		text = func_to_string (obj);
		g_print ("(%i)\t%s\n", i, text);
		g_free (text);
	}
}

/**
 * egg_obj_list_to_string:
 * @list: a valid #EggObjList instance
 *
 * Converts the list to a newline delimited string
 **/
gchar *
egg_obj_list_to_string (EggObjList *list)
{
	guint i;
	gpointer obj;
	GPtrArray *array;
	gchar *text;
	EggObjListToStringFunc func_to_string;
	GString *string;

	g_return_val_if_fail (list->priv->func_to_string != NULL, NULL);
	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);

	array = list->priv->array;
	func_to_string = list->priv->func_to_string;
	string = g_string_new ("");
	for (i=0; i<array->len; i++) {
		obj = g_ptr_array_index (array, i);
		text = func_to_string (obj);
		g_string_append_printf (string, "%s\n", text);
		g_free (text);
	}
	/* remove trailing newline */
	if (string->len != 0)
		g_string_set_size (string, string->len-1);

	return g_string_free (string, FALSE);
}

/**
 * egg_obj_list_add:
 * @list: a valid #EggObjList instance
 * @obj: a valid #gpointer object
 *
 * Adds a copy of the object to the list
 **/
void
egg_obj_list_add (EggObjList *list, gconstpointer obj)
{
	gpointer obj_new;

	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	g_return_if_fail (obj != NULL);
	g_return_if_fail (list->priv->func_copy != NULL);

	/* TODO: are we already in the list? */
	obj_new = list->priv->func_copy (obj);
	g_ptr_array_add (list->priv->array, obj_new);
	list->len = list->priv->array->len;
}

/**
 * egg_obj_list_add_list:
 *
 * Makes a deep copy of the list
 **/
void
egg_obj_list_add_list (EggObjList *list, const EggObjList *data)
{
	guint i;
	gconstpointer obj;

	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	g_return_if_fail (EGG_IS_OBJ_LIST (data));

	/* add data items to list */
	for (i=0; i < data->len; i++) {
		obj = egg_obj_list_index (data, i);
		egg_obj_list_add (list, obj);
	}
}

/**
 * egg_obj_list_remove_list:
 *
 * Makes a deep copy of the list
 **/
void
egg_obj_list_remove_list (EggObjList *list, const EggObjList *data)
{
	guint i;
	gconstpointer obj;

	g_return_if_fail (EGG_IS_OBJ_LIST (list));
	g_return_if_fail (EGG_IS_OBJ_LIST (data));

	/* remove data items from list */
	for (i=0; i < data->len; i++) {
		obj = egg_obj_list_index (data, i);
		egg_obj_list_remove (list, obj);
	}
}

/**
 * egg_obj_list_find_obj:
 * @list: a valid #EggObjList instance
 * @obj: a valid #gpointer object
 *
 * Return value: the object
 *
 * Removes an item from a list
 **/
static gboolean
egg_obj_list_obj_equal (EggObjList *list, gconstpointer obj1, gconstpointer obj2)
{
	EggObjListCompareFunc func_compare;

	/* two less pointer deferences... */
	func_compare = list->priv->func_compare;

	/* trivial case */
	if (func_compare == NULL)
		return obj1 == obj2;

	/* use helper function */
	return func_compare (obj1, obj2) == 0;
}

/**
 * egg_obj_list_remove_duplicate:
 *
 * Removes duplicate entries
 **/
void
egg_obj_list_remove_duplicate (EggObjList *list)
{
	guint i, j;
	gconstpointer obj1;
	gconstpointer obj2;

	for (i=0; i<list->len; i++) {
		obj1 = egg_obj_list_index (list, i);
		for (j=0; j<list->len; j++) {
			if (i == j)
				break;
			obj2 = egg_obj_list_index (list, j);
			if (egg_obj_list_obj_equal (list, obj1, obj2))
				egg_obj_list_remove_index (list, i);
		}
	}
}

/**
 * egg_obj_list_find_obj:
 * @list: a valid #EggObjList instance
 * @obj: a valid #gpointer object
 *
 * Return value: the object
 *
 * Removes an item from a list
 **/
static gpointer
egg_obj_list_find_obj (EggObjList *list, gconstpointer obj)
{
	guint i;
	gconstpointer obj_tmp;
	EggObjListCompareFunc func_compare;

	/* the pointers point to the same thing */
	func_compare = list->priv->func_compare;
	if (func_compare == NULL)
		return (gpointer) obj;

	/* remove data items from list */
	for (i=0; i < list->len; i++) {
		obj_tmp = egg_obj_list_index (list, i);
		if (func_compare (obj_tmp, obj) == 0)
			return (gpointer) obj_tmp;
	}

	/* nothing found */
	return NULL;
}

/**
 * egg_obj_list_remove:
 * @list: a valid #EggObjList instance
 * @obj: a valid #gpointer object
 *
 * Return value: TRUE is we removed something
 *
 * Removes all the items from a list matching obj
 **/
gboolean
egg_obj_list_remove (EggObjList *list, gconstpointer obj)
{
	gboolean ret;
	gpointer obj_new;
	gboolean found = FALSE;

	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
	g_return_val_if_fail (obj != NULL, FALSE);
	g_return_val_if_fail (list->priv->func_free != NULL, FALSE);

	do {
		/* get the object */
		obj_new = egg_obj_list_find_obj (list, obj);
		if (obj_new == NULL)
			break;

		/* try to remove */
		ret = g_ptr_array_remove (list->priv->array, obj_new);

		/* no compare function, and pointer not found */
		if (!ret)
			break;

		found = TRUE;
		list->priv->func_free (obj_new);
		list->len = list->priv->array->len;
	} while (ret);

	return found;
}

/**
 * egg_obj_list_remove_index:
 * @list: a valid #EggObjList instance
 * @index: the number to remove
 *
 * Return value: TRUE is we removed something
 *
 * Removes an item from a list
 **/
gboolean
egg_obj_list_remove_index (EggObjList *list, guint index)
{
	gpointer obj;

	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
	g_return_val_if_fail (list->priv->func_free != NULL, FALSE);

	/* get the object */
	obj = g_ptr_array_remove_index (list->priv->array, index);
	if (obj == NULL)
		return FALSE;
	list->priv->func_free (obj);
	list->len = list->priv->array->len;
	return TRUE;
}

/**
 * egg_obj_list_to_file:
 * @list: a valid #EggObjList instance
 * @filename: a filename
 *
 * Saves a copy of the list to a file
 **/
gboolean
egg_obj_list_to_file (EggObjList *list, const gchar *filename)
{
	guint i;
	gconstpointer obj;
	gchar *part;
	GString *string;
	gboolean ret = TRUE;
	GError *error = NULL;
	EggObjListFreeFunc func_free;
	EggObjListToStringFunc func_to_string;

	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
	g_return_val_if_fail (list->priv->func_to_string != NULL, FALSE);
	g_return_val_if_fail (list->priv->func_free != NULL, FALSE);

	func_free = list->priv->func_free;
	func_to_string = list->priv->func_to_string;

	/* generate data */
	string = g_string_new ("");
	for (i=0; i<list->len; i++) {
		obj = egg_obj_list_index (list, i);
		part = func_to_string (obj);
		if (part == NULL) {
			ret = FALSE;
			break;
		}
		g_string_append_printf (string, "%s\n", part);
		g_free (part);
	}
	part = g_string_free (string, FALSE);

	/* we failed to convert to string */
	if (!ret) {
		egg_warning ("failed to convert");
		goto out;
	}

	/* save to disk */
	ret = g_file_set_contents (filename, part, -1, &error);
	if (!ret) {
		egg_warning ("failed to set data: %s", error->message);
		g_error_free (error);
		goto out;
	}
	egg_debug ("saved %s", filename);

out:
	g_free (part);
	return ret;
}

/**
 * egg_obj_list_from_file:
 * @list: a valid #EggObjList instance
 * @filename: a filename
 *
 * Appends the list from a file
 **/
gboolean
egg_obj_list_from_file (EggObjList *list, const gchar *filename)
{
	gboolean ret;
	GError *error = NULL;
	gchar *data = NULL;
	gchar **parts = NULL;
	guint i;
	guint length;
	gpointer obj;
	EggObjListFreeFunc func_free;
	EggObjListFromStringFunc func_from_string;

	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
	g_return_val_if_fail (list->priv->func_from_string != NULL, FALSE);
	g_return_val_if_fail (list->priv->func_free != NULL, FALSE);

	func_free = list->priv->func_free;
	func_from_string = list->priv->func_from_string;

	/* do we exist */
	ret = g_file_test (filename, G_FILE_TEST_EXISTS);
	if (!ret) {
		egg_debug ("failed to get data from %s as file does not exist", filename);
		goto out;
	}

	/* get contents */
	ret = g_file_get_contents (filename, &data, NULL, &error);
	if (!ret) {
		egg_warning ("failed to get data: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* split by line ending */
	parts = g_strsplit (data, "\n", 0);
	length = g_strv_length (parts);
	if (length == 0) {
		egg_debug ("no data in %s", filename);
		goto out;
	}

	/* add valid entries */
	egg_debug ("loading %i items of data from %s", length, filename);
	for (i=0; i<length-1; i++) {
		obj = func_from_string (parts[i]);
		if (obj != NULL)
			egg_obj_list_add (list, obj);
		func_free (obj);
	}

out:
	g_strfreev (parts);
	g_free (data);

	return ret;
}

/**
 * egg_obj_list_index:
 * @list: a valid #EggObjList instance
 * @index: the element to return
 *
 * Gets an object from the list
 **/
gconstpointer
egg_obj_list_index (const EggObjList *list, guint index)
{
	gconstpointer obj;

	g_return_val_if_fail (EGG_IS_OBJ_LIST (list), NULL);

	obj = g_ptr_array_index (list->priv->array, index);
	return obj;
}

/**
 * egg_obj_list_finalize:
 * @object: a valid #EggObjList instance
 **/
static void
egg_obj_list_finalize (GObject *object)
{
	EggObjListFreeFunc func_free;
	gpointer obj;
	guint i;
	EggObjList *list;
	GPtrArray *array;
	g_return_if_fail (EGG_IS_OBJ_LIST (object));
	list = EGG_OBJ_LIST (object);

	array = list->priv->array;
	func_free = list->priv->func_free;
	for (i=0; i<array->len; i++) {
		obj = g_ptr_array_index (array, i);
		if (func_free != NULL)
			func_free (obj);
	}
	g_ptr_array_free (array, TRUE);

	G_OBJECT_CLASS (egg_obj_list_parent_class)->finalize (object);
}

/**
 * egg_obj_list_class_init:
 * @klass: a valid #EggObjListClass instance
 **/
static void
egg_obj_list_class_init (EggObjListClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = egg_obj_list_finalize;
	g_type_class_add_private (klass, sizeof (EggObjListPrivate));
}

/**
 * egg_obj_list_init:
 * @list: a valid #EggObjList instance
 *
 * Initializes the obj_list class.
 **/
static void
egg_obj_list_init (EggObjList *list)
{
	list->priv = EGG_OBJ_LIST_GET_PRIVATE (list);
	list->priv->func_new = NULL;
	list->priv->func_copy = NULL;
	list->priv->func_free = NULL;
	list->priv->func_compare = NULL;
	list->priv->func_to_string = NULL;
	list->priv->func_from_string = NULL;
	list->priv->array = g_ptr_array_new ();
	list->len = list->priv->array->len;
}

/**
 * egg_obj_list_new:
 *
 * Return value: A new list class instance.
 **/
EggObjList *
egg_obj_list_new (void)
{
	EggObjList *list;
	list = g_object_new (EGG_TYPE_OBJ_LIST, NULL);
	return EGG_OBJ_LIST (list);
}

/***************************************************************************
 ***                          MAKE CHECK TESTS                           ***
 ***************************************************************************/
#ifdef EGG_TEST
#include "egg-test.h"

void
egg_obj_list_test (EggTest *test)
{
	EggObjList *list;

	if (!egg_test_start (test, "EggObjList"))
		return;

	/************************************************************/
	egg_test_title (test, "get an instance");
	list = egg_obj_list_new ();
	egg_test_assert (test, list != NULL);

	g_object_unref (list);

	egg_test_end (test);
}
#endif