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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2016 Red Hat, Inc. (www.redhat.com)
*
* This library is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* This library 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "evolution-data-server-config.h"
#include <stdio.h>
#include <string.h>
#include "camel-named-flags.h"
G_DEFINE_BOXED_TYPE (CamelNamedFlags,
camel_named_flags,
camel_named_flags_copy,
camel_named_flags_free)
/**
* camel_named_flags_new:
*
* Creates a new #CamelNamedFlags.
*
* Returns: (transfer full): A newly allocated #CamelNamedFlags.
* Free it with camel_named_flags_free() when done with it.
*
* Since: 3.24
**/
CamelNamedFlags *
camel_named_flags_new (void)
{
return (CamelNamedFlags *) g_ptr_array_new_with_free_func (g_free);
}
/**
* camel_named_flags_new_sized:
* @reserve_size: an array size to reserve
*
* Created a new #CamelNamedFlags, which has reserved @reserve_size
* elements. This value doesn't influence the camel_named_flags_get_length(),
* which returns zero on the array returned from this function.
*
* Returns: (transfer full): A newly allocated #CamelNameValueArray.
* Free it with camel_named_flags_free() when done with it.
*
* See: camel_name_value_array_new, camel_name_value_array_copy
*
* Since: 3.24
**/
CamelNamedFlags *
camel_named_flags_new_sized (guint reserve_size)
{
return (CamelNamedFlags *) g_ptr_array_new_full (reserve_size, g_free);
}
/**
* camel_named_flags_copy:
* @named_flags: (nullable): a #CamelNamedFlags
*
* Creates a copy of the @named_flags and returns it.
*
* Returns: (transfer full): A newly allocated #CamelNamedFlags.
* Free it with camel_named_flags_free() when done with it.
*
* Since: 3.24
**/
CamelNamedFlags *
camel_named_flags_copy (const CamelNamedFlags *named_flags)
{
const GPtrArray *src = (const GPtrArray *) named_flags;
GPtrArray *arr;
guint ii;
if (!src)
return NULL;
arr = (GPtrArray *) camel_named_flags_new_sized (src->len);
for (ii = 0; ii < src->len; ii++) {
const gchar *name = g_ptr_array_index (src, ii);
if (name && *name)
g_ptr_array_add (arr, g_strdup (name));
}
return (CamelNamedFlags *) arr;
}
/**
* camel_named_flags_free:
* @named_flags: (nullable): a #CamelNamedFlags, or %NULL
*
* Frees memory associated iwth the @named_flags. Does nothing,
* if @named_flags is %NULL.
*
* Since: 3.24
**/
void
camel_named_flags_free (CamelNamedFlags *named_flags)
{
if (named_flags)
g_ptr_array_unref ((GPtrArray *) named_flags);
}
static guint
camel_named_flags_find (const CamelNamedFlags *named_flags,
const gchar *name)
{
GPtrArray *arr = (GPtrArray *) named_flags;
guint ii;
g_return_val_if_fail (named_flags != NULL, (guint) -1);
g_return_val_if_fail (name != NULL, (guint) -1);
for (ii = 0; ii < arr->len; ii++) {
const gchar *nm = g_ptr_array_index (arr, ii);
if (g_strcmp0 (nm, name) == 0)
return ii;
}
return (guint) -1;
}
/**
* camel_named_flags_insert:
* @named_flags: a #CamelNamedFlags
* @name: name of the flag
*
* Inserts a flag named @name into the @named_flags, if it is not included
* already (comparing case sensitively), or does nothing otherwise.
*
* Returns: %TRUE the flag named @name was inserted; %FALSE otherwise.
*
* Since: 3.24
**/
gboolean
camel_named_flags_insert (CamelNamedFlags *named_flags,
const gchar *name)
{
GPtrArray *arr = (GPtrArray *) named_flags;
guint index;
g_return_val_if_fail (named_flags != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
index = camel_named_flags_find (named_flags, name);
/* already there */
if (index != (guint) -1)
return FALSE;
g_ptr_array_add (arr, g_strdup (name));
return TRUE;
}
/**
* camel_named_flags_remove:
* @named_flags: a #CamelNamedFlags
* @name: name of the flag
*
* Removes a flag named @name from the @named_flags.
*
* Returns: %TRUE when the @named_flags contained a flag named @name,
* comparing case sensitively, and it was removed; %FALSE otherwise.
*
* Since: 3.24
**/
gboolean
camel_named_flags_remove (CamelNamedFlags *named_flags,
const gchar *name)
{
GPtrArray *arr = (GPtrArray *) named_flags;
guint index;
g_return_val_if_fail (named_flags != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
index = camel_named_flags_find (named_flags, name);
/* not there */
if (index == (guint) -1)
return FALSE;
g_ptr_array_remove_index (arr, index);
return TRUE;
}
/**
* camel_named_flags_contains:
* @named_flags: a #CamelNamedFlags
* @name: name of the flag
*
* Returns: Whether the @named_flags contains a flag named @name,
* comparing case sensitively.
*
* Since: 3.24
**/
gboolean
camel_named_flags_contains (const CamelNamedFlags *named_flags,
const gchar *name)
{
g_return_val_if_fail (named_flags != NULL, FALSE);
g_return_val_if_fail (name != NULL, FALSE);
return camel_named_flags_find (named_flags, name) != (guint) -1;
}
/**
* camel_named_flags_clear:
* @named_flags: a #CamelNamedFlags
*
* Removes all the elements of the array.
*
* Since: 3.24
**/
void
camel_named_flags_clear (CamelNamedFlags *named_flags)
{
GPtrArray *arr = (GPtrArray *) named_flags;
g_return_if_fail (named_flags != NULL);
if (arr->len)
g_ptr_array_remove_range (arr, 0, arr->len);
}
/**
* camel_named_flags_get_length:
* @named_flags: (nullable): a #CamelNamedFlags
*
* Returns: Length of the array, aka how many named flags are stored there.
*
* Since: 3.24
**/
guint
camel_named_flags_get_length (const CamelNamedFlags *named_flags)
{
const GPtrArray *arr = (const GPtrArray *) named_flags;
if (!named_flags)
return 0;
return arr->len;
}
/**
* camel_named_flags_get:
* @named_flags: a #CamelNamedFlags
* @index: an index of an element
*
* Returns: (transfer none) (nullable): Name of the flag in at the given @index,
* or %NULL on error.
*
* Since: 3.24
**/
const gchar *
camel_named_flags_get (const CamelNamedFlags *named_flags,
guint index)
{
const GPtrArray *arr = (const GPtrArray *) named_flags;
g_return_val_if_fail (named_flags != NULL, NULL);
if (index >= camel_named_flags_get_length (named_flags))
return NULL;
return g_ptr_array_index (arr, index);
}
/**
* camel_named_flags_equal:
* @named_flags_a: (nullable): the first #CamelNamedFlags
* @named_flags_b: (nullable): the second #CamelNamedFlags
*
* Compares content of the two #CamelNamedFlags and returns whether
* they equal. Note this is an expensive operation for large sets.
*
* Returns: Whether the two #CamelNamedFlags have the same content.
*
* Since: 3.24
**/
gboolean
camel_named_flags_equal (const CamelNamedFlags *named_flags_a,
const CamelNamedFlags *named_flags_b)
{
guint ii, len;
if (named_flags_a == named_flags_b)
return TRUE;
if (!named_flags_a || !named_flags_b)
return camel_named_flags_get_length (named_flags_a) == camel_named_flags_get_length (named_flags_b);
len = camel_named_flags_get_length (named_flags_a);
if (len != camel_named_flags_get_length (named_flags_b))
return FALSE;
for (ii = 0; ii < len; ii++) {
if (!camel_named_flags_contains (named_flags_a, camel_named_flags_get (named_flags_b, ii)))
return FALSE;
}
return TRUE;
}
|