FileWrapper/FileWrapper_Win.cpp

82 lines
1.4 KiB
C++

#ifdef _WIN32
#include "FileWrapper.h"
#include <Windows.h>
struct DirWalk::_impl
{
HANDLE hand;
WIN32_FIND_DATA fnd;
std::string root;
// -1 Not Opened 0 End 1 Ready
int status;
};
DirWalk::DirWalk(const std::string& DirName) : _p(new _impl)
{
std::string realname = DirName;
int sz = realname.size();
if (realname[sz - 1] != '\'')
{
realname.push_back('\\');
}
_p->root = realname;
realname.push_back('*');
_p->hand = FindFirstFile(realname.c_str(), &_p->fnd);
if (_p->hand != INVALID_HANDLE_VALUE)
{
_p->status = 1;
}
else
{
_p->status = -1;
}
}
DirWalk::~DirWalk()
{
if (_p->hand != INVALID_HANDLE_VALUE)
{
FindClose(_p->hand);
}
delete _p;
}
bool DirWalk::isReady() const
{
return _p->status >= 0;
}
std::string DirWalk::getroot() const
{
return _p->root;
}
int DirWalk::next(std::string& name, bool& is_dir)
{
if (_p->status == 1)
{
name = _p->fnd.cFileName;
if (_p->fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
is_dir = true;
}
else
{
is_dir = false;
}
// Step down
if (!FindNextFile(_p->hand, &_p->fnd))
{
_p->status = 0;
}
return 1;
}
else
{
return 0;
}
}
#endif // End of _WIN32