1 // filesystem path_traits.hpp --------------------------------------------------------//
3 // Copyright Beman Dawes 2009
5 // Distributed under the Boost Software License, Version 1.0.
6 // See http://www.boost.org/LICENSE_1_0.txt
8 // Library home page: http://www.boost.org/libs/filesystem
10 #ifndef BOOST_FILESYSTEM_PATH_TRAITS_HPP
11 #define BOOST_FILESYSTEM_PATH_TRAITS_HPP
13 #include <boost/filesystem/config.hpp>
14 #include <boost/system/error_category.hpp>
15 #include <boost/type_traits/is_array.hpp>
16 #include <boost/type_traits/decay.hpp>
17 #include <boost/core/enable_if.hpp>
19 #include <cwchar> // for mbstate_t
25 #include <boost/assert.hpp>
27 #include <boost/filesystem/detail/header.hpp> // must be the last #include
30 namespace filesystem {
32 BOOST_FILESYSTEM_DECL system::error_category const& codecvt_error_category() BOOST_NOEXCEPT;
33 // uses std::codecvt_base::result used for error codes:
35 // ok: Conversion successful.
36 // partial: Not all source characters converted; one or more additional source
37 // characters are needed to produce the final target character, or the
38 // size of the target intermediate buffer was too small to hold the result.
39 // error: A character in the source could not be converted to the target encoding.
40 // noconv: The source and target characters have the same type and encoding, so no
41 // conversion was necessary.
43 class directory_entry;
45 namespace path_traits {
47 typedef std::codecvt< wchar_t, char, std::mbstate_t > codecvt_type;
49 // is_pathable type trait; allows disabling over-agressive class path member templates
54 static const bool value = false;
58 struct is_pathable< char* >
60 static const bool value = true;
64 struct is_pathable< const char* >
66 static const bool value = true;
70 struct is_pathable< wchar_t* >
72 static const bool value = true;
76 struct is_pathable< const wchar_t* >
78 static const bool value = true;
82 struct is_pathable< std::string >
84 static const bool value = true;
88 struct is_pathable< std::wstring >
90 static const bool value = true;
94 struct is_pathable< std::vector< char > >
96 static const bool value = true;
100 struct is_pathable< std::vector< wchar_t > >
102 static const bool value = true;
106 struct is_pathable< std::list< char > >
108 static const bool value = true;
112 struct is_pathable< std::list< wchar_t > >
114 static const bool value = true;
118 struct is_pathable< directory_entry >
120 static const bool value = true;
125 template< class Container >
127 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
128 // conforming compilers. Replace by plain "bool" at some future date (2012?)
129 typename boost::disable_if< boost::is_array< Container >, bool >::type
130 empty(Container const& c)
132 return c.begin() == c.end();
136 inline bool empty(T* const& c_str)
142 template< typename T, std::size_t N >
143 inline bool empty(T (&x)[N])
148 // value types differ ---------------------------------------------------------------//
150 // A from_end argument of 0 is less efficient than a known end, so use only if needed
154 BOOST_FILESYSTEM_DECL
155 void convert(const char* from,
156 const char* from_end, // 0 for null terminated MBCS
157 std::wstring& to, codecvt_type const& cvt);
159 BOOST_FILESYSTEM_DECL
160 void convert(const wchar_t* from,
161 const wchar_t* from_end, // 0 for null terminated MBCS
162 std::string& to, codecvt_type const& cvt);
164 inline void convert(const char* from, std::wstring& to, codecvt_type const& cvt)
167 convert(from, 0, to, cvt);
170 inline void convert(const wchar_t* from, std::string& to, codecvt_type const& cvt)
173 convert(from, 0, to, cvt);
178 inline void convert(const char* from,
179 const char* from_end, // 0 for null terminated MBCS
182 inline void convert(const wchar_t* from,
183 const wchar_t* from_end, // 0 for null terminated MBCS
186 inline void convert(const char* from, std::wstring& to);
188 inline void convert(const wchar_t* from, std::string& to);
190 // value types same -----------------------------------------------------------------//
194 inline void convert(const char* from, const char* from_end, std::string& to, codecvt_type const&)
197 BOOST_ASSERT(from_end);
198 to.append(from, from_end);
201 inline void convert(const char* from, std::string& to, codecvt_type const&)
207 // wchar_t with codecvt
209 inline void convert(const wchar_t* from, const wchar_t* from_end, std::wstring& to, codecvt_type const&)
212 BOOST_ASSERT(from_end);
213 to.append(from, from_end);
216 inline void convert(const wchar_t* from, std::wstring& to, codecvt_type const&)
222 // char without codecvt
224 inline void convert(const char* from, const char* from_end, std::string& to)
227 BOOST_ASSERT(from_end);
228 to.append(from, from_end);
231 inline void convert(const char* from, std::string& to)
237 // wchar_t without codecvt
239 inline void convert(const wchar_t* from, const wchar_t* from_end, std::wstring& to)
242 BOOST_ASSERT(from_end);
243 to.append(from, from_end);
246 inline void convert(const wchar_t* from, std::wstring& to)
252 // Source dispatch -----------------------------------------------------------------//
254 // contiguous containers with codecvt
256 inline void dispatch(std::string const& c, U& to, codecvt_type const& cvt)
259 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
262 inline void dispatch(std::wstring const& c, U& to, codecvt_type const& cvt)
265 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
268 inline void dispatch(std::vector< char > const& c, U& to, codecvt_type const& cvt)
271 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
274 inline void dispatch(std::vector< wchar_t > const& c, U& to, codecvt_type const& cvt)
277 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
280 // contiguous containers without codecvt
282 inline void dispatch(std::string const& c, U& to)
285 convert(&*c.begin(), &*c.begin() + c.size(), to);
288 inline void dispatch(std::wstring const& c, U& to)
291 convert(&*c.begin(), &*c.begin() + c.size(), to);
294 inline void dispatch(std::vector< char > const& c, U& to)
297 convert(&*c.begin(), &*c.begin() + c.size(), to);
300 inline void dispatch(std::vector< wchar_t > const& c, U& to)
303 convert(&*c.begin(), &*c.begin() + c.size(), to);
306 // non-contiguous containers with codecvt
307 template< class Container, class U >
309 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
310 // conforming compilers. Replace by plain "void" at some future date (2012?)
311 typename boost::disable_if< boost::is_array< Container >, void >::type
312 dispatch(Container const& c, U& to, codecvt_type const& cvt)
316 std::basic_string< typename Container::value_type > s(c.begin(), c.end());
317 convert(s.c_str(), s.c_str() + s.size(), to, cvt);
322 template< class T, class U >
323 inline void dispatch(T* const& c_str, U& to, codecvt_type const& cvt)
325 // std::cout << "dispatch() const T *\n";
327 convert(c_str, to, cvt);
330 // Note: there is no dispatch on C-style arrays because the array may
331 // contain a string smaller than the array size.
333 BOOST_FILESYSTEM_DECL
334 void dispatch(directory_entry const& de,
335 #ifdef BOOST_WINDOWS_API
340 codecvt_type const&);
342 // non-contiguous containers without codecvt
343 template< class Container, class U >
345 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
346 // conforming compilers. Replace by plain "void" at some future date (2012?)
347 typename boost::disable_if< boost::is_array< Container >, void >::type
348 dispatch(Container const& c, U& to)
352 std::basic_string< typename Container::value_type > seq(c.begin(), c.end());
353 convert(seq.c_str(), seq.c_str() + seq.size(), to);
358 template< class T, class U >
359 inline void dispatch(T* const& c_str, U& to)
361 // std::cout << "dispatch() const T *\n";
366 // Note: there is no dispatch on C-style arrays because the array may
367 // contain a string smaller than the array size.
369 BOOST_FILESYSTEM_DECL
370 void dispatch(directory_entry const& de,
371 #ifdef BOOST_WINDOWS_API
378 } // namespace path_traits
379 } // namespace filesystem
382 #include <boost/filesystem/detail/footer.hpp>
384 #endif // BOOST_FILESYSTEM_PATH_TRAITS_HPP