Overpass API regression suite coverage (Nova Scotia) / overpass_api/output_formats/output_csv.cc

overpass_api/output_formats/output_csv.cc

143 of 167 instrumented lines run by the suite (85.6%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#include "../../expat/escape_json.h"
20#include "output_csv.h"
23bool Output_CSV::write_http_headers()
240{
250 std::cout<<"Content-type: text/csv\n";
260 return true;
270}
30// Escape CSV output according to RFC 4180
31std::string escape_csv(const std::string& input)
3271025 q{
3371025 q std::string result;
3471025 q bool quotes_needed = false;
36689125 q for (int i = input.size() - 1; i >= 0; --i)
37618125 q {
38618125 q if (input[i] == '\n' || input[i] == ',')
390 quotes_needed = true;
40618125 q else if (input[i] == '\"')
410 {
420 quotes_needed = true;
430 if (result.empty())
440 result = input.substr(0, i) + "\"" + input.substr(i);
450 else
460 result = result.substr(0, i) + "\"" + result.substr(i);
470 }
48618125 q }
5071025 q if (!quotes_needed)
5171025 q return input;
520 else if (result.empty())
530 return std::string("\"") + input + "\"";
540 else
550 return std::string("\"") + result + "\"";
5671025 q}
59void Output_CSV::write_payload_header
60 (const std::string& db_dir, const std::string& timestamp, const std::string& area_timestamp)
622626 q if (csv_settings.with_headerline)
632525 q {
642525 q for (std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
656725 q it != csv_settings.keyfields.end(); ++it)
664225 q {
674225 q std::cout<<(it->second ? "@" : "")<<escape_csv(it->first);
684225 q if (it + 1 != csv_settings.keyfields.end())
691711 q std::cout<<csv_settings.separator;
704225 q }
712525 q std::cout<<'\n';
722525 q }
76void Output_CSV::write_footer()
78 // Intentionally empty
82void Output_CSV::display_remark(const std::string& text)
830{
84 // Intentionally empty
850}
88void Output_CSV::display_error(const std::string& text)
890{
90 // Intentionally empty
910}
94std::string Output_CSV::dump_config() const
9515626 q{
9615626 q std::string result = "(";
9815626 q for (std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
9942626 q it != csv_settings.keyfields.end(); ++it)
10027026 q {
10127026 q if (it != csv_settings.keyfields.begin())
10211412 q result += ",";
10427026 q if (it->second)
10522822 q result += "::" + it->first;
106427 q else
107427 q result += "\"" + escape_cstr(it->first) + "\"";
10827026 q }
11015626 q if (csv_settings.separator != "\t")
111183 q result += std::string(";") + (csv_settings.with_headerline ? "true" : "false") + ";\""
112183 q + escape_cstr(csv_settings.separator) + "\"";
11313823 q else if (!csv_settings.with_headerline)
1140 result += ";false";
11615626 q return result + ")";
120template< typename OSM_Element_Metadata_Skeleton >
121void print_meta(const std::string& keyfield,
122 const OSM_Element_Metadata_Skeleton& meta, const std::map< uint32, std::string >* users)
123900037 q{
124900037 q if (keyfield == "version")
12519642 q std::cout<<meta.version;
126880396 q else if (keyfield == "timestamp")
1270 std::cout<<Timestamp(meta.timestamp).str();
128880396 q else if (keyfield == "changeset")
12942 q std::cout<<meta.changeset;
130880354 q else if (keyfield == "uid")
131862212 q std::cout<<meta.user_id;
13218143 q else if (users && keyfield == "user")
13491 q std::map< uint32, std::string >::const_iterator uit = users->find(meta.user_id);
13591 q if (uit != users->end())
13691 q std::cout<<uit->second;
138900037 q}
141template< >
142void print_meta< int >(const std::string& keyfield,
1430 const int& meta, const std::map< uint32, std::string >* users) {}
145std::string get_count_tag(const std::vector< std::pair< std::string, std::string> >* tags, std::string tag)
147726 q if (tags)
148726 q for (std::vector< std::pair< std::string, std::string> >::const_iterator it_tags = tags->begin();
1491586 q it_tags != tags->end(); ++it_tags)
1501536 q if (it_tags->first == tag)
151674 q return it_tags->second;
15252 q return "0";
156template< typename Id_Type, typename OSM_Element_Metadata_Skeleton >
157void process_csv_line(int otype, const std::string& type, Id_Type id, const Opaque_Geometry& geometry,
158 const OSM_Element_Metadata_Skeleton* meta,
159 const std::vector< std::pair< std::string, std::string> >* tags,
160 const std::map< uint32, std::string >* users,
161 const Csv_Settings& csv_settings,
162 Output_Mode mode)
16314616626 q{
16414616626 q std::vector< std::pair< std::string, bool > >::const_iterator it = csv_settings.keyfields.begin();
16515330526 q while (true)
16615330526 q {
16715330526 q if (!it->second)
168438197 q {
169438197 q if (tags)
17072657 q {
17172657 q for (std::vector< std::pair< std::string, std::string> >::const_iterator it_tags = tags->begin();
172467867 q it_tags != tags->end(); ++it_tags)
173401897 q {
174401897 q if (it_tags->first == it->first)
1756683 q {
1766683 q std::cout<<escape_csv(it_tags->second);
1776683 q break;
1786683 q }
179401897 q }
18072657 q }
181438197 q }
18210948622 q else
18310948622 q {
18410948622 q if (meta)
185900037 q print_meta(it->first, *meta, users);
18710948622 q if (it->first == "id")
18872175 q {
18972175 q if (mode.mode & Output_Mode::ID)
19072145 q std::cout<<id.val();
19172175 q }
19210226921 q else if (it->first == "otype")
193293 q std::cout<<otype;
19410224018 q else if (it->first == "type")
19522123 q std::cout<<type;
19610002817 q else if (it->first == "lat")
19743986 q {
19843986 q if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
19943986 q && geometry.has_center())
20011664 q std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lat();
20143986 q }
2029563017 q else if (it->first == "lon")
20332557 q {
20432557 q if ((mode.mode & (Output_Mode::COORDS | Output_Mode::GEOMETRY | Output_Mode::BOUNDS | Output_Mode::CENTER))
20532557 q && geometry.has_center())
20611686 q std::cout<<std::fixed<<std::setprecision(7)<<geometry.center_lon();
20732557 q }
20810948622 q if (type == "count")
20950822 q {
21050822 q if (it->first == "count")
21172 q std::cout << get_count_tag(tags, "total");
21250120 q else if (it->first == "count:nodes")
213151 q std::cout << get_count_tag(tags, "nodes");
21448620 q else if (it->first == "count:ways")
215451 q std::cout << get_count_tag(tags, "ways");
21644120 q else if (it->first == "count:relations")
2170 std::cout << get_count_tag(tags, "relations");
21844120 q else if (it->first == "count:areas")
21952 q std::cout << get_count_tag(tags, "areas");
22050822 q }
22110948622 q }
22315330526 q if (++it == csv_settings.keyfields.end())
22414616626 q break;
225713912 q std::cout<<csv_settings.separator;
226713912 q }
22714616626 q std::cout<<"\n";
22814616626 q}
231void Output_CSV::print_item(const Node_Skeleton& skel,
232 const Opaque_Geometry& geometry,
233 const std::vector< std::pair< std::string, std::string > >* tags,
234 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* meta,
235 const std::map< uint32, std::string >* users,
236 Output_Mode mode,
237 const Feature_Action& action,
238 const Node_Skeleton* new_skel,
239 const Opaque_Geometry* new_geometry,
240 const std::vector< std::pair< std::string, std::string > >* new_tags,
241 const OSM_Element_Metadata_Skeleton< Node::Id_Type >* new_meta)
2424574413 q{
2434574413 q process_csv_line(1, "node", skel.id, geometry, meta, tags, users, csv_settings, mode);
2444574413 q}
247void Output_CSV::print_item(const Way_Skeleton& skel,
248 const Opaque_Geometry& geometry,
249 const std::vector< std::pair< std::string, std::string > >* tags,
250 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* meta,
251 const std::map< uint32, std::string >* users,
252 Output_Mode mode,
253 const Feature_Action& action,
254 const Way_Skeleton* new_skel,
255 const Opaque_Geometry* new_geometry,
256 const std::vector< std::pair< std::string, std::string > >* new_tags,
257 const OSM_Element_Metadata_Skeleton< Way::Id_Type >* new_meta)
2588394919 q{
2598394919 q process_csv_line(2, "way", skel.id, geometry, meta, tags, users, csv_settings, mode);
2608394919 q}
263void Output_CSV::print_item(const Relation_Skeleton& skel,
264 const Opaque_Geometry& geometry,
265 const std::vector< std::pair< std::string, std::string > >* tags,
266 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* meta,
267 const std::map< uint32, std::string >* roles,
268 const std::map< uint32, std::string >* users,
269 Output_Mode mode,
270 const Feature_Action& action,
271 const Relation_Skeleton* new_skel,
272 const Opaque_Geometry* new_geometry,
273 const std::vector< std::pair< std::string, std::string > >* new_tags,
274 const OSM_Element_Metadata_Skeleton< Relation::Id_Type >* new_meta)
27599359 q{
27699359 q process_csv_line(3, "relation", skel.id, geometry, meta, tags, users, csv_settings, mode);
27799359 q}
280void Output_CSV::print_item(const Derived_Skeleton& skel,
281 const Opaque_Geometry& geometry,
282 const std::vector< std::pair< std::string, std::string > >* tags,
283 Output_Mode mode,
284 const Feature_Action& action)
285653826 q{
286653826 q process_csv_line< Derived_Skeleton::Id_Type, int >(
287653826 q 4, skel.type_name, skel.id, geometry, 0, tags, 0, csv_settings, mode);
288653826 q}