path_spec.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Copyright 2007-2008 Andreas Pokorny, Christian Henning
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_IO_PATH_SPEC_HPP
  9. #define BOOST_GIL_IO_PATH_SPEC_HPP
  10. #include <boost/gil/io/detail/filesystem.hpp>
  11. #include <cstdlib>
  12. #include <string>
  13. #include <type_traits>
  14. namespace boost { namespace gil { namespace detail {
  15. template<typename P> struct is_supported_path_spec : std::false_type {};
  16. template<> struct is_supported_path_spec< std::string > : std::true_type {};
  17. template<> struct is_supported_path_spec< const std::string > : std::true_type {};
  18. template<> struct is_supported_path_spec< std::wstring > : std::true_type {};
  19. template<> struct is_supported_path_spec< const std::wstring > : std::true_type {};
  20. template<> struct is_supported_path_spec< char const* > : std::true_type {};
  21. template<> struct is_supported_path_spec< char* > : std::true_type {};
  22. template<> struct is_supported_path_spec< const wchar_t* > : std::true_type {};
  23. template<> struct is_supported_path_spec< wchar_t* > : std::true_type {};
  24. template<int i> struct is_supported_path_spec<const char [i]> : std::true_type {};
  25. template<int i> struct is_supported_path_spec<char [i]> : std::true_type {};
  26. template<int i> struct is_supported_path_spec<const wchar_t [i]> : std::true_type {};
  27. template<int i> struct is_supported_path_spec<wchar_t [i]> : std::true_type {};
  28. template<> struct is_supported_path_spec<filesystem::path> : std::true_type {};
  29. template<> struct is_supported_path_spec<filesystem::path const> : std::true_type {};
  30. inline std::string convert_to_string( std::string const& obj)
  31. {
  32. return obj;
  33. }
  34. inline std::string convert_to_string( std::wstring const& s )
  35. {
  36. std::size_t len = wcslen( s.c_str() );
  37. char* c = reinterpret_cast<char*>( alloca( len ));
  38. wcstombs( c, s.c_str(), len );
  39. return std::string( c, c + len );
  40. }
  41. inline std::string convert_to_string( char const* str )
  42. {
  43. return std::string( str );
  44. }
  45. inline std::string convert_to_string( char* str )
  46. {
  47. return std::string( str );
  48. }
  49. inline std::string convert_to_string(filesystem::path const& path)
  50. {
  51. return convert_to_string(path.string());
  52. }
  53. inline char const* convert_to_native_string( char* str )
  54. {
  55. return str;
  56. }
  57. inline char const* convert_to_native_string( char const* str )
  58. {
  59. return str;
  60. }
  61. inline char const* convert_to_native_string( const std::string& str )
  62. {
  63. return str.c_str();
  64. }
  65. inline char const* convert_to_native_string( const wchar_t* str )
  66. {
  67. std::size_t len = wcslen( str ) + 1;
  68. char* c = new char[len];
  69. wcstombs( c, str, len );
  70. return c;
  71. }
  72. inline char const* convert_to_native_string( std::wstring const& str )
  73. {
  74. std::size_t len = wcslen( str.c_str() ) + 1;
  75. char* c = new char[len];
  76. wcstombs( c, str.c_str(), len );
  77. return c;
  78. }
  79. }}} // namespace boost::gil::detail
  80. #endif