From 2a33ead4cc33a759c5eba46e35b32268d99ec381 Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Tue, 6 Jan 2015 00:33:33 -0800 Subject: [PATCH] finish centralizing boost::filesystem usage to single cpp file - refs #1177 --- include/mapnik/util/fs.hpp | 3 +++ src/datasource_cache.cpp | 16 +++------------- src/font_engine_freetype.cpp | 16 +++------------- src/fs.cpp | 30 ++++++++++++++++++++++++++++-- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/include/mapnik/util/fs.hpp b/include/mapnik/util/fs.hpp index 540b1fb4c..3e01b9694 100644 --- a/include/mapnik/util/fs.hpp +++ b/include/mapnik/util/fs.hpp @@ -28,6 +28,7 @@ // stl #include +#include namespace mapnik { namespace util { @@ -39,6 +40,8 @@ MAPNIK_DECL bool is_relative(std::string const& value); MAPNIK_DECL std::string make_relative(std::string const& filepath, std::string const& base); MAPNIK_DECL std::string make_absolute(std::string const& filepath, std::string const& base); MAPNIK_DECL std::string dirname(std::string const& value); +MAPNIK_DECL std::string basename(std::string const& value); +MAPNIK_DECL std::vector list_directory(std::string const& value); }} diff --git a/src/datasource_cache.cpp b/src/datasource_cache.cpp index 5a17a6faf..946a03d6f 100644 --- a/src/datasource_cache.cpp +++ b/src/datasource_cache.cpp @@ -34,7 +34,6 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-local-typedef" -#include #include #include #pragma GCC diagnostic pop @@ -171,18 +170,9 @@ bool datasource_cache::register_datasources(std::string const& dir, bool recurse bool success = false; try { - boost::filesystem::directory_iterator end_itr; -#ifdef _WINDOWS - std::wstring wide_dir(mapnik::utf8_to_utf16(dir)); - for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr) + for (std::string const& file_name : mapnik::util::list_directory(dir)) { - std::string file_name = mapnik::utf16_to_utf8(itr->path().wstring()); -#else - for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr) - { - std::string file_name = itr->path().string(); -#endif - if (boost::filesystem::is_directory(*itr) && recurse) + if (mapnik::util::is_directory(file_name) && recurse) { if (register_datasources(file_name, true)) { @@ -191,7 +181,7 @@ bool datasource_cache::register_datasources(std::string const& dir, bool recurse } else { - std::string base_name = itr->path().filename().string(); + std::string base_name = mapnik::util::basename(file_name); if (!boost::algorithm::starts_with(base_name,".") && mapnik::util::is_regular_file(file_name) && is_input_plugin(file_name)) diff --git a/src/font_engine_freetype.cpp b/src/font_engine_freetype.cpp index 4d530626d..412b59511 100644 --- a/src/font_engine_freetype.cpp +++ b/src/font_engine_freetype.cpp @@ -35,7 +35,6 @@ #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-local-typedef" #include -#include #include #pragma GCC diagnostic pop @@ -189,18 +188,9 @@ bool freetype_engine::register_fonts_impl(std::string const& dir, bool success = false; try { - boost::filesystem::directory_iterator end_itr; -#ifdef _WINDOWS - std::wstring wide_dir(mapnik::utf8_to_utf16(dir)); - for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr) + for (std::string const& file_name : mapnik::util::list_directory(dir)) { - std::string file_name = mapnik::utf16_to_utf8(itr->path().wstring()); -#else - for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr) - { - std::string file_name = itr->path().string(); -#endif - if (boost::filesystem::is_directory(*itr) && recurse) + if (mapnik::util::is_directory(file_name) && recurse) { if (register_fonts_impl(file_name, library, font_file_mapping, true)) { @@ -209,7 +199,7 @@ bool freetype_engine::register_fonts_impl(std::string const& dir, } else { - std::string base_name = itr->path().filename().string(); + std::string base_name = mapnik::util::basename(file_name); if (!boost::algorithm::starts_with(base_name,".") && mapnik::util::is_regular_file(file_name) && is_font_file(file_name)) diff --git a/src/fs.cpp b/src/fs.cpp index c6e957b7d..655472487 100644 --- a/src/fs.cpp +++ b/src/fs.cpp @@ -25,7 +25,8 @@ #include // boost -#include +#include // for absolute, exists, etc +#include // for path, operator/ // stl #include @@ -81,7 +82,6 @@ namespace util { return (! child_path.has_root_directory() && ! child_path.has_root_name()); } - std::string make_relative(std::string const& filepath, std::string const& base) { #ifdef _WINDOWS @@ -109,6 +109,32 @@ namespace util { return bp.parent_path().string(); } + std::string basename(std::string const& value) + { + boost::filesystem::path bp(value); + return bp.filename().string(); + } + + std::vector list_directory(std::string const& dir) + { + std::vector listing; + boost::filesystem::directory_iterator end_itr; +#ifdef _WINDOWS + std::wstring wide_dir(mapnik::utf8_to_utf16(dir)); + for (boost::filesystem::directory_iterator itr(wide_dir); itr != end_itr; ++itr) + { + listing.emplace_back(mapnik::utf16_to_utf8(itr->path().wstring())); + } +#else + for (boost::filesystem::directory_iterator itr(dir); itr != end_itr; ++itr) + { + listing.emplace_back(itr->path().string()); + } +#endif + return listing; + } + + } // end namespace util } // end namespace mapnik