2013-06-03 04:28:24 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2013-06-03 04:28:24 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
// mapnik
|
2015-06-02 12:10:41 +02:00
|
|
|
#include <mapnik/util/utf_conv_win.hpp>
|
2013-06-03 04:28:24 +02:00
|
|
|
#include <mapnik/util/fs.hpp>
|
|
|
|
|
2020-11-19 15:30:30 +01:00
|
|
|
#include <mapnik/warning.hpp>
|
2023-03-09 14:17:28 +01:00
|
|
|
|
|
|
|
#if __cplusplus >= 201703L && !defined(USE_BOOST_FILESYSTEM)
|
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#else
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_PUSH
|
2016-03-10 01:42:15 +01:00
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2022-01-26 10:43:31 +01:00
|
|
|
#include <boost/filesystem/operations.hpp> // for absolute, exists, etc
|
|
|
|
#include <boost/filesystem/path.hpp> // for path, operator/
|
2020-11-19 15:30:30 +01:00
|
|
|
MAPNIK_DISABLE_WARNING_POP
|
2023-03-09 14:17:28 +01:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
#endif
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2013-06-03 05:19:33 +02:00
|
|
|
// stl
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
namespace mapnik {
|
|
|
|
|
|
|
|
namespace util {
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool exists(std::string const& filepath)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::exists(mapnik::utf8_to_utf16(filepath));
|
2013-06-03 04:28:24 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::exists(filepath);
|
2013-06-03 04:28:24 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool is_directory(std::string const& filepath)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::is_directory(mapnik::utf8_to_utf16(filepath));
|
2013-06-03 04:28:24 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::is_directory(filepath);
|
2013-06-03 04:28:24 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool is_regular_file(std::string const& filepath)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::is_regular_file(mapnik::utf8_to_utf16(filepath));
|
2014-06-18 21:55:59 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::is_regular_file(filepath);
|
2014-06-18 21:55:59 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
2014-06-18 21:55:59 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool remove(std::string const& filepath)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::remove(mapnik::utf8_to_utf16(filepath));
|
2013-06-03 04:28:24 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::remove(filepath);
|
2013-06-03 04:28:24 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bool is_relative(std::string const& filepath)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path child_path(mapnik::utf8_to_utf16(filepath));
|
2013-06-03 04:28:24 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path child_path(filepath);
|
2013-06-03 04:28:24 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
return (!child_path.has_root_directory() && !child_path.has_root_name());
|
|
|
|
}
|
2013-06-03 04:28:24 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
std::string make_relative(std::string const& filepath, std::string const& base)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path absolute_path(mapnik::utf8_to_utf16(base));
|
2013-06-03 04:28:24 +02:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path absolute_path(base);
|
2013-06-03 04:28:24 +02:00
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
// support symlinks
|
2023-03-09 14:17:28 +01:00
|
|
|
if (fs::is_symlink(absolute_path))
|
2013-06-03 04:28:24 +02:00
|
|
|
{
|
2023-03-09 14:17:28 +01:00
|
|
|
absolute_path = fs::read_symlink(absolute_path);
|
2013-06-03 04:28:24 +02:00
|
|
|
}
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::absolute(absolute_path.parent_path() / filepath).string();
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string make_absolute(std::string const& filepath, std::string const& base)
|
|
|
|
{
|
|
|
|
// TODO - normalize is now deprecated, use make_preferred?
|
2023-03-09 14:17:28 +01:00
|
|
|
return fs::absolute(fs::path(base) / filepath).string();
|
2022-01-26 10:43:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string dirname(std::string const& filepath)
|
|
|
|
{
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path bp(filepath);
|
2022-01-26 10:43:31 +01:00
|
|
|
return bp.parent_path().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string basename(std::string const& value)
|
|
|
|
{
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::path bp(value);
|
2022-01-26 10:43:31 +01:00
|
|
|
return bp.filename().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> list_directory(std::string const& dir)
|
|
|
|
{
|
|
|
|
std::vector<std::string> listing;
|
2023-03-09 14:17:28 +01:00
|
|
|
fs::directory_iterator end_itr;
|
2022-01-26 10:43:31 +01:00
|
|
|
#ifdef _WIN32
|
|
|
|
std::wstring wide_dir(mapnik::utf8_to_utf16(dir));
|
2023-03-09 14:17:28 +01:00
|
|
|
for (fs::directory_iterator itr(wide_dir); itr != end_itr; ++itr)
|
2013-06-03 04:28:24 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
listing.emplace_back(mapnik::utf16_to_utf8(itr->path().wstring()));
|
2013-06-03 04:28:24 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
#else
|
2023-03-09 14:17:28 +01:00
|
|
|
for (fs::directory_iterator itr(dir); itr != end_itr; ++itr)
|
2015-01-06 09:33:33 +01:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
listing.emplace_back(itr->path().string());
|
2015-01-06 09:33:33 +01:00
|
|
|
}
|
|
|
|
#endif
|
2022-01-26 10:43:31 +01:00
|
|
|
return listing;
|
|
|
|
}
|
2015-01-06 09:33:33 +01:00
|
|
|
|
2013-06-03 04:28:24 +02:00
|
|
|
} // end namespace util
|
|
|
|
|
|
|
|
} // end namespace mapnik
|