]> git.lyx.org Git - lyx.git/blobdiff - src/support/path.h
* src/text2.C: deleteEmptyParagraphMechanism(): fix a crash in
[lyx.git] / src / support / path.h
index ad5a905b4008aa0989d14c68835a515fad08de20..13296353aa6a87ce6b919773e6c2804915cb1663 100644 (file)
@@ -1,55 +1,68 @@
 // -*- C++ -*-
+/**
+ * \file path.h
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Lars Gullik Bjønnes
+ *
+ * Full author contact details are available in file CREDITS.
+ */
+
 #ifndef PATH_H
 #define PATH_H
 
-#include <unistd.h>
-#include "LString.h"
-#include "gettext.h"
-#include "support/filetools.h"
-#include "lyx_gui_misc.h"
+#include "support/filename.h"
+
+#include <boost/utility.hpp>
+
+#include <string>
+
+namespace lyx {
+namespace support {
 
-class Path {
+/**
+ * Path - utility closs for stackable working directories
+ *
+ * You can use a local variable of this type to temporarily
+ * change to a directory as the cwd, for example :
+ *
+ * if (blah) {
+ *     Path p("/tmp/blah");
+ *     ...
+ * }
+ *
+ * At the end of p's scope the cwd is reset to its previous value.
+ */
+class Path : boost::noncopyable {
 public:
-       ///
-       Path(string const & path)
-               : popped_(false)
-       {
-               if (!path.empty()) { 
-                       pushedDir_ = GetCWD();
-                       if (pushedDir_.empty() || chdir(path.c_str())) {
-                               WriteFSAlert(_("Error: Could not change to directory: "), 
-                                            path);
-                       }
-               } else {
-                       popped_ = true;
-               }
-       }
-       ///
-       ~Path()
-       {
-               if (!popped_) pop();
-       }
-       ///
-       int pop()
-       {
-               if (popped_) {
-                       WriteFSAlert(_("Error: Dir already popped: "),
-                                    pushedDir_);
-                       return 0;
-               }
-               if (chdir(pushedDir_.c_str())) {
-                       WriteFSAlert(
-                               _("Error: Could not change to directory: "), 
-                               pushedDir_);
-               }
-               popped_ = true;
-               return 0;
-       }
+       /// change to the given directory
+       explicit Path(std::string const & path);
+
+       /// set cwd to the previous value if needed
+       ~Path();
+
+       /// set cwd to the previous value if needed
+       int pop();
 private:
-       ///
+       /// whether we are in the new cwd or not
        bool popped_;
-       ///
-       string pushedDir_;
+       /// the previous cwd
+       FileName pushedDir_;
 };
 
-#endif
+// To avoid the wrong usage:
+// Path("/tmp");   // wrong
+// Path p("/tmp");  // right
+// we add this macro:
+///
+// With boost 1.34 this is not usable anymore
+//#ifndef PATH_C
+//#define Path(x) unnamed_Path;
+//#endif
+// Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
+
+} // namespace support
+} // namespace lyx
+
+#endif // PATH_H