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

This commit is contained in:
Eric Fischer
2017-10-10 16:22:47 -07:00
parent 86a4ce67a6
commit 68a55b8749
4 changed files with 61 additions and 5 deletions
+59
View File
@@ -92,3 +92,62 @@ void readcsv(const char *fn, std::vector<std::string> &header, std::map<std::str
exit(EXIT_FAILURE); 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;
}
+1
View File
@@ -11,5 +11,6 @@ std::vector<std::string> csv_split(const char *s);
std::string csv_dequote(std::string s); std::string csv_dequote(std::string s);
void readcsv(const char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping); void readcsv(const char *fn, std::vector<std::string> &header, std::map<std::string, std::vector<std::string>> &mapping);
std::string csv_getline(FILE *f); std::string csv_getline(FILE *f);
bool is_number(std::string const &s);
#endif #endif
-4
View File
@@ -190,10 +190,6 @@ void out(std::string const &s, int type, json_object *properties) {
std::string prev_joinkey; std::string prev_joinkey;
bool is_number(std::string const &s) {
return false; // XXX
}
void join_csv(json_object *j) { void join_csv(json_object *j) {
if (header.size() == 0) { if (header.size() == 0) {
std::string s = csv_getline(csvfile); std::string s = csv_getline(csvfile);
+1 -1
View File
@@ -207,7 +207,7 @@ void handle(std::string message, int z, unsigned x, unsigned y, std::map<std::st
if (joinval.size() > 0) { if (joinval.size() > 0) {
if (joinval[0] == '"') { if (joinval[0] == '"') {
joinval = csv_dequote(joinval); joinval = csv_dequote(joinval);
} else if ((joinval[0] >= '0' && joinval[0] <= '9') || joinval[0] == '-') { } else if (is_number(joinval)) {
attr_type = mvt_double; attr_type = mvt_double;
} }
} }