[ttssh2-commit] [9414] GetDefaultFNameW() で _SHGetKnownFolderPath() を使用するよう修正

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 9月 12日 (日) 01:13:02 JST


Revision: 9414
          https://osdn.net/projects/ttssh2/scm/svn/commits/9414
Author:   zmatsuo
Date:     2021-09-12 01:13:02 +0900 (Sun, 12 Sep 2021)
Log Message:
-----------
GetDefaultFNameW() で _SHGetKnownFolderPath() を使用するよう修正

- GetDefaultFName() を GetDefaultFNameW() のラッパ関数にした

Modified Paths:
--------------
    trunk/teraterm/common/ttlib.c
    trunk/teraterm/common/ttlib_static_cpp.cpp

-------------- next part --------------
Modified: trunk/teraterm/common/ttlib.c
===================================================================
--- trunk/teraterm/common/ttlib.c	2021-09-11 16:12:53 UTC (rev 9413)
+++ trunk/teraterm/common/ttlib.c	2021-09-11 16:13:02 UTC (rev 9414)
@@ -830,35 +830,13 @@
 
 void GetDefaultFName(const char *home, const char *file, char *dest, int destlen)
 {
-	// My Documents \x82\xC9 file \x82\xAA\x82\xA0\x82\xE9\x8Fꍇ\x81A
-	// \x82\xBB\x82\xEA\x82\xF0\x93ǂݍ\x9E\x82ނ悤\x82ɂ\xB5\x82\xBD\x81B(2007.2.18 maya)
-	char MyDoc[MAX_PATH];
-	char MyDocSetupFName[MAX_PATH];
-	LPITEMIDLIST pidl;
-
-	IMalloc *pmalloc;
-	SHGetMalloc(&pmalloc);
-	if (SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl) == S_OK) {
-		SHGetPathFromIDList(pidl, MyDoc);
-		pmalloc->lpVtbl->Free(pmalloc, pidl);
-		pmalloc->lpVtbl->Release(pmalloc);
-	}
-	else {
-		pmalloc->lpVtbl->Release(pmalloc);
-		goto homedir;
-	}
-	strncpy_s(MyDocSetupFName, sizeof(MyDocSetupFName), MyDoc, _TRUNCATE);
-	AppendSlash(MyDocSetupFName,sizeof(MyDocSetupFName));
-	strncat_s(MyDocSetupFName, sizeof(MyDocSetupFName), file, _TRUNCATE);
-	if (GetFileAttributes(MyDocSetupFName) != INVALID_FILE_ATTRIBUTES) {
-		strncpy_s(dest, destlen, MyDocSetupFName, _TRUNCATE);
-		return;
-	}
-
-homedir:
-	strncpy_s(dest, destlen, home, _TRUNCATE);
-	AppendSlash(dest,destlen);
-	strncat_s(dest, destlen, file, _TRUNCATE);
+	wchar_t *homeW = ToWcharA(home);
+	wchar_t *fileW = ToWcharA(file);
+	wchar_t *destW = GetDefaultFNameW(homeW, fileW);
+	WideCharToACP_t(destW, dest, destlen);
+	free(destW);
+	free(fileW);
+	free(homeW);
 }
 
 /*

Modified: trunk/teraterm/common/ttlib_static_cpp.cpp
===================================================================
--- trunk/teraterm/common/ttlib_static_cpp.cpp	2021-09-11 16:12:53 UTC (rev 9413)
+++ trunk/teraterm/common/ttlib_static_cpp.cpp	2021-09-11 16:13:02 UTC (rev 9414)
@@ -914,32 +914,25 @@
 
 /**
  *	\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82̃t\x83\x8B\x83p\x83X\x82\xF0\x8E擾\x82\xB7\x82\xE9
- *	GetDefaultFName() \x82\xCC wchar_t\x94\xC5
  *
  *	@param[in]	home	ttermpro.exe \x93\x99\x82̎\xC0\x8Ds\x83t\x83@\x83C\x83\x8B\x82̂\xA0\x82\xE9\x83t\x83H\x83\x8B\x83_
  *						My Documents \x82Ƀt\x83@\x83C\x83\x8B\x82\xAA\x82\xA0\x82\xC1\x82\xBD\x8Fꍇ\x82͎g\x97p\x82\xB3\x82\xEA\x82Ȃ\xA2
- *	@param[in]	file	\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x96\xBC
- *	@return		\x83t\x83\x8B\x83p\x83X
+ *	@param[in]	file	\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x96\xBC(\x83p\x83X\x82͊܂܂Ȃ\xA2)
+ *	@return		\x83t\x83\x8B\x83p\x83X (\x95s\x97v\x82ɂȂ\xC1\x82\xBD\x82\xE7 free() \x82\xB7\x82邱\x82\xC6)
+ *
+ *	- My Documents \x82Ƀt\x83@\x83C\x83\x8B\x82\xAA\x82\xA0\x82\xC1\x82\xBD\x8Fꍇ,
+ *		- "%USERPROFILE%\My Documents\{file}"
+ *		- "%USERPROFILE%\Documents\{file}" \x82Ȃ\xC7
+ *	- \x82Ȃ\xA9\x82\xC1\x82\xBD\x8Fꍇ
+ *		- "{home}\{file}"
  */
 wchar_t *GetDefaultFNameW(const wchar_t *home, const wchar_t *file)
 {
 	// My Documents \x82\xC9 file \x82\xAA\x82\xA0\x82\xE9\x8Fꍇ\x81A
 	// \x82\xBB\x82\xEA\x82\xF0\x93ǂݍ\x9E\x82ނ悤\x82ɂ\xB5\x82\xBD\x81B(2007.2.18 maya)
-	wchar_t MyDoc[MAX_PATH];
-	LPITEMIDLIST pidl;
+	wchar_t *MyDoc;
+	_SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &MyDoc);
 
-	IMalloc *pmalloc;
-	SHGetMalloc(&pmalloc);
-	if (SHGetSpecialFolderLocation(NULL, CSIDL_PERSONAL, &pidl) == S_OK) {
-		SHGetPathFromIDListW(pidl, MyDoc);
-		pmalloc->Free(pidl);
-		pmalloc->Release();
-	}
-	else {
-		pmalloc->Release();
-		MyDoc[0] = 0;
-	}
-
 	if (MyDoc[0] != 0) {
 		// My Documents \x82\xC9 file \x82\xAA\x82\xA0\x82邩?
 		size_t destlen = (wcslen(MyDoc) + wcslen(file) + 1 + 1) * sizeof(wchar_t);
@@ -947,12 +940,17 @@
 		wcscpy(dest, MyDoc);
 		AppendSlashW(dest,destlen);
 		wcsncat_s(dest, destlen, file, _TRUNCATE);
-		if (GetFileAttributesW(dest) != INVALID_FILE_ATTRIBUTES) {
+		DWORD r = GetFileAttributesW(dest);
+		free(MyDoc);
+		if (r != INVALID_FILE_ATTRIBUTES) {
 			// My Documents \x82̐ݒ\xE8\x83t\x83@\x83C\x83\x8B
 			return dest;
 		}
 		free(dest);
 	}
+	else {
+		free(MyDoc);
+	}
 
 	size_t destlen = (wcslen(home) + wcslen(file) + 1 + 1) * sizeof(wchar_t);
 	wchar_t *dest = (wchar_t *)malloc(sizeof(wchar_t) * destlen);


ttssh2-commit メーリングリストの案内
Back to archive index