1 /*
2  * Distributed under the Boost Software License, Version 1.0.(See accompanying
3  * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
4  *
5  * See http://www.boost.org/libs/iostreams for documentation.
6 
7  * File:        boost/iostreams/detail/execute.hpp
8  * Date:        Thu Dec 06 13:21:54 MST 2007
9  * Copyright:   2007-2008 CodeRage, LLC
10  * Author:      Jonathan Turkanis
11  * Contact:     turkanis at coderage dot com
12  *
13  * Defines the function boost::iostreams::detail::current_directory, used by
14  * boost::iostreams::detail::absolute_path.
15  */
16 
17 #ifndef BOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED
18 #define BOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED
19 
20 #include <boost/config.hpp>  // make sure size_t is in std.
21 #include <cstddef>           // size_t
22 #include <string>
23 #include <boost/iostreams/detail/buffer.hpp>
24 #include <boost/iostreams/detail/config/windows_posix.hpp>
25 #include <boost/iostreams/detail/system_failure.hpp>
26 #ifdef BOOST_IOSTREAMS_WINDOWS
27 # define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
28 # include <windows.h>
29 #else
30 # include <unistd.h>        // sysconf.
31 #endif
32 
33 // Must come last.
34 #include <boost/iostreams/detail/config/disable_warnings.hpp>
35 
36 namespace boost { namespace iostreams { namespace detail {
37 
38 // Returns the current working directory
current_directory()39 inline std::string current_directory()
40 {
41 #ifdef BOOST_IOSTREAMS_WINDOWS
42     DWORD               length;
43     basic_buffer<char>  buf(MAX_PATH);
44     while (true) {
45         length = ::GetCurrentDirectoryA(buf.size(), buf.data());
46         if (!length)
47             throw_system_failure("failed determining current directory");
48         if (length < static_cast<DWORD>(buf.size()))
49             break;
50         buf.resize(buf.size() * 2);
51     }
52     return std::string(buf.data(), length);
53 #else // #ifdef BOOST_IOSTREAMS_WINDOWS
54     basic_buffer<char> buf(pathconf(".", _PC_PATH_MAX));
55     if (!getcwd(buf.data(), static_cast<size_t>(buf.size())))
56         throw_system_failure("failed determining current directory");
57     return std::string(buf.data());
58 #endif // #ifdef BOOST_IOSTREAMS_WINDOWS
59 }
60 
61 } } } // End namespaces detail, iostreams, boost.
62 
63 #include <boost/iostreams/detail/config/enable_warnings.hpp>
64 
65 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED
66