Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/expat/escape_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,16 @@ std::string escape_cstr(const std::string& s)
}
return result;
}


std::ostream& operator<<(std::ostream& out, const Json_Escaped& e)
{
// escape_cstr changes exactly the characters '"', '\\' and those below 32.
for (std::string::size_type i = 0; i < e.s.size(); ++i)
{
unsigned char c = e.s[i];
if (c == '\"' || c == '\\' || c < 32)
return out<<escape_cstr(e.s);
}
return out.write(e.s.data(), e.s.size());
}
13 changes: 13 additions & 0 deletions src/expat/escape_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@
#ifndef DE__OSM3S___EXPAT__ESCAPE_JSON_H
#define DE__OSM3S___EXPAT__ESCAPE_JSON_H

#include <ostream>
#include <string>


std::string escape_cstr(const std::string& s);


/* Writes escape_cstr(s) to a stream. Most strings need no escaping; those are written
directly, without building an escaped copy. Use as out<<Json_Escaped(s); the
referenced string must outlive the expression. */
struct Json_Escaped
{
explicit Json_Escaped(const std::string& s_) : s(s_) {}
const std::string& s;
};

std::ostream& operator<<(std::ostream& out, const Json_Escaped& e);


#endif
13 changes: 13 additions & 0 deletions src/expat/escape_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,16 @@ std::string escape_xml(const std::string& s)
}
return result;
}


std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e)
{
// escape_xml changes exactly the characters '&', '"', '<', '>' and those below 32.
for (std::string::size_type i = 0; i < e.s.size(); ++i)
{
unsigned char c = e.s[i];
if (c == '&' || c == '\"' || c == '<' || c == '>' || c < 32)
return out<<escape_xml(e.s);
}
return out.write(e.s.data(), e.s.size());
}
13 changes: 13 additions & 0 deletions src/expat/escape_xml.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,23 @@
#ifndef DE__OSM3S___EXPAT__ESCAPE_XML_H
#define DE__OSM3S___EXPAT__ESCAPE_XML_H

#include <ostream>
#include <string>


std::string escape_xml(const std::string& s);


/* Writes escape_xml(s) to a stream. Most strings need no escaping; those are written
directly, without building an escaped copy. Use as out<<Xml_Escaped(s); the
referenced string must outlive the expression. */
struct Xml_Escaped
{
explicit Xml_Escaped(const std::string& s_) : s(s_) {}
const std::string& s;
};

std::ostream& operator<<(std::ostream& out, const Xml_Escaped& e);


#endif
2 changes: 1 addition & 1 deletion src/overpass_api/output_formats/output_custom.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ std::string process_members(const std::string& raw_template, uint32 id, const Re
else if (raw_template.substr(new_pos, 10) == "{{{role}}}")
{
std::map< uint32, std::string >::const_iterator rit = roles.find(entry.role);
result<<escape_xml(rit != roles.end() ? rit->second : "???");
result<<Xml_Escaped(rit != roles.end() ? rit->second : "???");
}
else if (raw_template.substr(new_pos, 8) == "{{first:")
;
Expand Down
8 changes: 4 additions & 4 deletions src/overpass_api/output_formats/output_json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void print_meta_json(const OSM_Element_Metadata_Skeleton< Id_Type >& meta,
",\n \"changeset\": "<<meta.changeset;
std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id);
if (it != users.end())
std::cout<<",\n \"user\": \""<<escape_cstr(it->second)<<"\"";
std::cout<<",\n \"user\": \""<<Json_Escaped(it->second)<<"\"";
std::cout<<",\n \"uid\": "<<meta.user_id;
}

Expand All @@ -97,9 +97,9 @@ void print_tags(const std::vector< std::pair< std::string, std::string > >* tags
{
std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin();
std::cout<<",\n \"tags\": {"
"\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
"\n \""<<Json_Escaped(it->first)<<"\": \""<<Json_Escaped(it->second)<<"\"";
for (++it; it != tags->end(); ++it)
std::cout<<",\n \""<<escape_cstr(it->first)<<"\": \""<<escape_cstr(it->second)<<"\"";
std::cout<<",\n \""<<Json_Escaped(it->first)<<"\": \""<<Json_Escaped(it->second)<<"\"";
std::cout<<"\n }";
}
}
Expand Down Expand Up @@ -242,7 +242,7 @@ void Output_JSON::print_item(const Relation_Skeleton& skel,
std::cout <<"\n {"
"\n \"type\": \""<<member_type_name(skel.members[i].type)<<
"\",\n \"ref\": "<<skel.members[i].ref.val()<<
",\n \"role\": \""<<escape_cstr(rit != roles->end() ? rit->second : "???") << "\"";
",\n \"role\": \""<<Json_Escaped(rit != roles->end() ? rit->second : "???") << "\"";

if (skel.members[i].type == Relation_Entry::NODE &&
geometry.has_faithful_relation_geometry() && geometry.relation_pos_is_valid(i))
Expand Down
6 changes: 3 additions & 3 deletions src/overpass_api/output_formats/output_xml.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void print_meta_xml(const OSM_Element_Metadata_Skeleton< Id_Type >& meta,
<<"\" changeset=\""<<meta.changeset<<"\" uid=\""<<meta.user_id<<"\"";
std::map< uint32, std::string >::const_iterator it = users.find(meta.user_id);
if (it != users.end())
std::cout<<" user=\""<<escape_xml(it->second)<<"\"";
std::cout<<" user=\""<<Xml_Escaped(it->second)<<"\"";
}


Expand Down Expand Up @@ -152,7 +152,7 @@ void print_tags(const std::vector< std::pair< std::string, std::string > >* tags
}
for (std::vector< std::pair< std::string, std::string > >::const_iterator it = tags->begin();
it != tags->end(); ++it)
std::cout<<" <tag k=\""<<escape_xml(it->first)<<"\" v=\""<<escape_xml(it->second)<<"\"/>\n";
std::cout<<" <tag k=\""<<Xml_Escaped(it->first)<<"\" v=\""<<Xml_Escaped(it->second)<<"\"/>\n";
}
}

Expand Down Expand Up @@ -322,7 +322,7 @@ void print_members(const Relation_Skeleton& skel, const Opaque_Geometry& geometr
std::map< uint32, std::string >::const_iterator it = roles.find(skel.members[i].role);
std::cout<<" <member type=\""<<member_type_name(skel.members[i].type)
<<"\" ref=\""<<skel.members[i].ref.val()
<<"\" role=\""<<escape_xml(it != roles.end() ? it->second : "???")<<"\"";
<<"\" role=\""<<Xml_Escaped(it != roles.end() ? it->second : "???")<<"\"";

if (skel.members[i].type == Relation_Entry::NODE)
{
Expand Down