]> git.lyx.org Git - lyx.git/blob - src/support/path.h
Documentation + file removals as sent to list
[lyx.git] / src / support / path.h
1 // -*- C++ -*-
2 /**
3  * \file path.h
4  * Copyright 1995-2002 the LyX Team
5  * Read the file COPYING
6  *
7  * \author unknown
8  */
9
10 #ifndef PATH_H
11 #define PATH_H
12
13 #include "LString.h"
14 //#include "filetools.h"
15 #include "lyxlib.h"
16 #include <boost/utility.hpp>
17
18 #ifdef __GNUG__
19 #pragma interface
20 #endif
21
22
23 /**
24  * Path - utility closs for stackable working directories
25  *
26  * You can use a local variable of this type to temporarily
27  * change to a directory as the cwd, for example :
28  *
29  * if (blah) {
30  *      Path p("/tmp/blah");
31  *      ...
32  * }
33  *
34  * At the end of p's scope the cwd is reset to its previous value.
35  */
36 class Path : boost::noncopyable {
37 public:
38         /// change to the given directory
39         explicit
40         Path(string const & path)
41                 : popped_(false)
42         {
43                 if (!path.empty()) { 
44                         pushedDir_ = lyx::getcwd();
45                         if (pushedDir_.empty() || lyx::chdir(path))
46                                 /* FIXME: throw */;
47                 } else {
48                         popped_ = true;
49                 }
50         }
51  
52         /// set cwd to the previous value if needed
53         ~Path()
54         {
55                 if (!popped_) pop();
56         }
57  
58         /// set cwd to the previous value if needed
59         int pop();
60 private:
61         /// whether we are in the new cwd or not
62         bool popped_;
63         /// the previous cwd
64         string pushedDir_;
65 };
66
67 // To avoid the wrong usage:
68 // Path("/tmp");   // wrong
69 // Path p("/tmp");  // right
70 // we add this macro:
71 ///
72 #define Path(x) unnamed_Path;
73 // Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
74
75 #endif // PATH_H