2010-11-15 14:38:12 +00:00
|
|
|
#include "FolderInfo.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
namespace PluginFolderInfo {
|
|
|
|
|
2010-12-06 07:43:30 +00:00
|
|
|
FolderInfo::FolderInfo(const wchar_t* aPath, const wchar_t* aIniPath)
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
|
|
|
mySubFolderFlag = false;
|
|
|
|
myHiddenFileFlag = false;
|
|
|
|
mySystemFileFlag = false;
|
2010-11-15 15:23:32 +00:00
|
|
|
myRegExpFilter = NULL;
|
|
|
|
myRegExpFilterExtra = NULL;
|
2010-11-15 14:38:12 +00:00
|
|
|
myLastUpdateTime = 0;
|
|
|
|
Clear();
|
2010-12-06 07:43:30 +00:00
|
|
|
SetPath(aPath, aIniPath);
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void FolderInfo::Clear()
|
|
|
|
{
|
|
|
|
mySize = 0;
|
|
|
|
myFileCount = 0;
|
|
|
|
myFolderCount = 0;
|
|
|
|
}
|
|
|
|
|
2010-12-06 07:43:30 +00:00
|
|
|
void FolderInfo::SetPath(const wchar_t* aPath, const wchar_t* aIniPath)
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
|
|
|
if (!aPath || 0 == aPath[0]) {
|
|
|
|
myPath = L"";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
myPath = aPath;
|
2010-12-06 07:43:30 +00:00
|
|
|
if (wcsncmp(aPath, L".\\", 2) == 0 || wcsncmp(aPath, L"..\\", 3) == 0) {
|
|
|
|
wchar_t* buf = new wchar_t[wcslen(aIniPath) + 1];
|
|
|
|
wcscpy(buf, aIniPath);
|
|
|
|
wchar_t* iniFileName = wcsrchr(buf, '\\');
|
|
|
|
if (iniFileName) {
|
|
|
|
iniFileName[1] = 0;
|
|
|
|
myPath = buf;
|
|
|
|
myPath += aPath;
|
|
|
|
}
|
|
|
|
delete[] buf;
|
|
|
|
}
|
|
|
|
|
2010-11-15 14:38:12 +00:00
|
|
|
if (myPath[myPath.size() - 1] != L'\\') {
|
|
|
|
myPath += L"\\";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderInfo::Update()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
|
|
|
|
if (myPath.length() == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CalculateSize();
|
|
|
|
myLastUpdateTime = GetTickCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FolderInfo::CalculateSize()
|
|
|
|
{
|
|
|
|
std::list<std::wstring> folderQueue;
|
|
|
|
folderQueue.push_back(myPath.c_str());
|
|
|
|
|
|
|
|
wchar_t searchPattern[MAX_PATH + 10];
|
|
|
|
wchar_t buffer[MAX_PATH];
|
|
|
|
char utf8Buf[MAX_PATH * 3];
|
|
|
|
WIN32_FIND_DATA findData;
|
|
|
|
HANDLE findHandle;
|
|
|
|
while (!folderQueue.empty()) {
|
|
|
|
std::list<std::wstring>::reference ref = folderQueue.front();
|
|
|
|
wsprintf(searchPattern, L"%s%s", ref.c_str(), L"\\*.*");
|
|
|
|
findHandle = ::FindFirstFile(searchPattern, &findData);
|
|
|
|
if (INVALID_HANDLE_VALUE == findHandle) {
|
|
|
|
folderQueue.pop_front();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
// special case for "." and ".."
|
2011-07-11 05:35:15 +00:00
|
|
|
if (wcscmp(findData.cFileName, L".") == 0 ||
|
|
|
|
wcscmp(findData.cFileName, L"..") == 0) {
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFolder = (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
|
|
|
|
|
|
|
|
if (!myHiddenFileFlag && (findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (!mySystemFileFlag && (findData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-11-15 15:23:32 +00:00
|
|
|
else if (!isFolder && myRegExpFilter) {
|
2010-11-15 14:38:12 +00:00
|
|
|
int utf8BufLen = WideCharToMultiByte(CP_UTF8, 0, findData.cFileName, wcslen(findData.cFileName) + 1, utf8Buf, MAX_PATH * 3, NULL, NULL);
|
2010-11-15 15:23:32 +00:00
|
|
|
if (0 != pcre_exec(myRegExpFilter, myRegExpFilterExtra, utf8Buf, utf8BufLen, 0, 0, NULL, 0)) {
|
2010-11-15 14:38:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isFolder) {
|
|
|
|
myFolderCount++;
|
|
|
|
if (mySubFolderFlag) {
|
|
|
|
wsprintf(buffer, L"%s\\%s", ref.c_str(), findData.cFileName);
|
|
|
|
folderQueue.push_back(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
myFileCount++;
|
|
|
|
mySize += ((UINT64)findData.nFileSizeHigh << 32) + findData.nFileSizeLow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (::FindNextFile(findHandle, &findData));
|
|
|
|
FindClose(findHandle);
|
|
|
|
|
|
|
|
folderQueue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-15 15:23:32 +00:00
|
|
|
void FolderInfo::SetRegExpFilter(const wchar_t* aFilter)
|
2010-11-15 14:38:12 +00:00
|
|
|
{
|
2010-11-15 15:23:32 +00:00
|
|
|
if (myRegExpFilter) {
|
|
|
|
pcre_free(myRegExpFilter);
|
|
|
|
myRegExpFilter = NULL;
|
|
|
|
myRegExpFilterExtra = NULL;
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aFilter == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int filterLen = wcslen(aFilter) + 1;
|
|
|
|
int bufLen = WideCharToMultiByte(CP_UTF8, 0, aFilter, filterLen, NULL, 0, NULL, NULL);
|
|
|
|
|
|
|
|
char* buf = new char[bufLen];
|
|
|
|
WideCharToMultiByte(CP_UTF8, 0, aFilter, filterLen, buf, bufLen, NULL, NULL);
|
|
|
|
|
|
|
|
const char* error;
|
|
|
|
int erroffset;
|
2010-11-15 15:23:32 +00:00
|
|
|
myRegExpFilter = pcre_compile(buf, PCRE_UTF8, &error, &erroffset, NULL);
|
|
|
|
if (myRegExpFilter) {
|
|
|
|
myRegExpFilterExtra = pcre_study(myRegExpFilter, 0, &error);
|
2010-11-15 14:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace PluginFolderInfo
|