icalcomponent *icalparser_parse(icalparser *parser, icalparser_line_gen_func line_gen_func) { char *line; icalcomponent *c = 0; icalcomponent *root = 0; icalerrorstate es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR); int cont; icalerror_check_arg_rz((parser != 0), "parser"); icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); do { line = icalparser_get_line(parser, line_gen_func); if ((c = icalparser_add_line(parser, line)) != 0) { if (icalcomponent_get_parent(c) != 0) { /* This is bad news... assert? */ } assert(parser->root_component == 0); assert(pvl_count(parser->components) == 0); if (root == 0) { /* Just one component */ root = c; } else if (icalcomponent_isa(root) != ICAL_XROOT_COMPONENT) { /*Got a second component, so move the two components under an XROOT container */ icalcomponent *tempc = icalcomponent_new(ICAL_XROOT_COMPONENT); icalcomponent_add_component(tempc, root); icalcomponent_add_component(tempc, c); root = tempc; } else if (icalcomponent_isa(root) == ICAL_XROOT_COMPONENT) { /* Already have an XROOT container, so add the component to it */ icalcomponent_add_component(root, c); } else { /* Badness */ assert(0); } c = 0; } cont = 0; if (line != 0) { icalmemory_free_buffer(line); cont = 1; } } while (cont); icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR, es); return root; }
void MainWindow::loadFromIcsFile() { char* line; icalcomponent *c; icalparser *parser = icalparser_new(); FILE* stream = fopen("/home/quentin/Public/test.ics", "r"); icalparser_set_gen_data(parser, stream); do { line = icalparser_get_line(parser, read_stream); c = icalparser_add_line(parser, line); if (c != 0) { icalcomponent *inner = icalcomponent_get_first_component(c, ICAL_ANY_COMPONENT); while (inner != NULL) { if (icalcomponent_isa(inner) == ICAL_VTODO_COMPONENT) { const char* name = icalcomponent_get_summary(inner); const char* uid = icalcomponent_get_uid(inner); icalproperty *p = icalcomponent_get_first_property(inner, ICAL_RELATEDTO_PROPERTY); const char* parent = icalproperty_get_relatedto(p); addTask(name, uid, parent); } if (icalcomponent_isa(inner) == ICAL_VEVENT_COMPONENT) { const char* name = icalcomponent_get_summary(inner); const char* uid = icalcomponent_get_uid(inner); icalproperty *p = icalcomponent_get_first_property(inner, ICAL_RELATEDTO_PROPERTY); const char* parent = icalproperty_get_relatedto(p); p = icalcomponent_get_first_property(inner, ICAL_DTSTART_PROPERTY); icaltimetype start = icalproperty_get_dtstart(p); p = icalcomponent_get_first_property(inner, ICAL_DTEND_PROPERTY); icaltimetype end = icalproperty_get_dtend(p); Task* task = findTask(parent); addEvent(task, uid, name, start, end); } inner = icalcomponent_get_next_component(c, ICAL_ANY_COMPONENT); } } } while (line != 0); }
void parse_text(int argc, char *argv[]) { char *line; FILE *stream; icalcomponent *c; /* Create a new parser object */ icalparser *parser = icalparser_new(); stream = fopen(argv[1], "r"); assert(stream != 0); /* Tell the parser what input routie it should use. */ icalparser_set_gen_data(parser, stream); do { /* Get a single content line by making one or more calls to read_stream()*/ line = icalparser_get_line(parser, read_stream); /* Now, add that line into the parser object. If that line completes a component, c will be non-zero */ c = icalparser_add_line(parser, line); if(c != 0) { printf("%s", icalcomponent_as_ical_string(c)); printf("\n---------------\n"); icalcomponent_free(c); } } while(line != 0); icalparser_free(parser); }
int main(int argc, char *argv[]) { char *line; FILE *stream; icalcomponent *c; icalparser *parser = icalparser_new(); if (argc != 2) { fprintf(stderr, "Usage: parser [file.ics]\n"); return 0; } stream = fopen(argv[1], "r"); if (stream == (FILE *) NULL) { fprintf(stderr, "Cannot open file \"%s\" for reading\n", argv[1]); return 1; } icalparser_set_gen_data(parser, stream); do { line = icalparser_get_line(parser, read_stream); c = icalparser_add_line(parser, line); icalmemory_free_buffer(line); if (c != 0) { /*icalcomponent_convert_errors(c); */ printf("%s", icalcomponent_as_ical_string(c)); printf("\n---------------\n"); icalcomponent_free(c); } } while (line != 0); icalparser_free(parser); fclose(stream); return 0; }