Remove Directory class replaced with filesystem API

pull/3/head
Hleb Valoshka 2019-07-25 15:37:29 +03:00
parent b6b543438d
commit 48bef14233
4 changed files with 0 additions and 373 deletions

View File

@ -1,82 +0,0 @@
// directory.h
//
// Copyright (C) 2003, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <iostream>
#include "directory.h"
using namespace std;
bool Directory::enumFiles(EnumFilesHandler& handler, bool deep)
{
string filename;
while (nextFile(filename))
{
// Skip all files beginning with a period, most importantly, . and ..
if (filename[0] == '.')
continue;
// TODO: optimize this to avoid allocating so many strings
string pathname = handler.getPath() + string("/") + filename;
if (IsDirectory(pathname))
{
if (deep)
{
Directory* dir = OpenDirectory(pathname);
bool cont = true;
if (dir != nullptr)
{
handler.pushDir(filename);
cont = dir->enumFiles(handler, deep);
handler.popDir();
delete dir;
}
if (!cont)
return false;
}
}
else
{
if (!handler.process(filename))
return false;
}
}
return true;
}
void EnumFilesHandler::pushDir(const std::string& dirName)
{
if (!dirStack.empty())
dirStack.push_back(dirStack.back() + string("/") + dirName);
else
dirStack.push_back(dirName);
}
void EnumFilesHandler::popDir()
{
dirStack.pop_back();
}
const string& EnumFilesHandler::getPath() const
{
// need this so we can return a non-temporary value:
static const string emptyString;
if (!dirStack.empty())
return dirStack.back();
return emptyString;
}

View File

@ -1,48 +0,0 @@
// directory.h
//
// Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELUTIL_DIRECTORY_H_
#define _CELUTIL_DIRECTORY_H_
#include <string>
#include <vector>
class EnumFilesHandler
{
public:
EnumFilesHandler() = default;
virtual ~EnumFilesHandler() = default;
void pushDir(const std::string&);
void popDir();
const std::string& getPath() const;
virtual bool process(const std::string& filename) = 0;
private:
std::vector<std::string> dirStack;
};
class Directory
{
public:
Directory() = default;
virtual ~Directory() = default;
virtual bool nextFile(std::string&) = 0;
virtual bool enumFiles(EnumFilesHandler& handler, bool deep);
};
extern std::string WordExp(const std::string&);
extern Directory* OpenDirectory(const std::string&);
extern bool IsDirectory(const std::string&);
#endif // _CELUTIL_DIRECTORY_H_

View File

@ -1,135 +0,0 @@
// unixdirectory.cpp
//
// Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <wordexp.h>
#include <utility>
#include "directory.h"
using namespace std;
#ifdef TARGET_OS_MAC
#ifdef QT_CORE_LIB
// Crash on Mac OS X / Qt4 version when calling wordfree.
// This seems to happen only with Leopard.
#define WORDEXP_PROBLEM
#endif
#endif
class UnixDirectory : public Directory
{
public:
UnixDirectory(std::string /*_dirname*/);
~UnixDirectory() override;
bool nextFile(std::string& /*filename*/) override;
enum {
DirGood = 0,
DirBad = 1
};
private:
string dirname;
int status;
DIR* dir;
};
UnixDirectory::UnixDirectory(std::string _dirname) :
dirname(std::move(_dirname)),
status(DirGood),
dir(nullptr)
{
}
UnixDirectory::~UnixDirectory()
{
if (dir != nullptr)
{
closedir(dir);
dir = nullptr;
}
}
bool UnixDirectory::nextFile(std::string& filename)
{
if (status != DirGood)
return false;
if (dir == nullptr)
{
dir = opendir(dirname.c_str());
if (dir == nullptr)
{
status = DirBad;
return false;
}
}
struct dirent* ent = readdir(dir);
if (ent == nullptr)
{
status = DirBad;
return false;
}
filename = ent->d_name;
return true;
}
Directory* OpenDirectory(const std::string& dirname)
{
return new UnixDirectory(dirname);
}
bool IsDirectory(const std::string& filename)
{
struct stat buf;
return stat(filename.c_str(), &buf) == 0 ? S_ISDIR(buf.st_mode) : false;
}
std::string WordExp(const std::string& filename)
{
#ifndef WORDEXP_PROBLEM
wordexp_t result;
std::string expanded;
switch(wordexp(filename.c_str(), &result, WRDE_NOCMD)) {
case 0: // successful
break;
case WRDE_NOSPACE:
// If the error was `WRDE_NOSPACE',
// then perhaps part of the result was allocated.
wordfree(&result);
default: // some other error
return filename;
}
if (result.we_wordc != 1) {
wordfree(&result);
return filename;
}
expanded = result.we_wordv[0];
wordfree(&result);
#else
std::string expanded = filename;
#endif
return expanded;
}

View File

@ -1,108 +0,0 @@
// windirectory.cpp
//
// Copyright (C) 2002, Chris Laurel <claurel@shatters.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include <iostream>
#include <utility>
#include <windows.h>
#include "directory.h"
using namespace std;
class WindowsDirectory : public Directory
{
public:
WindowsDirectory(std::string /*_dirname*/);
~WindowsDirectory() override;
bool nextFile(std::string& /*filename*/) override;
enum {
DirGood = 0,
DirBad = 1
};
private:
string dirname;
string searchName;
int status;
HANDLE searchHandle;
};
WindowsDirectory::WindowsDirectory(std::string _dirname) :
dirname(std::move(_dirname)),
status(DirGood),
searchHandle(INVALID_HANDLE_VALUE)
{
searchName = dirname + string("\\*");
// Check to make sure that this file is a directory
}
WindowsDirectory::~WindowsDirectory()
{
if (searchHandle != INVALID_HANDLE_VALUE)
FindClose(searchHandle);
searchHandle = nullptr;
}
bool WindowsDirectory::nextFile(std::string& filename)
{
WIN32_FIND_DATAA findData;
if (status != DirGood)
return false;
if (searchHandle == INVALID_HANDLE_VALUE)
{
searchHandle = FindFirstFileA(const_cast<char*>(searchName.c_str()),
&findData);
if (searchHandle == INVALID_HANDLE_VALUE)
{
status = DirBad;
return false;
}
filename = findData.cFileName;
return true;
}
else
{
if (FindNextFileA(searchHandle, &findData))
{
filename = findData.cFileName;
return true;
}
status = DirBad;
return false;
}
}
Directory* OpenDirectory(const std::string& dirname)
{
return new WindowsDirectory(dirname);
}
bool IsDirectory(const std::string& filename)
{
DWORD attr = GetFileAttributesA(const_cast<char*>(filename.c_str()));
if (attr == 0xffffffff)
return false;
return ((attr & FILE_ATTRIBUTE_DIRECTORY) != 0);
}
std::string WordExp(const std::string& filename) {
return filename;
}