Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
vcardparameter.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: vcardparameter.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 "vcardparameter.h"
19#include "vcardparameterimpl.h"
20#include "icalerror_p.h"
21#include "icalmemory.h"
22
23#include <errno.h>
24#include <stdlib.h>
25
26LIBICAL_VCARD_EXPORT struct vcardparameter_impl *vcardparameter_new_impl(vcardparameter_kind kind)
27{
28 struct vcardparameter_impl *v;
29
30 if ((v = (struct vcardparameter_impl *)icalmemory_new_buffer(sizeof(struct vcardparameter_impl))) == 0) {
32 return 0;
33 }
34
35 memset(v, 0, sizeof(struct vcardparameter_impl));
36
37 v->id = ICAL_STRUCTURE_TYPE_PARAMETER;
38 v->kind = kind;
39 v->value_kind = vcardparameter_kind_value_kind(kind, &v->is_multivalued);
40
41 return v;
42}
43
45{
46 struct vcardparameter_impl *v = vcardparameter_new_impl(kind);
47
48 return (vcardparameter *)v;
49}
50
52{
53 if (!param || param->parent != 0) {
54 return;
55 }
56
57 if (param->string != 0) {
58 icalmemory_free_buffer((void *)param->string);
59 } else if (param->values != 0) {
60 if (param->value_kind == VCARD_TEXT_VALUE) {
61 vcardstrarray_free(param->values);
62 } else {
63 vcardenumarray_free(param->values);
64 }
65 } else if (param->structured != 0) {
66 vcardstructured_unref(param->structured);
67 }
68
69 if (param->x_name != 0) {
70 icalmemory_free_buffer((void *)param->x_name);
71 }
72
73 memset(param, 0, sizeof(vcardparameter));
74
75 param->parent = 0;
76 param->id = ICAL_STRUCTURE_TYPE_PARAMETER_EMPTY;
78}
79
81{
82 struct vcardparameter_impl *clone;
83
84 icalerror_check_arg_rz((old != 0), "param");
85
86 clone = vcardparameter_new_impl(old->kind);
87
88 if (clone == 0) {
89 return 0;
90 }
91
92 memcpy(clone, old, sizeof(struct vcardparameter_impl));
93
94 /* Nothing owned by old may be shared with the clone:
95 every pointer below is either deep-copied or left NULL. */
96 clone->parent = 0;
97 clone->string = 0;
98 clone->x_name = 0;
99 clone->values = 0;
100 clone->structured = 0;
101
102 if (old->string != 0) {
103 clone->string = icalmemory_strdup(old->string);
104 if (clone->string == 0) {
105 clone->parent = 0;
106 vcardparameter_free(clone);
107 return 0;
108 }
109 }
110
111 if (old->x_name != 0) {
112 clone->x_name = icalmemory_strdup(old->x_name);
113 if (clone->x_name == 0) {
114 clone->parent = 0;
115 vcardparameter_free(clone);
116 return 0;
117 }
118 }
119
120 if (old->values != 0) {
121 clone->values = old->value_kind == VCARD_TEXT_VALUE ? vcardstrarray_clone(old->values) : vcardenumarray_clone(old->values);
122 if (clone->values == 0) {
123 clone->parent = 0;
124 vcardparameter_free(clone);
125 return 0;
126 }
127 }
128
129 if (old->structured != 0) {
130 clone->structured = vcardstructured_clone(old->structured);
131 if (clone->structured == 0) {
132 vcardparameter_free(clone);
133 return 0;
134 }
135 }
136
137 return clone;
138}
139
141{
142 char *eq;
143 char *cpy;
145 vcardparameter *param;
146
147 icalerror_check_arg_rz(str != 0, "str");
148
149 cpy = icalmemory_strdup(str);
150
151 if (cpy == 0) {
153 return 0;
154 }
155
156 eq = strchr(cpy, '=');
157
158 if (eq == 0) {
161 return 0;
162 }
163
164 *eq = '\0';
165
166 eq++;
167
169
170 if (kind == VCARD_NO_PARAMETER) {
173 return 0;
174 }
175
176 param = vcardparameter_new_from_value_string(kind, eq);
177
178 if (kind == VCARD_X_PARAMETER) {
179 vcardparameter_set_xname(param, cpy);
180 } else if (kind == VCARD_IANA_PARAMETER) {
182 }
183
185
186 return param;
187}
188
190{
191 char *buf;
192
195 return buf;
196}
197
198/*
199 * - param = param-name "=" param-value
200 * - param-name = iana-token / x-token
201 * - param-value = paramtext /quoted-string
202 * - paramtext = *SAFE-CHAR
203 * - quoted-string= DQUOTE *QSAFE-CHAR DQUOTE
204 * - QSAFE-CHAR = any character except CTLs and DQUOTE
205 * - SAFE-CHAR = any character except CTLs, DQUOTE. ";", ":", ","
206 */
208{
209 size_t buf_size = 1024;
210 char *buf;
211 char *buf_ptr;
212
213 icalerror_check_arg_rz((param != 0), "parameter");
214
215 /* Create new buffer that we can append names, parameters and a
216 * value to, and reallocate as needed.
217 */
218
219 buf = icalmemory_new_buffer(buf_size);
220 buf_ptr = buf;
221
222 if (param->kind == VCARD_X_PARAMETER) {
223 icalmemory_append_string(&buf, &buf_ptr,
224 &buf_size, vcardparameter_get_xname(param));
225 } else if (param->kind == VCARD_IANA_PARAMETER) {
226 icalmemory_append_string(&buf, &buf_ptr,
227 &buf_size, vcardparameter_get_iana_name(param));
228 } else {
229 const char *kind_string = vcardparameter_kind_to_string(param->kind);
230
231 if (param->kind == VCARD_NO_PARAMETER ||
232 param->kind == VCARD_ANY_PARAMETER || kind_string == 0) {
235 return 0;
236 }
237
238 /* Put the parameter name into the string */
239 icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
240 }
241
242 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "=");
243
244 if (param->string != 0) {
246 &buf_size, param->string);
247 } else if (param->data != 0) {
248 char *intbuf = NULL;
249 const char *str;
250
251 if (param->value_kind == VCARD_INTEGER_VALUE) {
252#define VCARD_INTEGER_LENGTH 21
254 snprintf(intbuf, VCARD_INTEGER_LENGTH - 1, "%d", param->data);
255
256 str = intbuf;
257 } else {
258 str = vcardparameter_enum_to_string(param->data);
259 }
260
261 icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
262
263 } else if (param->values != 0) {
264 size_t i;
265 const char *sep = "";
266
267 for (i = 0; i < param->values->num_elements; i++) {
268 icalmemory_append_string(&buf, &buf_ptr, &buf_size, sep);
269
270 if (param->value_kind == VCARD_TEXT_VALUE) {
271 const char *str = vcardstrarray_element_at(param->values, i);
272
274 &buf_size, str);
275 } else {
276 const vcardenumarray_element *elem =
277 vcardenumarray_element_at(param->values, i);
278 if (elem->xvalue != 0) {
280 &buf_size, elem->xvalue);
281 } else {
282 const char *str = vcardparameter_enum_to_string(elem->val);
283
284 icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
285 }
286 }
287 sep = ",";
288 }
289 } else if (vcardparameter_is_structured(param)) {
290 char *str = vcardstructured_as_vcard_string_r(param->structured, true);
291
292 icalmemory_append_encoded_string(&buf, &buf_ptr, &buf_size, str);
294 } else if (vcardtime_is_valid_time(param->date)) {
295 const char *str = vcardtime_as_vcard_string(param->date, 0);
296
297 icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
298
299 } else {
302 return 0;
303 }
304
305 return buf;
306}
307
309{
310 if (parameter == 0) {
311 return VCARD_NO_PARAMETER;
312 }
313
314 return parameter->kind;
315}
316
317bool vcardparameter_isa_parameter(void *parameter)
318{
319 const struct vcardparameter_impl *impl = (struct vcardparameter_impl *)parameter;
320
321 if (parameter == 0) {
322 return false;
323 }
324
325 return (impl->id == ICAL_STRUCTURE_TYPE_PARAMETER);
326}
327
328void vcardparameter_set_xname(vcardparameter *param, const char *v)
329{
330 icalerror_check_arg_rv((param != 0), "param");
331 icalerror_check_arg_rv((v != 0), "v");
332
333 if (param->x_name != 0) {
334 icalmemory_free_buffer((void *)param->x_name);
335 }
336
337 param->x_name = icalmemory_strdup(v);
338
339 if (param->x_name == 0) {
340 errno = ENOMEM;
341 }
342}
343
345{
346 icalerror_check_arg_rz((param != 0), "param");
347
348 return param->x_name;
349}
350
351void vcardparameter_set_xvalue(vcardparameter *param, const char *v)
352{
353 icalerror_check_arg_rv((param != 0), "param");
354 icalerror_check_arg_rv((v != 0), "v");
355
356 if (param->string != 0) {
357 icalmemory_free_buffer((void *)param->string);
358 }
359
360 param->string = icalmemory_strdup(v);
361
362 if (param->string == 0) {
363 errno = ENOMEM;
364 }
365}
366
368{
369 icalerror_check_arg_rz((param != 0), "param");
370
371 return param->string;
372}
373
375{
377}
378
380{
381 return vcardparameter_get_xvalue(param);
382}
383
385{
386 vcardparameter_set_xname(param, v);
387}
388
390{
391 return vcardparameter_get_xname(param);
392}
393
394void vcardparameter_set_parent(vcardparameter *param, vcardproperty *property)
395{
396 icalerror_check_arg_rv((param != 0), "param");
397
398 param->parent = property;
399}
400
401vcardproperty *vcardparameter_get_parent(const vcardparameter *param)
402{
403 icalerror_check_arg_rz((param != 0), "param");
404
405 return param->parent;
406}
407
409{
412 const char *name1;
413 const char *name2;
414
415 icalerror_check_arg_rz((param1 != 0), "param1");
416 icalerror_check_arg_rz((param2 != 0), "param2");
417
418 kind1 = vcardparameter_isa(param1);
419 kind2 = vcardparameter_isa(param2);
420
421 if (kind1 != kind2) {
422 return false;
423 }
424
425 if (kind1 == VCARD_X_PARAMETER) {
426 name1 = vcardparameter_get_xname(param1);
427 name2 = vcardparameter_get_xname(param2);
428 if (strcasecmp(name1, name2) != 0) {
429 return false;
430 }
431 } else if (kind1 == VCARD_IANA_PARAMETER) {
432 name1 = vcardparameter_get_iana_name(param1);
433 name2 = vcardparameter_get_iana_name(param2);
434 if (strcasecmp(name1, name2) != 0) {
435 return false;
436 }
437 }
438 return true;
439}
440
442{
443 icalerror_check_arg_rz((param != 0), "param");
444
445 return param->is_multivalued != 0;
446}
447
449{
450 icalerror_check_arg_rz((param != 0), "param");
451
452 return (param->value_kind == VCARD_STRUCTURED_VALUE);
453}
454
455/* Everything below this line is machine generated. Do not edit. */
456/* ALTREP */
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
@ ICAL_BADARG_ERROR
Definition icalerror.h:47
@ ICAL_MALFORMEDDATA_ERROR
Definition icalerror.h:59
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
void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const char *string)
Appends a string to a buffer.
Definition icalmemory.c:365
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
Definition icalmemory.c:315
void icalmemory_append_encoded_string(char **buf, char **pos, size_t *buf_size, const char *string)
Definition icalmemory.c:478
void icalmemory_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Definition icalmemory.c:158
void * icalmemory_tmp_buffer(size_t size)
Creates a new temporary buffer on the ring and returns it.
Definition icalmemory.c:182
Common memory management routines.
struct vcardparameter_impl vcardparameter
vcardparameter_kind
@ VCARD_NO_PARAMETER
@ VCARD_X_PARAMETER
@ VCARD_IANA_PARAMETER
@ VCARD_ANY_PARAMETER
const char * vcardparameter_enum_to_string(int e)
@ VCARD_INTEGER_VALUE
@ VCARD_STRUCTURED_VALUE
@ VCARD_TEXT_VALUE
#define vcardenumarray_clone(array)
icalenumarray_element vcardenumarray_element
#define vcardenumarray_element_at(array, position)
#define vcardenumarray_free(array)
vcardparameter * vcardparameter_new_from_string(const char *str)
Creates new vcardparameter object from string.
bool vcardparameter_has_same_name(const vcardparameter *param1, const vcardparameter *param2)
Determines if two parameters have the same name.
char * vcardparameter_as_vcard_string(vcardparameter *param)
Converts vcardparameter into a string representation.
bool vcardparameter_is_structured(const vcardparameter *param)
struct vcardparameter_impl * vcardparameter_new_impl(vcardparameter_kind kind)
void vcardparameter_set_xname(vcardparameter *param, const char *v)
Sets the X-name of param to v.
const char * vcardparameter_get_iana_value(const vcardparameter *param)
Returns the IANA value of param.
void vcardparameter_free(vcardparameter *param)
Frees an vcardparameter object.
void vcardparameter_set_iana_value(vcardparameter *param, const char *v)
Sets the IANA value of param to v.
const char * vcardparameter_get_iana_name(const vcardparameter *param)
Returns the IANA name of param.
void vcardparameter_set_parent(vcardparameter *param, vcardproperty *property)
bool vcardparameter_is_multivalued(const vcardparameter *param)
bool vcardparameter_isa_parameter(void *parameter)
vcardparameter_kind vcardparameter_isa(const vcardparameter *parameter)
vcardproperty * vcardparameter_get_parent(const vcardparameter *param)
void vcardparameter_set_iana_name(vcardparameter *param, const char *v)
Sets the IANA name of param to v.
vcardparameter * vcardparameter_clone(const vcardparameter *old)
Creates new vcardparameter as a clone of the given one.
void vcardparameter_set_xvalue(vcardparameter *param, const char *v)
Sets the X-value of param to v.
const char * vcardparameter_get_xvalue(const vcardparameter *param)
Returns the X-value of param.
const char * vcardparameter_get_xname(const vcardparameter *param)
Returns the X-name of param.
#define VCARD_INTEGER_LENGTH
vcardparameter * vcardparameter_new(vcardparameter_kind kind)
Creates new vcardparameter object.
char * vcardparameter_as_vcard_string_r(vcardparameter *param)
Converts vcardparameter into an string representation according to RFC5445/RFC6868.
Defines the data structure representing vCard parameters.
const char * vcardparameter_kind_to_string(vcardparameter_kind kind)
Returns a string representing the given vcardparameter_kind.
vcardparameter * vcardparameter_new_from_value_string(vcardparameter_kind kind, const char *value)
Creates new vcardparameter of a given kind with a given value.
vcardparameter_kind vcardparameter_string_to_kind(const char *string)
Returns the vcardparameter_kind for a given string.
vcardvalue_kind vcardparameter_kind_value_kind(const vcardparameter_kind kind, int *is_multivalued)
#define vcardstrarray_clone(array)
#define vcardstrarray_free(array)
#define vcardstrarray_element_at(array, position)
vcardstructuredtype * vcardstructured_clone(const vcardstructuredtype *st)
Clones a vcardstructuredtype.
void vcardstructured_unref(vcardstructuredtype *st)
Decrements the reference count of the vcardstructuredtype.
char * vcardstructured_as_vcard_string_r(const vcardstructuredtype *st, bool is_param)
Formats a vcardstructuredtype as a vCard property or parameter value.
Definition vcardvalue.c:814
bool vcardtime_is_valid_time(const struct vcardtimetype t)
Definition vcardtime.c:105
const char * vcardtime_as_vcard_string(const vcardtimetype t, unsigned flags)
Definition vcardtime.c:285