From face450102c70d25ade8f4d96f1c77403922009d Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Tue, 10 Oct 2017 15:06:12 -0700 Subject: [PATCH] Fix the arbitrary line length limit --- csv.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/csv.cpp b/csv.cpp index 5fc0b0d..678c604 100644 --- a/csv.cpp +++ b/csv.cpp @@ -2,11 +2,11 @@ #define MAXLINE 10000 /* XXX */ -std::vector csv_split(char *s) { +std::vector csv_split(const char *s) { std::vector ret; while (*s && *s != '\n' && *s != '\r') { - char *start = s; + const char *start = s; int within = 0; for (; *s && *s != '\n' && *s != '\r'; s++) { @@ -48,6 +48,18 @@ std::string csv_dequote(std::string s) { return out; } +std::string getline(FILE *f) { + std::string out; + int c; + while ((c = getc(f)) != EOF) { + out.push_back(c); + if (c == '\n') { + break; + } + } + return out; +} + void readcsv(char *fn, std::vector &header, std::map> &mapping) { FILE *f = fopen(fn, "r"); if (f == NULL) { @@ -55,16 +67,16 @@ void readcsv(char *fn, std::vector &header, std::map 0) { + header = csv_split(s.c_str()); for (size_t i = 0; i < header.size(); i++) { header[i] = csv_dequote(header[i]); } } - while (fgets(s, MAXLINE, f)) { - std::vector line = csv_split(s); + while ((s = getline(f)).size() > 0) { + std::vector line = csv_split(s.c_str()); if (line.size() > 0) { line[0] = csv_dequote(line[0]); } @@ -80,4 +92,3 @@ void readcsv(char *fn, std::vector &header, std::map