1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2010 Stefan Walter
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "config.h"
21 
22 #include "gcr-comparable.h"
23 
24 #include <string.h>
25 
26 /**
27  * SECTION:gcr-comparable
28  * @title: GcrComparable
29  * @short_description: Interface for comparing objects
30  *
31  * The #GcrComparable interface is implemented by objects when they should be
32  * comparable against one another.
33  */
34 
35 /**
36  * GcrComparable:
37  *
38  * The #GcrComparable interface is implemented by comparable objects.
39  */
40 
41 /**
42  * GcrComparableIface:
43  * @parent: type interface
44  * @compare: Compare whether tow objects represent the same thing.
45  *
46  * The interface to implement for #GcrComparable
47  */
48 
49 typedef GcrComparableIface GcrComparableInterface;
50 G_DEFINE_INTERFACE (GcrComparable, gcr_comparable, G_TYPE_OBJECT);
51 
52 static void
gcr_comparable_default_init(GcrComparableIface * iface)53 gcr_comparable_default_init (GcrComparableIface *iface)
54 {
55 
56 }
57 
58 
59 /**
60  * gcr_comparable_compare:
61  * @self: The comparable object
62  * @other: (nullable): Another comparable object
63  *
64  * Compare whether two objects represent the same thing. The return value can
65  * also be used to sort the objects.
66  *
67  * Returns: Zero if the two objects represent the same thing, non-zero if not.
68  */
69 gint
gcr_comparable_compare(GcrComparable * self,GcrComparable * other)70 gcr_comparable_compare (GcrComparable *self, GcrComparable *other)
71 {
72 	g_return_val_if_fail (GCR_IS_COMPARABLE (self), -1);
73 	g_return_val_if_fail (GCR_COMPARABLE_GET_INTERFACE (self)->compare, -1);
74 	g_return_val_if_fail (G_IS_OBJECT (self), -1);
75 	return GCR_COMPARABLE_GET_INTERFACE (self)->compare (self, other);
76 }
77 
78 /**
79  * gcr_comparable_memcmp: (skip)
80  * @mem1: (array length=size1) (element-type guint8): First block of memory
81  * @size1: Length of first block
82  * @mem2: (array length=size2) (element-type guint8): Second block of memory
83  * @size2: Length of second block
84  *
85  * Compare two blocks of memory. The return value can be used to sort
86  * the blocks of memory.
87  *
88  * Returns: Zero if the blocks are identical, negative if first
89  *          less than secend, possitive otherwise.
90  */
91 gint
gcr_comparable_memcmp(gconstpointer mem1,gsize size1,gconstpointer mem2,gsize size2)92 gcr_comparable_memcmp (gconstpointer mem1,
93                        gsize size1,
94                        gconstpointer mem2,
95                        gsize size2)
96 {
97 	gint result;
98 
99 	if (mem1 == mem2 && size1 == size2)
100 		return 0;
101 
102 	if (!mem1)
103 		return 1;
104 	if (!mem2)
105 		return -1;
106 
107 	result = memcmp (mem1, mem2, MIN (size1, size2));
108 	if (result != 0)
109 		return result;
110 
111 	if (size1 == size2)
112 		return 0;
113 	if (size1 < size2)
114 		return -1;
115 	return 1;
116 }
117