From 9f9476c233dae1b45492891f20b085e1b82d3c73 Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Thu, 19 Oct 2023 08:33:26 -0400 Subject: [PATCH] init --- Makefile | 9 +++++++++ README | 19 +++++++++++++++++++ config.mk | 6 ++++++ include/csvparser.h | 7 +++++++ man/libcsvparser.3 | 16 ++++++++++++++++ src/libcsvparser.c | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+) create mode 100644 Makefile create mode 100644 README create mode 100644 config.mk create mode 100644 include/csvparser.h create mode 100644 man/libcsvparser.3 create mode 100644 src/libcsvparser.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8da406e --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +include config.mk + +all: + $(CC) -c src/libcsvparser.c -o src/libcsvparser.o + ar -rc src/libcsvparser.a src/libcsvparser.o + +install: + install src/libcsvparser.a $(PREFIX)/lib + install include/libcsvparser.h $(PREFIX)/include diff --git a/README b/README new file mode 100644 index 0000000..5db1bd6 --- /dev/null +++ b/README @@ -0,0 +1,19 @@ ++ libcsvparser + +A very simple CSV parsing library + ++ Usage + + #include + + int main() { + parse_tokens(FILE); + } + + (user)$ gcc prog.c -lcsvparser -o prog + + See 'man 3 libcsvparser' for more information. + ++ Installation + + Customize config.mk, then run 'make', followed by 'make install' as root. diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..df51552 --- /dev/null +++ b/config.mk @@ -0,0 +1,6 @@ +# +# config.mk +# + +CC = /bin/gcc +PREFIX = /usr diff --git a/include/csvparser.h b/include/csvparser.h new file mode 100644 index 0000000..99708e3 --- /dev/null +++ b/include/csvparser.h @@ -0,0 +1,7 @@ +#ifndef CSVPARSER_H_ +#define CSVPARSER_H_ + +/* Functions */ +int parse_tokens(char *file); + +#endif diff --git a/man/libcsvparser.3 b/man/libcsvparser.3 new file mode 100644 index 0000000..8e72ae9 --- /dev/null +++ b/man/libcsvparser.3 @@ -0,0 +1,16 @@ +.\" Manpage for libcsvparser. +.TH man 3 "19 October 2023" "1.0" "Everest Linux Libraries Manual" +.SH NAME +libcsvparser \- csv parsing library +.SH SYNOPSIS +#include + +parse_tokens(FILE); + +(user)$ gcc -lcsvparser prog.c -o prog +.SH DESCRIPTION +libcsvparser is a library that can read a .csv file and print out its tokens individually. It is provided as a static library only. +.SH BUGS +Report all bugs on the issues page at https://git.everestlinux.org/EverestLinux/libcsvparser +.SH AUTHOR +Liam Waldron (liamwaldron@everestlinux.org) diff --git a/src/libcsvparser.c b/src/libcsvparser.c new file mode 100644 index 0000000..ed1ca47 --- /dev/null +++ b/src/libcsvparser.c @@ -0,0 +1,35 @@ +/* + libcsvparser.c - simple CSV parser + */ + +#include +#include +#include + +#define MAXCHAR 1000 + +int +parse_tokens(char *file) +{ + FILE *fp; + char row[MAXCHAR]; + char *token; + + fp = fopen(file, "r"); + if (fp == NULL) { + printf("csv file %s not found\n", file); + return 1; + } + + while (feof(fp) != true) { + fgets(row, MAXCHAR, fp); + token = strtok(row, ","); + + while (token != NULL) { + printf("%s\n", token); + token = strtok(NULL, ","); + } + } + + return 0; +}