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 
19 namespace boost { namespace gil { namespace detail {
20 
21 template<typename P> struct is_supported_path_spec : std::false_type {};
22 template<> struct is_supported_path_spec< std::string > : std::true_type {};
23 template<> struct is_supported_path_spec< const std::string > : std::true_type {};
24 template<> struct is_supported_path_spec< std::wstring > : std::true_type {};
25 template<> struct is_supported_path_spec< const std::wstring > : std::true_type {};
26 template<> struct is_supported_path_spec< char const* > : std::true_type {};
27 template<> struct is_supported_path_spec< char* > : std::true_type {};
28 template<> struct is_supported_path_spec< const wchar_t* > : std::true_type {};
29 template<> struct is_supported_path_spec< wchar_t* > : std::true_type {};
30 
31 template<int i> struct is_supported_path_spec<const char [i]> : std::true_type {};
32 template<int i> struct is_supported_path_spec<char [i]> : std::true_type {};
33 template<int i> struct is_supported_path_spec<const wchar_t [i]> : std::true_type {};
34 template<int i> struct is_supported_path_spec<wchar_t [i]> : std::true_type {};
35 
36 template<> struct is_supported_path_spec<filesystem::path> : std::true_type {};
37 template<> struct is_supported_path_spec<filesystem::path const> : std::true_type {};
38 
39 inline std::string convert_to_string( std::string const& obj)
40 {
41  return obj;
42 }
43 
44 inline 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 
55 inline std::string convert_to_string( char const* str )
56 {
57  return std::string( str );
58 }
59 
60 inline std::string convert_to_string( char* str )
61 {
62  return std::string( str );
63 }
64 
65 inline std::string convert_to_string(filesystem::path const& path)
66 {
67  return convert_to_string(path.string());
68 }
69 
70 inline char const* convert_to_native_string( char* str )
71 {
72  return str;
73 }
74 
75 inline char const* convert_to_native_string( char const* str )
76 {
77  return str;
78 }
79 
80 inline char const* convert_to_native_string( const std::string& str )
81 {
82  return str.c_str();
83 }
84 
85 inline 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 
95 inline 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