Browse Source

Remove directoryEntries()

sendfile
Martchus 3 years ago
parent
commit
c701fc25ee
  1. 52
      io/path.cpp
  2. 21
      io/path.h
  3. 16
      tests/iotests.cpp
  4. 14
      tests/testutils.cpp

52
io/path.cpp

@ -4,23 +4,6 @@
#include <fstream>
#include <sstream>
#include <string>
#if defined(PLATFORM_UNIX)
#include <dirent.h>
#include <pwd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#elif defined(PLATFORM_WINDOWS)
#ifdef UNICODE
#undef UNICODE
#endif
#ifdef _UNICODE
#undef _UNICODE
#endif
#include <windows.h>
#else
#error Platform not supported.
#endif
using namespace std;
@ -84,39 +67,4 @@ void removeInvalidChars(std::string &fileName)
}
}
/*!
* \brief Returns the names of the directory entries in the specified \a path with the specified \a types.
* \deprecated This function has FIXMEs. Since it can be replaced by using fs abstraction lib it is a good candidate for being replaced.
*/
std::list<std::string> directoryEntries(const char *path, DirectoryEntryType types)
{
#ifdef PLATFORM_UNIX
list<string> entries;
if (auto dir = opendir(path)) {
while (auto dirEntry = readdir(dir)) {
bool filter = false;
switch (dirEntry->d_type) {
case DT_REG:
filter = (types & DirectoryEntryType::File) != DirectoryEntryType::None;
break;
case DT_DIR:
filter = (types & DirectoryEntryType::Directory) != DirectoryEntryType::None;
break;
case DT_LNK:
filter = (types & DirectoryEntryType::Symlink) != DirectoryEntryType::None;
break;
default:
filter = (types & DirectoryEntryType::All) != DirectoryEntryType::None;
}
if (filter) {
entries.emplace_back(dirEntry->d_name);
}
}
closedir(dir);
}
return entries;
#else
return list<string>(); // TODO
#endif
}
} // namespace IoUtilities

21
io/path.h

@ -20,30 +20,9 @@
namespace IoUtilities {
/*!
* \brief The DirectoryEntryType enum specifies the type of a directory entry (file, directory or symlink).
*/
enum class DirectoryEntryType : unsigned char { None = 0, File = 1, Directory = 2, Symlink = 4, All = 0xFF };
constexpr DirectoryEntryType operator|(DirectoryEntryType lhs, DirectoryEntryType rhs)
{
return static_cast<DirectoryEntryType>(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
}
inline DirectoryEntryType &operator|=(DirectoryEntryType &lhs, DirectoryEntryType rhs)
{
return (lhs = static_cast<DirectoryEntryType>(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs)));
}
constexpr DirectoryEntryType operator&(DirectoryEntryType lhs, DirectoryEntryType rhs)
{
return static_cast<DirectoryEntryType>(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
}
CPP_UTILITIES_EXPORT std::string fileName(const std::string &path);
CPP_UTILITIES_EXPORT std::string directory(const std::string &path);
CPP_UTILITIES_EXPORT void removeInvalidChars(std::string &fileName);
CPP_UTILITIES_EXPORT std::list<std::string> directoryEntries(const char *path, DirectoryEntryType types = DirectoryEntryType::All);
} // namespace IoUtilities
#endif // IOUTILITIES_PATHHELPER_H

16
tests/iotests.cpp

@ -272,22 +272,6 @@ void IoTests::testPathUtilities()
string invalidPath("lib/c++uti*lities.so?");
removeInvalidChars(invalidPath);
CPPUNIT_ASSERT(invalidPath == "libc++utilities.so");
#ifdef PLATFORM_UNIX
const string iniFilePath = TestUtilities::testFilePath("test.ini");
const string testFilesDir = iniFilePath.substr(0, iniFilePath.size() - 9);
auto testFilesDirEntries = directoryEntries(testFilesDir.c_str(), DirectoryEntryType::All);
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "test.ini") != testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), ".") != testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "..") != testFilesDirEntries.cend());
testFilesDirEntries = directoryEntries(testFilesDir.c_str(), DirectoryEntryType::Directory);
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "test.ini") == testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), ".") != testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "..") != testFilesDirEntries.cend());
testFilesDirEntries = directoryEntries(testFilesDir.c_str(), DirectoryEntryType::File);
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "test.ini") != testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), ".") == testFilesDirEntries.cend());
CPPUNIT_ASSERT(find(testFilesDirEntries.cbegin(), testFilesDirEntries.cend(), "..") == testFilesDirEntries.cend());
#endif
}
/*!

14
tests/testutils.cpp

@ -552,25 +552,17 @@ string TestApplication::readTestfilePathFromSrcRef()
cerr << Phrases::Warning << "The file \"srcdirref\" is empty." << Phrases::EndFlush;
return string();
}
srcDirContent += "/testfiles/";
// check whether the referenced source directory contains a "testfiles" directory
#ifdef PLATFORM_UNIX // directoryEntries() is not implemented under Windows so we can only to the check under UNIX
bool hasTestfilesDir = false;
for (const string &dir : directoryEntries(srcDirContent.data(), DirectoryEntryType::Directory)) {
if (dir == "testfiles") {
hasTestfilesDir = true;
break;
}
}
if (!hasTestfilesDir) {
if (!dirExists(srcDirContent)) {
cerr << Phrases::Warning
<< "The source directory referenced by the file \"srcdirref\" does not contain a \"testfiles\" directory or does not exist."
<< Phrases::End << "Referenced source directory: " << srcDirContent << endl;
return string();
}
#endif // PLATFORM_UNIX
return srcDirContent;
return srcDirContent += "/testfiles/";
} catch (const std::ios_base::failure &) {
cerr << Phrases::Warning << "The file \"srcdirref\" can not be opened. It likely just doesn't exist in the working directory."
<< Phrases::EndFlush;

Loading…
Cancel
Save