Skip to content

Commit

Permalink
Add ctkUtils::rmdir allowing to delete a directory recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Sep 1, 2011
1 parent 71129b1 commit 951e9ea
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Libs/Core/Testing/Cpp/CMakeLists.txt
Expand Up @@ -41,6 +41,7 @@ SET(KITTests_SRCS
ctkUtilsTest1.cpp
ctkUtilsTest2.cpp
ctkUtilsTest3.cpp
ctkUtilsTest4.cpp
ctkDependencyGraphTest1.cpp
ctkDependencyGraphTest2.cpp
ctkPimplTest1.cpp
Expand Down Expand Up @@ -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 changes: 119 additions & 0 deletions 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 changes: 31 additions & 0 deletions Libs/Core/ctkUtils.cpp
Expand Up @@ -20,6 +20,7 @@

// Qt includes
#include <QDebug>
#include <QDir>
#include <QRegExp>
#include <QString>
#include <QStringList>
Expand Down Expand Up @@ -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 changes: 8 additions & 0 deletions Libs/Core/ctkUtils.h
Expand Up @@ -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

0 comments on commit 951e9ea

Please sign in to comment.