FileWrapper/FileWrapper_Lin.cpp

75 lines
1.3 KiB
C++

#ifndef _WIN32
#include "FileWrapper.h"
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#include <cstring>
struct DirWalk::_impl
{
DIR* dir;
std::string root;
};
DirWalk::DirWalk(const std::string& DirName) : _p(new _impl)
{
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;
}
int DirWalk::next(std::string& name, bool& is_dir)
{
dirent* file = NULL;
if ((file = readdir(_p->dir)) != NULL)
{
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;
}
}
std::string DirWalk::getroot() const
{
return _p->root;
}
#endif // End of ifndef _WIN32