2018-06-25 11:16:59 +08:00
|
|
|
#ifndef _WIN32
|
2018-06-25 11:20:11 +08:00
|
|
|
#include "FileWrapper.h"
|
2018-06-25 11:16:59 +08:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <cstdlib>
|
2018-06-25 11:22:12 +08:00
|
|
|
#include <cstring>
|
2018-06-25 11:16:59 +08:00
|
|
|
|
|
|
|
struct DirWalk::_impl
|
|
|
|
{
|
|
|
|
DIR* dir;
|
|
|
|
std::string root;
|
|
|
|
};
|
|
|
|
|
2018-06-25 11:26:39 +08:00
|
|
|
DirWalk::DirWalk(const std::string& DirName) : _p(new _impl)
|
2018-06-25 11:16:59 +08:00
|
|
|
{
|
|
|
|
std::string realname = DirName;
|
|
|
|
int sz = realname.size();
|
|
|
|
if (realname[sz-1] != '/')
|
|
|
|
{
|
|
|
|
realname.push_back('/');
|
|
|
|
}
|
|
|
|
if ((_p->dir = opendir(realname .c_str())) != NULL)
|
|
|
|
{
|
|
|
|
_p->root = realname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DirWalk::~DirWalk()
|
|
|
|
{
|
|
|
|
closedir(_p->dir);
|
|
|
|
delete _p;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DirWalk::isReady() const
|
|
|
|
{
|
|
|
|
return _p->dir != NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:21:45 +08:00
|
|
|
int DirWalk::next(std::string& name, bool& is_dir)
|
2018-06-25 11:16:59 +08:00
|
|
|
{
|
|
|
|
dirent* file = NULL;
|
2018-06-25 11:22:50 +08:00
|
|
|
if ((file = readdir(_p->dir)) != NULL)
|
2018-06-25 11:16:59 +08:00
|
|
|
{
|
|
|
|
if (file->d_type == DT_DIR)
|
|
|
|
{
|
|
|
|
if (strcmp(file->d_name, ".") != 0 &&
|
|
|
|
strcmp(file->d_name, "..") != 0)
|
|
|
|
{
|
|
|
|
is_dir = true;
|
|
|
|
name = file->d_name;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else return next(name, is_dir);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = file->d_name;
|
|
|
|
is_dir = false;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-25 11:21:45 +08:00
|
|
|
std::string DirWalk::getroot() const
|
2018-06-25 11:16:59 +08:00
|
|
|
{
|
|
|
|
return _p->root;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // End of ifndef _WIN32
|