ソースを参照

Merge branch 'ctkutil-rmdir'

* ctkutil-rmdir:
  Add ctkUtils::rmdir allowing to delete a directory recursively
Jean-Christophe Fillion-Robin 13 年 前
コミット
fd8cfead27
共有4 個のファイルを変更した160 個の追加0 個の削除を含む
  1. 2 0
      Libs/Core/Testing/Cpp/CMakeLists.txt
  2. 119 0
      Libs/Core/Testing/Cpp/ctkUtilsTest4.cpp
  3. 31 0
      Libs/Core/ctkUtils.cpp
  4. 8 0
      Libs/Core/ctkUtils.h

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

@@ -41,6 +41,7 @@ SET(KITTests_SRCS
   ctkUtilsTest1.cpp
   ctkUtilsTest2.cpp
   ctkUtilsTest3.cpp
+  ctkUtilsTest4.cpp
   ctkDependencyGraphTest1.cpp
   ctkDependencyGraphTest2.cpp
   ctkPimplTest1.cpp
@@ -148,6 +149,7 @@ SIMPLE_TEST( ctkUtilsSignificantDecimalsTest1 )
 SIMPLE_TEST( ctkUtilsTest1 )
 SIMPLE_TEST( ctkUtilsTest2 )
 SIMPLE_TEST( ctkUtilsTest3 )
+SIMPLE_TEST( ctkUtilsTest4 )
 SIMPLE_TEST( ctkWorkflowTest1 )
 SIMPLE_TEST( ctkWorkflowTest2 )
 SIMPLE_TEST( ctkWorkflowTest3 )

+ 119 - 0
Libs/Core/Testing/Cpp/ctkUtilsTest4.cpp

@@ -0,0 +1,119 @@
+/*=========================================================================
+
+  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.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 <QDebug>
+#include <QDir>
+#include <QTextStream>
+#include <QTime>
+
+// CTK includes
+#include "ctkUtils.h"
+
+// STD includes
+#include <cstdlib>
+#include <iostream>
+
+namespace
+{
+//-----------------------------------------------------------------------------
+bool createFile(int line, const QDir& dir, const QString& relativePath, const QString& fileName)
+{
+  QDir newDir(dir);
+  newDir.mkpath(relativePath);
+  newDir.cd(relativePath);
+  QString filePath = QFileInfo(newDir, fileName).filePath();
+  QFile file(filePath);
+  file.open(QIODevice::Text | QIODevice::WriteOnly);
+  QTextStream out(&file);
+  out << "Generated by ctkUtilsTest4" << endl;
+  file.close();
+
+  if (!QFile::exists(filePath))
+    {
+    std::cerr << "Line " << line << " - Failed to create file" << qPrintable(filePath) << std::endl;
+    return false;
+    }
+
+  return true;
+}
+
+} // end of anonymous namespace
+
+
+//-----------------------------------------------------------------------------
+int ctkUtilsTest4(int argc, char * argv [] )
+{
+  Q_UNUSED(argc);
+  Q_UNUSED(argv);
+
+  // --------------------------------------------------------------------------
+  // Test rmdir(const QString & dirName);
+  // --------------------------------------------------------------------------
+  
+  QDir tmp = QDir::temp();
+  QString temporaryDirName =
+      QString("ctkUtilsTest4.%1").arg(QTime::currentTime().toString("hhmmsszzz"));
+
+  // Attempt to delete nonexistent relative directory
+  QString nonexistentRelativeDirPath = temporaryDirName;
+  if (ctk::rmdir(nonexistentRelativeDirPath))
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctk::rmdir() !"
+              << " - It should fail to delete nonexistent directory: " <<
+              qPrintable(nonexistentRelativeDirPath)<< std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Attempt to delete nonexistent absolute directory
+  QString nonexistentAbsoluteDirPath = QFileInfo(tmp, temporaryDirName).dir().absolutePath();
+  if (ctk::rmdir(nonexistentAbsoluteDirPath))
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctk::rmdir() !"
+              << " - It should fail to delete nonexistent directory: " <<
+              qPrintable(nonexistentAbsoluteDirPath)<< std::endl;
+    return EXIT_FAILURE;
+    }
+
+  // Create a directory structure
+  tmp.mkdir(temporaryDirName);
+  tmp.cd(temporaryDirName);
+  if (!createFile(__LINE__, tmp, "foo", "a.txt"))
+    {
+    return EXIT_FAILURE;
+    }
+  if (!createFile(__LINE__, tmp, "foo/bar", "b.txt"))
+    {
+    return EXIT_FAILURE;
+    }
+  if (!createFile(__LINE__, tmp, "foo/zoo", "c.txt"))
+    {
+    return EXIT_FAILURE;
+    }
+
+  if (!ctk::rmdir(tmp.absolutePath()))
+    {
+    std::cerr << "Line " << __LINE__ << " - Problem with ctk::rmdir()"
+              << " - Failed to delete directory:" << qPrintable(tmp.absolutePath()) << std::endl;
+    return EXIT_FAILURE;
+    }
+
+  return EXIT_SUCCESS;
+}

+ 31 - 0
Libs/Core/ctkUtils.cpp

@@ -20,6 +20,7 @@
 
 // Qt includes
 #include <QDebug>
+#include <QDir>
 #include <QRegExp>
 #include <QString>
 #include <QStringList>
@@ -274,3 +275,33 @@ double ctk::closestPowerOfTen(double value)
     }
   return magnitude * sign;
 }
+
+//-----------------------------------------------------------------------------
+bool ctk::rmdir(const QString & dirName)
+{
+  bool result = false;
+  QDir dir(dirName);
+
+  if (dir.exists(dirName))
+    {
+    foreach (QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst))
+      {
+      if (info.isDir())
+        {
+        result = ctk::rmdir(info.absoluteFilePath());
+        }
+      else
+        {
+        result = QFile::remove(info.absoluteFilePath());
+        }
+
+      if (!result)
+        {
+        return result;
+        }
+      }
+    result = dir.rmdir(dirName);
+    }
+
+  return result;
+}

+ 8 - 0
Libs/Core/ctkUtils.h

@@ -108,6 +108,14 @@ int CTK_CORE_EXPORT orderOfMagnitude(double value);
 /// See more cases in the test ctkUtilsClosestPowerOfTenTest1
 double CTK_CORE_EXPORT closestPowerOfTen(double value);
 
+///
+/// \ingroup Core
+/// Remove a directory recursively.
+/// \param dirName The directory to remove
+/// \return <code>true</code> on success, <code>false</code> otherwise.
+/// \sa QDir::rmdir
+bool CTK_CORE_EXPORT rmdir(const QString & dirName);
+
 }
 
 #endif