current_directory.hpp 2.3 KB

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