From e095cd0a755191d61f652ac400cf045d38f5b5d0 Mon Sep 17 00:00:00 2001
From: George Hazan <george.hazan@gmail.com>
Date: Sat, 13 Jun 2015 11:32:45 +0000
Subject: - fix for the UNC names in folders; - major code cleaning; - version
 bump

git-svn-id: http://svn.miranda-ng.org/main/trunk@14141 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
---
 plugins/Folders/src/commonheaders.h  |   5 +
 plugins/Folders/src/dlg_handlers.cpp |   5 +-
 plugins/Folders/src/events.h         |   5 -
 plugins/Folders/src/folderItem.cpp   |  21 ++--
 plugins/Folders/src/folderItem.h     |   6 +-
 plugins/Folders/src/services.cpp     |  50 ++--------
 plugins/Folders/src/services.h       |   8 +-
 plugins/Folders/src/utils.cpp        | 185 +++++------------------------------
 plugins/Folders/src/utils.h          |  15 +--
 plugins/Folders/src/version.h        |   2 +-
 10 files changed, 55 insertions(+), 247 deletions(-)

(limited to 'plugins/Folders')

diff --git a/plugins/Folders/src/commonheaders.h b/plugins/Folders/src/commonheaders.h
index ea5965af69..c7117b2a97 100644
--- a/plugins/Folders/src/commonheaders.h
+++ b/plugins/Folders/src/commonheaders.h
@@ -28,10 +28,15 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <windows.h>
 #include <richedit.h>
 
+#include <stdarg.h>
+#include <string.h>
+#include <malloc.h>
+
 #include <newpluginapi.h>
 #include <m_database.h>
 #include <m_system_cpp.h>
 #include <m_options.h>
+#include <m_string.h>
 
 #include <m_variables.h>
 #include <m_folders.h>
diff --git a/plugins/Folders/src/dlg_handlers.cpp b/plugins/Folders/src/dlg_handlers.cpp
index cbd03b44c2..ff347d9ba2 100644
--- a/plugins/Folders/src/dlg_handlers.cpp
+++ b/plugins/Folders/src/dlg_handlers.cpp
@@ -71,10 +71,9 @@ static void LoadRegisteredFolderItems(HWND hWnd)
 
 static void RefreshPreview(HWND hWnd)
 {
-	TCHAR tmp[MAX_FOLDER_SIZE], res[MAX_FOLDER_SIZE];
+	TCHAR tmp[MAX_FOLDER_SIZE];
 	GetEditText(hWnd, tmp, MAX_FOLDER_SIZE);
-	ExpandPath(res, tmp, MAX_FOLDER_SIZE);
-	SetDlgItemText(hWnd, IDC_PREVIEW_EDIT, res);
+	SetDlgItemText(hWnd, IDC_PREVIEW_EDIT, ExpandPath(tmp));
 }
 
 static void LoadItem(HWND hWnd, PFolderItem item)
diff --git a/plugins/Folders/src/events.h b/plugins/Folders/src/events.h
index 35dfa2fe8d..69a2cc44a5 100644
--- a/plugins/Folders/src/events.h
+++ b/plugins/Folders/src/events.h
@@ -21,14 +21,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #ifndef M_FOLDERS_EVENTS_H
 #define M_FOLDERS_EVENTS_H
 
-#include "commonheaders.h"
-#include "m_folders.h"
-
 int InitEvents();
 int DestroyEvents();
 
 int CallPathChangedEvents();
 
-
-
 #endif //M_FOLDERS_EVENTS_H
\ No newline at end of file
diff --git a/plugins/Folders/src/folderItem.cpp b/plugins/Folders/src/folderItem.cpp
index 5647d03250..a66984ab7e 100644
--- a/plugins/Folders/src/folderItem.cpp
+++ b/plugins/Folders/src/folderItem.cpp
@@ -70,9 +70,9 @@ int CFolderItem::operator ==(const CFolderItem *other)
 	return IsEqual(other);
 }
 
-void CFolderItem::Expand(TCHAR *buffer, int size)
+CMString CFolderItem::Expand()
 {
-	ExpandPath(buffer, m_tszFormat, size);
+	return ExpandPath(m_tszFormat);
 }
 
 void CFolderItem::Save()
@@ -87,8 +87,7 @@ int CFolderItem::FolderCreateDirectory(int showFolder)
 	if (m_tszFormat == NULL)
 		return FOLDER_SUCCESS;
 
-	TCHAR buffer[MAX_FOLDER_SIZE];
-	ExpandPath(buffer, m_tszFormat, SIZEOF(buffer));
+	CMString buffer(ExpandPath(m_tszFormat));
 	CreateDirectoryTreeT(buffer);
 	if (showFolder)
 		ShellExecute(NULL, L"explore", buffer, NULL, NULL, SW_SHOW);
@@ -104,8 +103,7 @@ int CFolderItem::FolderDeleteOldDirectory(int showFolder)
 	if (!mir_tstrcmp(m_tszFormat, m_tszOldFormat)) //format wasn't changed
 		return FOLDER_SUCCESS;
 
-	TCHAR buffer[MAX_FOLDER_SIZE];
-	ExpandPath(buffer, m_tszOldFormat, SIZEOF(buffer));
+	CMString buffer(ExpandPath(m_tszOldFormat));
 	RemoveDirectories(buffer);
 	int res = (DirectoryExists(buffer)) ? FOLDER_FAILURE : FOLDER_SUCCESS;
 	if ((res == FOLDER_FAILURE) && (showFolder))
@@ -115,13 +113,12 @@ int CFolderItem::FolderDeleteOldDirectory(int showFolder)
 
 void CFolderItem::GetDataFromDatabase(const TCHAR *szNotFound)
 {
-	char name[256];
-	strcpy_s(name, sizeof(name), m_szSection);
-	strcat_s(name, sizeof(name), m_szName);
+	char szSettingName[256];
+	strcpy_s(szSettingName, SIZEOF(szSettingName), m_szSection);
+	strcat_s(szSettingName, SIZEOF(szSettingName), m_szName);
 
-	TCHAR buffer[MAX_FOLDER_SIZE];
-	GetStringFromDatabase(name, szNotFound, buffer, SIZEOF(buffer));
-	SetFormat(buffer);
+	ptrT tszValue(db_get_tsa(NULL, ModuleName, szSettingName));
+	SetFormat(tszValue != NULL ? tszValue : szNotFound);
 }
 
 void CFolderItem::WriteDataToDatabase()
diff --git a/plugins/Folders/src/folderItem.h b/plugins/Folders/src/folderItem.h
index 166017b476..ded5c12a6f 100644
--- a/plugins/Folders/src/folderItem.h
+++ b/plugins/Folders/src/folderItem.h
@@ -23,10 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #ifndef M_FOLDERS_FOLDER_ITEM_H
 #define M_FOLDERS_FOLDER_ITEM_H
 
-#include <string.h>
-#include <malloc.h>
-#include <windows.h>
-
 #define FOLDERS_NO_HELPER_FUNCTIONS
 #include "m_folders.h"
 #undef FOLDERS_NO_HELPER_FUNCTIONS
@@ -49,7 +45,7 @@ public:
 	CFolderItem(const char *sectionName, const char *name, const TCHAR *format, const TCHAR *userName);
 	virtual ~CFolderItem();
 
-	void Expand(TCHAR *buffer, int size);
+	CMString Expand();
 	void Save();
 
 	int IsEqual(const CFolderItem *other);
diff --git a/plugins/Folders/src/services.cpp b/plugins/Folders/src/services.cpp
index 4447fdece5..c09446f9d7 100644
--- a/plugins/Folders/src/services.cpp
+++ b/plugins/Folders/src/services.cpp
@@ -27,34 +27,6 @@ TCHAR szCurrentProfile[MAX_FOLDERS_PATH];
 TCHAR szMirandaPath[MAX_FOLDERS_PATH];
 TCHAR szUserDataPath[MAX_FOLDERS_PATH];
 
-INT_PTR ExpandPath(TCHAR *szResult, TCHAR *format, int size)
-{
-	szResult[0] = '\0';
-
-	TCHAR *input = NULL;
-	if (ServiceExists(MS_VARS_FORMATSTRING))
-		input = variables_parse(format, NULL, NULL);
-
-	if (input == NULL)
-		input = mir_tstrdup(format);
-
-	TCHAR *core_result = Utils_ReplaceVarsT(input);
-	_tcsncpy(szResult, core_result, size);
-
-	mir_free(core_result);
-
-	StrReplace(szResult, PROFILE_PATHT, szCurrentProfilePath);
-	StrReplace(szResult, CURRENT_PROFILET, szCurrentProfile);
-	StrReplace(szResult, MIRANDA_PATHT, szMirandaPath);
-	StrReplace(szResult, MIRANDA_USERDATAT, szUserDataPath);
-
-	StrTrim(szResult, _T("\t \\"));
-
-	mir_free(input);
-
-	return mir_tstrlen(szResult);
-}
-
 INT_PTR RegisterPathService(WPARAM, LPARAM lParam)
 {
 	FOLDERSDATA *data = (FOLDERSDATA*)lParam;
@@ -76,15 +48,8 @@ INT_PTR RegisterPathService(WPARAM, LPARAM lParam)
 
 INT_PTR GetPathSizeService(WPARAM wParam, LPARAM lParam)
 {
-	size_t len;
-
 	CFolderItem *p = (CFolderItem*)wParam;
-	if (lstRegisteredFolders.getIndex(p) != -1) {
-		TCHAR tmp[MAX_FOLDER_SIZE];
-		p->Expand(tmp, SIZEOF(tmp));
-		len = mir_tstrlen(tmp);
-	}
-	else len = 0;
+	size_t len = (lstRegisteredFolders.getIndex(p) != -1) ? p->Expand().GetLength() : 0;
 
 	if (lParam != NULL)
 		*((size_t*)lParam) = len;
@@ -102,14 +67,11 @@ INT_PTR GetPathService(WPARAM wParam, LPARAM lParam)
 	if (data->cbSize != sizeof(FOLDERSGETDATA))
 		return 1;
 
-	if (data->flags & FF_UNICODE) {
-		p->Expand(data->szPathT, data->nMaxPathSize);
-		return 0;
-	}
-
-	TCHAR buf[MAX_FOLDER_SIZE];
-	p->Expand(buf, SIZEOF(buf));
-	strncpy(data->szPath, _T2A(buf), data->nMaxPathSize);
+	CMString buf(p->Expand());
+	if (data->flags & FF_UNICODE)
+		_tcsncpy_s(data->szPathT, data->nMaxPathSize, buf, _TRUNCATE);
+	else
+		strncpy_s(data->szPath, data->nMaxPathSize, _T2A(buf), _TRUNCATE);
 	return 0;
 }
 
diff --git a/plugins/Folders/src/services.h b/plugins/Folders/src/services.h
index 6b71f10d8e..6f1acaf1ca 100644
--- a/plugins/Folders/src/services.h
+++ b/plugins/Folders/src/services.h
@@ -21,20 +21,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #ifndef M_FOLDERS_PROVIDED_SERVICES_H
 #define M_FOLDERS_PROVIDED_SERVICES_H
 
-#include "commonheaders.h"
-#include "m_folders.h"
-
 #define MAX_FOLDERS_PATH 512
 
 extern TCHAR szCurrentProfilePath[MAX_FOLDERS_PATH];
 extern TCHAR szCurrentProfile[MAX_FOLDERS_PATH];
 extern TCHAR szMirandaPath[MAX_FOLDERS_PATH];
+extern TCHAR szUserDataPath[MAX_FOLDERS_PATH];
 
 int InitServices();
-
-INT_PTR ExpandPath(TCHAR *szResult, TCHAR *format, int size);
-INT_PTR GetPath(int hRegisteredFolder, TCHAR *szResult, int size);
-
 void InitOptions();
 
 #endif //M_FOLDERS_PROVIDED_SERVICES_H
\ No newline at end of file
diff --git a/plugins/Folders/src/utils.cpp b/plugins/Folders/src/utils.cpp
index e41afda394..60bbd4142c 100644
--- a/plugins/Folders/src/utils.cpp
+++ b/plugins/Folders/src/utils.cpp
@@ -20,144 +20,34 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "commonheaders.h"
 
-char *StrCopy(char *source, size_t index, const char *what, size_t count)
+CMString ExpandPath(const TCHAR *format)
 {
-	for (size_t i = 0; i < count; i++)
-		source[index + i] = what[i];
-
-	return source;
-}
-
-wchar_t *StrCopy(wchar_t *source, size_t index, const wchar_t *what, size_t count)
-{
-	for (size_t i = 0; i < count; i++)
-		source[index + i] = what[i];
-
-	return source;
-}
-
-char *StrDelete(char *source, size_t index, size_t count)
-{
-	size_t len = mir_strlen(source);
-	size_t i;
-	count = (count + index > len) ? len - index : count;
-	for (i = index; i + count <= len; i++)
-		source[i] = source[i + count];
-
-	return source;
-}
-
-wchar_t *StrDelete(wchar_t *source, size_t index, size_t count)
-{
-	size_t len = mir_wstrlen(source);
-	count = (count + index > len) ? len - index : count;
-	for (size_t i = index; i + count <= len; i++)
-		source[i] = source[i + count];
-
-	return source;
-}
-
-char *StrInsert(char *source, size_t index, const char *what)
-{
-	size_t whatLen = mir_strlen(what);
-	size_t sourceLen = mir_strlen(source);
-	size_t i;
-	for (i = sourceLen; i >= index; i--)
-		source[i + whatLen] = source[i];
-
-	for (i = 0; i < whatLen; i++)
-		source[index + i] = what[i];
-
-	return source;
-}
-
-wchar_t *StrInsert(wchar_t *source, size_t index, const wchar_t *what)
-{
-	size_t whatLen = mir_wstrlen(what);
-	size_t sourceLen = mir_wstrlen(source);
-	size_t i;
-	for (i = sourceLen; i >= index; i--)
-		source[i + whatLen] = source[i];
-
-	for (i = 0; i < whatLen; i++)
-		source[index + i] = what[i];
-
-	return source;
-}
-
-char *StrReplace(char *source, const char *what, const char *withWhat)
-{
-	size_t whatLen = mir_strlen(what);
-	size_t withWhatLen = mir_strlen(withWhat);
-
-	char *pos;
-	while ((pos = strstr(source, what))) {
-		size_t minLen = min(whatLen, withWhatLen);
-		StrCopy(source, pos - source, withWhat, minLen);
-		size_t index = pos - source + minLen;
-		if (whatLen > withWhatLen)
-			StrDelete(source, index, whatLen - withWhatLen);
-		else {
-			if (whatLen < withWhatLen)
-				StrInsert(source, index, withWhat + minLen);
+	CMString res;
+
+	if (ServiceExists(MS_VARS_FORMATSTRING))
+		res = VARST(ptrT(variables_parse((TCHAR*)format, NULL, NULL)));
+	else
+		res = VARST(format);
+
+	res.Replace(PROFILE_PATHT, szCurrentProfilePath);
+	res.Replace(CURRENT_PROFILET, szCurrentProfile);
+	res.Replace(MIRANDA_PATHT, szMirandaPath);
+	res.Replace(MIRANDA_USERDATAT, szUserDataPath);
+	res.Trim();
+
+	// also remove the trailing slash
+	if (!res.IsEmpty()) {
+		int iNewSize = res.GetLength() - 1;
+		switch (res[iNewSize]) {
+		case '\\': case '/':
+			res.Truncate(iNewSize);
 		}
 	}
-	return source;
-}
-
-wchar_t *StrReplace(wchar_t *source, const wchar_t *what, const wchar_t *withWhat)
-{
-	size_t whatLen = mir_wstrlen(what);
-	size_t withWhatLen = mir_wstrlen(withWhat);
 
-	wchar_t *pos;
-	while ((pos = wcsstr(source, what))) {
-		size_t minLen = min(whatLen, withWhatLen);
-		StrCopy(source, pos - source, withWhat, minLen);
-		size_t index = pos - source + minLen;
-		if (whatLen > withWhatLen)
-			StrDelete(source, index, whatLen - withWhatLen);
-		else {
-			if (whatLen < withWhatLen)
-				StrInsert(source, index, withWhat + minLen);
-		}
-	}
-	return source;
-}
-
-char *StrTrim(char *szText, const char *szTrimChars)
-{
-	size_t i = mir_strlen(szText) - 1;
-	while (strchr(szTrimChars, szText[i]))
-		szText[i--] = '\0';
-
-	i = 0;
-	while ((i < mir_strlen(szText)) && (strchr(szTrimChars, szText[i])))
-		i++;
-
-	if (i)
-		StrDelete(szText, 0, i);
-
-	return szText;
-}
-
-wchar_t *StrTrim(wchar_t *szText, const wchar_t *szTrimChars)
-{
-	size_t i = mir_wstrlen(szText) - 1;
-	while (wcschr(szTrimChars, szText[i]))
-		szText[i--] = '\0';
-
-	i = 0;
-	while ((i < mir_wstrlen(szText)) && (wcschr(szTrimChars, szText[i])))
-		i++;
-
-	if (i)
-		StrDelete(szText, 0, i);
-
-	return szText;
+	return res;
 }
 
-void RemoveDirectories(TCHAR *path)
+void RemoveDirectories(const TCHAR *path)
 {
 	TCHAR *pos;
 	TCHAR *buffer = NEWWSTR_ALLOCA(path);
@@ -170,31 +60,10 @@ void RemoveDirectories(TCHAR *path)
 	}
 }
 
-int DirectoryExists(TCHAR *path)
+bool DirectoryExists(const TCHAR *path)
 {
-	TCHAR buffer[4096];
-	GetCurrentDirectory(SIZEOF(buffer), buffer);
-	int res = SetCurrentDirectory(path);
-	SetCurrentDirectory(buffer);
-	return res;
-}
-
-int GetStringFromDatabase(char *szSettingName, const wchar_t *szError, TCHAR *szResult, size_t size)
-{
-	size_t len;
-	DBVARIANT dbv;
-	if (db_get_ws(NULL, ModuleName, szSettingName, &dbv) == 0) {
-		size_t tmp = mir_tstrlen(dbv.ptszVal);
-		len = (tmp < size - 1) ? tmp : size - 1;
-		_tcsncpy(szResult, dbv.ptszVal, len);
-		szResult[len] = '\0';
-		db_free(&dbv);
-		return 0;
-	}
-
-	size_t tmp = mir_tstrlen(szError);
-	len = (tmp < size - 1) ? tmp : size - 1;
-	_tcsncpy(szResult, szError, len);
-	szResult[len] = '\0';
-	return 1;
+	DWORD dwAttributes = GetFileAttributes(path);
+	if (dwAttributes == INVALID_FILE_ATTRIBUTES || !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
+		return false;
+	return true;
 }
diff --git a/plugins/Folders/src/utils.h b/plugins/Folders/src/utils.h
index 987b422e08..3307335e14 100644
--- a/plugins/Folders/src/utils.h
+++ b/plugins/Folders/src/utils.h
@@ -21,18 +21,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #ifndef M_FOLDERS_UTILS_H
 #define M_FOLDERS_UTILS_H
 
-#include <stdarg.h>
-#include "commonheaders.h"
+CMString ExpandPath(const TCHAR *format);
 
-TCHAR *StrReplace(TCHAR *source, const TCHAR *what, const TCHAR *withWhat);
-TCHAR *StrCopy(TCHAR *source, size_t index, const TCHAR *what, size_t count);
-TCHAR *StrDelete(TCHAR *source, size_t index, size_t count);
-TCHAR *StrInsert(TCHAR *source, size_t index, const TCHAR *what);
-TCHAR *StrTrim(TCHAR *szText, const TCHAR *szTrimChars);
-
-void RemoveDirectories(TCHAR *szPath);
-int DirectoryExists(TCHAR *szPath);
-
-int GetStringFromDatabase(char *szSettingName, const TCHAR *szError, TCHAR *szResult, size_t size);
+void RemoveDirectories(const TCHAR *szPath);
+bool DirectoryExists(const TCHAR *szPath);
 
 #endif
\ No newline at end of file
diff --git a/plugins/Folders/src/version.h b/plugins/Folders/src/version.h
index df007f3098..4644966547 100644
--- a/plugins/Folders/src/version.h
+++ b/plugins/Folders/src/version.h
@@ -1,7 +1,7 @@
 #define __MAJOR_VERSION          0
 #define __MINOR_VERSION          2
 #define __RELEASE_NUM            0
-#define __BUILD_NUM              1
+#define __BUILD_NUM              2
 
 #include <stdver.h>
 
-- 
cgit v1.2.3