]> git.lyx.org Git - lyx.git/blobdiff - src/support/getcwd.C
the convert patch
[lyx.git] / src / support / getcwd.C
index 305c8107b9bd5ad74ede6259549c92dc9457879b..47d67fa2743a86f1068c8cd9b5ac2f7f185b7171 100644 (file)
@@ -3,17 +3,26 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  *
- * Full author contact details are available in file CREDITS
+ * Full author contact details are available in file CREDITS.
  */
 
 #include <config.h>
 
+#include "support/lyxlib.h"
+
+#include <boost/scoped_array.hpp>
+
 #include <cerrno>
-#include <unistd.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+using boost::scoped_array;
+
+using std::string;
 
-#include "support/lyxlib.h"
 
 namespace {
 
@@ -31,22 +40,21 @@ char * l_getcwd(char * buffer, size_t size)
 
 
 // Returns current working directory
-string const lyx::getcwd()
+string const lyx::support::getcwd()
 {
        int n = 256;    // Assume path is less than 256 chars
        char * err;
-       char * tbuf = new char[n];
+       scoped_array<char> tbuf(new char[n]);
 
        // Safe. Hopefully all getcwds behave this way!
-       while (((err = l_getcwd(tbuf, n)) == 0) && (errno == ERANGE)) {
+       while (((err = l_getcwd(tbuf.get(), n)) == 0) && (errno == ERANGE)) {
                // Buffer too small, double the buffersize and try again
-               delete[] tbuf;
-               n = 2 * n;
-               tbuf = new char[n];
+               n *= 2;
+               tbuf.reset(new char[n]);
        }
 
        string result;
-       if (err) result = tbuf;
-       delete[] tbuf;
+       if (err)
+               result = tbuf.get();
        return result;
 }