blob: fda69501b8717ef6c1f282c644817804c05ef078 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001/*
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: ndnboost/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 ndnboost::iostreams::detail::current_directory, used by
14 * ndnboost::iostreams::detail::absolute_path.
15 */
16
17#ifndef NDNBOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED
18#define NDNBOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED
19
20#include <ndnboost/config.hpp> // make sure size_t is in std.
21#include <cstddef> // size_t
22#include <string>
23#include <ndnboost/iostreams/detail/buffer.hpp>
24#include <ndnboost/iostreams/detail/config/windows_posix.hpp>
25#include <ndnboost/iostreams/detail/system_failure.hpp>
26#ifdef NDNBOOST_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 <ndnboost/iostreams/detail/config/disable_warnings.hpp>
35
36namespace ndnboost { namespace iostreams { namespace detail {
37
38// Returns the current working directory
39inline std::string current_directory()
40{
41#ifdef NDNBOOST_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 NDNBOOST_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 NDNBOOST_IOSTREAMS_WINDOWS
59}
60
61} } } // End namespaces detail, iostreams, boost.
62
63#include <ndnboost/iostreams/detail/config/enable_warnings.hpp>
64
65#endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_CURRENT_DIRECTORY_HPP_INCLUDED