From a244effa91c04f9e46e506e72213fbd1080516af Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Mon, 13 Mar 2023 14:27:02 +0000 Subject: [PATCH] Add mapnik/filesystem.hpp ref:https://github.com/mapnik/mapnik/pull/4383#discussion_r1131405532 --- include/mapnik/filesystem.hpp | 43 ++++++++++++++++++++ src/fs.cpp | 15 +------ test/standalone/map_xml_test.cpp | 62 ++++++++++++----------------- test/unit/imaging/image_io_test.cpp | 13 +----- test/unit/renderer/cairo_io.cpp | 16 +------- test/unit/run.cpp | 10 +---- test/visual/config.hpp | 15 ++----- test/visual/renderer.hpp | 31 ++++++--------- test/visual/report.cpp | 15 ++++--- test/visual/report.hpp | 6 +-- test/visual/run.cpp | 14 +++---- test/visual/runner.cpp | 4 +- test/visual/runner.hpp | 2 +- 13 files changed, 109 insertions(+), 137 deletions(-) create mode 100644 include/mapnik/filesystem.hpp diff --git a/include/mapnik/filesystem.hpp b/include/mapnik/filesystem.hpp new file mode 100644 index 000000000..09a3731e4 --- /dev/null +++ b/include/mapnik/filesystem.hpp @@ -0,0 +1,43 @@ +/***************************************************************************** + * + * This file is part of Mapnik (c++ mapping toolkit) + * + * Copyright (C) 2023 Artem Pavlenko + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + *****************************************************************************/ + +#ifndef MAPNIK_FILESYSTEM_HPP +#define MAPNIK_FILESYSTEM_HPP + +#if defined(__cpp_lib_filesystem) && !defined(USE_BOOST_FILESYSTEM) +#include +#else +#include // for absolute, exists, etc +#include // for path, operator/ +#endif + +namespace mapnik { +#if defined(__cpp_lib_filesystem) && !defined(USE_BOOST_FILESYSTEM) +namespace fs = std::filesystem; +using error_code = std::error_code; +#else +namespace fs = boost::filesystem; +using error_code = boost::system::error_code; +#endif +} // namespace mapnik + +#endif // MAPNIK_FILESYSTEM_HPP diff --git a/src/fs.cpp b/src/fs.cpp index 0d3f72d85..6139218b4 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -23,20 +23,7 @@ // mapnik #include #include - -#include - -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -MAPNIK_DISABLE_WARNING_PUSH -#include -#include // for absolute, exists, etc -#include // for path, operator/ -MAPNIK_DISABLE_WARNING_POP -namespace fs = boost::filesystem; -#endif +#include // stl #include diff --git a/test/standalone/map_xml_test.cpp b/test/standalone/map_xml_test.cpp index 73a6f1ef5..35026d5f1 100644 --- a/test/standalone/map_xml_test.cpp +++ b/test/standalone/map_xml_test.cpp @@ -8,18 +8,7 @@ #include #include #include - -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -#include -namespace fs = std::filesystem; -using error_code = std::error_code; -#else -#include -namespace fs = boost::filesystem; -using error_code = boost::system::error_code; -#endif - +#include #include #include @@ -42,13 +31,13 @@ std::string unique_mapnik_name() class tmp_dir { private: - fs::path m_path; + mapnik::fs::path m_path; public: tmp_dir() - : m_path(fs::temp_directory_path() / unique_mapnik_name()) + : m_path(mapnik::fs::temp_directory_path() / unique_mapnik_name()) { - fs::create_directories(m_path); + mapnik::fs::create_directories(m_path); } ~tmp_dir() @@ -57,16 +46,16 @@ class tmp_dir // running, which isn't necessarily an error as far as this // code is concerned - it just wants to delete everything // underneath the temporary directory. - error_code err; + mapnik::error_code err; // catch all errors - we don't want to throw in the destructor try { // but loop while the path exists and the errors are // ignorable. - while (fs::exists(m_path)) + while (mapnik::fs::exists(m_path)) { - fs::remove_all(m_path, err); + mapnik::fs::remove_all(m_path, err); // for any non-ignorable error, there's not much we can // do from the destructor. it's in /tmp anyway, so it'll @@ -89,22 +78,22 @@ class tmp_dir } } - fs::path path() const { return m_path; } + mapnik::fs::path path() const { return m_path; } }; -void compare_map(fs::path xml) +void compare_map(mapnik::fs::path xml) { tmp_dir dir; mapnik::Map m(256, 256); REQUIRE(m.register_fonts("fonts", true)); - fs::path abs_base = xml.parent_path(); + mapnik::fs::path abs_base = xml.parent_path(); // first, load the XML into a map object and save it. this // is a normalisation step to ensure that the file is in // whatever the current version of mapnik uses as the // standard indentation, quote style, etc... REQUIRE_NOTHROW(mapnik::load_map(m, xml.generic_string(), false, abs_base.generic_string())); - fs::path test_map1 = dir.path() / "mapnik-temp-map1.xml"; + mapnik::fs::path test_map1 = dir.path() / "mapnik-temp-map1.xml"; REQUIRE_NOTHROW(mapnik::save_map(m, test_map1.generic_string())); // create a new map, load the one saved in the previous @@ -112,23 +101,24 @@ void compare_map(fs::path xml) mapnik::Map new_map(256, 256); REQUIRE(new_map.register_fonts("fonts", true)); REQUIRE_NOTHROW(mapnik::load_map(new_map, test_map1.generic_string(), false, abs_base.generic_string())); - fs::path test_map2 = dir.path() / "mapnik-temp-map2.xml"; + mapnik::fs::path test_map2 = dir.path() / "mapnik-temp-map2.xml"; REQUIRE_NOTHROW(mapnik::save_map(new_map, test_map2.generic_string())); // if all the information survived the load/save round-trip // then the two files ought to be identical. - REQUIRE(fs::is_regular_file(test_map1)); - REQUIRE(fs::is_regular_file(test_map2)); - REQUIRE(fs::file_size(test_map1) == fs::file_size(test_map2)); + REQUIRE(mapnik::fs::is_regular_file(test_map1)); + REQUIRE(mapnik::fs::is_regular_file(test_map2)); + REQUIRE(mapnik::fs::file_size(test_map1) == mapnik::fs::file_size(test_map2)); std::ifstream in_map1(test_map1.native()), in_map2(test_map2.native()); REQUIRE(std::equal(std::istream_iterator(in_map1), std::istream_iterator(), std::istream_iterator(in_map2))); } -void add_xml_files(fs::path dir, std::vector& xml_files) +void add_xml_files(mapnik::fs::path dir, std::vector& xml_files) { - for (auto const& entry : boost::make_iterator_range(fs::directory_iterator(dir), fs::directory_iterator())) + for (auto const& entry : + boost::make_iterator_range(mapnik::fs::directory_iterator(dir), mapnik::fs::directory_iterator())) { auto path = entry.path(); if (path.extension().generic_string() == ".xml") @@ -138,7 +128,7 @@ void add_xml_files(fs::path dir, std::vector& xml_files) } } -void load_map(mapnik::Map& m, fs::path const& path) +void load_map(mapnik::Map& m, mapnik::fs::path const& path) { try { @@ -161,7 +151,7 @@ void load_map(mapnik::Map& m, fs::path const& path) } // anonymous namespace #ifndef MAPNIK_STATIC_PLUGINS const bool registered = - mapnik::datasource_cache::instance().register_datasources((fs::path("plugins") / "input").generic_string()); + mapnik::datasource_cache::instance().register_datasources((mapnik::fs::path("plugins") / "input").generic_string()); #endif TEST_CASE("map xml I/O") { @@ -177,8 +167,8 @@ TEST_CASE("map xml I/O") SECTION("good maps") { - std::vector good_maps; - add_xml_files(fs::path("test") / "data" / "good_maps", good_maps); + std::vector good_maps; + add_xml_files(mapnik::fs::path("test") / "data" / "good_maps", good_maps); for (auto const& path : good_maps) { @@ -197,7 +187,7 @@ TEST_CASE("map xml I/O") SECTION("duplicate styles only throw in strict mode") { std::string duplicate_stylename( - (fs::path("test") / "data" / "broken_maps" / "duplicate_stylename.xml").generic_string()); + (mapnik::fs::path("test") / "data" / "broken_maps" / "duplicate_stylename.xml").generic_string()); CAPTURE(duplicate_stylename); mapnik::Map m(256, 256); REQUIRE(m.register_fonts("fonts", true)); @@ -209,9 +199,9 @@ TEST_CASE("map xml I/O") SECTION("broken maps") { - std::vector broken_maps; - add_xml_files(fs::path("test") / "data" / "broken_maps", broken_maps); - broken_maps.emplace_back(fs::path("test") / "data" / "broken_maps" / "does_not_exist.xml"); + std::vector broken_maps; + add_xml_files(mapnik::fs::path("test") / "data" / "broken_maps", broken_maps); + broken_maps.emplace_back(mapnik::fs::path("test") / "data" / "broken_maps" / "does_not_exist.xml"); for (auto const& path : broken_maps) { diff --git a/test/unit/imaging/image_io_test.cpp b/test/unit/imaging/image_io_test.cpp index 45d98c754..9ce008d80 100644 --- a/test/unit/imaging/image_io_test.cpp +++ b/test/unit/imaging/image_io_test.cpp @@ -20,21 +20,12 @@ MAPNIK_DISABLE_WARNING_PUSH #include MAPNIK_DISABLE_WARNING_POP -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -MAPNIK_DISABLE_WARNING_PUSH -#include -MAPNIK_DISABLE_WARNING_POP -namespace fs = boost::filesystem; -#endif - +#include #include inline void make_directory(std::string const& dir) { - fs::create_directories(dir); + mapnik::fs::create_directories(dir); } namespace { diff --git a/test/unit/renderer/cairo_io.cpp b/test/unit/renderer/cairo_io.cpp index 1b1725039..64b65bbd3 100644 --- a/test/unit/renderer/cairo_io.cpp +++ b/test/unit/renderer/cairo_io.cpp @@ -1,20 +1,8 @@ #include "catch.hpp" #include +#include #include - -#include -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -MAPNIK_DISABLE_WARNING_PUSH -#include -#include -MAPNIK_DISABLE_WARNING_POP -namespace fs = boost::filesystem; -#endif - #include #if defined(HAVE_CAIRO) @@ -28,7 +16,7 @@ TEST_CASE("cairo_io") SECTION("save_to_cairo_file - SVG") { std::string directory_name("/tmp/mapnik-tests/"); - fs::create_directories(directory_name); + mapnik::fs::create_directories(directory_name); REQUIRE(mapnik::util::exists(directory_name)); std::string output_file(directory_name + "test_save_to_cairo_file.svg"); diff --git a/test/unit/run.cpp b/test/unit/run.cpp index 993e247fd..dd9a8b363 100644 --- a/test/unit/run.cpp +++ b/test/unit/run.cpp @@ -3,15 +3,9 @@ #include #include +#include #include #include -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -#include -namespace fs = boost::filesystem; -#endif #include "cleanup.hpp" // run_cleanup() @@ -49,7 +43,7 @@ int main(int argc, char** argv) std::clog << "Could not find " << working_dir << "\n"; return -1; } - fs::current_path(working_dir); + mapnik::fs::current_path(working_dir); } if (result == 0) diff --git a/test/visual/config.hpp b/test/visual/config.hpp index bbfd16cff..a0d04cde5 100644 --- a/test/visual/config.hpp +++ b/test/visual/config.hpp @@ -28,16 +28,7 @@ #include #include #include - -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -// boost -#include -namespace fs = boost::filesystem; -#endif - +#include #include namespace visual_tests { @@ -82,8 +73,8 @@ struct result map_size size; map_size tiles; double scale_factor; - fs::path actual_image_path; - fs::path reference_image_path; + mapnik::fs::path actual_image_path; + mapnik::fs::path reference_image_path; std::string error_message; unsigned diff; std::chrono::high_resolution_clock::duration duration; diff --git a/test/visual/renderer.hpp b/test/visual/renderer.hpp index bdfe5c3be..bbc97c625 100644 --- a/test/visual/renderer.hpp +++ b/test/visual/renderer.hpp @@ -57,14 +57,7 @@ #include #endif -#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM) -#include -namespace fs = std::filesystem; -#else -// boost -#include -namespace fs = boost::filesystem; -#endif +#include namespace visual_tests { @@ -76,7 +69,7 @@ struct raster_renderer_base static constexpr const char* ext = ".png"; static constexpr const bool support_tiles = true; - unsigned compare(image_type const& actual, fs::path const& reference) const + unsigned compare(image_type const& actual, mapnik::fs::path const& reference) const { std::unique_ptr reader(mapnik::get_image_reader(reference.string(), "png")); if (!reader.get()) @@ -90,7 +83,7 @@ struct raster_renderer_base return mapnik::compare(actual, reference_image, 0, true); } - void save(image_type const& image, fs::path const& path) const + void save(image_type const& image, mapnik::fs::path const& path) const { mapnik::save_to_file(image, path.string(), "png32"); } @@ -102,7 +95,7 @@ struct vector_renderer_base static constexpr const bool support_tiles = false; - unsigned compare(image_type const& actual, fs::path const& reference) const + unsigned compare(image_type const& actual, mapnik::fs::path const& reference) const { std::ifstream stream(reference.string().c_str(), std::ios_base::in | std::ios_base::binary); if (!stream) @@ -113,7 +106,7 @@ struct vector_renderer_base return std::max(actual.size(), expected.size()) - std::min(actual.size(), expected.size()); } - void save(image_type const& image, fs::path const& path) const + void save(image_type const& image, mapnik::fs::path const& path) const { std::ofstream file(path.string().c_str(), std::ios::out | std::ios::trunc | std::ios::binary); if (!file) @@ -308,7 +301,7 @@ class renderer using renderer_type = Renderer; using image_type = typename Renderer::image_type; - renderer(fs::path const& _output_dir, fs::path const& _reference_dir, bool _overwrite) + renderer(mapnik::fs::path const& _output_dir, mapnik::fs::path const& _reference_dir, bool _overwrite) : ren() , output_dir(_output_dir) , reference_dir(_reference_dir) @@ -346,8 +339,8 @@ class renderer map_size const& tiles, double scale_factor) const { - fs::path reference = reference_dir / image_file_name(name, size, tiles, scale_factor, true); - bool reference_exists = fs::exists(reference); + mapnik::fs::path reference = reference_dir / image_file_name(name, size, tiles, scale_factor, true); + bool reference_exists = mapnik::fs::exists(reference); result res; res.state = reference_exists ? STATE_OK : STATE_OVERWRITE; @@ -361,8 +354,8 @@ class renderer if (res.diff) { - fs::create_directories(output_dir); - fs::path path = output_dir / image_file_name(name, size, tiles, scale_factor, false); + mapnik::fs::create_directories(output_dir); + mapnik::fs::path path = output_dir / image_file_name(name, size, tiles, scale_factor, false); res.actual_image_path = path; res.state = STATE_FAIL; ren.save(image, path); @@ -400,8 +393,8 @@ class renderer } const Renderer ren; - const fs::path output_dir; - const fs::path reference_dir; + const mapnik::fs::path output_dir; + const mapnik::fs::path reference_dir; const bool overwrite; }; diff --git a/test/visual/report.cpp b/test/visual/report.cpp index 1de87b6da..8d0eb41ae 100644 --- a/test/visual/report.cpp +++ b/test/visual/report.cpp @@ -172,7 +172,7 @@ void console_short_report::report(result const& r) } } -void html_report::report(result const& r, fs::path const& output_dir) +void html_report::report(result const& r, mapnik::fs::path const& output_dir) { if (r.state == STATE_ERROR) { @@ -180,8 +180,7 @@ void html_report::report(result const& r, fs::path const& output_dir) } else if (r.state == STATE_FAIL) { - using namespace fs; - + using namespace mapnik::fs; path reference = output_dir / r.reference_image_path.filename(); path actual = output_dir / r.actual_image_path.filename(); @@ -238,7 +237,7 @@ constexpr const char* html_footer = R"template( )template"; -void html_report::summary(result_list const& results, fs::path const& output_dir) +void html_report::summary(result_list const& results, mapnik::fs::path const& output_dir) { s << html_header; @@ -253,11 +252,11 @@ void html_report::summary(result_list const& results, fs::path const& output_dir s << html_footer; } -void html_summary(result_list const& results, fs::path output_dir) +void html_summary(result_list const& results, mapnik::fs::path output_dir) { - fs::path html_root = output_dir / "visual-test-results"; - fs::create_directories(html_root); - fs::path html_report_path = html_root / "index.html"; + mapnik::fs::path html_root = output_dir / "visual-test-results"; + mapnik::fs::create_directories(html_root); + mapnik::fs::path html_report_path = html_root / "index.html"; std::clog << "View failure report at " << html_report_path << "\n"; std::ofstream output_file(html_report_path.string()); html_report report(output_file); diff --git a/test/visual/report.hpp b/test/visual/report.hpp index 6e0ac5479..b0c40aee3 100644 --- a/test/visual/report.hpp +++ b/test/visual/report.hpp @@ -76,8 +76,8 @@ class html_report : s(_s) {} - void report(result const& r, fs::path const& output_dir); - void summary(result_list const& results, fs::path const& output_dir); + void report(result const& r, mapnik::fs::path const& output_dir); + void summary(result_list const& results, mapnik::fs::path const& output_dir); protected: std::ostream& s; @@ -119,7 +119,7 @@ class summary_visitor result_list const& result_; }; -void html_summary(result_list const& results, fs::path output_dir); +void html_summary(result_list const& results, mapnik::fs::path output_dir); } // namespace visual_tests diff --git a/test/visual/run.cpp b/test/visual/run.cpp index 93e03b27c..cab19c168 100644 --- a/test/visual/run.cpp +++ b/test/visual/run.cpp @@ -59,19 +59,15 @@ std::string unique_name() { std::mt19937 gen(entropy()); std::uniform_int_distribution<> distrib(0, 65535); - auto fmt = boost::format("%1$04x-%2$04x-%3$04x-%4$04x") - % distrib(gen) - % distrib(gen) - % distrib(gen) - %distrib(gen); + auto fmt = boost::format("%1$04x-%2$04x-%3$04x-%4$04x") % distrib(gen) % distrib(gen) % distrib(gen) % distrib(gen); return fmt.str(); } -} +} // namespace runner::renderer_container - create_renderers(po::variables_map const& args, fs::path const& output_dir, bool force_append = false) + create_renderers(po::variables_map const& args, mapnik::fs::path const& output_dir, bool force_append = false) { - fs::path reference_dir(args["images-dir"].as()); + mapnik::fs::path reference_dir(args["images-dir"].as()); bool overwrite = args.count("overwrite"); runner::renderer_container renderers; @@ -205,7 +201,7 @@ int main(int argc, char** argv) mapnik::freetype_engine::register_fonts(vm["fonts"].as(), true); mapnik::datasource_cache::instance().register_datasources(vm["plugins"].as()); - fs::path output_dir(vm["output-dir"].as()); + mapnik::fs::path output_dir(vm["output-dir"].as()); if (vm.count("unique-subdir")) { diff --git a/test/visual/runner.cpp b/test/visual/runner.cpp index 9eac5e9f8..9394d1b47 100644 --- a/test/visual/runner.cpp +++ b/test/visual/runner.cpp @@ -149,8 +149,8 @@ runner::runner(runner::path_type const& styles_dir, result_list runner::test_all(report_type& report) const { - fs::directory_iterator begin(styles_dir_); - fs::directory_iterator end; + mapnik::fs::directory_iterator begin(styles_dir_); + mapnik::fs::directory_iterator end; std::vector files(begin, end); return test_parallel(files, report, jobs_); } diff --git a/test/visual/runner.hpp b/test/visual/runner.hpp index d806b97b6..1530640e0 100644 --- a/test/visual/runner.hpp +++ b/test/visual/runner.hpp @@ -31,7 +31,7 @@ namespace visual_tests { class runner { - using path_type = fs::path; + using path_type = mapnik::fs::path; using files_iterator = std::vector::const_iterator; public: