#include #include #include #include "ical_parser.h" static void print_calendar(const ical_component_t *calendar); static void print_component(const ical_component_t *component, int indent); static void print_property(const ical_property_t *property, int indent); static void print_usage(const char *program_name); static void print_indent(int indent); int main(int argc, char *argv[]) { ical_parser_t *parser = ical_parser_create(); if (!parser) { fprintf(stderr, "Error: Failed to create iCalendar parser\n"); return 1; } ical_component_t *calendar = NULL; ical_error_t result; char *filename = NULL; /* Parse command line arguments */ for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--help") == 0) { print_usage(argv[0]); ical_parser_destroy(parser); return 0; } else if (argv[i][0] != '-') { filename = argv[i]; } else { fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]); print_usage(argv[0]); ical_parser_destroy(parser); return 1; } } if (filename) { /* Parse file argument */ FILE *file = fopen(filename, "r"); if (!file) { fprintf(stderr, "Error: Cannot open file '%s'\n", filename); ical_parser_destroy(parser); return 1; } printf("Parsing iCalendar file: %s\n", filename); result = ical_parser_parse_file(parser, file, &calendar); fclose(file); } else { /* Parse from stdin */ printf("iCalendar Parser - Enter iCalendar data (Ctrl+D to end):\n"); result = ical_parser_parse_file(parser, stdin, &calendar); } if (result == ICAL_SUCCESS) { printf("Parsing completed successfully.\n"); print_calendar(calendar); ical_component_free(calendar); } else { printf("Parsing failed: %s\n", ical_error_string(result)); ical_error_info_t error_info = ical_parser_get_error_info(parser); if (error_info.has_location) { printf("Error details: %s (line %d, column %d)\n", error_info.message, error_info.line, error_info.column); } else { printf("Error details: %s\n", error_info.message); } } ical_parser_destroy(parser); return result == ICAL_SUCCESS ? 0 : 1; } static void print_calendar(const ical_component_t *calendar) { if (!calendar) { printf("Calendar component is NULL\n"); return; } printf("\nDocument summary:\n"); printf("- Component type: %s\n", calendar->type); printf("- Properties: %zu\n", calendar->properties ? v_length(calendar->properties) : 0); printf("- Subcomponents: %zu\n", calendar->subcomponents ? v_length(calendar->subcomponents) : 0); /* Print the full calendar structure */ printf("\nCalendar structure:\n"); print_component(calendar, 0); } static void print_component(const ical_component_t *component, int indent) { if (!component) return; print_indent(indent); printf("BEGIN:%s\n", component->type); /* Print properties */ if (component->properties) { for (size_t i = 0; i < v_length(component->properties); i++) { ical_property_t *property = v_at(component->properties, i); print_property(property, indent + 1); } } /* Print subcomponents */ if (component->subcomponents) { for (size_t i = 0; i < v_length(component->subcomponents); i++) { ical_component_t *subcomponent = v_at(component->subcomponents, i); print_component(subcomponent, indent + 1); } } print_indent(indent); printf("END:%s\n", component->type); } static void print_property(const ical_property_t *property, int indent) { if (!property) return; print_indent(indent); printf("%s", property->name); /* Print parameters */ if (property->parameters && v_length(property->parameters) > 0) { for (size_t i = 0; i < v_length(property->parameters); i++) { ical_parameter_t *param = v_at(property->parameters, i); if (param) { printf(";%s=%s", param->name, param->value); } } } printf(":%s\n", property->value ? property->value : ""); } static void print_usage(const char *program_name) { printf("Usage: %s [filename]\n", program_name); printf("Options:\n"); printf(" --help Show this help message\n"); printf("\n"); printf(" If filename is provided, parse that file\n"); printf(" Otherwise, read from standard input\n"); } static void print_indent(int indent) { for (int i = 0; i < indent; i++) { printf(" "); } }