Overpass API regression suite coverage (Saxony) / overpass_api/statements/osm_script.cc

overpass_api/statements/osm_script.cc

117 of 192 instrumented lines run by the suite (60.9%) 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 <fstream>
20#include <iostream>
21#include <sstream>
23#include "../core/settings.h"
24#include "../frontend/output_handler_parser.h"
25#include "bbox_query.h"
26#include "osm_script.h"
27#include "print.h"
30Generic_Statement_Maker< Osm_Script_Statement > Osm_Script_Statement::statement_maker("osm-script");
33int64 eval_number_with_suffix(const std::string& arg)
341384692 q{
351384692 q char* pos = 0;
361384692 q errno = 0;
371384692 q int64 result = strtoll(&arg[0], &pos, 0);
381384692 q if (errno)
390 return 0;
401384692 q auto offset = std::distance(&arg[0], (const char*)pos);
411384692 q if ((decltype(offset))arg.size() <= offset)
421384692 q return result;
430 std::string suffix = arg.substr(offset);
440 if (suffix == "ki" || suffix == " ki")
450 return result<<10;
460 if (suffix == "Mi" || suffix == " Mi")
470 return result<<20;
480 if (suffix == "Gi" || suffix == " Gi")
490 return result<<30;
500 if (suffix == "k" || suffix == " k")
510 return result * 1000;
520 if (suffix == "M" || suffix == " M")
530 return result * 1000000;
540 if (suffix == "G" || suffix == " G")
550 return result * 1000000000;
560 return 0;
570}
60Osm_Script_Statement::Osm_Script_Statement
61 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
62692692 q : Statement(line_number_),
63692692 q desired_timestamp(NOW), comparison_timestamp(0), add_deletion_information(false),
64692692 q max_allowed_time(0), max_allowed_space(0),
65692692 q factory(0)
67692692 q std::map< std::string, std::string > attributes;
69692692 q attributes["bbox"] = "";
70692692 q attributes["timeout"] = "180";
71692692 q attributes["element-limit"] = "536870912";
72692692 q attributes["output"] = "xml";
73692692 q attributes["output-config"] = "";
74692692 q attributes["date"] = "";
75692692 q attributes["from"] = "";
76692692 q attributes["augmented"] = "";
78692692 q eval_attributes_array(get_name(), attributes, input_attributes);
80692692 q int64 timeout = eval_number_with_suffix(attributes["timeout"]);
81692692 q if (timeout <= 0)
820 {
830 std::ostringstream temp;
840 temp<<"For the attribute \"timeout\" of the element \"osm-script\""
850 <<" the only allowed values are positive integers optionally with suffix \"ki\", \"Mi\", or \"Gi\".";
860 add_static_error(temp.str());
870 }
88692692 q max_allowed_time = timeout;
90692692 q int64 max_space = eval_number_with_suffix(attributes["element-limit"]);
91692692 q if (max_space <= 0)
920 add_static_error("For the attribute \"element-limit\" of the element \"osm-script\""
930 " the only allowed values are positive integers optionally with suffix \"ki\", \"Mi\", or \"Gi\".");
95692692 q max_allowed_space = max_space;
98692692 q if (!global_settings.get_output_handler())
99563563 q {
100563563 q Output_Handler_Parser* format_parser = Output_Handler_Parser::get_format_parser(attributes["output"]);
101563563 q if (!format_parser)
1020 add_static_error("Unknown output format: " + attributes["output"]);
103563563 q else
105563563 q if (attributes["output-config"] == "")
106563563 q global_settings.set_output_handler(format_parser, 0, 0);
1070 else
1080 {
1090 std::istringstream in(attributes["output-config"]);
1100 Tokenizer_Wrapper token(in);
1110 global_settings.set_output_handler(format_parser, &token, 0);
1120 }
117692692 q if (attributes["bbox"] != "")
1192323 q std::map< std::string, std::string > bbox_attributes;
1212323 q std::string& bbox_s = attributes["bbox"];
1222323 q std::string::size_type pos = bbox_s.find(",");
1232323 q std::string::size_type from = 0;
1242323 q if (pos != std::string::npos)
1262323 q bbox_attributes["s"] = bbox_s.substr(0, pos);
1272323 q from = pos + 1;
1282323 q pos = bbox_s.find(",", from);
1300 else
1310 {
1320 std::ostringstream temp;
1330 temp<<"A bounding box needs four comma-separated values.";
1340 add_static_error(temp.str());
1350 }
1362323 q if (pos != std::string::npos)
1382323 q bbox_attributes["w"] = bbox_s.substr(from, pos-from);
1392323 q from = pos + 1;
1402323 q pos = bbox_s.find(",", from);
1420 else
1430 {
1440 std::ostringstream temp;
1450 temp<<"A bounding box needs four comma-separated values.";
1460 add_static_error(temp.str());
1470 }
1482323 q if (pos != std::string::npos)
1502323 q bbox_attributes["n"] = bbox_s.substr(from, pos-from);
1512323 q from = pos + 1;
1530 else
1540 {
1550 std::ostringstream temp;
1560 temp<<"A bounding box needs four comma-separated values.";
1570 add_static_error(temp.str());
1580 }
1592323 q bbox_attributes["e"] = bbox_s.substr(from);
1612323 q double south = atof(bbox_attributes["s"].c_str());
1622323 q double north = atof(bbox_attributes["n"].c_str());
1632323 q if (south < -90.0 || south > 90.0 || north < -90.0 || north > 90.0)
1640 {
1650 std::ostringstream temp;
1660 temp<<"Latitudes in bounding boxes must be between -90.0 and 90.0.";
1670 add_static_error(temp.str());
1680 }
1702323 q double west = atof(bbox_attributes["w"].c_str());
1712323 q double east = atof(bbox_attributes["e"].c_str());
1722323 q if (west < -180.0 || west > 180.0 || east < -180.0 || east > 180.0)
1730 {
1740 std::ostringstream temp;
1750 temp<<"Longitudes in bounding boxes must be between -180.0 and 180.0.";
1760 add_static_error(temp.str());
1770 }
1792323 q if (south >= -90.0 && south <= 90.0 && north >= -90.0 && north <= 90.0
1802323 q && west >= -180.0 && west <= 180.0 && east >= -180.0 && east <= 180.0)
1812323 q global_settings.set_global_bbox(Bbox_Double(south, west, north, east));
184692692 q if (attributes["date"] != "")
186223223 q desired_timestamp = Timestamp(attributes["date"]).timestamp;
187223223 q if (desired_timestamp == 0)
1880 add_static_error("The attribute \"date\" must be empty or contain a timestamp exactly in the form \"yyyy-mm-ddThh:mm:ssZ\".");
191692692 q if (attributes["from"] != "")
1938181 q comparison_timestamp = Timestamp(attributes["from"]).timestamp;
1948181 q if (comparison_timestamp == 0)
1950 add_static_error("The attribute \"from\" must be empty or contain a timestamp exactly in the form \"yyyy-mm-ddThh:mm:ssZ\".");
1968181 q else if (global_settings.get_output_handler() && !global_settings.get_output_handler()->supports_diff())
1970 add_static_error("The selected output format does not support the diff or adiff mode.");
200692692 q if (attributes["augmented"] != "")
2023333 q if (attributes["augmented"] == "deletions" && attributes["from"] != "")
2033333 q add_deletion_information = true;
2053333 q if (attributes["augmented"] != "deletions")
2060 {
2070 std::ostringstream temp;
2080 temp<<"The only allowed values for \"augmented\" are an empty value or \"deletions\".";
2090 add_static_error(temp.str());
2100 }
2113333 q if (attributes["from"] == "")
2120 {
2130 std::ostringstream temp;
2140 temp<<"The attribute \"augmented\" can only be set if the attribute \"from\" is set.";
2150 add_static_error(temp.str());
2160 }
220void Osm_Script_Statement::add_statement(Statement* statement, std::string text)
2225689689 q assure_no_text(text, this->get_name());
2245689689 q if (statement)
2255689689 q {
2265689689 q if (statement->get_name() != "newer")
2275689689 q substatements.push_back(statement);
2280 else
2290 add_static_error("\"newer\" can appear only inside \"query\" statements.");
2305689689 q }
234void Osm_Script_Statement::execute(Resource_Manager& rman)
236673673 q rman.set_limits(max_allowed_time, max_allowed_space);
237673673 q rman.get_global_settings().trigger_print_bounds();
239673673 q if (comparison_timestamp > 0)
2418181 q rman.start_diff(comparison_timestamp, desired_timestamp);
2438181 q for (std::vector< Statement* >::iterator it(substatements.begin());
24477881 q it != substatements.end(); ++it)
24569781 q (*it)->execute(rman);
2478181 q rman.switch_diff_rhs(add_deletion_information);
250673673 q for (std::vector< Statement* >::iterator it(substatements.begin());
2516228673 q it != substatements.end(); ++it)
2525555672 q (*it)->execute(rman);
254673673 q if (rman.area_updater())
2550 rman.area_updater()->flush();
256673673 q rman.health_check(*this);