]> begriffs open source - sa-parse/blob - src/csv_driver.c
Remove lisp parser, it was just a test
[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_document(const csv_document_t *doc);
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         csv_document_t *document = NULL;
18         csv_error_t result;
19         bool has_header = false;
20         char *filename = NULL;
21         
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) {
25                         has_header = true;
26                 } else if (strcmp(argv[i], "--help") == 0) {
27                         print_usage(argv[0]);
28                         csv_parser_destroy(parser);
29                         return 0;
30                 } else if (argv[i][0] != '-') {
31                         filename = argv[i];
32                 } else {
33                         fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]);
34                         print_usage(argv[0]);
35                         csv_parser_destroy(parser);
36                         return 1;
37                 }
38         }
39         
40         if (filename) {
41                 /* Parse file argument */
42                 FILE *file = fopen(filename, "r");
43                 if (!file) {
44                         fprintf(stderr, "Error: Cannot open file '%s'\n", filename);
45                         csv_parser_destroy(parser);
46                         return 1;
47                 }
48                 
49                 printf("Parsing CSV file: %s\n", filename);
50                 result = csv_parser_parse_file(parser, file, &document);
51                 fclose(file);
52         } else {
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);
56         }
57         
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));
64                         }
65                 }
66                 
67                 printf("Parsing completed successfully.\n");
68                 print_document(document);
69                 csv_document_free(document);
70         } else {
71                 printf("Parsing failed: %s\n", csv_error_string(result));
72                 
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);
77                 } else {
78                         printf("Error details: %s\n", error_info.message);
79                 }
80         }
81         
82         csv_parser_destroy(parser);
83         return result == CSV_SUCCESS ? 0 : 1;
84 }
85
86 static void print_document(const csv_document_t *doc)
87 {
88         if (!doc) {
89                 printf("Document is NULL\n");
90                 return;
91         }
92         
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");
96         
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);
101                 int field_num = 1;
102                 while (field) {
103                         printf("  Field %d: '%s'\n", field_num++, csv_field_get_content(field));
104                         field = csv_field_get_next(field);
105                 }
106         }
107         
108         /* Print data records */
109         printf("\nData records:\n");
110         csv_record_t *record = csv_document_get_first_record(doc);
111         int record_num = 1;
112         
113         while (record) {
114                 printf("Record %d (%zu fields):\n", record_num++, record->field_count);
115                 
116                 csv_field_t *field = csv_record_get_first_field(record);
117                 int field_num = 1;
118                 while (field) {
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);
122                 }
123                 
124                 record = csv_record_get_next(record);
125         }
126 }
127
128 static void print_usage(const char *program_name)
129 {
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");
134         printf("\n");
135         printf("  If filename is provided, parse that file\n");
136         printf("  Otherwise, read from standard input\n");
137 }