[Ttssh2-commit] [9307] layer_for_unicode に API 追加

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 6月 13日 (日) 00:29:41 JST


Revision: 9307
          https://osdn.net/projects/ttssh2/scm/svn/commits/9307
Author:   zmatsuo
Date:     2021-06-13 00:29:41 +0900 (Sun, 13 Jun 2021)
Log Message:
-----------
layer_for_unicode に API 追加

- 追加
  - _GetModuleHandleW()
  - _GetSystemDirectoryW()
  - _GetTempFileNameW()
  - _GetTempPathW()

Modified Paths:
--------------
    trunk/teraterm/common/codeconv.cpp
    trunk/teraterm/common/codeconv.h
    trunk/teraterm/common/compat_win.cpp
    trunk/teraterm/common/compat_win.h
    trunk/teraterm/common/layer_for_unicode.cpp
    trunk/teraterm/common/layer_for_unicode.h

-------------- next part --------------
Modified: trunk/teraterm/common/codeconv.cpp
===================================================================
--- trunk/teraterm/common/codeconv.cpp	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/codeconv.cpp	2021-06-12 15:29:41 UTC (rev 9307)
@@ -1056,6 +1056,27 @@
 	}
 }
 
+/**
+ *	MultiByteToWideChar() \x82\xCC TRUNCATE + CP_ACP \x94\xC5
+ *	wchar_t \x82̃p\x83X,\x83t\x83@\x83C\x83\x8B\x96\xBC\x82\xF0 char \x82ɕϊ\xB7\x82\xB7\x82\xE9\x82Ƃ\xAB\x82Ɏg\x97p
+ *
+ *	@param[in]		str_ptr		char \x82̕\xB6\x8E\x9A\x97\xF1
+ *	@param[in,out]	wstr_ptr	wchar_t \x95\xB6\x8E\x9A\x97\xF1\x8Fo\x97͐\xE6ptr
+ *	@param[in]		wstr_len	wchar_t \x95\xB6\x8E\x9A\x97\xF1\x8Fo\x97͐敶\x8E\x9A\x90\x94
+ *	@return			\x95ϊ\xB7\x82\xB5\x82\xBD\x95\xB6\x8E\x9A\x90\x94(L'^0'\x8A܂\xDE), wcslen(wstr_ptr) + 1
+ */
+size_t ACPToWideChar_t(const char *str_ptr, wchar_t *wstr_ptr, size_t wstr_len)
+{
+	const DWORD flags = 0;
+	size_t out_len = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
+										 str_ptr, -1,
+										 wstr_ptr, wstr_len);
+	if (out_len == wstr_len) {
+		wstr_ptr[wstr_len-1] = 0;
+	}
+	return out_len;
+}
+
 char *ToCharW(const wchar_t *strW)
 {
 	if (strW == NULL) return NULL;

Modified: trunk/teraterm/common/codeconv.h
===================================================================
--- trunk/teraterm/common/codeconv.h	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/codeconv.h	2021-06-12 15:29:41 UTC (rev 9307)
@@ -58,6 +58,7 @@
 					 char32_t *u32_ptr, size_t *u32_len_);
 int UTF8ToWideChar(const char *u8_ptr, int u8_len, wchar_t *wstr_ptr, int wstr_len);
 void WideCharToACP_t(const wchar_t *wstr_ptr, char *mb_ptr, size_t mb_len);
+size_t ACPToWideChar_t(const char *str_ptr, wchar_t *wstr_ptr, size_t wstr_len);
 
 // API wrappers
 char *_WideCharToMultiByte(const wchar_t *wstr_ptr, size_t wstr_len, int code_page, size_t *mb_len_);

Modified: trunk/teraterm/common/compat_win.cpp
===================================================================
--- trunk/teraterm/common/compat_win.cpp	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/compat_win.cpp	2021-06-12 15:29:41 UTC (rev 9307)
@@ -38,7 +38,7 @@
 #include "ttlib.h"
 
 // for debug
-//#define UNICODE_API_DISABLE	1
+#define UNICODE_API_DISABLE	1
 
 ATOM (WINAPI *pRegisterClassW)(const WNDCLASSW *lpWndClass);
 HWND (WINAPI *pCreateWindowExW)(DWORD dwExStyle, LPCWSTR lpClassName, LPCWSTR lpWindowName, DWORD dwStyle, int X, int Y, int nWidth, int nHeight,
@@ -84,6 +84,7 @@
 DWORD (WINAPI *pGetPrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
 										 LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
 BOOL (WINAPI *pWritePrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpString, LPCWSTR lpFileName);
+UINT (WINAPI *pGetPrivateProfileIntW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault, LPCWSTR lpFileName);
 BOOL (WINAPI *pCreateProcessW)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
 							   LPSECURITY_ATTRIBUTES lpProcessAttributes,
 							   LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
@@ -102,6 +103,8 @@
 HMODULE (WINAPI *pLoadLibraryW)(LPCWSTR lpLibFileName);
 DWORD (WINAPI *pGetModuleFileNameW)(HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
 DWORD (WINAPI *pExpandEnvironmentStringsW)(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize);
+DWORD (WINAPI *pGetTempPathW)(DWORD nBufferLength, LPWSTR lpBuffer);
+UINT (WINAPI *pGetTempFileNameW)(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName);
 
 // gdi32
 int (WINAPI *pAddFontResourceExW)(LPCWSTR name, DWORD fl, PVOID res);
@@ -251,6 +254,7 @@
 	{ "SetCurrentDirectoryW", (void **)&pSetCurrentDirectoryW },
 	{ "GetPrivateProfileStringW", (void **)&pGetPrivateProfileStringW },
 	{ "WritePrivateProfileStringW", (void **)&pWritePrivateProfileStringW },
+	{ "GetPrivateProfileIntW", (void **)&pGetPrivateProfileIntW },
 	{ "CreateProcessW", (void **)&pCreateProcessW },
 	{ "CopyFileW", (void **)&pCopyFileW },
 	{ "DeleteFileW", (void **)&pDeleteFileW },
@@ -263,6 +267,8 @@
 	{ "LoadLibraryW", (void **)&pLoadLibraryW },
 	{ "GetModuleFileNameW", (void **)&pGetModuleFileNameW },
 	{ "ExpandEnvironmentStringsW", (void **)&pExpandEnvironmentStringsW },
+	{ "GetTempPathW", (void **)&pGetTempPathW },
+	{ "GetTempFileNameW", (void **)&pGetTempFileNameW },
 #endif
 	{ "GetConsoleWindow", (void **)&pGetConsoleWindow },
 	{},
@@ -363,7 +369,11 @@
 		pFindFirstFileW = NULL;
 		pFindNextFileW = NULL;
 		pRemoveDirectoryW = NULL;
+		pGetPrivateProfileIntW = NULL;
 		pGetModuleFileNameW = NULL;
+		pLoadLibraryW = NULL;
+		pGetTempPathW = NULL;
+		pGetTempFileNameW = NULL;
 	}
 
 	// GetConsoleWindow\x93\xC1\x95ʏ\x88\x97\x9D

Modified: trunk/teraterm/common/compat_win.h
===================================================================
--- trunk/teraterm/common/compat_win.h	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/compat_win.h	2021-06-12 15:29:41 UTC (rev 9307)
@@ -79,8 +79,6 @@
 extern BOOL(WINAPI *pModifyMenuW)(HMENU hMnu, UINT uPosition, UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem);
 extern int(WINAPI *pGetMenuStringW)(HMENU hMenu, UINT uIDItem, LPWSTR lpString, int cchMax, UINT flags);
 extern BOOL(WINAPI *pSetWindowTextW)(HWND hWnd, LPCWSTR lpString);
-extern DWORD(WINAPI *pGetPrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
-												LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
 extern UINT(WINAPI *pDragQueryFileW)(HDROP hDrop, UINT iFile, LPWSTR lpszFile, UINT cch);
 extern BOOL (WINAPI *pSetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPCWSTR lpString);
 extern BOOL (WINAPI *pGetDlgItemTextW)(HWND hDlg, int nIDDlgItem, LPWSTR lpString, int cchMax);
@@ -128,6 +126,7 @@
 extern DWORD (WINAPI *pGetPrivateProfileStringW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
 										 LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
 extern BOOL (WINAPI *pWritePrivateProfileStringW)(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpString,LPCWSTR lpFileName);
+extern UINT (WINAPI *pGetPrivateProfileIntW)(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault, LPCWSTR lpFileName);
 extern BOOL (WINAPI *pCreateProcessW)(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
 									  LPSECURITY_ATTRIBUTES lpProcessAttributes,
 									  LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
@@ -160,6 +159,8 @@
 extern HMODULE (WINAPI *pLoadLibraryW)(LPCWSTR lpLibFileName);
 extern DWORD (WINAPI *pGetModuleFileNameW)(HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
 extern DWORD (WINAPI *pExpandEnvironmentStringsW)(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize);
+extern DWORD (WINAPI *pGetTempPathW)(DWORD nBufferLength, LPWSTR lpBuffer);
+extern UINT (WINAPI *pGetTempFileNameW)(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName);
 
 // shlobj_core.h
 extern LPITEMIDLIST (WINAPI *pSHBrowseForFolderW)(LPBROWSEINFOW lpbi);

Modified: trunk/teraterm/common/layer_for_unicode.cpp
===================================================================
--- trunk/teraterm/common/layer_for_unicode.cpp	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/layer_for_unicode.cpp	2021-06-12 15:29:41 UTC (rev 9307)
@@ -683,6 +683,22 @@
 	return r;
 }
 
+UINT _GetPrivateProfileIntW(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault, LPCWSTR lpFileName)
+{
+	if (pGetPrivateProfileIntW != NULL) {
+		return pGetPrivateProfileIntW(lpAppName, lpKeyName, nDefault, lpFileName);
+	}
+
+	char *appA = ToCharW(lpAppName);
+	char *keyA = ToCharW(lpKeyName);
+	char *fileA = ToCharW(lpFileName);
+	UINT r = GetPrivateProfileIntA(appA, keyA, nDefault, fileA);
+	free(appA);
+	free(keyA);
+	free(fileA);
+	return r;
+}
+
 BOOL _CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
 					 LPSECURITY_ATTRIBUTES lpProcessAttributes,
 					 LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
@@ -887,11 +903,11 @@
 
 	char filenameA[MAX_PATH];
 	DWORD r = GetModuleFileNameA(hModule, filenameA, sizeof(filenameA));
-	wchar_t *bufW = ToWcharA(filenameA);
-	wcsncpy_s(lpFilename, nSize, bufW, _TRUNCATE);
-	r = (DWORD)wcslen(lpFilename);
-	free(bufW);
-	return r;
+	if (r == 0) {
+		return 0;
+	}
+	DWORD wlen = ACPToWideChar_t(filenameA, lpFilename, nSize);
+	return wlen - 1;	// not including the terminating null character
 }
 
 DWORD _ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize)
@@ -910,3 +926,53 @@
 	free(dstW);
 	return r;
 }
+
+HMODULE _GetModuleHandleW(LPCWSTR lpModuleName)
+{
+	char *lpStringA = ToCharW(lpModuleName);
+	HMODULE h = GetModuleHandleA(lpStringA);
+	free(lpStringA);
+	return h;
+}
+
+UINT _GetSystemDirectoryW(LPWSTR lpBuffer, UINT uSize)
+{
+	char buf[MAX_PATH];
+	UINT r = GetSystemDirectoryA(buf, _countof(buf));
+	if (r == 0) {
+		return 0;
+	}
+	size_t wlen = ACPToWideChar_t(buf, lpBuffer, uSize);
+	return wlen - 1;	// not including the terminating null character
+}
+
+DWORD _GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
+{
+	if (pGetTempPathW != NULL) {
+		return pGetTempPathW(nBufferLength, lpBuffer);
+	}
+
+	char buf[MAX_PATH];
+	DWORD r = GetTempPathA(_countof(buf), buf);
+	if (r == 0) {
+		return 0;
+	}
+	size_t wlen = ACPToWideChar_t(buf, lpBuffer, nBufferLength);
+	return wlen - 1;	// not including the terminating null character
+}
+
+UINT _GetTempFileNameW(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName)
+{
+	if (pGetTempFileNameW != NULL) {
+		return pGetTempFileNameW(lpPathName, lpPrefixString, uUnique, lpTempFileName);
+	}
+
+	char buf[MAX_PATH];
+	char *lpPathNameA = ToCharW(lpPathName);
+	char *lpPrefixStringA = ToCharW(lpPrefixString);
+	UINT r = GetTempFileNameA(lpPathNameA, lpPrefixStringA, uUnique, buf);
+	ACPToWideChar_t(buf, lpTempFileName, MAX_PATH);
+	free(lpPathNameA);
+	free(lpPrefixStringA);
+	return r;
+}

Modified: trunk/teraterm/common/layer_for_unicode.h
===================================================================
--- trunk/teraterm/common/layer_for_unicode.h	2021-06-12 15:29:32 UTC (rev 9306)
+++ trunk/teraterm/common/layer_for_unicode.h	2021-06-12 15:29:41 UTC (rev 9307)
@@ -121,6 +121,7 @@
 DWORD _GetPrivateProfileStringW(LPCWSTR lpAppName, LPCWSTR lpKeyName, LPCWSTR lpDefault,
 								LPWSTR lpReturnedString, DWORD nSize, LPCWSTR lpFileName);
 BOOL _WritePrivateProfileStringW(LPCWSTR lpAppName,LPCWSTR lpKeyName,LPCWSTR lpString,LPCWSTR lpFileName);
+UINT _GetPrivateProfileIntW(LPCWSTR lpAppName, LPCWSTR lpKeyName, INT nDefault, LPCWSTR lpFileName);
 BOOL _CreateProcessW(LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
 					 LPSECURITY_ATTRIBUTES lpProcessAttributes,
 					 LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles,
@@ -139,6 +140,10 @@
 HMODULE _LoadLibraryW(LPCWSTR lpLibFileName);
 DWORD _GetModuleFileNameW(HMODULE hModule, LPWSTR lpFilename, DWORD nSize);
 DWORD _ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst, DWORD nSize);
+HMODULE _GetModuleHandleW(LPCWSTR lpModuleName);
+UINT _GetSystemDirectoryW(LPWSTR lpBuffer, UINT uSize);
+DWORD _GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer);
+UINT _GetTempFileNameW(LPCWSTR lpPathName, LPCWSTR lpPrefixString, UINT uUnique, LPWSTR lpTempFileName);
 
 // gdi32.lib
 int _AddFontResourceW(LPCWSTR lpFileName);


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