2010-05-16 19:26:45 -04:00
|
|
|
#include "folderiterator.h"
|
2011-10-01 09:12:28 -04:00
|
|
|
#include "rsstring.h"
|
2010-05-16 19:26:45 -04:00
|
|
|
|
|
|
|
|
|
|
|
namespace librs { namespace util {
|
|
|
|
|
|
|
|
|
|
|
|
FolderIterator::FolderIterator(const std::string& folderName)
|
|
|
|
{
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
std::wstring utf16Name;
|
|
|
|
if(! ConvertUtf8ToUtf16(folderName, utf16Name)) {
|
|
|
|
validity = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-15 08:42:55 -04:00
|
|
|
utf16Name += L"/*.*";
|
2010-05-16 19:26:45 -04:00
|
|
|
|
|
|
|
handle = FindFirstFileW(utf16Name.c_str(), &fileInfo);
|
|
|
|
validity = handle != INVALID_HANDLE_VALUE;
|
|
|
|
isFirstCall = true;
|
|
|
|
#else
|
|
|
|
handle = opendir(folderName.c_str());
|
|
|
|
validity = handle != NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
FolderIterator::~FolderIterator()
|
|
|
|
{
|
|
|
|
closedir();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FolderIterator::readdir() {
|
|
|
|
if(!validity)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
if(isFirstCall) {
|
|
|
|
isFirstCall = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return FindNextFileW(handle, &fileInfo) != 0;
|
|
|
|
#else
|
2010-05-16 19:59:39 -04:00
|
|
|
ent = ::readdir(handle);
|
|
|
|
return ent != 0;
|
2010-05-16 19:26:45 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FolderIterator::d_name(std::string& dest)
|
|
|
|
{
|
|
|
|
if(!validity)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
if(! ConvertUtf16ToUtf8(fileInfo.cFileName, dest)) {
|
|
|
|
validity = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2010-05-16 19:59:39 -04:00
|
|
|
if(ent == 0)
|
|
|
|
return false;
|
|
|
|
dest = ent->d_name;
|
2010-05-16 19:26:45 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FolderIterator::closedir()
|
|
|
|
{
|
|
|
|
if(!validity)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
validity = false;
|
|
|
|
|
|
|
|
#ifdef WINDOWS_SYS
|
|
|
|
return FindClose(handle) != 0;
|
|
|
|
#else
|
2010-05-16 19:59:39 -04:00
|
|
|
return ::closedir(handle) == 0;
|
2010-05-16 19:26:45 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} } // librs::util
|