18#include "vcardproperty_p.h"
22#include "icalerror_p.h"
26#include "icaltypes_p.h"
30struct vcardproperty_impl {
32 vcardproperty_kind kind;
35 icalpvl_list parameters;
36 icalpvl_elem parameter_iterator;
38 vcardcomponent *parent;
41LIBICAL_VCARD_EXPORT
struct vcardproperty_impl *vcardproperty_new_impl(vcardproperty_kind kind)
54 memset(prop, 0,
sizeof(vcardproperty));
56 prop->id = ICAL_STRUCTURE_TYPE_PROPERTY;
58 prop->parameters = icalpvl_newlist();
63void vcardproperty_add_parameters(vcardproperty *prop, va_list args)
67 while ((vp = va_arg(args,
void *)) != 0) {
68 if (vcardvalue_isa_value(vp) != 0) {
70 vcardproperty_add_parameter((vcardproperty *)prop,
71 (vcardparameter *)vp);
78vcardproperty *vcardproperty_new(vcardproperty_kind kind)
80 if (kind == VCARD_NO_PROPERTY) {
84 return (vcardproperty *)vcardproperty_new_impl(kind);
92 icalerror_check_arg_rz((old != 0),
"old");
93 clone = vcardproperty_new_impl(old->kind);
94 icalerror_check_arg_rz((clone != 0),
"clone");
96 if (old->value != 0) {
97 clone->value = vcardvalue_clone(old->value);
100 if (old->x_name != 0) {
103 if (clone->x_name == 0) {
104 vcardproperty_free(clone);
110 for (p = icalpvl_head(old->parameters); p != 0; p = icalpvl_next(p)) {
114 vcardproperty_free(clone);
119 icalpvl_push(clone->parameters, param);
125vcardproperty *vcardproperty_new_from_string(
const char *str)
127 size_t buf_size = 1024;
131 vcardcomponent *comp;
134 icalerror_check_arg_rz((str != 0),
"str");
146 comp = vcardparser_parse_string(buf);
156 prop = vcardcomponent_get_first_property(comp, VCARD_ANY_PROPERTY);
158 vcardcomponent_remove_property(comp, prop);
160 vcardcomponent_free(comp);
164 vcardproperty_free(prop);
171void vcardproperty_free(vcardproperty *p)
173 vcardparameter *param;
175 icalerror_check_arg_rv((p != 0),
"prop");
177 if (p->parent != 0) {
183 vcardvalue_free(p->value);
186 while ((param = icalpvl_pop(p->parameters)) != 0) {
190 icalpvl_free(p->parameters);
192 if (p->x_name != 0) {
200 p->kind = VCARD_NO_PROPERTY;
202 p->parameter_iterator = 0;
205 p->id = ICAL_STRUCTURE_TYPE_PROPERTY_EMPTY;
212static const size_t MAX_LINE_LEN = 75;
214static char *get_next_line_start(
char *line_start,
size_t chars_left)
220 if (chars_left < MAX_LINE_LEN) {
221 return line_start + chars_left;
227 pos = line_start + MAX_LINE_LEN - 2;
228 while (pos > line_start) {
229 if (*pos ==
';' || *pos ==
':' || *pos ==
' ') {
238 pos = line_start + MAX_LINE_LEN - 1;
239 while (pos > line_start) {
241 if ((*pos & 128) == 0) {
246 if ((*pos & 192) == 192) {
256 return line_start + MAX_LINE_LEN - 1;
265static char *fold_property_line(
char *text)
267 size_t buf_size, len;
269 char *buf, *buf_ptr, *line_start, *next_line_start;
281 chars_left = (ssize_t)len;
284 if (chars_left <= 0) {
289 next_line_start = get_next_line_start(line_start, (
size_t)chars_left);
300 char ch = *next_line_start;
301 *next_line_start =
'\0';
303 *next_line_start = ch;
306 chars_left -= (next_line_start - line_start);
307 line_start = next_line_start;
318static const char *vcardproperty_get_value_kind(vcardproperty *prop,
319 const vcardvalue *value)
321 const char *kind_string = NULL;
325 vcardvalue_kind kind = VCARD_NO_VALUE;
326 vcardparameter *val_param =
327 vcardproperty_get_first_parameter(prop, VCARD_VALUE_PARAMETER);
328 vcardproperty_version version = vcardcomponent_get_version(prop->parent);
330 if (version == VCARD_VERSION_NONE) {
331 version = VCARD_VERSION_30;
335 kind = vcardparameter_value_to_value_kind(vcardparameter_get_value(val_param));
338 if (kind == VCARD_NO_VALUE && vcardproperty_isa(prop) != VCARD_X_PROPERTY) {
339 kind = vcardvalue_isa(value);
343 switch (vcardproperty_isa(prop)) {
344 case VCARD_GEO_PROPERTY:
348 if (kind != VCARD_X_VALUE) {
349 kind = VCARD_NO_VALUE;
353 case VCARD_KEY_PROPERTY:
354 case VCARD_LOGO_PROPERTY:
355 case VCARD_PHOTO_PROPERTY:
356 case VCARD_SOUND_PROPERTY:
360 if (version == VCARD_VERSION_40 ||
361 vcardproperty_get_first_parameter(prop, VCARD_ENCODING_PARAMETER)) {
362 kind = VCARD_NO_VALUE;
366 case VCARD_ANNIVERSARY_PROPERTY:
367 case VCARD_BDAY_PROPERTY:
368 case VCARD_DEATHDATE_PROPERTY:
372 if (version == VCARD_VERSION_40 &&
373 (kind == VCARD_TIME_VALUE || kind == VCARD_DATEANDORTIME_VALUE)) {
374 kind = VCARD_NO_VALUE;
375 }
else if (kind == VCARD_DATE_VALUE || kind == VCARD_DATETIME_VALUE) {
376 kind = VCARD_NO_VALUE;
380 case VCARD_TZ_PROPERTY:
384 if (version == VCARD_VERSION_40) {
385 if (kind == VCARD_TEXT_VALUE) {
386 kind = VCARD_NO_VALUE;
388 }
else if (kind == VCARD_UTCOFFSET_VALUE) {
389 kind = VCARD_NO_VALUE;
393 case VCARD_UID_PROPERTY:
397 if (version == VCARD_VERSION_40) {
398 if (kind == VCARD_URI_VALUE) {
399 kind = VCARD_NO_VALUE;
402 kind = VCARD_NO_VALUE;
410 if (kind != VCARD_NO_VALUE &&
411 !vcardproperty_value_kind_is_default(prop->kind, kind)) {
413 kind_string = vcardvalue_kind_to_string(kind);
421const char *vcardproperty_as_vcard_string(vcardproperty *prop)
425 buf = vcardproperty_as_vcard_string_r(prop);
430char *vcardproperty_as_vcard_string_r(vcardproperty *prop)
432 vcardparameter *param;
438 const char *property_name = 0;
439 size_t buf_size = 1024;
442 const vcardvalue *value;
444 const char *kind_string = 0;
446 icalerror_check_arg_rz((prop != 0),
"prop");
453 if (prop->kind == VCARD_X_PROPERTY && prop->x_name != 0) {
454 property_name = prop->x_name;
456 property_name = vcardproperty_kind_to_string(prop->kind);
459 if (property_name == 0) {
460 icalerror_warn(
"Got a property of an unknown kind.");
465 if (prop->group != 0) {
472 value = vcardproperty_get_value(prop);
473 kind_string = vcardproperty_get_value_kind(prop, value);
474 if (kind_string != 0) {
480 for (param = vcardproperty_get_first_parameter(prop, VCARD_ANY_PARAMETER);
481 param != 0; param = vcardproperty_get_next_parameter(prop, VCARD_ANY_PARAMETER)) {
486 if (kind_string == 0) {
487 icalerror_warn(
"Got a parameter of unknown kind for the following property");
489 icalerror_warn((property_name) ? property_name :
"(NULL)");
493 if (kind == VCARD_VALUE_PARAMETER) {
508 char *str = vcardvalue_as_vcard_string_r(value);
522 out_buf = fold_property_line(buf);
529vcardproperty_kind vcardproperty_isa(
const vcardproperty *p)
535 return VCARD_NO_PROPERTY;
538bool vcardproperty_isa_property(
void *property)
540 const vcardproperty *impl = (vcardproperty *)property;
542 icalerror_check_arg_rz((property != 0),
"property");
543 return (impl->id == ICAL_STRUCTURE_TYPE_PROPERTY);
546void vcardproperty_add_parameter(vcardproperty *p, vcardparameter *parameter)
548 icalerror_check_arg_rv((p != 0),
"prop");
549 icalerror_check_arg_rv((parameter != 0),
"parameter");
551 icalpvl_push(p->parameters, parameter);
554void vcardproperty_set_parameter(vcardproperty *prop, vcardparameter *parameter)
556 vcardparameter_kind kind;
558 icalerror_check_arg_rv((prop != 0),
"prop");
559 icalerror_check_arg_rv((parameter != 0),
"parameter");
562 if (kind == VCARD_X_PARAMETER) {
564 }
else if (kind == VCARD_IANA_PARAMETER) {
570 vcardproperty_add_parameter(prop, parameter);
573void vcardproperty_set_parameter_from_string(vcardproperty *prop,
574 const char *name,
const char *value)
576 vcardparameter_kind kind;
577 vcardparameter *param;
579 icalerror_check_arg_rv((prop != 0),
"prop");
580 icalerror_check_arg_rv((name != 0),
"name");
581 icalerror_check_arg_rv((value != 0),
"value");
585 if (kind == VCARD_NO_PARAMETER) {
597 if (kind == VCARD_X_PARAMETER) {
599 }
else if (kind == VCARD_IANA_PARAMETER) {
603 vcardproperty_set_parameter(prop, param);
606const char *vcardproperty_get_parameter_as_string(vcardproperty *prop,
const char *name)
610 buf = vcardproperty_get_parameter_as_string_r(prop, name);
615char *vcardproperty_get_parameter_as_string_r(vcardproperty *prop,
const char *name)
617 vcardparameter_kind kind;
618 vcardparameter *param;
624 icalerror_check_arg_rz((prop != 0),
"prop");
625 icalerror_check_arg_rz((name != 0),
"name");
629 if (kind == VCARD_NO_PARAMETER) {
634 for (param = vcardproperty_get_first_parameter(prop, kind);
635 param != 0; param = vcardproperty_get_next_parameter(prop, kind)) {
636 if (kind == VCARD_X_PARAMETER) {
640 }
else if (kind == VCARD_IANA_PARAMETER) {
655 t = strchr(str,
'=');
668 pvql = strchr(pv,
'"');
678 pvqr = strrchr(str,
'"');
693 icalerror_check_arg_rv((prop != 0),
"prop");
695 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
696 vcardparameter *param = (vcardparameter *)icalpvl_data(p);
699 (void)icalpvl_remove(prop->parameters, p);
710 icalerror_check_arg_rv((prop != 0),
"prop");
712 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
713 vcardparameter *param = (vcardparameter *)icalpvl_data(p);
714 const char *kind_string;
728 if (0 == strcmp(kind_string, name)) {
729 (void)icalpvl_remove(prop->parameters, p);
740 icalerror_check_arg_rv((prop != 0),
"prop");
741 icalerror_check_arg_rv((parameter != 0),
"parameter");
743 for (p = icalpvl_head(prop->parameters); p != 0; p = icalpvl_next(p)) {
744 vcardparameter *p_param = (vcardparameter *)icalpvl_data(p);
746 if (parameter == p_param) {
747 (void)icalpvl_remove(prop->parameters, p);
754int vcardproperty_count_parameters(
const vcardproperty *prop)
757 return icalpvl_count(prop->parameters);
764vcardparameter *vcardproperty_get_first_parameter(vcardproperty *p, vcardparameter_kind kind)
766 icalerror_check_arg_rz((p != 0),
"prop");
768 p->parameter_iterator = icalpvl_head(p->parameters);
770 if (p->parameter_iterator == 0) {
774 for (p->parameter_iterator = icalpvl_head(p->parameters);
775 p->parameter_iterator != 0; p->parameter_iterator = icalpvl_next(p->parameter_iterator)) {
776 vcardparameter *param = (vcardparameter *)icalpvl_data(p->parameter_iterator);
786vcardparameter *vcardproperty_get_next_parameter(vcardproperty *p, vcardparameter_kind kind)
788 icalerror_check_arg_rz((p != 0),
"prop");
790 if (p->parameter_iterator == 0) {
794 for (p->parameter_iterator = icalpvl_next(p->parameter_iterator);
795 p->parameter_iterator != 0; p->parameter_iterator = icalpvl_next(p->parameter_iterator)) {
796 vcardparameter *param = (vcardparameter *)icalpvl_data(p->parameter_iterator);
806void vcardproperty_set_value(vcardproperty *p, vcardvalue *value)
808 vcardvalue_kind kind;
810 icalerror_check_arg_rv((p != 0),
"prop");
811 icalerror_check_arg_rv((value != 0),
"value");
815 vcardvalue_free(p->value);
823 kind = vcardvalue_isa(value);
825 if (kind == VCARD_DATE_VALUE || kind == VCARD_DATETIME_VALUE) {
826 vcardparameter *val_param;
828 val_param = vcardproperty_get_first_parameter(p, VCARD_VALUE_PARAMETER);
831 vcardparameter_value_to_value_kind(vcardparameter_get_value(val_param)) != kind) {
837void vcardproperty_set_value_from_string(vcardproperty *prop,
const char *str,
const char *type)
840 vcardvalue_kind kind = VCARD_NO_VALUE;
842 icalerror_check_arg_rv((prop != 0),
"prop");
843 icalerror_check_arg_rv((str != 0),
"str");
844 icalerror_check_arg_rv((type != 0),
"type");
846 if (strcmp(type,
"NO") == 0) {
848 const vcardvalue *oval = vcardproperty_get_value(prop);
851 kind = vcardvalue_isa(oval);
854 kind = vcardproperty_kind_to_value_kind(vcardproperty_isa(prop));
858 kind = vcardvalue_string_to_kind(type);
861 if (kind == VCARD_NO_VALUE) {
867 nval = vcardvalue_new_from_string(kind, str);
875 vcardproperty_set_value(prop, nval);
878vcardvalue *vcardproperty_get_value(
const vcardproperty *prop)
880 icalerror_check_arg_rz((prop != 0),
"prop");
885const char *vcardproperty_get_value_as_string(
const vcardproperty *prop)
889 buf = vcardproperty_get_value_as_string_r(prop);
894char *vcardproperty_get_value_as_string_r(
const vcardproperty *prop)
896 const vcardvalue *value;
898 icalerror_check_arg_rz((prop != 0),
"prop");
902 return vcardvalue_as_vcard_string_r(value);
905void vcardproperty_set_x_name(vcardproperty *prop,
const char *name)
907 icalerror_check_arg_rv((name != 0),
"name");
908 icalerror_check_arg_rv((prop != 0),
"prop");
910 if (prop->x_name != 0) {
916 if (prop->x_name == 0) {
921const char *vcardproperty_get_x_name(
const vcardproperty *prop)
923 icalerror_check_arg_rz((prop != 0),
"prop");
928void vcardproperty_set_group(vcardproperty *prop,
const char *group)
930 icalerror_check_arg_rv((group != 0),
"group");
931 icalerror_check_arg_rv((prop != 0),
"prop");
933 if (prop->group != 0) {
939 if (prop->group == 0) {
944const char *vcardproperty_get_group(
const vcardproperty *prop)
946 icalerror_check_arg_rz((prop != 0),
"prop");
955 buf = vcardproperty_get_property_name_r(prop);
960char *vcardproperty_get_property_name_r(
const vcardproperty *prop)
962 const char *property_name = 0;
963 size_t buf_size = 256;
967 icalerror_check_arg_rz((prop != 0),
"prop");
972 if (prop->kind == VCARD_X_PROPERTY && prop->x_name != 0) {
973 property_name = prop->x_name;
975 property_name = vcardproperty_kind_to_string(prop->kind);
978 if (property_name == 0) {
994 icalerror_check_arg_rv((property != 0),
"property");
996 property->parent = comp;
1001 icalerror_check_arg_rz((property != 0),
"property");
1003 return property->parent;
1006static int param_compare(
void *a,
void *b)
1014 vcardproperty_kind prop_kind = vcardproperty_isa(prop);
1015 icalpvl_list sorted_params = icalpvl_newlist();
1016 vcardparameter *param;
1019 while ((param = icalpvl_pop(prop->parameters)) != 0) {
1024 case VCARD_VALUE_PARAMETER:
1026 switch (prop_kind) {
1027 case VCARD_TEL_PROPERTY:
1028 case VCARD_TZ_PROPERTY:
1029 case VCARD_BIRTHPLACE_PROPERTY:
1030 case VCARD_DEATHPLACE_PROPERTY:
1031 if (vcardparameter_get_value(param) == VCARD_VALUE_TEXT) {
1036 case VCARD_RELATED_PROPERTY:
1037 case VCARD_UID_PROPERTY:
1038 case VCARD_KEY_PROPERTY:
1039 if (vcardparameter_get_value(param) == VCARD_VALUE_URI) {
1044 case VCARD_BDAY_PROPERTY:
1045 case VCARD_ANNIVERSARY_PROPERTY:
1046 case VCARD_DEATHDATE_PROPERTY:
1047 if (vcardparameter_get_value(param) == VCARD_VALUE_DATEANDORTIME) {
1057 case VCARD_CALSCALE_PARAMETER:
1058 if (vcardparameter_get_calscale(param) == VCARD_CALSCALE_GREGORIAN) {
1063 case VCARD_PREF_PARAMETER:
1064 if (vcardparameter_get_pref(param) >= 100) {
1069 case VCARD_TYPE_PARAMETER:
1070 if (prop_kind == VCARD_TEL_PROPERTY) {
1072 vcardenumarray_element voice = {VCARD_TYPE_VOICE, NULL};
1074 if (vcardenumarray_find(vcardparameter_get_type(param),
1081 vcardenumarray_sort(vcardparameter_get_type(param));
1085 case VCARD_PID_PARAMETER:
1086 vcardstrarray_sort(vcardparameter_get_pid(param));
1089 case VCARD_SORTAS_PARAMETER:
1090 vcardstrarray_sort(vcardparameter_get_sortas(param));
1101 icalpvl_insert_ordered(sorted_params, param_compare, param);
1105 icalpvl_free(prop->parameters);
1106 prop->parameters = sorted_params;
1108 switch (prop_kind) {
1109 case VCARD_CATEGORIES_PROPERTY:
1110 vcardstrarray_sort(vcardproperty_get_categories(prop));
1113 case VCARD_NICKNAME_PROPERTY:
1114 vcardstrarray_sort(vcardproperty_get_nickname(prop));
1122void vcardproperty_add_type_parameter(vcardproperty *prop,
1123 vcardenumarray_element *type)
1125 vcardenumarray *types;
1126 vcardparameter *param =
1127 vcardproperty_get_first_parameter(prop, VCARD_TYPE_PARAMETER);
1130 types = vcardparameter_get_type(param);
1131 vcardenumarray_add(types, type);
1134 types = vcardenumarray_new(0);
1135 vcardenumarray_add(types, type);
1136 vcardparameter_set_type(param, types);
1137 vcardproperty_add_parameter(prop, param);
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
void icalerror_clear_errno(void)
Resets icalerrno to ICAL_NO_ERROR.
@ ICAL_MALFORMEDDATA_ERROR
#define icalerrno
Access the current icalerrno value.
void icalmemory_free_buffer(void *buf)
Releases a buffer.
char * icalmemory_strdup(const char *s)
Creates a duplicate of a string.
void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const char *string)
Appends a string to a buffer.
void * icalmemory_new_buffer(size_t size)
Creates new buffer with the specified size.
void icalmemory_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Common memory management routines.
bool icalproperty_get_allow_empty_properties(void)
Defines the data structure representing iCalendar properties.
int vcardcomponent_count_errors(vcardcomponent *comp)
Returns the number of errors encountered parsing the data.
Defines the data structure representing vCard components.
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.
Line-oriented parsing vCard format.
const char * vcardproperty_get_property_name(const vcardproperty *prop)
void vcardproperty_normalize(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.
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.
vcardproperty * vcardproperty_clone(const vcardproperty *old)
Deeply clones an vcardproperty.
void vcardproperty_remove_parameter_by_kind(vcardproperty *prop, vcardparameter_kind kind)
Removes all parameters with the specified kind.
bool vcardproperty_kind_is_valid(const vcardproperty_kind kind)
void vcardvalue_set_parent(vcardvalue *value, vcardproperty *property)
Defines the data structure representing vCard values.