Boost GIL


path_spec.hpp
1//
2// Copyright 2007-2008 Andreas Pokorny, Christian Henning
3// Copyright 2024 Dirk Stolle
4//
5// Distributed under the Boost Software License, Version 1.0
6// See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt
8//
9#ifndef BOOST_GIL_IO_PATH_SPEC_HPP
10#define BOOST_GIL_IO_PATH_SPEC_HPP
11
12#include <boost/gil/io/detail/filesystem.hpp>
13
14#include <cstdlib>
15#include <cwchar>
16#include <string>
17#include <type_traits>
18
19namespace boost { namespace gil { namespace detail {
20
21template<typename P> struct is_supported_path_spec : std::false_type {};
22template<> struct is_supported_path_spec< std::string > : std::true_type {};
23template<> struct is_supported_path_spec< const std::string > : std::true_type {};
24template<> struct is_supported_path_spec< std::wstring > : std::true_type {};
25template<> struct is_supported_path_spec< const std::wstring > : std::true_type {};
26template<> struct is_supported_path_spec< char const* > : std::true_type {};
27template<> struct is_supported_path_spec< char* > : std::true_type {};
28template<> struct is_supported_path_spec< const wchar_t* > : std::true_type {};
29template<> struct is_supported_path_spec< wchar_t* > : std::true_type {};
30
31template<int i> struct is_supported_path_spec<const char [i]> : std::true_type {};
32template<int i> struct is_supported_path_spec<char [i]> : std::true_type {};
33template<int i> struct is_supported_path_spec<const wchar_t [i]> : std::true_type {};
34template<int i> struct is_supported_path_spec<wchar_t [i]> : std::true_type {};
35
36template<> struct is_supported_path_spec<filesystem::path> : std::true_type {};
37template<> struct is_supported_path_spec<filesystem::path const> : std::true_type {};
38
39inline std::string convert_to_string( std::string const& obj)
40{
41 return obj;
42}
43
44inline std::string convert_to_string( std::wstring const& s )
45{
46 std::mbstate_t state = std::mbstate_t();
47 const wchar_t* str = s.c_str();
48 const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state);
49 std::string result(len, '\0');
50 std::wcstombs( &result[0], s.c_str(), len );
51
52 return result;
53}
54
55inline std::string convert_to_string( char const* str )
56{
57 return std::string( str );
58}
59
60inline std::string convert_to_string( char* str )
61{
62 return std::string( str );
63}
64
65inline std::string convert_to_string(filesystem::path const& path)
66{
67 return convert_to_string(path.string());
68}
69
70inline char const* convert_to_native_string( char* str )
71{
72 return str;
73}
74
75inline char const* convert_to_native_string( char const* str )
76{
77 return str;
78}
79
80inline char const* convert_to_native_string( const std::string& str )
81{
82 return str.c_str();
83}
84
85inline char const* convert_to_native_string( const wchar_t* str )
86{
87 std::mbstate_t state = std::mbstate_t();
88 const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state) + 1;
89 char* c = new char[len];
90 std::wcstombs( c, str, len );
91
92 return c;
93}
94
95inline char const* convert_to_native_string( std::wstring const& str )
96{
97 std::mbstate_t state = std::mbstate_t();
98 const wchar_t* wstr = str.c_str();
99 const std::size_t len = std::wcsrtombs(nullptr, &wstr, 0, &state) + 1;
100 char* c = new char[len];
101 std::wcstombs( c, str.c_str(), len );
102
103 return c;
104}
105
106}}} // namespace boost::gil::detail
107
108#endif
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36