Skip to content
This repository has been archived by the owner on Oct 5, 2024. It is now read-only.

Commit

Permalink
Fix for escaped JSON string, code improvement and removed verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
emi420 committed Jan 4, 2024
1 parent 103328c commit 6281af9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
48 changes: 39 additions & 9 deletions src/data/pq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

#include "utils/log.hh"
using namespace logger;
Expand Down Expand Up @@ -170,30 +171,59 @@ std::string Latin1ToUTF8(const std::string& latin1str) {
}

std::string
Pq::escapedString(std::string text)
Pq::escapedString(const std::string &s)
{
std::string newstr;
int i = 0;
while (i < text.size()) {
if (text[i] == '\'') {
while (i < s.size()) {
if (s[i] == '\'') {
newstr += "&apos;";
} else if (text[i] == '\"') {
} else if (s[i] == '\"') {
newstr += "&quot;";
} else if (text[i] == '\'') {
} else if (s[i] == '\'') {
newstr += "&quot;";
} else if (text[i] == ')') {
} else if (s[i] == ')') {
newstr += "&#41;";
} else if (text[i] == '(') {
} else if (s[i] == '(') {
newstr += "&#40;";
} else if ((text[i] == '\\') || (text[i] == '\n')) {
} else if ((s[i] == '\\') || (s[i] == '\n')) {
// drop this character
} else {
newstr += text[i];
newstr += s[i];
}
i++;
}

return sdb->esc(Latin1ToUTF8(newstr));
}

std::string
Pq::escapedJSON(const std::string &s) {
std::ostringstream o;
for (auto c = s.cbegin(); c != s.cend(); c++) {
switch (*c) {
case '\x00': o << "\\u0000"; break;
case '\x01': o << " "; break;
case '\x02': o << " "; break;
case '\x03': o << " "; break;
case '\x04': o << " "; break;
case '\x05': o << " "; break;
case '\x06': o << " "; break;
case '\x07': o << " "; break;
case '\x08': o << " "; break;
case '\x09': o << " "; break;
case '\x0a': o << "\\n"; break;
case '\x0b': o << " "; break;
case '\x0c': o << " "; break;
case '\x0d': o << " "; break;
case '\x0e': o << " "; break;
case '\x1f': o << "\\u001f"; break;
case '\x22': o << "\\\""; break;
case '\x5c': o << "\\\\"; break;
default: o << *c;
}
}
return o.str();
}

} // namespace pq
5 changes: 4 additions & 1 deletion src/data/pq.hh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class Pq {
void dump(void);

// Escape string
std::string escapedString(std::string text);
std::string escapedString(const std::string &s);

// Escape JSON
std::string escapedJSON(const std::string &s);

// Database connection
std::shared_ptr<pqxx::connection> sdb;
Expand Down
14 changes: 5 additions & 9 deletions src/raw/queryraw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,13 @@ QueryRaw::applyChange(const OsmNode &node) const
for (auto it = std::begin(node.tags); it != std::end(node.tags); ++it) {
std::string tag_format = "\"%s\" : \"%s\",";
boost::format tag_fmt(tag_format);
tag_fmt % dbconn->escapedString(it->first);
tag_fmt % dbconn->escapedString(it->second);
tag_fmt % dbconn->escapedString(dbconn->escapedJSON(it->first));
tag_fmt % dbconn->escapedString(dbconn->escapedJSON(it->second));
tags += tag_fmt.str();
}
tags.erase(tags.size() - 1);
tags = "'{" + tags + "}'";

} else {
tags = "null";
}
Expand Down Expand Up @@ -169,8 +170,8 @@ QueryRaw::applyChange(const OsmWay &way) const
for (auto it = std::begin(way.tags); it != std::end(way.tags); ++it) {
std::string tag_format = "\"%s\" : \"%s\",";
boost::format tag_fmt(tag_format);
tag_fmt % dbconn->escapedString(it->first);
tag_fmt % dbconn->escapedString(it->second);
tag_fmt % dbconn->escapedString(dbconn->escapedJSON(it->first));
tag_fmt % dbconn->escapedString(dbconn->escapedJSON(it->second));
tags += tag_fmt.str();
}
tags.erase(tags.size() - 1);
Expand Down Expand Up @@ -373,7 +374,6 @@ QueryRaw::getNodeCacheFromWays(std::shared_ptr<std::vector<OsmWay>> ways, std::m
}

std::map<std::string, std::string> parseTagsString(std::string input) {
// std::cout << "[INPUT] " << input << std::endl;
std::map<std::string, std::string> tags;
boost::property_tree::ptree pt;
try {
Expand All @@ -386,10 +386,6 @@ std::map<std::string, std::string> parseTagsString(std::string input) {
for (const auto& pair : pt) {
tags[pair.first] = pair.second.get_value<std::string>();
}
// for (const auto& pair : tags) {
// std::cout << pair.first << "=" << pair.second << std::endl;
// }
// std::cout << std::endl;
return tags;
}

Expand Down

0 comments on commit 6281af9

Please sign in to comment.