Move CSV code into its own file

This commit is contained in:
Eric Fischer
2017-11-03 15:15:28 -07:00
parent c217a77b0a
commit 21042a7308
4 changed files with 100 additions and 85 deletions
+2 -84
View File
@@ -23,6 +23,7 @@
#include "geometry.hpp"
#include "dirtiles.hpp"
#include "evaluator.hpp"
#include "csv.hpp"
#include <fstream>
#include <sstream>
#include <algorithm>
@@ -30,8 +31,6 @@
#include "jsonpull/jsonpull.h"
#include "milo/dtoa_milo.h"
std::string dequote(std::string s);
int pk = false;
int pC = false;
int pg = false;
@@ -213,7 +212,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::st
if (joinval.size() > 0) {
if (joinval[0] == '"') {
joinval = dequote(joinval);
joinval = csv_dequote(joinval);
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') {
attr_type = mvt_double;
}
@@ -979,87 +978,6 @@ void usage(char **argv) {
exit(EXIT_FAILURE);
}
#define MAXLINE 10000 /* XXX */
std::vector<std::string> split(char *s) {
std::vector<std::string> ret;
while (*s && *s != '\n' && *s != '\r') {
char *start = s;
int within = 0;
for (; *s && *s != '\n' && *s != '\r'; s++) {
if (*s == '"') {
within = !within;
}
if (*s == ',' && !within) {
break;
}
}
std::string v = std::string(start, s - start);
ret.push_back(v);
if (*s == ',') {
s++;
while (*s && isspace(*s)) {
s++;
}
}
}
return ret;
}
std::string dequote(std::string s) {
std::string out;
for (size_t i = 0; i < s.size(); i++) {
if (s[i] == '"') {
if (i + 1 < s.size() && s[i + 1] == '"') {
out.push_back('"');
}
} else {
out.push_back(s[i]);
}
}
return out;
}
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping) {
FILE *f = fopen(fn, "r");
if (f == NULL) {
perror(fn);
exit(EXIT_FAILURE);
}
char s[MAXLINE];
if (fgets(s, MAXLINE, f)) {
header = split(s);
for (size_t i = 0; i < header.size(); i++) {
header[i] = dequote(header[i]);
}
}
while (fgets(s, MAXLINE, f)) {
std::vector<std::string> line = split(s);
if (line.size() > 0) {
line[0] = dequote(line[0]);
}
for (size_t i = 0; i < line.size() && i < header.size(); i++) {
// printf("putting %s\n", line[0].c_str());
mapping.insert(std::pair<std::string, std::vector<std::string>>(line[0], line));
}
}
if (fclose(f) != 0) {
perror("fclose");
exit(EXIT_FAILURE);
}
}
int main(int argc, char **argv) {
char *out_mbtiles = NULL;
char *out_dir = NULL;