summaryrefslogtreecommitdiff
path: root/src/camel/camel-name-value-array.c
blob: 86a7fed0863cfc80fd7ff1a987ac2c911d607eb9 (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
/* -*- 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-string-utils.h"

#include "camel-name-value-array.h"

G_DEFINE_BOXED_TYPE (CamelNameValueArray,
		camel_name_value_array,
		camel_name_value_array_copy,
		camel_name_value_array_free)

typedef struct _CamelNameValuePair {
	gchar *name;
	gchar *value;
} CamelNameValuePair;

static void
free_name_value_content (gpointer ptr)
{
	CamelNameValuePair *pair = ptr;

	if (pair) {
		g_free (pair->name);
		g_free (pair->value);

		pair->name = NULL;
		pair->value = NULL;
	}
}

/**
 * camel_name_value_array_new:
 *
 * Creates a new #CamelNameValueArray. The returned pointer should be freed
 * with camel_name_value_array_free() when no longer needed.
 *
 * Returns: (transfer full): A new #CamelNameValueArray.
 *
 * See: camel_name_value_array_new_sized, camel_name_value_array_copy
 *
 * Since: 3.24
 **/
CamelNameValueArray *
camel_name_value_array_new (void)
{
	GArray *arr;

	arr = g_array_new (FALSE, FALSE, sizeof (CamelNameValuePair));
	g_array_set_clear_func (arr, free_name_value_content);

	return (CamelNameValueArray *) arr;
}

/**
 * camel_name_value_array_new_sized:
 * @reserve_size: an array size to reserve
 *
 * Creates a new #CamelNameValueArray, which has reserved @reserve_size
 * elements. This value doesn't influence the camel_name_value_array_get_length(),
 * which returns zero on the array returned from this function. The returned
 * pointer should be freed with camel_name_value_array_free() when no longer needed.
 *
 * Returns: (transfer full): A new #CamelNameValueArray.
 *
 * See: camel_name_value_array_new, camel_name_value_array_copy
 *
 * Since: 3.24
 **/
CamelNameValueArray *
camel_name_value_array_new_sized (guint reserve_size)
{
	GArray *arr;

	arr = g_array_sized_new (FALSE, FALSE, sizeof (CamelNameValuePair), reserve_size);
	g_array_set_clear_func (arr, free_name_value_content);

	return (CamelNameValueArray *) arr;
}

/**
 * camel_name_value_array_copy:
 * @array: (nullable): a #CamelNameValueArray
 *
 * Creates a new copy of the @array. The returned pointer should be freed
 * with camel_name_value_array_free() when no longer needed.
 *
 * Returns: (transfer full): A new copy of the @array.
 *
 * See: camel_name_value_array_new, camel_name_value_array_new_sized
 *
 * Since: 3.24
 **/
CamelNameValueArray *
camel_name_value_array_copy (const CamelNameValueArray *array)
{
	CamelNameValueArray *copy;
	guint ii, len;

	if (!array)
		return NULL;

	len = camel_name_value_array_get_length (array);
	copy = camel_name_value_array_new_sized (len);

	for (ii = 0; ii < len; ii++) {
		const gchar *name = NULL, *value = NULL;

		if (camel_name_value_array_get (array, ii, &name, &value))
			camel_name_value_array_append (copy, name, value);
	}

	return copy;
}

/**
 * camel_name_value_array_free:
 * @array: (nullable): a #CamelNameValueArray, or %NULL
 *
 * Frees the @array, previously allocated by camel_name_value_array_new(),
 * camel_name_value_array_new_sized() or camel_name_value_array_copy().
 * If the @array is %NULL, then does nothing.
 *
 * Since: 3.24
 **/
void
camel_name_value_array_free (CamelNameValueArray *array)
{
	if (array)
		g_array_free ((GArray *) array, TRUE);
}

/**
 * camel_name_value_array_get_length:
 * @array: (nullable): a #CamelNameValueArray
 *
 * Returns: Length of the @array, aka how many elements are stored in the @array.
 *
 * Since: 3.24
 **/
guint
camel_name_value_array_get_length (const CamelNameValueArray *array)
{
	GArray *arr = (GArray *) array;

	if (!array)
		return 0;

	return arr->len;
}

/**
 * camel_name_value_array_get:
 * @array: a #CamelNameValueArray
 * @index: an index
 * @out_name: (out) (nullable): A place to store the name of the element, or %NULL
 * @out_value: (out) (nullable): A place to store the value of the element, or %NULL
 *
 * Returns the name and the value of the element at index @index. Either
 * of the @out_name and @out_value can be %NULL, to not return that part.
 *
 * Returns: %TRUE on success, %FALSE otherwise.
 *
 * See: camel_name_value_array_get_name, camel_name_value_array_get_value, camel_name_value_array_get_named
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_get (const CamelNameValueArray *array,
			    guint index,
			    const gchar **out_name,
			    const gchar **out_value)
{
	GArray *arr = (GArray *) array;
	CamelNameValuePair *pair;

	g_return_val_if_fail (array != NULL, FALSE);

	if (index >= camel_name_value_array_get_length (array))
		return FALSE;

	pair = &g_array_index (arr, CamelNameValuePair, index);

	if (out_name)
		*out_name = pair->name;
	if (out_value)
		*out_value= pair->value;

	return TRUE;
}

static guint
camel_name_value_array_find_named (const CamelNameValueArray *array,
				   CamelCompareType compare_type,
				   const gchar *name)
{
	GArray *arr = (GArray *) array;
	gboolean case_sensitive;
	gint ii;

	g_return_val_if_fail (array != NULL, (guint) -1);
	g_return_val_if_fail (name != NULL, (guint) -1);

	case_sensitive = compare_type == CAMEL_COMPARE_CASE_SENSITIVE;

	for (ii = 0; ii < arr->len; ii++) {
		CamelNameValuePair *pair = &g_array_index (arr, CamelNameValuePair, ii);

		if ((case_sensitive && g_strcmp0 (name, pair->name) == 0) ||
		    (!case_sensitive && pair->name && camel_strcase_equal (name, pair->name))) {
			return ii;
		}
	}

	return (guint) -1;
}

/**
 * camel_name_value_array_get_named:
 * @array: a #CamelNameValueArray
 * @compare_type: a compare type, one of #CamelCompareType
 * @name: a name
 *
 * Returns the value of the first element named @name, or %NULL when there
 * is no element of such @name in the @array. The @compare_type determines
 * how to compare the names.
 *
 * Returns: (transfer none) (nullable): Value of the first element named @name, or %NULL.
 *
 * See: camel_name_value_array_get, camel_name_value_array_get_name
 *
 * Since: 3.24
 **/
const gchar *
camel_name_value_array_get_named (const CamelNameValueArray *array,
				  CamelCompareType compare_type,
				  const gchar *name)
{
	guint index;

	g_return_val_if_fail (array != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	index = camel_name_value_array_find_named (array, compare_type, name);
	if (index == (guint) -1)
		return NULL;

	return camel_name_value_array_get_value (array, index);
}

/**
 * camel_name_value_array_get_name:
 * @array: a #CamelNameValueArray
 * @index: an index
 *
 * Returns the name of the element at index @index.
 *
 * Returns: (transfer none) (nullable): Name of the element at the given @index,
 *    or %NULL on error.
 *
 * See: camel_name_value_array_get, camel_name_value_array_get_value
 *
 * Since: 3.24
 **/
const gchar *
camel_name_value_array_get_name (const CamelNameValueArray *array,
				 guint index)
{
	const gchar *name = NULL;

	g_return_val_if_fail (array != NULL, NULL);

	if (!camel_name_value_array_get (array, index, &name, NULL))
		return NULL;

	return name;
}

/**
 * camel_name_value_array_get_value:
 * @array: a #CamelNameValueArray
 * @index: an index
 *
 * Returns the value of the element at index @index.
 *
 * Returns: (transfer none) (nullable): Value of the element at the given @index,
 *    or %NULL on error.
 *
 * See: camel_name_value_array_get, camel_name_value_array_get_name
 *
 * Since: 3.24
 **/
const gchar *
camel_name_value_array_get_value (const CamelNameValueArray *array,
				  guint index)
{
	const gchar *value = NULL;

	g_return_val_if_fail (array != NULL, NULL);

	if (!camel_name_value_array_get (array, index, NULL, &value))
		return NULL;

	return value;
}

/**
 * camel_name_value_array_append:
 * @array: a #CamelNameValueArray
 * @name: a name
 * @value: a value
 *
 * Appends a new element of the name @name and the value @value
 * at the end of @array.
 *
 * See: camel_name_value_array_set_named
 *
 * Since: 3.24
 **/
void
camel_name_value_array_append (CamelNameValueArray *array,
			       const gchar *name,
			       const gchar *value)
{
	GArray *arr = (GArray *) array;
	CamelNameValuePair pair;

	g_return_if_fail (array != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (value != NULL);

	pair.name = g_strdup (name);
	pair.value = g_strdup (value);

	g_array_append_val (arr, pair);
}

static gboolean
camel_name_value_array_set_internal (CamelNameValueArray *array,
				     guint index,
				     const gchar *name,
				     const gchar *value)
{
	GArray *arr = (GArray *) array;
	CamelNameValuePair *pair;
	gboolean changed = FALSE;

	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (index < camel_name_value_array_get_length (array), FALSE);

	pair = &g_array_index (arr, CamelNameValuePair, index);

	if (name && g_strcmp0 (pair->name, name) != 0) {
		g_free (pair->name);
		pair->name = g_strdup (name);
		changed = TRUE;
	}

	if (value && g_strcmp0 (pair->value, value) != 0) {
		g_free (pair->value);
		pair->value = g_strdup (value);
		changed = TRUE;
	}

	return changed;
}

/**
 * camel_name_value_array_set:
 * @array: a #CamelNameValueArray
 * @index: an index
 * @name: a name
 * @value: a value
 *
 * Sets both the @name and the @value of the element at index @index.
 *
 * Returns: Whether the @array changed.
 *
 * See: camel_name_value_array_append, camel_name_value_array_set_name, camel_name_value_array_set_value
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_set (CamelNameValueArray *array,
			    guint index,
			    const gchar *name,
			    const gchar *value)
{
	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (index < camel_name_value_array_get_length (array), FALSE);
	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);

	return camel_name_value_array_set_internal (array, index, name, value);
}

/**
 * camel_name_value_array_set_name:
 * @array: a #CamelNameValueArray
 * @index: an index
 * @name: a name
 *
 * Sets the @name of the element at index @index.
 *
 * Returns: Whether the @array changed.
 *
 * See: camel_name_value_array_set, camel_name_value_array_set_value
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_set_name (CamelNameValueArray *array,
				 guint index,
				 const gchar *name)
{
	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (index < camel_name_value_array_get_length (array), FALSE);
	g_return_val_if_fail (name != NULL, FALSE);

	return camel_name_value_array_set_internal (array, index, name, NULL);
}

/**
 * camel_name_value_array_set_value:
 * @array: a #CamelNameValueArray
 * @index: an index
 * @value: a value
 *
 * Sets the @value of the element at index @index.
 *
 * Returns: Whether the @array changed.
 *
 * See: camel_name_value_array_set, camel_name_value_array_set_name
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_set_value (CamelNameValueArray *array,
				  guint index,
				  const gchar *value)
{
	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (index < camel_name_value_array_get_length (array), FALSE);
	g_return_val_if_fail (value != NULL, FALSE);

	return camel_name_value_array_set_internal (array, index, NULL, value);
}

/**
 * camel_name_value_array_set_named:
 * @array: a #CamelNameValueArray
 * @compare_type: a compare type, one of #CamelCompareType
 * @name: a name
 * @value: a value
 *
 * Finds an element named @name and sets its value to @value, or appends
 * a new element, in case no such named element exists in the @array yet.
 * In case there are more elements named with @name only the first
 * occurrence is changed. The @compare_type determines how to compare
 * the names.
 *
 * Returns: Whether the @array changed.
 *
 * See: camel_name_value_array_append, camel_name_value_array_set
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_set_named (CamelNameValueArray *array,
				  CamelCompareType compare_type,
				  const gchar *name,
				  const gchar *value)
{
	gboolean changed = FALSE;
	guint index;

	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (name != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);

	index = camel_name_value_array_find_named (array, compare_type, name);
	if (index == (guint) -1) {
		camel_name_value_array_append (array, name, value);
		changed = TRUE;
	} else {
		changed = camel_name_value_array_set_value (array, index, value);
	}

	return changed;
}

/**
 * camel_name_value_array_remove:
 * @array: a #CamelNameValueArray
 * @index: an index to remove
 *
 * Removes element at index @index.
 *
 * Returns: Whether the element was removed.
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_remove (CamelNameValueArray *array,
			       guint index)
{
	g_return_val_if_fail (array != NULL, FALSE);
	g_return_val_if_fail (index < camel_name_value_array_get_length (array), FALSE);

	g_array_remove_index ((GArray *) array, index);

	return TRUE;
}

/**
 * camel_name_value_array_remove_named:
 * @array: a #CamelNameValueArray
 * @compare_type: a compare type, one of #CamelCompareType
 * @name: a name to remove
 * @all_occurrences: whether to remove all occurrences of the @name
 *
 * Removes elements of the @array with the given @name.
 * The @compare_type determines hot to compare the names.
 * If the @all_occurrences is set to %TRUE, then every elements with the @name
 * are removed, otherwise only the first occurrence is removed.
 *
 * Returns: How many elements had been removed.
 *
 * Since: 3.24
 **/
guint
camel_name_value_array_remove_named (CamelNameValueArray *array,
				     CamelCompareType compare_type,
				     const gchar *name,
				     gboolean all_occurrences)
{
	guint index, removed = 0;

	g_return_val_if_fail (array != NULL, 0);
	g_return_val_if_fail (name != NULL, 0);

	while (index = camel_name_value_array_find_named (array, compare_type, name), index != (guint) -1) {
		if (!camel_name_value_array_remove (array, index))
			break;

		removed++;

		if (!all_occurrences)
			break;
	}

	return removed;
}

/**
 * camel_name_value_array_clear:
 * @array: a #CamelNameValueArray
 *
 * Removes all elements of the @array.
 *
 * Since: 3.24
 **/
void
camel_name_value_array_clear (CamelNameValueArray *array)
{
	GArray *arr = (GArray *) array;

	g_return_if_fail (array != NULL);

	g_array_remove_range (arr, 0, arr->len);
}

/**
 * camel_name_value_array_equal:
 * @array_a: (nullable): the first #CamelNameValueArray
 * @array_b: (nullable): the second #CamelNameValueArray
 * @compare_type: a compare type, one of #CamelCompareType
 *
 * Compares content of the two #CamelNameValueArray and returns whether
 * they equal. Note this is an expensive operation for large arrays.
 *
 * Returns: Whether the two #CamelNameValueArray have the same content.
 *
 * Since: 3.24
 **/
gboolean
camel_name_value_array_equal (const CamelNameValueArray *array_a,
			      const CamelNameValueArray *array_b,
			      CamelCompareType compare_type)
{
	guint ii, len;

	if (array_a == array_b)
		return TRUE;

	if (!array_a || !array_b)
		return camel_name_value_array_get_length (array_a) == camel_name_value_array_get_length (array_b);

	len = camel_name_value_array_get_length (array_a);
	if (len != camel_name_value_array_get_length (array_b))
		return FALSE;

	for (ii = 0; ii < len; ii++) {
		const gchar *value1, *value2;

		value1 = camel_name_value_array_get_value (array_a, ii);
		value2 = camel_name_value_array_get_named (array_b, compare_type,
			camel_name_value_array_get_name (array_a, ii));

		if (g_strcmp0 (value1, value2) != 0)
			return FALSE;
	}

	return TRUE;
}