4 #include "csv_parser.h"
6 static void print_document(const csv_document_t *doc);
7 static void print_usage(const char *program_name);
9 int main(int argc, char *argv[])
11 csv_parser_t *parser = csv_parser_create();
13 fprintf(stderr, "Error: Failed to create CSV parser\n");
17 csv_document_t *document = NULL;
19 bool has_header = false;
20 char *filename = NULL;
22 /* Parse command line arguments */
23 for (int i = 1; i < argc; i++) {
24 if (strcmp(argv[i], "--header") == 0 || strcmp(argv[i], "-h") == 0) {
26 } else if (strcmp(argv[i], "--help") == 0) {
28 csv_parser_destroy(parser);
30 } else if (argv[i][0] != '-') {
33 fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]);
35 csv_parser_destroy(parser);
41 /* Parse file argument */
42 FILE *file = fopen(filename, "r");
44 fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
45 csv_parser_destroy(parser);
49 printf("Parsing CSV file: %s\n", filename);
50 result = csv_parser_parse_file(parser, file, &document);
53 /* Parse from stdin */
54 printf("CSV Parser - Enter CSV data (Ctrl+D to end):\n");
55 result = csv_parser_parse_file(parser, stdin, &document);
58 if (result == CSV_SUCCESS) {
59 /* If user specified header option, treat first record as header */
60 if (has_header && document && document->records) {
61 csv_error_t header_result = csv_document_set_first_record_as_header(document);
62 if (header_result != CSV_SUCCESS) {
63 printf("Warning: Failed to set header: %s\n", csv_error_string(header_result));
67 printf("Parsing completed successfully.\n");
68 print_document(document);
69 csv_document_free(document);
71 printf("Parsing failed: %s\n", csv_error_string(result));
73 csv_error_info_t error_info = csv_parser_get_error_info(parser);
74 if (error_info.has_location) {
75 printf("Error details: %s (line %d, column %d)\n",
76 error_info.message, error_info.line, error_info.column);
78 printf("Error details: %s\n", error_info.message);
82 csv_parser_destroy(parser);
83 return result == CSV_SUCCESS ? 0 : 1;
86 static void print_document(const csv_document_t *doc)
89 printf("Document is NULL\n");
93 printf("\nDocument summary:\n");
94 printf("- Records: %zu\n", doc->record_count);
95 printf("- Has header: %s\n", csv_document_has_header(doc) ? "yes" : "no");
97 /* Print header if present */
98 if (csv_document_has_header(doc)) {
99 printf("\nHeader:\n");
100 csv_field_t *field = csv_record_get_first_field(doc->header);
103 printf(" Field %d: '%s'\n", field_num++, csv_field_get_content(field));
104 field = csv_field_get_next(field);
108 /* Print data records */
109 printf("\nData records:\n");
110 csv_record_t *record = csv_document_get_first_record(doc);
114 printf("Record %d (%zu fields):\n", record_num++, record->field_count);
116 csv_field_t *field = csv_record_get_first_field(record);
119 const char *content = csv_field_get_content(field);
120 printf(" Field %d: '%s'\n", field_num++, content ? content : "");
121 field = csv_field_get_next(field);
124 record = csv_record_get_next(record);
128 static void print_usage(const char *program_name)
130 printf("Usage: %s [options] [filename]\n", program_name);
131 printf("Options:\n");
132 printf(" -h, --header Treat the first record as a header\n");
133 printf(" --help Show this help message\n");
135 printf(" If filename is provided, parse that file\n");
136 printf(" Otherwise, read from standard input\n");