struct icaltriggertype icaltriggertype_from_string(const char* str) { struct icaltriggertype tr, null_tr; int old_ieaf = icalerror_errors_are_fatal; tr.time= icaltime_null_time(); tr.duration = icaldurationtype_from_int(0); null_tr = tr; if(str == 0) goto error; icalerror_errors_are_fatal = 0; tr.time = icaltime_from_string(str); icalerror_errors_are_fatal = old_ieaf; if (icaltime_is_null_time(tr.time)){ tr.duration = icaldurationtype_from_string(str); if(icaldurationtype_as_int(tr.duration) == 0) goto error; } return tr; error: icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); return null_tr; }
struct icalperiodtype icalperiodtype_from_string (const char* str) { struct icalperiodtype p, null_p; char *s = icalmemory_strdup(str); char *start, *end = s; icalerrorstate es; /* Errors are normally generated in the following code, so save the error state for resoration later */ icalerrorenum e = icalerrno; p.start = p.end = icaltime_null_time(); p.duration = icaldurationtype_from_int(0); null_p = p; if(s == 0) goto error; start = s; end = strchr(s, '/'); if(end == 0) goto error; *end = 0; end++; p.start = icaltime_from_string(start); if (icaltime_is_null_time(p.start)) goto error; es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR); icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL); p.end = icaltime_from_string(end); icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es); if (icaltime_is_null_time(p.end)) { p.duration = icaldurationtype_from_string(end); if(icaldurationtype_as_int(p.duration) == 0) goto error; } icalerrno = e; icalmemory_free_buffer(s); return p; error: icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); if (s) icalmemory_free_buffer (s); return null_p; }
VALUE duration_to_fixnum( VALUE self, VALUE duration ) { struct icaldurationtype dur_struct; char * _duration; ID to_string = rb_intern( "to_string" ); if( TYPE( duration ) != T_STRING && rb_respond_to( duration, to_string ) ) duration = rb_funcall( duration, to_string, 0 ); Check_Type(duration, T_STRING); _duration = RSTRING(duration)->ptr; icalerror_clear_errno(); icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); dur_struct = icaldurationtype_from_string( _duration ); if( icaldurationtype_is_bad_duration( dur_struct ) ) rb_raise(rb_eArgError, "Malformed Duration"); return LONG2FIX(icaldurationtype_as_int( dur_struct )); }
struct icaltriggertype icaltriggertype_from_string(const char* str) { struct icaltriggertype tr, null_tr; icalerrorstate es = ICAL_ERROR_DEFAULT; icalerrorenum e; tr.time= icaltime_null_time(); tr.duration = icaldurationtype_from_int(0); null_tr = tr; /* Suppress errors so a failure in icaltime_from_string() does not cause an abort */ es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR); if(str == 0) goto error; icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL); e = icalerrno; icalerror_set_errno(ICAL_NO_ERROR); tr.time = icaltime_from_string(str); if (icaltime_is_null_time(tr.time)){ tr.duration = icaldurationtype_from_string(str); if (icaldurationtype_is_bad_duration(tr.duration)) goto error; } icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es); icalerror_set_errno(e); return tr; error: icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es); icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR); return tr; }
NS_IMETHODIMP calDuration::SetIcalString(const nsACString& aIcalString) { mDuration = icaldurationtype_from_string(PromiseFlatCString(aIcalString).get()); return NS_OK; }
/* * Construct an iCalendar property value from XML content. */ static icalvalue *xml_element_to_icalvalue(xmlNodePtr xtype, icalvalue_kind kind) { icalvalue *value = NULL; xmlNodePtr node; xmlChar *content = NULL; switch (kind) { case ICAL_GEO_VALUE: { struct icalgeotype geo; node = xmlFirstElementChild(xtype); if (!node) { syslog(LOG_WARNING, "Missing <latitude> XML element"); break; } else if (xmlStrcmp(node->name, BAD_CAST "latitude")) { syslog(LOG_WARNING, "Expected <latitude> XML element, received %s", node->name); break; } content = xmlNodeGetContent(node); geo.lat = atof((const char *) content); node = xmlNextElementSibling(node); if (!node) { syslog(LOG_WARNING, "Missing <longitude> XML element"); break; } else if (xmlStrcmp(node->name, BAD_CAST "longitude")) { syslog(LOG_WARNING, "Expected <longitude> XML element, received %s", node->name); break; } xmlFree(content); content = xmlNodeGetContent(node); geo.lon = atof((const char *) content); value = icalvalue_new_geo(geo); break; } case ICAL_PERIOD_VALUE: { struct icalperiodtype p; p.start = p.end = icaltime_null_time(); p.duration = icaldurationtype_from_int(0); node = xmlFirstElementChild(xtype); if (!node) { syslog(LOG_WARNING, "Missing <start> XML element"); break; } else if (xmlStrcmp(node->name, BAD_CAST "start")) { syslog(LOG_WARNING, "Expected <start> XML element, received %s", node->name); break; } content = xmlNodeGetContent(node); p.start = icaltime_from_string((const char *) content); if (icaltime_is_null_time(p.start)) break; node = xmlNextElementSibling(node); if (!node) { syslog(LOG_WARNING, "Missing <end> / <duration> XML element"); break; } else if (!xmlStrcmp(node->name, BAD_CAST "end")) { xmlFree(content); content = xmlNodeGetContent(node); p.end = icaltime_from_string((const char *) content); if (icaltime_is_null_time(p.end)) break; } else if (!xmlStrcmp(node->name, BAD_CAST "duration")) { xmlFree(content); content = xmlNodeGetContent(node); p.duration = icaldurationtype_from_string((const char *) content); if (icaldurationtype_as_int(p.duration) == 0) break; } else { syslog(LOG_WARNING, "Expected <end> / <duration> XML element, received %s", node->name); break; } value = icalvalue_new_period(p); break; } case ICAL_RECUR_VALUE: { struct buf rrule = BUF_INITIALIZER; struct hash_table byrules; struct icalrecurrencetype rt; char *sep = ""; construct_hash_table(&byrules, 10, 1); /* create an iCal RRULE string from xCal <recur> sub-elements */ for (node = xmlFirstElementChild(xtype); node; node = xmlNextElementSibling(node)) { content = xmlNodeGetContent(node); if (!xmlStrncmp(node->name, BAD_CAST "by", 2)) { /* BY* rules can have a list of values - assemble them using a hash table */ struct buf *vals = hash_lookup((const char *) node->name, &byrules); if (vals) { /* append this value to existing list */ buf_printf(vals, ",%s", (char *) content); } else { /* create new list with this valiue */ vals = xzmalloc(sizeof(struct buf)); buf_setcstr(vals, (char *) content); hash_insert((char *) node->name, vals, &byrules); } } else { /* single value rpart */ buf_printf(&rrule, "%s%s=%s", sep, ucase((char *) node->name), (char *) content); sep = ";"; } xmlFree(content); content = NULL; } /* append the BY* rules to RRULE buffer */ hash_enumerate(&byrules, (void (*)(const char*, void*, void*)) &append_byrule, &rrule); free_hash_table(&byrules, NULL); /* parse our iCal RRULE string */ rt = icalrecurrencetype_from_string(buf_cstring(&rrule)); buf_free(&rrule); if (rt.freq != ICAL_NO_RECURRENCE) value = icalvalue_new_recur(rt); break; } case ICAL_REQUESTSTATUS_VALUE: { struct icalreqstattype rst = { ICAL_UNKNOWN_STATUS, NULL, NULL }; short maj, min; node = xmlFirstElementChild(xtype); if (!node) { syslog(LOG_WARNING, "Missing <code> XML element"); break; } else if (xmlStrcmp(node->name, BAD_CAST "code")) { syslog(LOG_WARNING, "Expected <code> XML element, received %s", node->name); break; } content = xmlNodeGetContent(node); if (sscanf((const char *) content, "%hd.%hd", &maj, &min) == 2) { rst.code = icalenum_num_to_reqstat(maj, min); } if (rst.code == ICAL_UNKNOWN_STATUS) { syslog(LOG_WARNING, "Unknown request-status code"); break; } node = xmlNextElementSibling(node); if (!node) { syslog(LOG_WARNING, "Missing <description> XML element"); break; } else if (xmlStrcmp(node->name, BAD_CAST "description")) { syslog(LOG_WARNING, "Expected <description> XML element, received %s", node->name); break; } xmlFree(content); content = xmlNodeGetContent(node); rst.desc = (const char *) content; node = xmlNextElementSibling(node); if (node) { if (xmlStrcmp(node->name, BAD_CAST "data")) { syslog(LOG_WARNING, "Expected <data> XML element, received %s", node->name); break; } xmlFree(content); content = xmlNodeGetContent(node); rst.debug = (const char *) content; } value = icalvalue_new_requeststatus(rst); break; } case ICAL_UTCOFFSET_VALUE: { int n, utcoffset, hours, minutes, seconds = 0; char sign; content = xmlNodeGetContent(xtype); n = sscanf((const char *) content, "%c%02d:%02d:%02d", &sign, &hours, &minutes, &seconds); if (n < 3) { syslog(LOG_WARNING, "Unexpected utc-offset format"); break; } utcoffset = hours*3600 + minutes*60 + seconds; if (sign == '-') utcoffset = -utcoffset; value = icalvalue_new_utcoffset(utcoffset); break; } default: content = xmlNodeGetContent(xtype); value = icalvalue_new_from_string(kind, (const char *) content); break; } if (content) xmlFree(content); return value; }
Duration( std::string const & fromString ) : icaldurationtype( icaldurationtype_from_string( fromString.c_str() ) ) { }