csv

Unnamed repository
Log | Files | Refs

commit 9f1ea74d9dbd70df603318e6ca3fdd6bd473c039
Author: phoebos <ben@bvnf.space>
Date:   Fri,  1 Oct 2021 15:37:43 +0000

initial

Diffstat:
A.gitignore | 1+
AMakefile | 5+++++
Acsv.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest.csv | 5+++++
4 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +csv diff --git a/Makefile b/Makefile @@ -0,0 +1,5 @@ +.POSIX: +X := ${CFLAGS} +CFLAGS = ${X} -Og -ggdb3 + +csv: diff --git a/csv.c b/csv.c @@ -0,0 +1,58 @@ +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define MAX_LINE 2048 + +int +main(int argc, char **argv) { + if (argc > 2) { + fprintf(stderr, "usage: %s [file]\n\tif no filename given, data is \ +read from stdin\n", argv[0]); + return 1; + } + FILE *f; + if (argc == 1) { + f = stdin; + } else { + f = fopen(argv[1], "r"); + if (f < 0) { + perror(argv[1]); + return 1; + } + } + + char **header = malloc(sizeof (char *) * MAX_LINE); + char **headerp = header; + + /* read header */ + int c; + char *n = malloc(MAX_LINE); + char *np = n; + while ((c = fgetc(f)) != EOF) { + fprintf(stderr, "got %c\n", c); + if (c == '\n') goto storeit; + if (c == ',') { + printf("its a ,\n"); +storeit: + *np++ = '\0'; + *headerp = malloc(sizeof n * sizeof *n); + strcpy(*headerp, n); + headerp++; + //*header = n; + //header += sizeof n; + n = np; + continue; + } + *np++ = c; + } + fprintf(stderr, "reading\n"); + + do { + printf("%s ", *header); + } while (*++header); + fclose(f); + return 0; +} diff --git a/test.csv b/test.csv @@ -0,0 +1,5 @@ +date,name,quantity +1,me,0 +2,ben,5 +3,mia,123 +4,you,65225