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/config.hpp>
15 # if defined( BOOST_NO_STD_WSTRING )
16 # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
19 #include <boost/filesystem/config.hpp>
20 #include <boost/utility/enable_if.hpp>
21 #include <boost/type_traits/is_array.hpp>
22 #include <boost/type_traits/decay.hpp>
23 #include <boost/system/error_code.hpp>
24 #include <cwchar> // for mbstate_t
30 #include <boost/assert.hpp>
31 // #include <iostream> //**** comment me out ****
33 #include <boost/config/abi_prefix.hpp> // must be the last #include
35 namespace boost { namespace filesystem {
37 BOOST_FILESYSTEM_DECL const system::error_category& codecvt_error_category();
38 // uses std::codecvt_base::result used for error codes:
40 // ok: Conversion successful.
41 // partial: Not all source characters converted; one or more additional source
42 // characters are needed to produce the final target character, or the
43 // size of the target intermediate buffer was too small to hold the result.
44 // error: A character in the source could not be converted to the target encoding.
45 // noconv: The source and target characters have the same type and encoding, so no
46 // conversion was necessary.
48 class directory_entry;
50 namespace path_traits {
52 typedef std::codecvt<wchar_t, char, std::mbstate_t> codecvt_type;
54 // is_pathable type trait; allows disabling over-agressive class path member templates
57 struct is_pathable { static const bool value = false; };
59 template<> struct is_pathable<char*> { static const bool value = true; };
60 template<> struct is_pathable<const char*> { static const bool value = true; };
61 template<> struct is_pathable<wchar_t*> { static const bool value = true; };
62 template<> struct is_pathable<const wchar_t*> { static const bool value = true; };
63 template<> struct is_pathable<std::string> { static const bool value = true; };
64 template<> struct is_pathable<std::wstring> { static const bool value = true; };
65 template<> struct is_pathable<std::vector<char> > { static const bool value = true; };
66 template<> struct is_pathable<std::vector<wchar_t> > { static const bool value = true; };
67 template<> struct is_pathable<std::list<char> > { static const bool value = true; };
68 template<> struct is_pathable<std::list<wchar_t> > { static const bool value = true; };
69 template<> struct is_pathable<directory_entry> { static const bool value = true; };
73 template <class Container> inline
74 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
75 // conforming compilers. Replace by plain "bool" at some future date (2012?)
76 typename boost::disable_if<boost::is_array<Container>, bool>::type
77 empty(const Container & c)
78 { return c.begin() == c.end(); }
80 template <class T> inline
81 bool empty(T * const & c_str)
87 template <typename T, size_t N> inline
91 // value types differ ---------------------------------------------------------------//
93 // A from_end argument of 0 is less efficient than a known end, so use only if needed
98 void convert(const char* from,
99 const char* from_end, // 0 for null terminated MBCS
101 const codecvt_type& cvt);
103 BOOST_FILESYSTEM_DECL
104 void convert(const wchar_t* from,
105 const wchar_t* from_end, // 0 for null terminated MBCS
107 const codecvt_type& cvt);
110 void convert(const char* from,
112 const codecvt_type& cvt)
115 convert(from, 0, to, cvt);
119 void convert(const wchar_t* from,
121 const codecvt_type& cvt)
124 convert(from, 0, to, cvt);
130 void convert(const char* from,
131 const char* from_end, // 0 for null terminated MBCS
135 void convert(const wchar_t* from,
136 const wchar_t* from_end, // 0 for null terminated MBCS
140 void convert(const char* from,
144 void convert(const wchar_t* from,
147 // value types same -----------------------------------------------------------------//
152 void convert(const char* from, const char* from_end, std::string & to,
156 BOOST_ASSERT(from_end);
157 to.append(from, from_end);
161 void convert(const char* from,
169 // wchar_t with codecvt
172 void convert(const wchar_t* from, const wchar_t* from_end, std::wstring & to,
176 BOOST_ASSERT(from_end);
177 to.append(from, from_end);
181 void convert(const wchar_t* from,
189 // char without codecvt
192 void convert(const char* from, const char* from_end, std::string & to)
195 BOOST_ASSERT(from_end);
196 to.append(from, from_end);
200 void convert(const char* from, std::string & to)
206 // wchar_t without codecvt
209 void convert(const wchar_t* from, const wchar_t* from_end, std::wstring & to)
212 BOOST_ASSERT(from_end);
213 to.append(from, from_end);
217 void convert(const wchar_t* from, std::wstring & to)
223 // Source dispatch -----------------------------------------------------------------//
225 // contiguous containers with codecvt
226 template <class U> inline
227 void dispatch(const std::string& c, U& to, const codecvt_type& cvt)
230 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
232 template <class U> inline
233 void dispatch(const std::wstring& c, U& to, const codecvt_type& cvt)
236 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
238 template <class U> inline
239 void dispatch(const std::vector<char>& c, U& to, const codecvt_type& cvt)
242 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
244 template <class U> inline
245 void dispatch(const std::vector<wchar_t>& c, U& to, const codecvt_type& cvt)
248 convert(&*c.begin(), &*c.begin() + c.size(), to, cvt);
251 // contiguous containers without codecvt
252 template <class U> inline
253 void dispatch(const std::string& c, U& to)
256 convert(&*c.begin(), &*c.begin() + c.size(), to);
258 template <class U> inline
259 void dispatch(const std::wstring& c, U& to)
262 convert(&*c.begin(), &*c.begin() + c.size(), to);
264 template <class U> inline
265 void dispatch(const std::vector<char>& c, U& to)
268 convert(&*c.begin(), &*c.begin() + c.size(), to);
270 template <class U> inline
271 void dispatch(const std::vector<wchar_t>& c, U& to)
274 convert(&*c.begin(), &*c.begin() + c.size(), to);
277 // non-contiguous containers with codecvt
278 template <class Container, class U> inline
279 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
280 // conforming compilers. Replace by plain "void" at some future date (2012?)
281 typename boost::disable_if<boost::is_array<Container>, void>::type
282 dispatch(const Container & c, U& to, const codecvt_type& cvt)
286 std::basic_string<typename Container::value_type> s(c.begin(), c.end());
287 convert(s.c_str(), s.c_str()+s.size(), to, cvt);
292 template <class T, class U> inline
293 void dispatch(T * const & c_str, U& to, const codecvt_type& cvt)
295 // std::cout << "dispatch() const T *\n";
297 convert(c_str, to, cvt);
300 // Note: there is no dispatch on C-style arrays because the array may
301 // contain a string smaller than the array size.
303 BOOST_FILESYSTEM_DECL
304 void dispatch(const directory_entry & de,
305 # ifdef BOOST_WINDOWS_API
310 const codecvt_type&);
312 // non-contiguous containers without codecvt
313 template <class Container, class U> inline
314 // disable_if aids broken compilers (IBM, old GCC, etc.) and is harmless for
315 // conforming compilers. Replace by plain "void" at some future date (2012?)
316 typename boost::disable_if<boost::is_array<Container>, void>::type
317 dispatch(const Container & c, U& to)
321 std::basic_string<typename Container::value_type> seq(c.begin(), c.end());
322 convert(seq.c_str(), seq.c_str()+seq.size(), to);
327 template <class T, class U> inline
328 void dispatch(T * const & c_str, U& to)
330 // std::cout << "dispatch() const T *\n";
335 // Note: there is no dispatch on C-style arrays because the array may
336 // contain a string smaller than the array size.
338 BOOST_FILESYSTEM_DECL
339 void dispatch(const directory_entry & de,
340 # ifdef BOOST_WINDOWS_API
348 }}} // namespace boost::filesystem::path_traits
350 #include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
352 #endif // BOOST_FILESYSTEM_PATH_TRAITS_HPP