Libical API Documentation 4.0 STABLE VERSION [Visit the v3.0 documentation]
Loading...
Searching...
No Matches
vcardproperty.c
Go to the documentation of this file.
1/*======================================================================
2 FILE: vcardproperty.c
3 CREATOR: Ken Murchison 24 Aug 2022
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 "vcardproperty_p.h"
19#include "vcardcomponent.h"
20#include "vcardparser.h"
21#include "vcardvalue.h"
22#include "icalerror_p.h"
23#include "icalmemory.h"
24#include "icalproperty.h"
25#include "icalpvl_p.h"
26#include "icaltypes_p.h"
27
28#include <stdlib.h>
29
30struct vcardproperty_impl {
31 icalstructuretype id;
33 char *x_name;
34 char *group;
35 icalpvl_list parameters;
36 icalpvl_elem parameter_iterator;
37 vcardvalue *value;
38 vcardcomponent *parent;
39};
40
41LIBICAL_VCARD_EXPORT struct vcardproperty_impl *vcardproperty_new_impl(vcardproperty_kind kind)
42{
43 vcardproperty *prop;
44
45 if (!vcardproperty_kind_is_valid(kind)) {
46 return NULL;
47 }
48
49 if ((prop = (vcardproperty *)icalmemory_new_buffer(sizeof(vcardproperty))) == 0) {
51 return 0;
52 }
53
54 memset(prop, 0, sizeof(vcardproperty));
55
56 prop->id = ICAL_STRUCTURE_TYPE_PROPERTY;
57 prop->kind = kind;
58 prop->parameters = icalpvl_newlist();
59
60 return prop;
61}
62
63void vcardproperty_add_parameters(vcardproperty *prop, va_list args)
64{
65 void *vp;
66
67 while ((vp = va_arg(args, void *)) != 0) {
68 if (vcardvalue_isa_value(vp)) {
69 } else if (vcardparameter_isa_parameter(vp)) {
70 vcardproperty_add_parameter((vcardproperty *)prop,
71 (vcardparameter *)vp);
72 } else {
74 }
75 }
76}
77
79{
80 if (kind == VCARD_NO_PROPERTY) {
81 return 0;
82 }
83
84 return (vcardproperty *)vcardproperty_new_impl(kind);
85}
86
87vcardproperty *vcardproperty_clone(const vcardproperty *old)
88{
89 vcardproperty *clone;
90 icalpvl_elem p;
91
92 icalerror_check_arg_rz((old != 0), "old");
93 clone = vcardproperty_new_impl(old->kind);
94 icalerror_check_arg_rz((clone != 0), "clone");
95
96 if (old->value != 0) {
97 clone->value = vcardvalue_clone(old->value);
98
99 if (clone->value == 0) {
100 vcardproperty_free(clone);
102 return 0;
103 }
104
105 vcardvalue_set_parent(clone->value, clone);
106 }
107
108 if (old->x_name != 0) {
109 clone->x_name = icalmemory_strdup(old->x_name);
110
111 if (clone->x_name == 0) {
112 vcardproperty_free(clone);
114 return 0;
115 }
116 }
117
118 if (old->group != 0) {
119 clone->group = icalmemory_strdup(old->group);
120
121 if (clone->group == 0) {
122 vcardproperty_free(clone);
124 return 0;
125 }
126 }
127
128 for (p = icalpvl_head(old->parameters); p != 0; p = icalpvl_next(p)) {
129 vcardparameter *param = vcardparameter_clone(icalpvl_data(p));
130
131 if (param == 0) {
132 vcardproperty_free(clone);
134 return 0;
135 }
136
137 icalpvl_push(clone->parameters, param);
138 }
139
140 return clone;
141}
142
143vcardproperty *vcardproperty_new_from_string(const char *str)
144{
145 size_t buf_size = 1024;
146 char *buf;
147 char *buf_ptr;
148 vcardproperty *prop;
149 vcardcomponent *comp;
150 int errors = 0;
151
152 icalerror_check_arg_rz((str != 0), "str");
153
154 buf = icalmemory_new_buffer(buf_size);
155 buf_ptr = buf;
156
157 /* Is this a HACK or a crafty reuse of code? */
158
159 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "BEGIN:VCARD\r\n");
160 icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
161 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "\r\n");
162 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "END:VCARD\r\n");
163
164 comp = vcardparser_parse_string(buf);
165
166 if (comp == 0) {
169 return 0;
170 }
171
172 errors = vcardcomponent_count_errors(comp);
173
175
177
180
181 if (errors > 0) {
182 vcardproperty_free(prop);
183 return 0;
184 } else {
185 return prop;
186 }
187}
188
189void vcardproperty_free(vcardproperty *p)
190{
191 vcardparameter *param;
192
193 icalerror_check_arg_rv((p != 0), "prop");
194
195 if (p->parent != 0) {
196 return;
197 }
198
199 if (p->value != 0) {
200 vcardvalue_set_parent(p->value, 0);
201 vcardvalue_free(p->value);
202 }
203
204 while ((param = icalpvl_pop(p->parameters)) != 0) {
205 vcardparameter_free(param);
206 }
207
208 icalpvl_free(p->parameters);
209
210 if (p->x_name != 0) {
211 icalmemory_free_buffer(p->x_name);
212 }
213
214 if (p->group != 0) {
215 icalmemory_free_buffer(p->group);
216 }
217
218 p->kind = VCARD_NO_PROPERTY;
219 p->parameters = 0;
220 p->parameter_iterator = 0;
221 p->value = 0;
222 p->x_name = 0;
223 p->id = ICAL_STRUCTURE_TYPE_PROPERTY_EMPTY;
224
226}
227
228/* This returns where the start of the next line should be. chars_left does
229 not include the trailing '\0'. */
230static const size_t MAX_LINE_LEN = 75;
231
232static char *get_next_line_start(char *line_start, size_t chars_left)
233{
234 char *pos;
235
236 /* If we have 74 chars or less left, we can output all of them.
237 we return a pointer to the '\0' at the end of the string. */
238 if (chars_left < MAX_LINE_LEN) {
239 return line_start + chars_left;
240 }
241
242 /* Now we jump to the last possible character of the line, and step back
243 trying to find a ';' ':' or ' '. If we find one, we return the character
244 after it. */
245 pos = line_start + MAX_LINE_LEN - 2;
246 while (pos > line_start) {
247 if (*pos == ';' || *pos == ':' || *pos == ' ') {
248 return pos + 1;
249 }
250 pos--;
251 }
252 /* Now try to split on a UTF-8 boundary defined as a 7-bit
253 value or as a byte with the two high-most bits set:
254 11xxxxxx. See https://czyborra.com/utf/ */
255
256 pos = line_start + MAX_LINE_LEN - 1;
257 while (pos > line_start) {
258 /* plain ascii */
259 if ((*pos & 128) == 0) {
260 return pos;
261 }
262
263 /* utf8 escape byte */
264 if ((*pos & 192) == 192) {
265 return pos;
266 }
267
268 pos--;
269 }
270
271 /* Give up, just break at 74 chars (the 75th char is the space at
272 the start of the line). */
273
274 return line_start + MAX_LINE_LEN - 1;
275}
276
283static char *fold_property_line(char *text)
284{
285 size_t buf_size, len;
286 ssize_t chars_left;
287 char *buf, *buf_ptr, *line_start, *next_line_start;
288 int first_line;
289
290 /* Start with a buffer twice the size of our property line, so we almost
291 certainly won't overflow it. */
292 len = strlen(text);
293 buf_size = len * 2;
294 buf = icalmemory_new_buffer(buf_size);
295 buf_ptr = buf;
296
297 /* Step through the text, finding each line to add to the output. */
298 line_start = text;
299 chars_left = (ssize_t)len;
300 first_line = 1;
301 for (;;) {
302 if (chars_left <= 0) {
303 break;
304 }
305
306 /* This returns the first character for the next line. */
307 next_line_start = get_next_line_start(line_start, (size_t)chars_left);
308
309 /* If this isn't the first line, we need to output a newline and space
310 first. */
311 if (!first_line) {
312 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "\r\n ");
313 }
314 first_line = 0;
315
316 /* This adds the line to our tmp buffer. We temporarily place a '\0'
317 in text, so we can copy the line in one go. */
318 char ch = *next_line_start;
319 *next_line_start = '\0';
320 icalmemory_append_string(&buf, &buf_ptr, &buf_size, line_start);
321 *next_line_start = ch;
322
323 /* Now we move on to the next line. */
324 chars_left -= (next_line_start - line_start);
325 line_start = next_line_start;
326 }
327
328 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "\r\n");
329
330 return buf;
331}
332
333/* Determine what VALUE parameter to include. The VALUE parameters
334 are ignored in the normal parameter printing ( the block after
335 this one, so we need to do it here */
336static const char *vcardproperty_get_value_kind(vcardproperty *prop,
337 const vcardvalue *value)
338{
339 const char *kind_string = NULL;
340 if (!prop) {
341 return kind_string;
342 }
344 vcardparameter *val_param =
347
348 if (version == VCARD_VERSION_NONE) {
349 version = VCARD_VERSION_30;
350 }
351
352 if (val_param) {
354 }
355
356 if (kind == VCARD_NO_VALUE && vcardproperty_isa(prop) != VCARD_X_PROPERTY) {
357 kind = vcardvalue_isa(value);
358 }
359
360 /* Try to omit VERSION-dependent default VALUE kinds */
361 switch (vcardproperty_isa(prop)) {
363 /* v3 default: VALUE=STRUCTURED
364 * v4 default: VALUE=URL
365 */
366 if (kind != VCARD_X_VALUE) {
367 kind = VCARD_NO_VALUE;
368 }
369 break;
370
375 /* v3 default: VALUE=BINARY (ENCODING=b parameter)
376 * v4 default: VALUE=URL
377 */
378 if (version == VCARD_VERSION_40 ||
380 kind = VCARD_NO_VALUE;
381 }
382 break;
383
387 /* TIME is v4 specific.
388 * All other types are self-evident.
389 * TIMESTAMP is not a valid value type of these properties,
390 * but a timestamp is a valid date-time.
391 */
392 if (version == VCARD_VERSION_40 &&
393 (kind == VCARD_TIME_VALUE || kind == VCARD_DATEANDORTIME_VALUE)) {
394 kind = VCARD_NO_VALUE;
395 } else if (kind == VCARD_DATE_VALUE || kind == VCARD_DATETIME_VALUE ||
396 kind == VCARD_TIMESTAMP_VALUE) {
397 kind = VCARD_NO_VALUE;
398 }
399 break;
400
402 /* v3 default: VALUE=UTCOFFSET
403 * v4 default: VALUE=TEXT
404 */
405 if (version == VCARD_VERSION_40) {
406 if (kind == VCARD_TEXT_VALUE) {
407 kind = VCARD_NO_VALUE;
408 }
409 } else if (kind == VCARD_UTCOFFSET_VALUE) {
410 kind = VCARD_NO_VALUE;
411 }
412 break;
413
415 /* v3 default: VALUE=TEXT
416 * v4 default: VALUE=URI
417 */
418 if (version == VCARD_VERSION_40) {
419 if (kind == VCARD_URI_VALUE) {
420 kind = VCARD_NO_VALUE;
421 }
422 } else {
423 kind = VCARD_NO_VALUE;
424 }
425 break;
426
427 default:
428 break;
429 }
430
431 if (kind != VCARD_NO_VALUE &&
432 !vcardproperty_value_kind_is_default(prop->kind, kind)) {
433 /* Not the default, so it must be specified */
434 kind_string = vcardvalue_kind_to_string(kind);
435 } else {
436 /* Don't include the VALUE parameter at all */
437 }
438
439 return kind_string;
440}
441
442const char *vcardproperty_as_vcard_string(vcardproperty *prop)
443{
444 char *buf;
445
448 return buf;
449}
450
451char *vcardproperty_as_vcard_string_r(vcardproperty *prop)
452{
453 vcardparameter *param;
454
455 /* Create new buffer that we can append names, parameters and a
456 * value to, and reallocate as needed.
457 */
458
459 const char *property_name = 0;
460 size_t buf_size = 1024;
461 char *buf;
462 char *buf_ptr;
463 const vcardvalue *value;
464 char *out_buf;
465 const char *kind_string = 0;
466
467 icalerror_check_arg_rz((prop != 0), "prop");
468
469 buf = icalmemory_new_buffer(buf_size);
470 buf_ptr = buf;
471
472 /* Append property name */
473
474 if (prop->kind == VCARD_X_PROPERTY && prop->x_name != 0) {
475 property_name = prop->x_name;
476 } else {
477 property_name = vcardproperty_kind_to_string(prop->kind);
478 }
479
480 if (property_name == 0) {
481 icalerror_warn("Got a property of an unknown kind.");
483 return 0;
484 }
485
486 if (prop->group != 0) {
487 icalmemory_append_string(&buf, &buf_ptr, &buf_size, prop->group);
488 icalmemory_append_string(&buf, &buf_ptr, &buf_size, ".");
489 }
490
491 icalmemory_append_string(&buf, &buf_ptr, &buf_size, property_name);
492
493 value = vcardproperty_get_value(prop);
494 kind_string = vcardproperty_get_value_kind(prop, value);
495 if (kind_string != 0) {
496 icalmemory_append_string(&buf, &buf_ptr, &buf_size, ";VALUE=");
497 icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
498 }
499
500 /* Append parameters */
502 param != 0; param = vcardproperty_get_next_parameter(prop, VCARD_ANY_PARAMETER)) {
504
505 kind_string = vcardparameter_as_vcard_string_r(param);
506
507 if (kind_string == 0) {
508 icalerror_warn("Got a parameter of unknown kind for the following property");
509
510 icalerror_warn((property_name) ? property_name : "(NULL)");
511 continue;
512 }
513
514 if (kind == VCARD_VALUE_PARAMETER) {
515 icalmemory_free_buffer((char *)kind_string);
516 continue;
517 }
518
519 icalmemory_append_string(&buf, &buf_ptr, &buf_size, ";");
520 icalmemory_append_string(&buf, &buf_ptr, &buf_size, kind_string);
521 icalmemory_free_buffer((char *)kind_string);
522 }
523
524 /* Append value */
525
526 icalmemory_append_string(&buf, &buf_ptr, &buf_size, ":");
527
528 if (value != 0) {
529 char *str = vcardvalue_as_vcard_string_r(value);
530
531 if (str != 0) {
532 icalmemory_append_string(&buf, &buf_ptr, &buf_size, str);
534 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "ERROR: No Value");
535 }
538 icalmemory_append_string(&buf, &buf_ptr, &buf_size, "ERROR: No Value");
539 }
540
541 /* We now use a function to fold the line properly every 75 characters.
542 That function also adds the newline for us. */
543 out_buf = fold_property_line(buf);
544
546
547 return out_buf;
548}
549
551{
552 if (p != 0) {
553 return p->kind;
554 }
555
556 return VCARD_NO_PROPERTY;
557}
558
559bool vcardproperty_isa_property(void *property)
560{
561 const vcardproperty *impl = (vcardproperty *)property;
562
563 icalerror_check_arg_rz((property != 0), "property");
564 return (impl->id == ICAL_STRUCTURE_TYPE_PROPERTY);
565}
566
567void vcardproperty_add_parameter(vcardproperty *p, vcardparameter *parameter)
568{
569 icalerror_check_arg_rv((p != 0), "prop");
570 icalerror_check_arg_rv((parameter != 0), "parameter");
571
572 icalpvl_push(p->parameters, parameter);
573}
574
575void vcardproperty_set_parameter(vcardproperty *prop, vcardparameter *parameter)
576{
578
579 icalerror_check_arg_rv((prop != 0), "prop");
580 icalerror_check_arg_rv((parameter != 0), "parameter");
581
582 kind = vcardparameter_isa(parameter);
583 if (kind == VCARD_X_PARAMETER) {
585 } else if (kind == VCARD_IANA_PARAMETER) {
587 } else {
589 }
590
591 vcardproperty_add_parameter(prop, parameter);
592}
593
595 const char *name, const char *value)
596{
598 vcardparameter *param;
599
600 icalerror_check_arg_rv((prop != 0), "prop");
601 icalerror_check_arg_rv((name != 0), "name");
602 icalerror_check_arg_rv((value != 0), "value");
603
605
606 if (kind == VCARD_NO_PARAMETER) {
608 return;
609 }
610
611 param = vcardparameter_new_from_value_string(kind, value);
612
613 if (param == 0) {
615 return;
616 }
617
618 if (kind == VCARD_X_PARAMETER) {
619 vcardparameter_set_xname(param, name);
620 } else if (kind == VCARD_IANA_PARAMETER) {
621 vcardparameter_set_iana_name(param, name);
622 }
623
624 vcardproperty_set_parameter(prop, param);
625}
626
627const char *vcardproperty_get_parameter_as_string(vcardproperty *prop, const char *name)
628{
629 char *buf;
630
633 return buf;
634}
635
636char *vcardproperty_get_parameter_as_string_r(vcardproperty *prop, const char *name)
637{
639 vcardparameter *param;
640 char *str;
641 char *pv, *t;
642 char *pvql;
643 char *pvqr;
644
645 icalerror_check_arg_rz((prop != 0), "prop");
646 icalerror_check_arg_rz((name != 0), "name");
647
649
650 if (kind == VCARD_NO_PARAMETER) {
651 /* icalenum_string_to_parameter_kind will set icalerrno */
652 return 0;
653 }
654
655 for (param = vcardproperty_get_first_parameter(prop, kind);
656 param != 0; param = vcardproperty_get_next_parameter(prop, kind)) {
657 if (kind == VCARD_X_PARAMETER) {
658 if (strcmp(vcardparameter_get_xname(param), name) == 0) {
659 break;
660 }
661 } else if (kind == VCARD_IANA_PARAMETER) {
662 if (strcmp(vcardparameter_get_iana_name(param), name) == 0) {
663 break;
664 }
665 } else {
666 break;
667 }
668 }
669
670 if (param == 0) {
671 return 0;
672 }
673
675
676 t = strchr(str, '=');
677
678 if (t == 0) {
681 return 0;
682 }
683
684 /* Strip the property name and the equal sign */
685 pv = icalmemory_strdup(t + 1);
687
688 /* Is the string quoted? */
689 pvql = strchr(pv, '"');
690 if (pvql == 0) {
691 return pv; /* No quotes? Return it immediately. */
692 }
693
694 /* Strip everything up to the first quote */
695 str = icalmemory_strdup(pvql + 1);
697
698 /* Search for the end quote */
699 pvqr = strrchr(str, '"');
700 if (pvqr == 0) {
703 return 0;
704 }
705
706 *pvqr = '\0';
707 return str;
708}
709
711{
712 icalpvl_elem p;
713
714 icalerror_check_arg_rv((prop != 0), "prop");
715
716 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
717 vcardparameter *param = (vcardparameter *)icalpvl_data(p);
718
719 if (vcardparameter_isa(param) == kind) {
720 (void)icalpvl_remove(prop->parameters, p);
721 vcardparameter_free(param);
722 break;
723 }
724 }
725}
726
727void vcardproperty_remove_parameter_by_name(vcardproperty *prop, const char *name)
728{
729 icalpvl_elem p;
730
731 icalerror_check_arg_rv((prop != 0), "prop");
732
733 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
734 vcardparameter *param = (vcardparameter *)icalpvl_data(p);
735 const char *kind_string;
736
738 kind_string = vcardparameter_get_xname(param);
739 } else if (vcardparameter_isa(param) == VCARD_IANA_PARAMETER) {
740 kind_string = vcardparameter_get_iana_name(param);
741 } else {
743 }
744
745 if (!kind_string) {
746 continue;
747 }
748
749 if (0 == strcmp(kind_string, name)) {
750 (void)icalpvl_remove(prop->parameters, p);
751 vcardparameter_free(param);
752 break;
753 }
754 }
755}
756
757void vcardproperty_remove_parameter_by_ref(vcardproperty *prop, vcardparameter *parameter)
758{
759 icalpvl_elem p;
760
761 icalerror_check_arg_rv((prop != 0), "prop");
762 icalerror_check_arg_rv((parameter != 0), "parameter");
763
764 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
765 vcardparameter *p_param = (vcardparameter *)icalpvl_data(p);
766
767 if (parameter == p_param) {
768 (void)icalpvl_remove(prop->parameters, p);
769 vcardparameter_free(p_param);
770 break;
771 }
772 }
773}
774
775int vcardproperty_count_parameters(const vcardproperty *prop)
776{
777 if (prop != 0) {
778 return icalpvl_count(prop->parameters);
779 }
780
782 return -1;
783}
784
786{
787 icalerror_check_arg_rz((p != 0), "prop");
788
789 p->parameter_iterator = icalpvl_head(p->parameters);
790
791 if (p->parameter_iterator == 0) {
792 return 0;
793 }
794
795 for (p->parameter_iterator = icalpvl_head(p->parameters);
796 p->parameter_iterator != 0; p->parameter_iterator = icalpvl_next(p->parameter_iterator)) {
797 vcardparameter *param = (vcardparameter *)icalpvl_data(p->parameter_iterator);
798
799 if (vcardparameter_isa(param) == kind || kind == VCARD_ANY_PARAMETER) {
800 return param;
801 }
802 }
803
804 return 0;
805}
806
808{
809 icalerror_check_arg_rz((p != 0), "prop");
810
811 if (p->parameter_iterator == 0) {
812 return 0;
813 }
814
815 for (p->parameter_iterator = icalpvl_next(p->parameter_iterator);
816 p->parameter_iterator != 0; p->parameter_iterator = icalpvl_next(p->parameter_iterator)) {
817 vcardparameter *param = (vcardparameter *)icalpvl_data(p->parameter_iterator);
818
819 if (vcardparameter_isa(param) == kind || kind == VCARD_ANY_PARAMETER) {
820 return param;
821 }
822 }
823
824 return 0;
825}
826
827void vcardproperty_set_value(vcardproperty *p, vcardvalue *value)
828{
829 vcardvalue_kind kind;
830
831 icalerror_check_arg_rv((p != 0), "prop");
832 icalerror_check_arg_rv((value != 0), "value");
833
834 if (p->value != 0) {
835 vcardvalue_set_parent(p->value, 0);
836 vcardvalue_free(p->value);
837 p->value = 0;
838 }
839
840 p->value = value;
841
842 vcardvalue_set_parent(value, p);
843
844 kind = vcardvalue_isa(value);
845
846 if (kind == VCARD_DATE_VALUE || kind == VCARD_DATETIME_VALUE) {
847 vcardparameter *val_param;
848
850
851 if (val_param &&
854 }
855 }
856}
857
858void vcardproperty_set_value_from_string(vcardproperty *prop, const char *str, const char *type)
859{
860 vcardvalue *nval;
862
863 icalerror_check_arg_rv((prop != 0), "prop");
864 icalerror_check_arg_rv((str != 0), "str");
865 icalerror_check_arg_rv((type != 0), "type");
866
867 if (strcmp(type, "NO") == 0) {
868 /* Get the type from the value the property already has, if it exists */
869 const vcardvalue *oval = vcardproperty_get_value(prop);
870 if (oval != 0) {
871 /* Use the existing value kind */
872 kind = vcardvalue_isa(oval);
873 } else {
874 /* Use the default kind for the property */
876 }
877 } else {
878 /* Use the given kind string */
879 kind = vcardvalue_string_to_kind(type);
880 }
881
882 if (kind == VCARD_NO_VALUE) {
884 return;
885 }
886
888 nval = vcardvalue_new_from_string(kind, str);
889
890 if (nval == 0) {
891 /* vcardvalue_new_from_string sets errno */
892 icalassert(icalerrno != ICAL_NO_ERROR);
893 return;
894 }
895
896 vcardproperty_set_value(prop, nval);
897}
898
899vcardvalue *vcardproperty_get_value(const vcardproperty *prop)
900{
901 icalerror_check_arg_rz((prop != 0), "prop");
902
903 return prop->value;
904}
905
906const char *vcardproperty_get_value_as_string(const vcardproperty *prop)
907{
908 char *buf;
909
912 return buf;
913}
914
915char *vcardproperty_get_value_as_string_r(const vcardproperty *prop)
916{
917 const vcardvalue *value;
918
919 icalerror_check_arg_rz((prop != 0), "prop");
920
921 value = prop->value;
922
923 return vcardvalue_as_vcard_string_r(value);
924}
925
926void vcardproperty_set_x_name(vcardproperty *prop, const char *name)
927{
928 icalerror_check_arg_rv((name != 0), "name");
929 icalerror_check_arg_rv((prop != 0), "prop");
930
931 if (prop->x_name != 0) {
932 icalmemory_free_buffer(prop->x_name);
933 }
934
935 prop->x_name = icalmemory_strdup(name);
936
937 if (prop->x_name == 0) {
939 }
940}
941
942const char *vcardproperty_get_x_name(const vcardproperty *prop)
943{
944 icalerror_check_arg_rz((prop != 0), "prop");
945
946 return prop->x_name;
947}
948
949void vcardproperty_set_group(vcardproperty *prop, const char *group)
950{
951 icalerror_check_arg_rv((group != 0), "group");
952 icalerror_check_arg_rv((prop != 0), "prop");
953
954 if (prop->group != 0) {
955 icalmemory_free_buffer(prop->group);
956 }
957
958 prop->group = icalmemory_strdup(group);
959
960 if (prop->group == 0) {
962 }
963}
964
965const char *vcardproperty_get_group(const vcardproperty *prop)
966{
967 icalerror_check_arg_rz((prop != 0), "prop");
968
969 return prop->group;
970}
971
972const char *vcardproperty_get_property_name(const vcardproperty *prop)
973{
974 char *buf;
975
978 return buf;
979}
980
981char *vcardproperty_get_property_name_r(const vcardproperty *prop)
982{
983 const char *property_name = 0;
984 size_t buf_size = 256;
985 char *buf;
986 char *buf_ptr;
987
988 icalerror_check_arg_rz((prop != 0), "prop");
989
990 buf = icalmemory_new_buffer(buf_size);
991 buf_ptr = buf;
992
993 if (prop->kind == VCARD_X_PROPERTY && prop->x_name != 0) {
994 property_name = prop->x_name;
995 } else {
996 property_name = vcardproperty_kind_to_string(prop->kind);
997 }
998
999 if (property_name == 0) {
1002 return 0;
1003
1004 } else {
1005 /* _append_string will automatically grow the buffer if
1006 property_name is longer than the initial buffer size */
1007 icalmemory_append_string(&buf, &buf_ptr, &buf_size, property_name);
1008 }
1009
1010 return buf;
1011}
1012
1013void vcardproperty_set_parent(vcardproperty *property, vcardcomponent *comp)
1014{
1015 icalerror_check_arg_rv((property != 0), "property");
1016
1017 property->parent = comp;
1018}
1019
1020vcardcomponent *vcardproperty_get_parent(const vcardproperty *property)
1021{
1022 icalerror_check_arg_rz((property != 0), "property");
1023
1024 return property->parent;
1025}
1026
1027static int param_compare(void *a, void *b)
1028{
1031}
1032
1033void vcardproperty_normalize(vcardproperty *prop)
1034{
1035 vcardproperty_kind prop_kind = vcardproperty_isa(prop);
1036 icalpvl_list sorted_params = icalpvl_newlist();
1037 vcardparameter *param;
1038
1039 /* Normalize parameters into sorted list */
1040 while ((param = icalpvl_pop(prop->parameters)) != 0) {
1041 int remove = 0;
1042
1043 /* Remove parameters having default values */
1044 switch (vcardparameter_isa(param)) {
1046 /* Skip VALUE parameters for default property value types */
1047 switch (prop_kind) {
1048 case VCARD_TEL_PROPERTY:
1049 case VCARD_TZ_PROPERTY:
1053 remove = 1;
1054 }
1055 break;
1056
1058 case VCARD_UID_PROPERTY:
1059 case VCARD_KEY_PROPERTY:
1061 remove = 1;
1062 }
1063 break;
1064
1069 remove = 1;
1070 }
1071 break;
1072
1073 default:
1074 break;
1075 }
1076 break;
1077
1080 remove = 1;
1081 }
1082 break;
1083
1085 if (vcardparameter_get_pref(param) >= 100) {
1086 remove = 1;
1087 }
1088 break;
1089
1091 if (prop_kind == VCARD_TEL_PROPERTY) {
1092 /* Is it just TYPE=VOICE ? */
1094
1096 &voice) == 0) {
1097 remove = 1;
1098 }
1099 }
1100
1101 if (!remove) {
1103 }
1104 break;
1105
1108 break;
1109
1112 break;
1113
1114 default:
1115 break;
1116 }
1117
1118 if (remove) {
1119 vcardparameter_set_parent(param, 0); // MUST NOT have a parent to free
1120 vcardparameter_free(param);
1121 } else {
1122 icalpvl_insert_ordered(sorted_params, param_compare, param);
1123 }
1124 }
1125
1126 icalpvl_free(prop->parameters);
1127 prop->parameters = sorted_params;
1128
1129 switch (prop_kind) {
1132 break;
1133
1136 break;
1137
1138 default:
1139 break;
1140 }
1141}
1142
1143void vcardproperty_add_type_parameter(vcardproperty *prop,
1145{
1146 vcardenumarray *types;
1147 vcardparameter *param =
1149
1150 if (param) {
1151 types = vcardparameter_get_type(param);
1152 vcardenumarray_add(types, type);
1153 } else {
1155 types = vcardenumarray_new(0);
1156 vcardenumarray_add(types, type);
1157 vcardparameter_set_type(param, types);
1158 vcardproperty_add_parameter(prop, param);
1159 }
1160}
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
Definition icalerror.c:90
void icalerror_clear_errno(void)
Resets icalerrno to ICAL_NO_ERROR.
Definition icalerror.c:85
@ ICAL_NEWFAILED_ERROR
Definition icalerror.h:50
@ ICAL_BADARG_ERROR
Definition icalerror.h:47
@ ICAL_INTERNAL_ERROR
Definition icalerror.h:65
@ ICAL_NO_ERROR
Definition icalerror.h:44
@ ICAL_MALFORMEDDATA_ERROR
Definition icalerror.h:59
@ ICAL_PARSE_ERROR
Definition icalerror.h:62
@ ICAL_USAGE_ERROR
Definition icalerror.h:71
#define icalerrno
Access the current icalerrno value.
Definition icalerror.h:133
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_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Definition icalmemory.c:158
Common memory management routines.
bool icalproperty_get_allow_empty_properties(void)
Defines the data structure representing iCalendar properties.
void vcardcomponent_remove_property(vcardcomponent *comp, vcardproperty *property)
enum vcardproperty_version vcardcomponent_get_version(vcardcomponent *comp)
int vcardcomponent_count_errors(vcardcomponent *comp)
Returns the number of errors encountered parsing the data.
void vcardcomponent_free(vcardcomponent *c)
vcardproperty * vcardcomponent_get_first_property(vcardcomponent *c, vcardproperty_kind kind)
Defines the data structure representing vCard components.
vcardcomponent * vcardproperty_get_parent(const vcardproperty *property)
Returns the parent vcard for the specified property.
void vcardproperty_set_parent(vcardproperty *property, vcardcomponent *comp)
Sets the parent vcard for the specified vcardproperty property.
void vcardparameter_set_type(vcardparameter *value, vcardenumarray *v)
vcardstrarray * vcardparameter_get_pid(const vcardparameter *value)
struct vcardparameter_impl vcardparameter
vcardenumarray * vcardparameter_get_type(const vcardparameter *value)
vcardparameter_value vcardparameter_get_value(const vcardparameter *value)
int vcardparameter_get_pref(const vcardparameter *value)
vcardstrarray * vcardparameter_get_sortas(const vcardparameter *value)
vcardparameter_calscale vcardparameter_get_calscale(const vcardparameter *value)
@ VCARD_VALUE_DATEANDORTIME
@ VCARD_VALUE_URI
@ VCARD_VALUE_TEXT
vcardparameter_kind
@ VCARD_ENCODING_PARAMETER
@ VCARD_NO_PARAMETER
@ VCARD_TYPE_PARAMETER
@ VCARD_X_PARAMETER
@ VCARD_PID_PARAMETER
@ VCARD_SORTAS_PARAMETER
@ VCARD_CALSCALE_PARAMETER
@ VCARD_PREF_PARAMETER
@ VCARD_VALUE_PARAMETER
@ VCARD_IANA_PARAMETER
@ VCARD_ANY_PARAMETER
@ VCARD_CALSCALE_GREGORIAN
@ VCARD_TYPE_VOICE
vcardstrarray * vcardproperty_get_categories(const vcardproperty *prop)
vcardproperty_kind
@ VCARD_NO_PROPERTY
@ VCARD_X_PROPERTY
@ VCARD_TEL_PROPERTY
@ VCARD_GEO_PROPERTY
@ VCARD_RELATED_PROPERTY
@ VCARD_UID_PROPERTY
@ VCARD_CATEGORIES_PROPERTY
@ VCARD_ANY_PROPERTY
@ VCARD_PHOTO_PROPERTY
@ VCARD_DEATHPLACE_PROPERTY
@ VCARD_LOGO_PROPERTY
@ VCARD_SOUND_PROPERTY
@ VCARD_DEATHDATE_PROPERTY
@ VCARD_TZ_PROPERTY
@ VCARD_NICKNAME_PROPERTY
@ VCARD_ANNIVERSARY_PROPERTY
@ VCARD_BIRTHPLACE_PROPERTY
@ VCARD_BDAY_PROPERTY
@ VCARD_KEY_PROPERTY
vcardstrarray * vcardproperty_get_nickname(const vcardproperty *prop)
struct vcardvalue_impl vcardvalue
vcardproperty_version
@ VCARD_VERSION_30
@ VCARD_VERSION_40
@ VCARD_VERSION_NONE
vcardvalue_kind
@ VCARD_TIMESTAMP_VALUE
@ VCARD_URI_VALUE
@ VCARD_X_VALUE
@ VCARD_DATETIME_VALUE
@ VCARD_UTCOFFSET_VALUE
@ VCARD_DATE_VALUE
@ VCARD_DATEANDORTIME_VALUE
@ VCARD_NO_VALUE
@ VCARD_TEXT_VALUE
@ VCARD_TIME_VALUE
#define vcardenumarray_add(array, add)
icalenumarray_element vcardenumarray_element
#define vcardenumarray_sort(array)
#define vcardenumarray_new(increment_size)
icalenumarray vcardenumarray
#define vcardenumarray_find(array, needle)
char * vcardparameter_as_vcard_string(vcardparameter *param)
Converts vcardparameter into a string representation.
void vcardparameter_set_xname(vcardparameter *param, const char *v)
Sets the X-name of param to v.
void vcardparameter_free(vcardparameter *param)
Frees an vcardparameter object.
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_isa_parameter(void *parameter)
vcardparameter_kind vcardparameter_isa(const vcardparameter *parameter)
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.
const char * vcardparameter_get_xname(const vcardparameter *param)
Returns the X-name of param.
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.
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.
vcardcomponent * vcardparser_parse_string(const char *str)
Line-oriented parsing vCard format.
vcardproperty * vcardproperty_new(vcardproperty_kind kind)
void vcardproperty_set_parameter_from_string(vcardproperty *prop, const char *name, const char *value)
const char * vcardproperty_get_property_name(const vcardproperty *prop)
void vcardproperty_set_group(vcardproperty *prop, const char *group)
vcardproperty * vcardproperty_new_from_string(const char *str)
void vcardproperty_set_x_name(vcardproperty *prop, const char *name)
void vcardproperty_free(vcardproperty *p)
void vcardproperty_add_parameters(vcardproperty *prop, va_list args)
void vcardproperty_set_value_from_string(vcardproperty *prop, const char *str, const char *type)
const char * vcardproperty_as_vcard_string(vcardproperty *prop)
void vcardproperty_set_value(vcardproperty *p, vcardvalue *value)
void vcardproperty_normalize(vcardproperty *prop)
const char * vcardproperty_get_value_as_string(const vcardproperty *prop)
void vcardproperty_remove_parameter_by_name(vcardproperty *prop, const char *name)
Removes all parameters with the specified name.
void vcardproperty_remove_parameter_by_ref(vcardproperty *prop, vcardparameter *parameter)
Removes the specified parameter reference from the property.
char * vcardproperty_get_value_as_string_r(const vcardproperty *prop)
const char * vcardproperty_get_x_name(const vcardproperty *prop)
struct vcardproperty_impl * vcardproperty_new_impl(vcardproperty_kind kind)
int vcardproperty_count_parameters(const vcardproperty *prop)
bool vcardproperty_isa_property(void *property)
vcardparameter * vcardproperty_get_first_parameter(vcardproperty *p, vcardparameter_kind kind)
vcardvalue * vcardproperty_get_value(const vcardproperty *prop)
void vcardproperty_add_type_parameter(vcardproperty *prop, vcardenumarray_element *type)
const char * vcardproperty_get_group(const vcardproperty *prop)
vcardproperty * vcardproperty_clone(const vcardproperty *old)
Deeply clones an vcardproperty.
char * vcardproperty_get_parameter_as_string_r(vcardproperty *prop, const char *name)
void vcardproperty_remove_parameter_by_kind(vcardproperty *prop, vcardparameter_kind kind)
Removes all parameters with the specified kind.
char * vcardproperty_get_property_name_r(const vcardproperty *prop)
void vcardproperty_add_parameter(vcardproperty *p, vcardparameter *parameter)
char * vcardproperty_as_vcard_string_r(vcardproperty *prop)
void vcardproperty_set_parameter(vcardproperty *prop, vcardparameter *parameter)
vcardproperty_kind vcardproperty_isa(const vcardproperty *p)
vcardparameter * vcardproperty_get_next_parameter(vcardproperty *p, vcardparameter_kind kind)
const char * vcardproperty_get_parameter_as_string(vcardproperty *prop, const char *name)
bool vcardproperty_kind_is_valid(const vcardproperty_kind kind)
vcardvalue_kind vcardproperty_kind_to_value_kind(vcardproperty_kind kind)
void vcardvalue_set_parent(vcardvalue *value, vcardproperty *property)
vcardvalue_kind vcardparameter_value_to_value_kind(vcardparameter_value value)
const char * vcardproperty_kind_to_string(vcardproperty_kind kind)
#define vcardstrarray_sort(array)
vcardvalue * vcardvalue_new_from_string(vcardvalue_kind kind, const char *str)
Definition vcardvalue.c:601
bool vcardvalue_isa_value(void *value)
vcardvalue_kind vcardvalue_isa(const vcardvalue *value)
char * vcardvalue_as_vcard_string_r(const vcardvalue *value)
Definition vcardvalue.c:905
vcardvalue * vcardvalue_clone(const vcardvalue *old)
Definition vcardvalue.c:60
void vcardvalue_free(vcardvalue *v)
Definition vcardvalue.c:606
Defines the data structure representing vCard values.
vcardvalue_kind vcardvalue_string_to_kind(const char *str)
const char * vcardvalue_kind_to_string(const vcardvalue_kind kind)