Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
icalenumarray.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: icalenumarray.c
3 CREATOR: Ken Murchison 24 Aug 2022 <murch@fastmailteam.com>
4
5 SPDX-FileCopyrightText: 2022, Fastmail Pty. Ltd. (https://fastmail.com)
6 SPDX-License-Identifier: LGPL-2.1-only OR MPL-2.0
7 ======================================================================*/
8
13
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18#include "icalenumarray.h"
19#include "icalmemory.h"
20
21#include <string.h>
22
23size_t icalenumarray_size(const icalenumarray *array)
24{
25 if (!array) {
26 return 0;
27 }
28 return array->num_elements;
29}
30
31const icalenumarray_element *icalenumarray_element_at(icalenumarray *array, size_t position)
32{
33 if (position >= icalenumarray_size(array)) {
34 return NULL;
35 }
36 return icalarray_element_at(array, position);
37}
38
39static int enumcmp(const void *a, const void *b)
40{
41 const icalenumarray_element *aElem = (const icalenumarray_element *)a;
42 const icalenumarray_element *bElem = (const icalenumarray_element *)b;
43
44 /* Sort X-values alphabetically, but last */
45 if (!aElem->xvalue && bElem->xvalue) {
46 return -1;
47 } else if (aElem->xvalue && !bElem->xvalue) {
48 return 1;
49 }
50
51 if (aElem->val < bElem->val) {
52 return -1;
53 } else if (aElem->val > bElem->val) {
54 return 1;
55 }
56
57 if (aElem->xvalue) {
58 return strcmp(aElem->xvalue, bElem->xvalue);
59 } else {
60 return 0;
61 }
62}
63
64size_t icalenumarray_find(icalenumarray *array,
65 const icalenumarray_element *needle)
66{
67 if (!array || !needle) {
68 return icalenumarray_size(array);
69 }
70
71 size_t i;
72
73 for (i = 0; i < array->num_elements; i++) {
75 if (!enumcmp(e, needle)) {
76 return i;
77 }
78 }
79
80 return icalenumarray_size(array);
81}
82
83void icalenumarray_append(icalenumarray *array, const icalenumarray_element *elem)
84{
85 if (!array || !elem) {
86 return;
87 }
88
89 /* coverity[resource_leak] */
90 char *val = (elem->xvalue ? icalmemory_strdup(elem->xvalue) : NULL);
91 icalenumarray_element copy = {elem->val, val};
92
93 icalarray_append(array, &copy);
94}
95
96void icalenumarray_add(icalenumarray *array, const icalenumarray_element *elem)
97{
98 if (!array || !elem) {
99 return;
100 }
101
102 if (icalenumarray_find(array, elem) >= icalenumarray_size(array)) {
103 icalenumarray_append(array, elem);
104 }
105}
106
107void icalenumarray_remove_element_at(icalenumarray *array,
108 size_t position)
109{
110 if (position >= icalenumarray_size(array)) {
111 return;
112 }
113
114 icalenumarray_element *del = icalarray_element_at(array, position);
115
116 icalmemory_free_buffer((char *)del->xvalue);
117 icalarray_remove_element_at(array, position);
118}
119
120void icalenumarray_remove(icalenumarray *array, const icalenumarray_element *del)
121{
122 if (!array || !del) {
123 return;
124 }
125
126 size_t j = 0;
127
128 for (size_t i = 0; i < array->num_elements; i++) {
129 const icalenumarray_element *elem = icalarray_element_at(array, i);
130 if (enumcmp(elem, del)) {
131 icalarray_set_element_at(array, elem, j++);
132 } else {
133 icalmemory_free_buffer((char *)elem->xvalue);
134 }
135 }
136
137 array->num_elements = j;
138}
139
140void icalenumarray_free(icalenumarray *array)
141{
142 if (!array) {
143 return;
144 }
145
146 for (size_t i = 0; i < icalenumarray_size(array); i++) {
148 icalmemory_free_buffer((char *)del->xvalue);
149 }
150
151 icalarray_free(array);
152}
153
154void icalenumarray_sort(icalenumarray *array)
155{
156 if (!array) {
157 return;
158 }
159
160 icalarray_sort(array, (int (*)(const void *, const void *))&enumcmp);
161}
162
163icalenumarray *icalenumarray_clone(icalenumarray *array)
164{
165 if (!array) {
166 return NULL;
167 }
168
169 icalenumarray *clone = icalenumarray_new(array->increment_size);
170 size_t i;
171
172 for (i = 0; i < array->num_elements; i++) {
174 }
175
176 return clone;
177}
void * icalarray_element_at(icalarray *array, size_t position)
Access an array element.
Definition icalarray.c:135
void icalarray_free(icalarray *array)
Definition icalarray.c:104
void icalarray_sort(icalarray *array, int(*compare)(const void *, const void *))
Sorts the elements of an icalarray using the given comparison function.
Definition icalarray.c:182
void icalarray_append(icalarray *array, const void *element)
Appends an element to an array.
Definition icalarray.c:119
void icalarray_set_element_at(icalarray *array, const void *element, size_t position)
Overwrites an existing element in an array with a new value.
Definition icalarray.c:143
void icalarray_remove_element_at(icalarray *array, size_t position)
Removes a given element from an array.
Definition icalarray.c:148
void icalenumarray_free(icalenumarray *array)
Frees this array's memory and all its elements.
void icalenumarray_remove(icalenumarray *array, const icalenumarray_element *del)
Removes all occurrences of an element.
size_t icalenumarray_find(icalenumarray *array, const icalenumarray_element *needle)
Finds an element in the array.
void icalenumarray_sort(icalenumarray *array)
Sorts the elements in the array in ascending order.
void icalenumarray_append(icalenumarray *array, const icalenumarray_element *elem)
Appends an element to the array.
size_t icalenumarray_size(const icalenumarray *array)
Indicates the count of elements stored in the array.
icalenumarray * icalenumarray_clone(icalenumarray *array)
Clones the array and all its elements.
void icalenumarray_remove_element_at(icalenumarray *array, size_t position)
Removes the element at an array position.
const icalenumarray_element * icalenumarray_element_at(icalenumarray *array, size_t position)
Accesses an element stored in the array.
void icalenumarray_add(icalenumarray *array, const icalenumarray_element *elem)
Appends an element to the array, omitting duplicates.
Defines the data structure for handling arrays of enums.
#define icalenumarray_new(increment_size)
void icalmemory_free_buffer(void *buf)
Releases a buffer.
Definition icalmemory.c:355
char * icalmemory_strdup(const char *s)
Creates a duplicate of a string.
Definition icalmemory.c:242
Common memory management routines.