Skip to content

Commit

Permalink
Change from boost::filesystem to std::filesystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
evoskuil committed Jun 11, 2022
1 parent e16845c commit a4f9b4e
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 69 deletions.
3 changes: 1 addition & 2 deletions include/bitcoin/system/boost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
// Include boost in cpp files only from here, so exception disable works.
// Avoid use in header includes due to warning repetition (boost/format.hpp).
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
////#include <boost/format.hpp>
#include <boost/json.hpp>
#include <boost/locale.hpp>
#include <boost/multiprecision/cpp_int.hpp>
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/config/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef LIBBITCOIN_SYSTEM_CONFIG_PARSER_HPP
#define LIBBITCOIN_SYSTEM_CONFIG_PARSER_HPP

#include <filesystem>
#include <string>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <bitcoin/system/define.hpp>

Expand Down Expand Up @@ -49,7 +49,7 @@ class BC_API parser
const std::string& message) noexcept;
static bool get_option(variables_map& variables,
const std::string& name) noexcept;
static boost::filesystem::path get_config_option(variables_map& variables,
static std::filesystem::path get_config_option(variables_map& variables,
const std::string& name) noexcept;

virtual ~parser() noexcept;
Expand Down
9 changes: 7 additions & 2 deletions include/bitcoin/system/config/printer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ class BC_API printer
const std::string& application, const std::string& description="",
const std::string& command="") noexcept;

/// Destructor.
virtual ~printer() noexcept;

/// Defaults.
printer(printer&&) = default;
printer(const printer&) = default;
printer& operator=(printer&&) = default;
printer& operator=(const printer&) = default;
virtual ~printer() = default;

/// Convert a paragraph of text into a column.
/// This formats to 80 char width as: [ 23 | ' ' | 55 | '\n' ].
Expand Down
10 changes: 5 additions & 5 deletions include/bitcoin/system/unicode/utf8_everywhere/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
#define LIBBITCOIN_SYSTEM_UNICODE_UTF8_EVERYWHERE_ENVIRONMENT_HPP

#include <cstddef>
#include <filesystem>
#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
Expand Down Expand Up @@ -58,8 +58,8 @@ BC_API void set_binary_stdout() noexcept(false);
// ----------------------------------------------------------------------------

/// Get the default configuration file path with subdirectory.
BC_API boost::filesystem::path default_config_path(
const boost::filesystem::path& subdirectory) noexcept;
BC_API std::filesystem::path default_config_path(
const std::filesystem::path& subdirectory) noexcept;

// BC_USE_LIBBITCOIN_MAIN dependencies.
// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -91,10 +91,10 @@ BC_API int call_utf8_main(int argc, wchar_t* argv[],
#ifdef _MSC_VER
// Not thread safe.
BC_API std::wstring to_extended_path(
const boost::filesystem::path& path) noexcept;
const std::filesystem::path& path) noexcept;
#else
BC_API std::string to_extended_path(
const boost::filesystem::path& path) noexcept;
const std::filesystem::path& path) noexcept;
#endif

} // namespace system
Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/unicode/utf8_everywhere/ifstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef LIBBITCOIN_SYSTEM_UNICODE_UTF8_EVERYWHERE_IFSTREAM_HPP
#define LIBBITCOIN_SYSTEM_UNICODE_UTF8_EVERYWHERE_IFSTREAM_HPP

#include <filesystem>
#include <fstream>
#include <boost/filesystem.hpp>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
Expand All @@ -33,7 +33,7 @@ class BC_API ifstream
{
public:
/// This also opens the file.
ifstream(const boost::filesystem::path& path,
ifstream(const std::filesystem::path& path,
std::ifstream::openmode mode=std::ifstream::in) noexcept(false);
};

Expand Down
4 changes: 2 additions & 2 deletions include/bitcoin/system/unicode/utf8_everywhere/ofstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef LIBBITCOIN_SYSTEM_UNICODE_UTF8_EVERYWHERE_OFSTREAM_HPP
#define LIBBITCOIN_SYSTEM_UNICODE_UTF8_EVERYWHERE_OFSTREAM_HPP

#include <filesystem>
#include <fstream>
#include <boost/filesystem.hpp>
#include <bitcoin/system/define.hpp>

namespace libbitcoin {
Expand All @@ -33,7 +33,7 @@ class BC_API ofstream
{
public:
/// This also opens the file.
ofstream(const boost::filesystem::path& path,
ofstream(const std::filesystem::path& path,
std::ofstream::openmode mode=std::ofstream::out) noexcept(false);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
// const auto utf16 = L"acción.кошка.日本国";
// std::wcout << utf16;

// Regarding Boost:
// Regarding std lib (or boost) paths:
//
// When working with boost and utf8 narrow characters on Win32 the thread must
// be configured for utf8. Also when working with boost::filesystem::path the
// static path object must be imbued with the utf8 locale or paths will be
// incorrectly translated. These changes are automatically configured by
// be configured for utf8 (std::locale::global). This is configured by
// BC_USE_LIBBITCOIN_MAIN.
// When working with boost::filesystem::path the static path object must be
// imbued with the utf8 locale or paths will be incorrectly translated.
// When using std::filesystem::path this is handled by std::locale::global.
// en.cppreference.com/w/cpp/filesystem/path/string

// Regarding console applications:
//
Expand Down
3 changes: 2 additions & 1 deletion src/config/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <bitcoin/system/config/parser.hpp>

#include <filesystem>
#include <string>
#include <sstream>
#include <bitcoin/system/boost.hpp>
Expand All @@ -28,7 +29,7 @@ namespace libbitcoin {
namespace system {
namespace config {

using namespace boost::filesystem;
using namespace std::filesystem;
using namespace boost::program_options;
using namespace boost::system;

Expand Down
16 changes: 8 additions & 8 deletions src/config/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
#include <bitcoin/system/config/printer.hpp>

////#include <format>
#include <string>
#include <vector>
#include <boost/format.hpp>
#include <bitcoin/system/boost.hpp>
#include <bitcoin/system/config/parameter.hpp>
#include <bitcoin/system/data/data.hpp>
Expand Down Expand Up @@ -83,13 +85,10 @@ printer::printer(const po::options_description& settings,
{
}

printer::~printer() noexcept
{
}

/* Formatters */
// Formatters
// ----------------------------------------------------------------------------

static void enqueue_fragment(std::string& fragment,
static void enqueue_fragment(const std::string& fragment,
std::vector<std::string>& column) noexcept
{
if (!fragment.empty())
Expand Down Expand Up @@ -386,7 +385,7 @@ std::string printer::format_usage_parameters() noexcept
return trim_copy(usage.str());
}

/* Initialization */
// Initialization

static void enqueue_name(int count, std::string& name,
argument_list& names) noexcept
Expand Down Expand Up @@ -477,7 +476,8 @@ void printer::initialize() noexcept
generate_parameters();
}

/* Printers */
// Printers
// ----------------------------------------------------------------------------

void printer::commandline(std::ostream& output) noexcept
{
Expand Down
23 changes: 9 additions & 14 deletions src/unicode/utf8_everywhere/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <filesystem>
#include <iostream>
#include <locale>
#include <mutex>
Expand Down Expand Up @@ -268,8 +269,8 @@ static std::string windows_config_directory() noexcept
}
#endif

boost::filesystem::path default_config_path(
const boost::filesystem::path& subdirectory) noexcept
std::filesystem::path default_config_path(
const std::filesystem::path& subdirectory) noexcept
{
static const std::string directory =
#ifdef _MSC_VER
Expand All @@ -278,7 +279,7 @@ boost::filesystem::path default_config_path(
SYSCONFDIR;
#endif

return boost::filesystem::path{ directory } / subdirectory;
return std::filesystem::path{ directory } / subdirectory;
}

// BC_USE_LIBBITCOIN_MAIN
Expand Down Expand Up @@ -353,15 +354,10 @@ char** allocate_environment(wchar_t* environment[]) noexcept
int call_utf8_main(int argc, wchar_t* argv[],
int(*main)(int argc, char* argv[])) noexcept
{
// C++17: use std::filesystem.
// When working with boost and utf8 narrow characters on Win32 the thread
// must be configured for utf8. When working with boost::filesystem::path
// the static path object must be imbued with the utf8 locale or paths will
// be incorrectly translated.
// TODO: verify std::filesystem::path is inbued as it was with boost.
constexpr auto utf8_locale_name = "en_US.UTF8";
boost::locale::generator locale;
std::locale::global(locale(utf8_locale_name));
boost::filesystem::path::imbue(std::locale());

auto backup = environ;
auto environment = allocate_environment(_wenviron);
Expand All @@ -388,10 +384,9 @@ int call_utf8_main(int argc, wchar_t* argv[],
return result;
}

// C++17: use std::filesystem.
// docs.microsoft.com/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew
std::wstring to_fully_qualified_path(
const boost::filesystem::path& path) noexcept
const std::filesystem::path& path) noexcept
{
const auto replace_all = [](std::string text, char from, char to) noexcept
{
Expand Down Expand Up @@ -431,15 +426,15 @@ std::wstring to_fully_qualified_path(
#endif // _MSC_VER

#ifdef _MSC_VER
// C++17: use std::filesystem.
// TODO: update comments for std::filesystem conversion.
// Use to_extended_path with APIs that compile to wide with _MSC_VER defined
// and to UTF8 with _MSC_VER undefined. This includes some boost APIs - such as
// filesystem::remove, remove_all, and create_directories, as well as some
// Win32 API extensions to std libs - such as std::ofstream and std::ifstream.
// Otherwise use in any Win32 (W) APIs with _MSC_VER defined, such as we do in
// interprocess_lock::open_file -> CreateFileW, since the boost wrapper only
// calls CreateFileA. The length extension prefix requires Win32 (W) APIs.
std::wstring to_extended_path(const boost::filesystem::path& path) noexcept
std::wstring to_extended_path(const std::filesystem::path& path) noexcept
{
// The length extension prefix works only with a fully-qualified path.
// However this includes "considered relative" paths (with ".." segments).
Expand All @@ -448,7 +443,7 @@ std::wstring to_extended_path(const boost::filesystem::path& path) noexcept
return (full.length() > MAX_PATH) ? L"\\\\?\\" + full : full;
}
#else
std::string to_extended_path(const boost::filesystem::path& path) noexcept
std::string to_extended_path(const std::filesystem::path& path) noexcept
{
return path.string();
}
Expand Down
6 changes: 3 additions & 3 deletions src/unicode/utf8_everywhere/ifstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
#include <bitcoin/system/unicode/utf8_everywhere/ifstream.hpp>

#include <filesystem>
#include <fstream>
#include <bitcoin/system/boost.hpp>
#include <bitcoin/system/unicode/utf8_everywhere/environment.hpp>

namespace libbitcoin {
namespace system {

// C++17: use std::filesystem.

// VC++ EXTENSION: "construct with wide-named file".
ifstream::ifstream(const boost::filesystem::path& path,
ifstream::ifstream(const std::filesystem::path& path,
std::ifstream::openmode mode) noexcept(false)
: std::ifstream(to_extended_path(path), mode)
{
Expand Down
4 changes: 2 additions & 2 deletions src/unicode/utf8_everywhere/ofstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
*/
#include <bitcoin/system/unicode/utf8_everywhere/ofstream.hpp>

#include <filesystem>
#include <fstream>
#include <bitcoin/system/boost.hpp>
#include <bitcoin/system/unicode/utf8_everywhere/environment.hpp>

namespace libbitcoin {
namespace system {

// C++17: use std::filesystem.
// VC++ EXTENSION: "construct with wide-named file".
ofstream::ofstream(const boost::filesystem::path& path,
ofstream::ofstream(const std::filesystem::path& path,
std::ofstream::openmode mode) noexcept(false)
: std::ofstream(to_extended_path(path), mode)
{
Expand Down
3 changes: 2 additions & 1 deletion test/config/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "../test.hpp"
#include <filesystem>
#include <iostream>
#include <utility>
#include <vector>
Expand All @@ -42,7 +43,7 @@ enum opt

static void load_test_options(po::options_description& options)
{
using namespace boost::filesystem;
using namespace std::filesystem;
using namespace boost::program_options;
options.add_options()
("short_long,s", "Long and short name.")
Expand Down
2 changes: 1 addition & 1 deletion test/config/printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ BOOST_AUTO_TEST_CASE(printer__format_usage_parameters__unsorted_two_options_one_

BOOST_AUTO_TEST_CASE(printer__format_usage_parameters__unsorted_multiple_parameters__sorted_parameters)
{
using namespace boost::filesystem;
using namespace std::filesystem;
using namespace boost::program_options;
CONFIG_PRINTER_SETUP_ARGUMENTS(options.add_options()
("short_long,s", "Long and short name.")
Expand Down
Loading

0 comments on commit a4f9b4e

Please sign in to comment.