]> begriffs open source - sa-parse/blob - src/csv_driver.c
ical parser
[sa-parse] / src / csv_driver.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "csv_parser.h"
5
6 static void print_records(const vector *records);
7 static void print_usage(const char *program_name);
8
9 int main(int argc, char *argv[])
10 {
11         csv_parser_t *parser = csv_parser_create();
12         if (!parser) {
13                 fprintf(stderr, "Error: Failed to create CSV parser\n");
14                 return 1;
15         }
16         
17         vector *records = NULL;
18         csv_error_t result;
19         char *filename = NULL;
20         
21         /* Parse command line arguments */
22         for (int i = 1; i < argc; i++) {
23                 if (strcmp(argv[i], "--help") == 0) {
24                         print_usage(argv[0]);
25                         csv_parser_destroy(parser);
26                         return 0;
27                 } else if (argv[i][0] != '-') {
28                         filename = argv[i];
29                 } else {
30                         fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]);
31                         print_usage(argv[0]);
32                         csv_parser_destroy(parser);
33                         return 1;
34                 }
35         }
36         
37         if (filename) {
38                 /* Parse file argument */
39                 FILE *file = fopen(filename, "r");
40                 if (!file) {
41                         fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
42                         csv_parser_destroy(parser);
43                         return 1;
44                 }
45                 
46                 printf("Parsing CSV file: %s\n", filename);
47                 result = csv_parser_parse_file(parser, file, &records);
48                 fclose(file);
49         } else {
50                 /* Parse from stdin */
51                 printf("CSV Parser - Enter CSV data (Ctrl+D to end):\n");
52                 result = csv_parser_parse_file(parser, stdin, &records);
53         }
54         
55         if (result == CSV_SUCCESS) {
56                 printf("Parsing completed successfully.\n");
57                 print_records(records);
58                 v_free(records);
59         } else {
60                 printf("Parsing failed: %s\n", csv_error_string(result));
61                 
62                 csv_error_info_t error_info = csv_parser_get_error_info(parser);
63                 if (error_info.has_location) {
64                         printf("Error details: %s (line %d, column %d)\n", 
65                                    error_info.message, error_info.line, error_info.column);
66                 } else {
67                         printf("Error details: %s\n", error_info.message);
68                 }
69         }
70         
71         csv_parser_destroy(parser);
72         return result == CSV_SUCCESS ? 0 : 1;
73 }
74
75 static void print_records(const vector *records)
76 {
77         if (!records) {
78                 printf("Records vector is NULL\n");
79                 return;
80         }
81         
82         printf("\nDocument summary:\n");
83         printf("- Records: %zu\n", v_length(records));
84         
85         /* Print all records */
86         printf("\nRecords:\n");
87         for (size_t i = 0; i < v_length(records); i++) {
88                 csv_record_t *record = v_at(records, i);
89                 if (record) {
90                         printf("Record %zu (%zu fields):\n", i + 1, v_length(record->fields));
91                         
92                         for (size_t j = 0; j < v_length(record->fields); j++) {
93                                 const char *content = v_at(record->fields, j);
94                                 printf("  Field %zu: '%s'\n", j + 1, content ? content : "");
95                         }
96                 }
97         }
98 }
99
100 static void print_usage(const char *program_name)
101 {
102         printf("Usage: %s [filename]\n", program_name);
103         printf("Options:\n");
104         printf("  --help          Show this help message\n");
105         printf("\n");
106         printf("  If filename is provided, parse that file\n");
107         printf("  Otherwise, read from standard input\n");
108 }