Преглед на файлове

Add ctkScopedCurrentDir

Use this class to change the current application directory in a
given scope and automatically restore it.
Jean-Christophe Fillion-Robin преди 13 години
родител
ревизия
1adad9fd50

+ 2 - 0
Libs/Core/CMakeLists.txt

@@ -55,6 +55,8 @@ SET(KIT_SRCS
   ctkModelTester.cpp
   ctkModelTester.h
   ctkPimpl.h
+  ctkScopedCurrentDir.cpp
+  ctkScopedCurrentDir.h
   ctkSingleton.h
   ctkTransferFunction.cpp
   ctkTransferFunction.h

+ 2 - 0
Libs/Core/Testing/Cpp/CMakeLists.txt

@@ -45,6 +45,7 @@ SET(KITTests_SRCS
   ctkDependencyGraphTest1.cpp
   ctkDependencyGraphTest2.cpp
   ctkPimplTest1.cpp
+  ctkScopedCurrentDirTest1.cpp
   ctkSingletonTest1.cpp
   ctkTransferFunctionTest1.cpp
   ctkTransferFunctionRepresentationTest1.cpp
@@ -140,6 +141,7 @@ SET_TESTS_PROPERTIES(ctkErrorLogModelTest3 PROPERTIES PASS_REGULAR_EXPRESSION
 SIMPLE_TEST( ctkModelTesterTest1 )
 SIMPLE_TEST( ctkModelTesterTest2 )
 SIMPLE_TEST( ctkPimplTest1 )
+SIMPLE_TEST( ctkScopedCurrentDirTest1 )
 SIMPLE_TEST( ctkSingletonTest1 )
 SIMPLE_TEST( ctkTransferFunctionTest1 )
 SIMPLE_TEST( ctkTransferFunctionRepresentationTest1 )

+ 89 - 0
Libs/Core/Testing/Cpp/ctkScopedCurrentDirTest1.cpp

@@ -0,0 +1,89 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) [Organization Name]
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.commontk.org/LICENSE
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QDir>
+#include <QTemporaryFile>
+
+// CTK includes
+#include "ctkScopedCurrentDir.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+//-----------------------------------------------------------------------------
+int ctkScopedCurrentDirTest1(int argc, char * argv [])
+{
+  Q_UNUSED(argc);
+  Q_UNUSED(argv);
+
+  QString savedCurrentPath = QDir::currentPath();
+
+  QTemporaryFile foo(QDir::tempPath() + "/ctkScopedCurrentDirTest1-XXXXXX.txt");
+  foo.setAutoRemove(false);
+  bool opened = foo.open();
+  if(!opened)
+    {
+    std::cerr << "Line " << __LINE__ << " - Failed to create temporary file !" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  {
+  ctkScopedCurrentDir scopedCurrentDir(QDir::tempPath());
+
+  QString currentPath = scopedCurrentDir.currentPath();
+  QString expectedCurrentPath = QDir::tempPath();
+  if (currentPath != expectedCurrentPath)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
+              << "\tcurrentPath:" << qPrintable(currentPath) << "\n"
+              << "\texpectedCurrentPath:" << qPrintable(expectedCurrentPath) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (scopedCurrentDir.savedCurrentPath() != savedCurrentPath)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
+              << "\tsavedCurrentPath:" << qPrintable(scopedCurrentDir.savedCurrentPath()) << "\n"
+              << "\texpectedSavedCurrentPath:" << qPrintable(savedCurrentPath) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  if (!QFile::exists(foo.fileName()))
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
+              << "\tfile [" << qPrintable(foo.fileName()) <<"] doesn't exist "
+              << "in directory [" << qPrintable(QDir::currentPath()) << "]!" << std::endl;
+    return EXIT_FAILURE;
+    }
+  }
+
+  QString currentPath = QDir::currentPath();
+  if (savedCurrentPath != currentPath)
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctkScopedCurrentDir\n"
+              << "\tsavedCurrentPath [" << qPrintable(savedCurrentPath) << "]\n"
+              << "\tcurrentPath [" << qPrintable(currentPath) << "]" << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 73 - 0
Libs/Core/ctkScopedCurrentDir.cpp

@@ -0,0 +1,73 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QDir>
+
+// CTK includes
+#include "ctkScopedCurrentDir.h"
+
+class ctkScopedCurrentDirPrivate
+{
+public:
+  ctkScopedCurrentDirPrivate();
+
+  QString SavedCurrentPath;
+};
+
+// --------------------------------------------------------------------------
+// ctkScopedCurrentDirPrivate methods
+
+// --------------------------------------------------------------------------
+ctkScopedCurrentDirPrivate::ctkScopedCurrentDirPrivate()
+{
+}
+
+// --------------------------------------------------------------------------
+// ctkScopedCurrentDir methods
+
+// --------------------------------------------------------------------------
+ctkScopedCurrentDir::ctkScopedCurrentDir(const QString& path) :
+  d_ptr(new ctkScopedCurrentDirPrivate())
+{
+  Q_D(ctkScopedCurrentDir);
+  d->SavedCurrentPath = QDir::currentPath();
+  QDir::setCurrent(path);
+}
+
+// --------------------------------------------------------------------------
+ctkScopedCurrentDir::~ctkScopedCurrentDir()
+{
+  Q_D(ctkScopedCurrentDir);
+  QDir::setCurrent(d->SavedCurrentPath);
+}
+
+// --------------------------------------------------------------------------
+QString ctkScopedCurrentDir::currentPath()const
+{
+  return QDir::currentPath();
+}
+
+// --------------------------------------------------------------------------
+QString ctkScopedCurrentDir::savedCurrentPath()const
+{
+  Q_D(const ctkScopedCurrentDir);
+  return d->SavedCurrentPath;
+}

+ 68 - 0
Libs/Core/ctkScopedCurrentDir.h

@@ -0,0 +1,68 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+#ifndef __ctkScopedCurrentDir_h
+#define __ctkScopedCurrentDir_h
+
+// Qt includes
+#include <QScopedPointer>
+
+// CTK includes
+#include "ctkCoreExport.h"
+
+class ctkScopedCurrentDirPrivate;
+
+///
+/// \brief Use this class to change the current application directory in a given scope
+/// and automatically restore it.
+///
+/// This is particulary useful in case a plugin and its dependent libraries should be loaded from
+/// a known directory.
+/// Indeed, changing the application PATH, LD_LIBRARY_PATH or DYLD_LIBRARY_PATH within the current
+/// process won't have the desired effect. The loader check for these variable once when the process
+/// starts.
+/// \sa http://stackoverflow.com/questions/856116/changing-ld-library-path-at-runtime-for-ctypes
+/// \sa http://stackoverflow.com/questions/1178094/change-current-process-environment
+///
+/// \ingroup Core
+class CTK_CORE_EXPORT ctkScopedCurrentDir
+{
+public:
+  explicit ctkScopedCurrentDir(const QString& path);
+  virtual ~ctkScopedCurrentDir();
+
+  /// Return the current application path
+  /// \sa QDir::currentPath
+  QString currentPath()const;
+
+  /// Return saved current path
+  QString savedCurrentPath()const;
+  
+protected:
+  QScopedPointer<ctkScopedCurrentDirPrivate> d_ptr;
+
+private:
+  Q_DECLARE_PRIVATE(ctkScopedCurrentDir);
+  Q_DISABLE_COPY(ctkScopedCurrentDir);
+};
+
+#endif
+
+