Follow JSON rules for what looks like a number in a CSV

This commit is contained in:
Eric Fischer 2017-10-10 16:22:21 -07:00
parent 6467a5b70d
commit 1960eb8dae
3 changed files with 62 additions and 1 deletions

59
csv.cpp
View File

@ -92,3 +92,62 @@ void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, s
exit(EXIT_FAILURE);
}
}
// Follow JSON rules for what looks like a number
bool is_number(std::string const &s) {
const char *cp = s.c_str();
char c = *(cp++);
if (c == '-' || (c >= '0' && c <= '9')) {
if (c == '-') {
c = *(cp++);
}
if (c == '0') {
;
} else if (c >= '1' && c <= '9') {
c = *cp;
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
if (*cp == '.') {
cp++;
c = *cp;
if (c < '0' || c > '9') {
return false;
}
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
c = *cp;
if (c == 'e' || c == 'E') {
cp++;
c = *cp;
if (c == '+' || c == '-') {
cp++;
}
c = *cp;
if (c < '0' || c > '9') {
return false;
}
while (c >= '0' && c <= '9') {
cp++;
c = *cp;
}
}
return true;
}
return false;
}

View File

@ -10,5 +10,7 @@
std::vector<std::string> csv_split(char *s);
std::string csv_dequote(std::string s);
void readcsv(char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping);
std::string csv_getline(FILE *f);
bool is_number(std::string const &s);
#endif

View File

@ -213,7 +213,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::st
if (joinval.size() > 0) {
if (joinval[0] == '"') {
joinval = csv_dequote(joinval);
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') {
} else if (is_number(joinval)) {
attr_type = mvt_double;
}
}