19#include "vcardvalueimpl.h"
23#include "icalerror_p.h"
30#define TMP_BUF_SIZE 1024
34 struct vcardvalue_impl *v;
45 v->id = ICAL_STRUCTURE_TYPE_VALUE;
50 memset(&v->data, 0,
sizeof(v->data));
62 struct vcardvalue_impl *clone;
71 clone->kind = old->kind;
72 clone->size = old->size;
74 switch (clone->kind) {
78 if (old->data.v_string != 0) {
81 if (clone->data.v_string == 0) {
90 if (old->x_value != 0) {
93 if (clone->x_value == 0) {
104 if (old->data.v_structured != 0) {
105 clone->data.v_structured =
108 if (clone->data.v_structured == 0) {
117 if (old->data.v_textlist != 0) {
120 if (clone->data.v_textlist == 0) {
134 clone->data = old->data;
157 for (p = *str; *p != 0; p++) {
202 }
else if (sep && strchr(sep, *p)) {
228static char *vcardmemory_strdup_and_quote(
char **str,
char **str_p,
size_t *buf_sz,
229 const char *unquoted_str,
bool is_param)
234 *buf_sz = strlen(unquoted_str) + 1;
244 for (p = unquoted_str; *p != 0; p++) {
289 struct vcardvalue_impl *value;
293 value->data.v_enum = e;
297 value->data.v_enum = x_type;
310static bool simple_str_to_doublestr(
const char *from,
char *result,
int result_len,
char **to)
312 const char *start = NULL;
313 char *end = NULL, *cur = (
char *)from;
315 struct lconv *loc_data = localeconv();
320 if (!from || !result) {
325 while (*cur && isspace((
int)*cur)) {
334 while (*cur && (isdigit((
int)*cur) || *cur ==
'.' || *cur ==
'+' || *cur ==
'-')) {
338 len = (int)(ptrdiff_t)(end - start);
339 if (len + 1 >= result_len) {
341 len = result_len - 1;
348 for (i = 0; i < len; ++i) {
349 if (start[i] ==
'.' &&
350 loc_data && loc_data->decimal_point && loc_data->decimal_point[0] && loc_data->decimal_point[0] !=
'.') {
352 result[i] = loc_data->decimal_point[0];
354 result[i] = start[i];
363 if (sscanf(result,
"%lf", &dtest) != 1) {
372 vcardproperty **error)
374 struct vcardvalue_impl *value = 0;
376 icalerror_check_arg_rz(str != 0,
"str");
384 if (!strcmp(str,
"TRUE")) {
386 }
else if (!strcmp(str,
"FALSE")) {
388 }
else if (error != 0) {
392 snprintf(temp,
sizeof(temp),
393 "Could not parse %s as a %s property",
406 value = vcardvalue_new_enum(kind, (
int)
VCARD_KIND_X, str);
415 const int v = strtol(str, &s_end, 10);
424 const float v = strtof(str, &s_end);
432#pragma GCC diagnostic push
433#pragma GCC diagnostic ignored "-Wformat-nonliteral"
437 unsigned hour, min = 0;
438 int nchar = 0, len = (int)strlen(str);
444 fmt =
"%1[+-]%02u:%02u%n";
446 fmt =
"%1[+-]%02u%02u%n";
449 if (3 != sscanf(str, fmt, sign, &hour, &min, &nchar)) {
452 }
else if (2 != sscanf(str,
"%1[+-]%02u%n", sign, &hour, &nchar)) {
456#pragma GCC diagnostic pop
458 if (len && (len == nchar)) {
459 int utcoffset = (int)(hour * 3600 + min * 60);
462 utcoffset = -utcoffset;
465 }
else if (error != 0) {
469 snprintf(temp,
sizeof(temp),
470 "Could not parse %s as a %s property",
505 goto geo_parsing_error;
508 while (cur && isspace((
int)*cur)) {
513 if (!cur || *cur !=
';') {
514 goto geo_parsing_error;
520 while (cur && isspace((
int)*cur)) {
525 goto geo_parsing_error;
533 snprintf(temp,
sizeof(temp),
534 "Could not parse %s as a %s value",
556 value->data.v_time = tt;
574 snprintf(temp,
TMP_BUF_SIZE,
"Unknown type for \'%s\'", str);
580 "vcardvalue_new_from_string got an unknown value type (%s) for \'%s\'",
582 icalerror_warn(temp);
587 if (error != 0 && *error == 0 && value == 0) {
591 snprintf(temp,
TMP_BUF_SIZE,
"Failed to parse value: \'%s\'", str);
603 return vcardvalue_new_from_string_with_error(kind, str, (vcardproperty **)0);
608 icalerror_check_arg_rv((v != 0),
"value");
610 if (v->parent != 0) {
614 if (v->x_value != 0) {
622 if (v->data.v_string != 0) {
624 v->data.v_string = 0;
630 if (v->data.v_textlist != 0) {
633 v->data.v_textlist = 0;
639 v->data.v_structured = 0;
644 v->data.v_geo.coords.lat[0] =
'\0';
645 v->data.v_geo.coords.lon[0] =
'\0';
657 memset(&v->data, 0,
sizeof(v->data));
658 v->id = ICAL_STRUCTURE_TYPE_VALUE_EMPTY;
671static char *vcardvalue_boolean_as_vcard_string_r(
const vcardvalue *value)
676 icalerror_check_arg_rz((value != 0),
"value");
681 strncpy(str, data ?
"TRUE" :
"FALSE", 6);
686#define MAX_INT_DIGITS 12
688static char *vcardvalue_int_as_vcard_string_r(
const vcardvalue *value)
693 icalerror_check_arg_rz((value != 0),
"value");
703static char *vcardvalue_utcoffset_as_vcard_string_r(
const vcardvalue *value,
712 icalerror_check_arg_rz((value != 0),
"value");
717 if (abs(data) == data) {
724 m = (data - (h * 3600)) / 60;
725 s = (data - (h * 3600) - (m * 60));
730#pragma GCC diagnostic push
731#pragma GCC diagnostic ignored "-Wformat-nonliteral"
734 fmt =
"%c%02d%02d%02d";
736 fmt =
"%c%02d:%02d:%02d";
748 snprintf(str, size, fmt, sign, h, m, s);
749#pragma GCC diagnostic pop
753static char *vcardvalue_text_as_vcard_string_r(
const vcardvalue *value)
759 return vcardmemory_strdup_and_quote(&str, &str_p, &buf_sz,
760 value->data.v_string,
false);
763static char *vcardvalue_string_as_vcard_string_r(
const vcardvalue *value)
768 icalerror_check_arg_rz((value != 0),
"value");
769 data = value->data.v_string;
771 const size_t len_data = strlen(data) + 1;
774 strncpy(str, data, len_data);
775 str[len_data - 1] =
'\0';
780static void _vcardstrarray_as_vcard_string_r(
char **str,
char **str_p,
size_t *buf_sz,
785 (void)vcardmemory_strdup_and_quote(str, str_p, buf_sz,
"", is_param);
797 (void)vcardmemory_strdup_and_quote(str, str_p, buf_sz,
805 char *buf_ptr = NULL;
808 _vcardstrarray_as_vcard_string_r(&buf, &buf_ptr, &buf_size,
834 _vcardstrarray_as_vcard_string_r(&buf, &buf_ptr, &buf_size,
835 array,
',', is_param);
844static char *vcardvalue_textlist_as_vcard_string_r(
const vcardvalue *value,
847 icalerror_check_arg_rz((value != 0),
"value");
852static char *vcardvalue_structured_as_vcard_string_r(
const vcardvalue *value)
854 icalerror_check_arg_rz((value != 0),
"value");
859static char *vcardvalue_float_as_vcard_string_r(
const vcardvalue *value)
865 icalerror_check_arg_rz((value != 0),
"value");
872 (void)setlocale(LC_NUMERIC,
"C");
876 snprintf(str, 40,
"%f", data);
879 (void)setlocale(LC_NUMERIC, old_locale);
885static char *vcardvalue_geo_as_vcard_string_r(
const vcardvalue *value)
887 icalerror_check_arg_rz((value != 0),
"value");
890 snprintf(str, max_len,
"%s;%s",
891 value->data.v_geo.coords.lat, value->data.v_geo.coords.lon);
892 str[max_len - 1] =
'\0';
928 switch (value->kind) {
930 return vcardvalue_boolean_as_vcard_string_r(value);
933 return vcardvalue_int_as_vcard_string_r(value);
936 return vcardvalue_utcoffset_as_vcard_string_r(value, version);
939 return vcardvalue_text_as_vcard_string_r(value);
942 return vcardvalue_textlist_as_vcard_string_r(value,
943 is_structured ?
';' :
',');
946 return vcardvalue_structured_as_vcard_string_r(value);
949 return vcardvalue_geo_as_vcard_string_r(value);
953 return vcardvalue_string_as_vcard_string_r(value);
976 return vcardvalue_float_as_vcard_string_r(value);
981 if (value->x_value != 0) {
988 if (value->x_value != 0) {
1013 const struct vcardvalue_impl *impl = (
struct vcardvalue_impl *)value;
1015 icalerror_check_arg_rz((value != 0),
"value");
1017 return (impl->id == ICAL_STRUCTURE_TYPE_VALUE);
1029 switch (value->kind) {
1053 icalerror_check_arg_rv((value != 0),
"value");
1055 value->parent = property;
1060 return value->parent;
void icalerror_set_errno(icalerrorenum x)
Sets the icalerrno to a given error.
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_append_char(char **buf, char **pos, size_t *buf_size, char ch)
Appends a character to a buffer.
void icalmemory_add_tmp_buffer(void *buf)
Adds an externally allocated buffer to the ring.
Common memory management routines.
struct vcardgeotype::@162201204342263205067330224176321027032342016274 coords
enum vcardproperty_version vcardcomponent_get_version(vcardcomponent *comp)
Defines the data structure representing vCard components.
vcardcomponent * vcardproperty_get_parent(const vcardproperty *property)
Returns the parent vcard for the specified property.
struct vcardparameter_impl vcardparameter
vcardparameter * vcardparameter_new_xlicerrortype(vcardparameter_xlicerrortype v)
@ VCARD_XLICERRORTYPE_VALUEPARSEERROR
vcardproperty * vcardproperty_vanew_xlicerror(const char *v,...)
vcardvalue * vcardvalue_new_textlist(vcardstrarray *v)
struct vcardvalue_impl vcardvalue
vcardvalue * vcardvalue_new_geo(vcardgeotype v)
int vcardvalue_get_utcoffset(const vcardvalue *value)
vcardvalue * vcardvalue_new_integer(int v)
vcardvalue * vcardvalue_new_x(const char *v)
vcardvalue * vcardvalue_new_languagetag(const char *v)
int vcardvalue_get_integer(const vcardvalue *value)
void vcardvalue_set_x(vcardvalue *value, const char *v)
vcardvalue * vcardvalue_new_float(float v)
vcardvalue * vcardvalue_new_uri(const char *v)
float vcardvalue_get_float(const vcardvalue *value)
vcardvalue * vcardvalue_new_structured(vcardstructuredtype *v)
@ VCARD_DATEANDORTIME_VALUE
@ VCARD_LANGUAGETAG_VALUE
vcardvalue * vcardvalue_new_text(const char *v)
vcardvalue * vcardvalue_new_utcoffset(int v)
vcardvalue * vcardvalue_new_boolean(int v)
vcardproperty_kind vcardproperty_isa(const vcardproperty *p)
Defines the data structure representing vCard properties.
vcardproperty * vcardvalue_get_parent(const vcardvalue *value)
vcardproperty_kind vcardproperty_value_kind_to_kind(vcardvalue_kind kind)
char * vcardproperty_enum_to_string_r(int e)
void vcardvalue_set_parent(vcardvalue *value, vcardproperty *property)
int vcardproperty_kind_and_string_to_enum(const int kind, const char *str)
bool vcardproperty_enum_belongs_to_property(vcardproperty_kind kind, int e)
bool vcardproperty_is_structured(vcardproperty_kind pkind)
#define vcardstrarray_clone(array)
#define vcardstrarray_size(array)
#define vcardstrarray_free(array)
#define vcardstrarray_element_at(array, position)
icalstrarray vcardstrarray
vcardstrarray * vcardstructured_field_at(const vcardstructuredtype *st, size_t position)
Returns the field at the given position in a vcardstructuredtype.
vcardstructuredtype * vcardstructured_new_from_string(const char *str)
Creates a new instance of vcardstructuredtype from a string.
vcardstructuredtype * vcardstructured_clone(const vcardstructuredtype *st)
Clones a vcardstructuredtype.
void vcardstructured_unref(vcardstructuredtype *st)
Decrements the reference count of the vcardstructuredtype.
size_t vcardstructured_num_fields(const vcardstructuredtype *st)
Returns the number of fields in a vcardstructuredtype.
struct vcardstructuredtype_impl vcardstructuredtype
Represents a decoded, structured text value.
char * vcardstructured_as_vcard_string_r(const vcardstructuredtype *st, bool is_param)
Formats a vcardstructuredtype as a vCard property or parameter value.
vcardstrarray * vcardtextlist_new_from_string(const char *str, char sep)
Defines functions for creating vCard text lists.
bool vcardtime_is_timestamp(const vcardtimetype t)
bool vcardtime_is_date(const vcardtimetype t)
vcardtimetype vcardtime_from_string(const char *str, int is_bare_time)
bool vcardtime_is_null_datetime(const vcardtimetype t)
bool vcardtime_is_time(const vcardtimetype t)
char * vcardtime_as_vcard_string_r(const vcardtimetype t, unsigned flags)
#define VCARDTIME_BARE_TIME
vcardvalue * vcardvalue_new_from_string(vcardvalue_kind kind, const char *str)
const char * vcardvalue_as_vcard_string(const vcardvalue *value)
bool vcardvalue_isa_value(void *value)
char * vcardvalue_strdup_and_dequote_text(const char **str, const char *sep)
struct vcardvalue_impl * vcardvalue_new_impl(vcardvalue_kind kind)
vcardvalue_kind vcardvalue_isa(const vcardvalue *value)
char * vcardvalue_as_vcard_string_r(const vcardvalue *value)
vcardvalue * vcardvalue_clone(const vcardvalue *old)
void vcardvalue_reset_kind(vcardvalue *value)
bool vcardvalue_is_valid(const vcardvalue *value)
char * vcardstrarray_as_vcard_string_r(const vcardstrarray *array, const char sep)
vcardvalue * vcardvalue_new(vcardvalue_kind kind)
void vcardvalue_free(vcardvalue *v)
Defines the data structure representing vCard values.
const char * vcardvalue_kind_to_string(const vcardvalue_kind kind)
bool vcardvalue_kind_is_valid(const vcardvalue_kind kind)