csv

csv reading library
git clone git://bvnf.space/csv.git
Log | Files | Refs

main.c (1027B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
#include <stdio.h>
#include <unistd.h>
#include <csv.h>

int
main(int argc, char **argv) {
    int c;
    char flag_H = 0;
    while ((c = getopt(argc, argv, "Hh")) != -1) {
        switch (c) {
            case 'H':
                flag_H = 1;
                break;
            case 'h':
                goto usage;
        }
    }
    if (argc - optind > 1) {
usage:
        fprintf(stderr,
            "usage: %s [-H] [file]\n  if no filename given, data is read from stdin\n"
            "  -H\tread data headers from first line\n", argv[0]);
        return 1;
    }

    FILE *f;
    if (argc - optind == 0) {
        f = stdin;
    } else {
        f = fopen(argv[optind], "r");
        if (f == NULL) {
            perror(argv[optind]);
            return 1;
        }
    }
    struct csv *csv = csv_create();
    if (csv == NULL) {
        perror("csv_create");
        return 1;
    }
    int ret = csv_read_file(f, csv, flag_H);
    if (ret != 0) {
        csv_destroy(csv);
        return 1;
    }
    return 0;
}