1999-10-13 17:32:46 +00:00
|
|
|
|
// -*- C++ -*-
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file path.h
|
2002-09-25 10:03:41 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-05-30 02:24:33 +00:00
|
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS
|
2002-05-30 02:24:33 +00:00
|
|
|
|
*/
|
|
|
|
|
|
1999-10-13 17:32:46 +00:00
|
|
|
|
#ifndef PATH_H
|
|
|
|
|
#define PATH_H
|
|
|
|
|
|
|
|
|
|
#include "LString.h"
|
1999-11-01 04:22:28 +00:00
|
|
|
|
#include "lyxlib.h"
|
2000-10-02 00:55:02 +00:00
|
|
|
|
#include <boost/utility.hpp>
|
1999-10-13 17:32:46 +00:00
|
|
|
|
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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");
|
2002-12-01 22:59:25 +00:00
|
|
|
|
* ...
|
2002-05-30 02:24:33 +00:00
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* At the end of p's scope the cwd is reset to its previous value.
|
|
|
|
|
*/
|
2001-04-17 14:22:53 +00:00
|
|
|
|
class Path : boost::noncopyable {
|
1999-10-13 17:32:46 +00:00
|
|
|
|
public:
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/// change to the given directory
|
2000-04-08 17:02:02 +00:00
|
|
|
|
explicit
|
1999-10-13 17:32:46 +00:00
|
|
|
|
Path(string const & path)
|
|
|
|
|
: popped_(false)
|
|
|
|
|
{
|
2002-09-25 10:03:41 +00:00
|
|
|
|
if (!path.empty()) {
|
2002-05-30 02:24:33 +00:00
|
|
|
|
pushedDir_ = lyx::getcwd();
|
|
|
|
|
if (pushedDir_.empty() || lyx::chdir(path))
|
|
|
|
|
/* FIXME: throw */;
|
1999-10-13 17:32:46 +00:00
|
|
|
|
} else {
|
|
|
|
|
popped_ = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/// set cwd to the previous value if needed
|
1999-10-13 17:32:46 +00:00
|
|
|
|
~Path()
|
|
|
|
|
{
|
|
|
|
|
if (!popped_) pop();
|
|
|
|
|
}
|
2002-09-25 10:03:41 +00:00
|
|
|
|
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/// set cwd to the previous value if needed
|
2000-04-04 00:19:15 +00:00
|
|
|
|
int pop();
|
1999-10-13 17:32:46 +00:00
|
|
|
|
private:
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/// whether we are in the new cwd or not
|
1999-10-13 17:32:46 +00:00
|
|
|
|
bool popped_;
|
2002-05-30 02:24:33 +00:00
|
|
|
|
/// the previous cwd
|
1999-10-13 17:32:46 +00:00
|
|
|
|
string pushedDir_;
|
|
|
|
|
};
|
|
|
|
|
|
2000-01-13 16:28:54 +00:00
|
|
|
|
// To avoid the wrong usage:
|
|
|
|
|
// Path("/tmp"); // wrong
|
|
|
|
|
// Path p("/tmp"); // right
|
|
|
|
|
// we add this macro:
|
2000-08-07 20:58:24 +00:00
|
|
|
|
///
|
2000-01-13 16:28:54 +00:00
|
|
|
|
#define Path(x) unnamed_Path;
|
|
|
|
|
// Tip gotten from Bobby Schmidt's column in C/C++ Users Journal
|
|
|
|
|
|
2002-05-30 02:24:33 +00:00
|
|
|
|
#endif // PATH_H
|