]> git.lyx.org Git - lyx.git/blob - src/support/getcwd.cpp
cosmetics. mainly replace #include "dostring.h" by #include "strfwd.h"
[lyx.git] / src / support / getcwd.cpp
1 /**
2  * \file getcwd.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/lyxlib.h"
14 #include "support/docstring.h"
15 #include "support/os.h"
16
17 #include <boost/scoped_array.hpp>
18
19 #include <cerrno>
20
21 #ifdef HAVE_UNISTD_H
22 # include <unistd.h>
23 #endif
24
25 #ifdef _WIN32
26 # include <windows.h>
27 #endif
28
29 using boost::scoped_array;
30
31 using std::string;
32
33
34 namespace lyx {
35 namespace support {
36
37 namespace {
38
39 inline
40 char * l_getcwd(char * buffer, size_t size)
41 {
42 #ifdef _WIN32
43         GetCurrentDirectory(size, buffer);
44         return buffer;
45 #else
46         return ::getcwd(buffer, size);
47 #endif
48 }
49
50 } // namespace anon
51
52
53 // Returns current working directory
54 FileName const getcwd()
55 {
56         int n = 256;    // Assume path is less than 256 chars
57         char * err;
58         scoped_array<char> tbuf(new char[n]);
59
60         // Safe. Hopefully all getcwds behave this way!
61         while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
62                 // Buffer too small, double the buffersize and try again
63                 n *= 2;
64                 tbuf.reset(new char[n]);
65         }
66
67         string result;
68         if (err)
69                 result = tbuf.get();
70         return FileName(os::internal_path(to_utf8(from_filesystem8bit(result))));
71 }
72
73 } // namespace support
74 } // namespace lyx