mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-14 02:08:27 +08:00
cmWorkingDirectory: add class for changing the workdir
This commit is contained in:
@@ -381,6 +381,8 @@ set(SRCS
|
||||
cmVariableWatch.h
|
||||
cmVersion.cxx
|
||||
cmVersion.h
|
||||
cmWorkingDirectory.cxx
|
||||
cmWorkingDirectory.h
|
||||
cmXMLParser.cxx
|
||||
cmXMLParser.h
|
||||
cmXMLSafe.cxx
|
||||
|
24
Source/cmWorkingDirectory.cxx
Normal file
24
Source/cmWorkingDirectory.cxx
Normal file
@@ -0,0 +1,24 @@
|
||||
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||
#include "cmWorkingDirectory.h"
|
||||
|
||||
#include "cmSystemTools.h"
|
||||
|
||||
cmWorkingDirectory::cmWorkingDirectory(std::string const& newdir)
|
||||
{
|
||||
this->OldDir = cmSystemTools::GetCurrentWorkingDirectory();
|
||||
cmSystemTools::ChangeDirectory(newdir);
|
||||
}
|
||||
|
||||
cmWorkingDirectory::~cmWorkingDirectory()
|
||||
{
|
||||
this->Pop();
|
||||
}
|
||||
|
||||
void cmWorkingDirectory::Pop()
|
||||
{
|
||||
if (!this->OldDir.empty()) {
|
||||
cmSystemTools::ChangeDirectory(this->OldDir);
|
||||
this->OldDir.clear();
|
||||
}
|
||||
}
|
25
Source/cmWorkingDirectory.h
Normal file
25
Source/cmWorkingDirectory.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
||||
file Copyright.txt or https://cmake.org/licensing for details. */
|
||||
#ifndef cmWorkingDirectory_h
|
||||
#define cmWorkingDirectory_h
|
||||
|
||||
#include <cmConfigure.h> // IWYU pragma: keep
|
||||
|
||||
#include <string>
|
||||
|
||||
/** \class cmWorkingDirectory
|
||||
* \brief An RAII class to manipulate the working directory.
|
||||
*/
|
||||
class cmWorkingDirectory
|
||||
{
|
||||
public:
|
||||
cmWorkingDirectory(std::string const& newdir);
|
||||
~cmWorkingDirectory();
|
||||
|
||||
void Pop();
|
||||
|
||||
private:
|
||||
std::string OldDir;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user