path_spec.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #include <boost/gil/io/detail/filesystem.hpp>
  12. #include <cstdlib>
  13. #include <cwchar>
  14. #include <string>
  15. #include <type_traits>
  16. namespace boost { namespace gil { namespace detail {
  17. template<typename P> struct is_supported_path_spec : std::false_type {};
  18. template<> struct is_supported_path_spec< std::string > : std::true_type {};
  19. template<> struct is_supported_path_spec< const std::string > : std::true_type {};
  20. template<> struct is_supported_path_spec< std::wstring > : std::true_type {};
  21. template<> struct is_supported_path_spec< const std::wstring > : std::true_type {};
  22. template<> struct is_supported_path_spec< char const* > : std::true_type {};
  23. template<> struct is_supported_path_spec< char* > : std::true_type {};
  24. template<> struct is_supported_path_spec< const wchar_t* > : std::true_type {};
  25. template<> struct is_supported_path_spec< wchar_t* > : std::true_type {};
  26. template<int i> struct is_supported_path_spec<const char [i]> : std::true_type {};
  27. template<int i> struct is_supported_path_spec<char [i]> : std::true_type {};
  28. template<int i> struct is_supported_path_spec<const wchar_t [i]> : std::true_type {};
  29. template<int i> struct is_supported_path_spec<wchar_t [i]> : std::true_type {};
  30. template<> struct is_supported_path_spec<filesystem::path> : std::true_type {};
  31. template<> struct is_supported_path_spec<filesystem::path const> : std::true_type {};
  32. inline std::string convert_to_string( std::string const& obj)
  33. {
  34. return obj;
  35. }
  36. inline std::string convert_to_string( std::wstring const& s )
  37. {
  38. std::mbstate_t state = std::mbstate_t();
  39. const wchar_t* str = s.c_str();
  40. const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state);
  41. std::string result(len, '\0');
  42. std::wcstombs( &result[0], s.c_str(), len );
  43. return result;
  44. }
  45. inline std::string convert_to_string( char const* str )
  46. {
  47. return std::string( str );
  48. }
  49. inline std::string convert_to_string( char* str )
  50. {
  51. return std::string( str );
  52. }
  53. inline std::string convert_to_string(filesystem::path const& path)
  54. {
  55. return convert_to_string(path.string());
  56. }
  57. inline char const* convert_to_native_string( char* str )
  58. {
  59. return str;
  60. }
  61. inline char const* convert_to_native_string( char const* str )
  62. {
  63. return str;
  64. }
  65. inline char const* convert_to_native_string( const std::string& str )
  66. {
  67. return str.c_str();
  68. }
  69. inline char const* convert_to_native_string( const wchar_t* str )
  70. {
  71. std::mbstate_t state = std::mbstate_t();
  72. const std::size_t len = std::wcsrtombs(nullptr, &str, 0, &state) + 1;
  73. char* c = new char[len];
  74. std::wcstombs( c, str, len );
  75. return c;
  76. }
  77. inline char const* convert_to_native_string( std::wstring const& str )
  78. {
  79. std::mbstate_t state = std::mbstate_t();
  80. const wchar_t* wstr = str.c_str();
  81. const std::size_t len = std::wcsrtombs(nullptr, &wstr, 0, &state) + 1;
  82. char* c = new char[len];
  83. std::wcstombs( c, str.c_str(), len );
  84. return c;
  85. }
  86. }}} // namespace boost::gil::detail
  87. #endif