From e478768cac88145a0a9fd76ae9d189ac11aee175 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Tue, 10 Apr 2012 18:20:22 +0300 Subject: [PATCH] FolderInfo.dll: Added support for dynamic Folder= and fixed memory leaks --- Plugins/PluginFolderInfo/FolderInfo.cpp | 34 +++++-- Plugins/PluginFolderInfo/FolderInfo.h | 14 +-- Plugins/PluginFolderInfo/FolderInfoPlugin.cpp | 91 ++++++++++--------- 3 files changed, 77 insertions(+), 62 deletions(-) diff --git a/Plugins/PluginFolderInfo/FolderInfo.cpp b/Plugins/PluginFolderInfo/FolderInfo.cpp index deccadee..b82fae2c 100644 --- a/Plugins/PluginFolderInfo/FolderInfo.cpp +++ b/Plugins/PluginFolderInfo/FolderInfo.cpp @@ -23,9 +23,9 @@ #define UPDATE_TIME_MIN_MS 10000 -CFolderInfo::CFolderInfo(LPCWSTR path) : +CFolderInfo::CFolderInfo(void* ownerSkin) : m_InstanceCount(1), - m_Path(path), + m_Skin(ownerSkin), m_IncludeSubFolders(false), m_IncludeHiddenFiles(false), m_IncludeSystemFiles(false), @@ -160,19 +160,33 @@ void CFolderInfo::CalculateSize() } } +void CFolderInfo::SetPath(LPCWSTR path) +{ + if (wcscmp(m_Path.c_str(), path) != 0) + { + m_Path = path; + + // Force update next time + m_LastUpdateTime = 0; + } +} + void CFolderInfo::SetRegExpFilter(LPCWSTR filter) { FreePcre(); - int filterLen = wcslen(filter) + 1; - int bufLen = WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, NULL, 0, NULL, NULL); + if (*filter) + { + int filterLen = wcslen(filter) + 1; + int bufLen = WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, NULL, 0, NULL, NULL); - char* buf = new char[bufLen]; - WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, buf, bufLen, NULL, NULL); + char* buf = new char[bufLen]; + WideCharToMultiByte(CP_UTF8, 0, filter, filterLen, buf, bufLen, NULL, NULL); - const char* error; - int erroffset; - m_RegExpFilter = pcre_compile(buf, PCRE_UTF8, &error, &erroffset, NULL); + const char* error; + int erroffset; + m_RegExpFilter = pcre_compile(buf, PCRE_UTF8, &error, &erroffset, NULL); - delete [] buf; + delete [] buf; + } } diff --git a/Plugins/PluginFolderInfo/FolderInfo.h b/Plugins/PluginFolderInfo/FolderInfo.h index d91a53c4..b1f3415a 100644 --- a/Plugins/PluginFolderInfo/FolderInfo.h +++ b/Plugins/PluginFolderInfo/FolderInfo.h @@ -27,21 +27,20 @@ class CFolderInfo { public: - CFolderInfo(LPCWSTR path); + CFolderInfo(void* ownerSkin); ~CFolderInfo(); void AddInstance(); void RemoveInstance(); + void* GetSkin() { return m_Skin; } DWORD GetLastUpdateTime() { return m_LastUpdateTime; } + void SetPath(LPCWSTR path); void SetRegExpFilter(LPCWSTR filter); - - void IncludeSubFolders(bool flag) { m_IncludeSubFolders = flag; } - void IncludeHiddenFiles(bool flag) { m_IncludeHiddenFiles = flag; } - void IncludeSystemFiles(bool flag) { m_IncludeSystemFiles = flag; } - - LPCWSTR GetPath() { return m_Path.c_str(); } + void SetSubFolders(bool flag) { m_IncludeSubFolders = flag; } + void SetHiddenFiles(bool flag) { m_IncludeHiddenFiles = flag; } + void SetSystemFiles(bool flag) { m_IncludeSystemFiles = flag; } UINT64 GetSize() { return m_Size; } int GetFileCount() { return m_FileCount; } @@ -55,6 +54,7 @@ private: void CalculateSize(); UINT m_InstanceCount; + void* m_Skin; CRawString m_Path; bool m_IncludeSubFolders; diff --git a/Plugins/PluginFolderInfo/FolderInfoPlugin.cpp b/Plugins/PluginFolderInfo/FolderInfoPlugin.cpp index 1ec2442a..4963a615 100644 --- a/Plugins/PluginFolderInfo/FolderInfoPlugin.cpp +++ b/Plugins/PluginFolderInfo/FolderInfoPlugin.cpp @@ -35,11 +35,13 @@ struct MeasureData LPCWSTR section; CFolderInfo* folder; MeasureType type; + bool parent; MeasureData(LPCWSTR section) : section(section), folder(), - type(MEASURE_FILECOUNT) + type(MEASURE_FILECOUNT), + parent(false) { } }; @@ -51,58 +53,41 @@ PLUGIN_EXPORT void Initialize(void** data, void* rm) MeasureData* measure = new MeasureData(RmGetMeasureName(rm)); *data = measure; g_Measures.push_back(measure); + + void* skin = RmGetSkin(rm); + + LPCWSTR str = RmReadString(rm, L"Folder", L"", FALSE); + if (*str == L'[') + { + int len = wcslen(str); + for (auto iter = g_Measures.cbegin(); iter != g_Measures.cend(); ++iter) + { + if ((*iter)->folder && + (*iter)->folder->GetSkin() == skin && + wcsncmp(&str[1], (*iter)->section, len - 2) == 0) + { + measure->folder = (*iter)->folder; + measure->folder->AddInstance(); + return; + } + } + } + + measure->folder = new CFolderInfo(skin); + measure->parent = true; } PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue) { MeasureData* measure = (MeasureData*)data; + CFolderInfo* folder = measure->folder; - LPCWSTR str = RmReadString(rm, L"Folder", L"", FALSE); - if (*str == L'[') + if (!folder) { - CFolderInfo* oldFolder = measure->folder; - measure->folder = NULL; - - int len = wcslen(str); - for (auto iter = g_Measures.cbegin(); iter != g_Measures.cend(); ++iter) - { - if (wcsncmp(&str[1], (*iter)->section, len - 2) == 0) - { - measure->folder = (*iter)->folder; - measure->folder->AddInstance(); - } - } - - if (oldFolder) - { - oldFolder->RemoveInstance(); - } - } - else if (*str) - { - LPCWSTR path = RmPathToAbsolute(rm, str); - if (!measure->folder || wcscmp(measure->folder->GetPath(), path) != 0) - { - if (measure->folder) - { - measure->folder->RemoveInstance(); - } - - measure->folder = new CFolderInfo(path); - - str = RmReadString(rm, L"RegExpFilter", L""); - if (*str) - { - measure->folder->SetRegExpFilter(str); - } - - measure->folder->IncludeSubFolders((bool)RmReadInt(rm, L"IncludeSubFolders", 0)); - measure->folder->IncludeHiddenFiles((bool)RmReadInt(rm, L"IncludeHiddenFiles", 0)); - measure->folder->IncludeSystemFiles((bool)RmReadInt(rm, L"IncludeSystemFiles", 0)); - } + return; } - str = RmReadString(rm, L"InfoType", L""); + LPCWSTR str = RmReadString(rm, L"InfoType", L""); if (_wcsicmp(str, L"FolderSize") == 0 || _wcsicmp(str, L"FolderSizeStr") == 0) { measure->type = MEASURE_FOLDERSIZE; @@ -115,6 +100,19 @@ PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue) { measure->type = MEASURE_FILECOUNT; } + + if (measure->parent) + { + str = RmReadPath(rm, L"Folder", L""); + folder->SetPath(str); + + str = RmReadString(rm, L"RegExpFilter", L""); + folder->SetRegExpFilter(str); + + folder->SetSubFolders(RmReadInt(rm, L"IncludeSubFolders", 0) == 1); + folder->SetHiddenFiles(RmReadInt(rm, L"IncludeHiddenFiles", 0) == 1); + folder->SetSystemFiles(RmReadInt(rm, L"IncludeSystemFiles", 0) == 1); + } } PLUGIN_EXPORT double Update(void* data) @@ -125,7 +123,10 @@ PLUGIN_EXPORT double Update(void* data) return 0.0; } - measure->folder->Update(); + if (measure->parent) + { + measure->folder->Update(); + } switch (measure->type) {