[Ttssh2-commit] [9305] WIN32 API で文字列を動的に扱うヘルパを作成

アーカイブの一覧に戻る
scmno****@osdn***** scmno****@osdn*****
2021年 6月 13日 (日) 00:29:23 JST


Revision: 9305
          https://osdn.net/projects/ttssh2/scm/svn/commits/9305
Author:   zmatsuo
Date:     2021-06-13 00:29:23 +0900 (Sun, 13 Jun 2021)
Log Message:
-----------
WIN32 API で文字列を動的に扱うヘルパを作成

- 最大長を気にしなくてもよい
- 文字列が不要になったらfree()する
- ヘルパAPI
  - GetModuleFileNameW()
  - GetPrivateProfileStringW()
  - GetFullPathNameW()

Modified Paths:
--------------
    trunk/teraterm/common/CMakeLists.txt
    trunk/teraterm/common/common_static.v16.vcxproj
    trunk/teraterm/common/common_static.v8.vcproj

Added Paths:
-----------
    trunk/teraterm/common/win32helper.cpp
    trunk/teraterm/common/win32helper.h

-------------- next part --------------
Modified: trunk/teraterm/common/CMakeLists.txt
===================================================================
--- trunk/teraterm/common/CMakeLists.txt	2021-06-12 15:29:15 UTC (rev 9304)
+++ trunk/teraterm/common/CMakeLists.txt	2021-06-12 15:29:23 UTC (rev 9305)
@@ -41,6 +41,8 @@
   ttlib_static_cpp.cpp
   win16api.c
   win16api.h
+  win32helper.cpp
+  win32helper.h
   )
 
 target_include_directories(

Modified: trunk/teraterm/common/common_static.v16.vcxproj
===================================================================
--- trunk/teraterm/common/common_static.v16.vcxproj	2021-06-12 15:29:15 UTC (rev 9304)
+++ trunk/teraterm/common/common_static.v16.vcxproj	2021-06-12 15:29:23 UTC (rev 9305)
@@ -152,6 +152,7 @@
     <ClCompile Include="ttlib_static.c" />
     <ClCompile Include="ttlib_static_cpp.cpp" />
     <ClCompile Include="win16api.c" />
+    <ClCompile Include="win32helper.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="asprintf.h" />
@@ -169,6 +170,7 @@
     <ClInclude Include="layer_for_unicode.h" />
     <ClInclude Include="ttlib.h" />
     <ClInclude Include="win16api.h" />
+    <ClInclude Include="win32helper.h" />
   </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">

Modified: trunk/teraterm/common/common_static.v8.vcproj
===================================================================
--- trunk/teraterm/common/common_static.v8.vcproj	2021-06-12 15:29:15 UTC (rev 9304)
+++ trunk/teraterm/common/common_static.v8.vcproj	2021-06-12 15:29:23 UTC (rev 9305)
@@ -312,6 +312,14 @@
 			RelativePath=".\win16api.h"
 			>
 		</File>
+		<File
+			RelativePath=".\win32helper.cpp"
+			>
+		</File>
+		<File
+			RelativePath=".\win32helper.h"
+			>
+		</File>
 	</Files>
 	<Globals>
 	</Globals>

Added: trunk/teraterm/common/win32helper.cpp
===================================================================
--- trunk/teraterm/common/win32helper.cpp	                        (rev 0)
+++ trunk/teraterm/common/win32helper.cpp	2021-06-12 15:29:23 UTC (rev 9305)
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2021- TeraTerm Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <windows.h>
+#define _CRTDBG_MAP_ALLOC
+#include <stdlib.h>
+#include <crtdbg.h>
+
+#include "win32helper.h"
+
+/**
+ *	GetModuleFileNameW() \x82̓\xAE\x93I\x83o\x83b\x83t\x83@\x94\xC5
+ *
+ *	@param buf	\x95\xB6\x8E\x9A\x97\xF1\x82\xF0\x8Ai\x94[\x82\xB7\x82\xE9\x83o\x83b\x83t\x83@
+ *				\x95s\x97v\x82ɂȂ\xC1\x82\xBD\x82\xE7free()\x82\xB7\x82\xE9
+ *	@return	\x83G\x83\x89\x81[\x83R\x81[\x83h,0(=NO_ERROR)\x82̂Ƃ\xAB\x83G\x83\x89\x81[\x82Ȃ\xB5
+ */
+DWORD hGetModuleFileNameW(HMODULE hModule, wchar_t **buf)
+{
+	size_t size = MAX_PATH;
+	wchar_t *b = (wchar_t*)malloc(sizeof(wchar_t) * size);
+	DWORD error;
+	if (b == NULL) {
+		error = ERROR_NOT_ENOUGH_MEMORY;
+		goto error_return;
+	}
+
+	for(;;) {
+		DWORD r = GetModuleFileNameW(hModule, b, (DWORD)size);
+		if (r == 0) {
+			// \x8A֐\x94\x82\xAA\x8E\xB8\x94s
+			error = GetLastError();
+			break;
+		} else if (r < size - 1) {
+			// \x8E擾\x90\xAC\x8C\xF7
+			size = r + 1;
+			b = (wchar_t*)realloc(b, sizeof(wchar_t) * size);
+			*buf = b;
+			return NO_ERROR;
+		} else {
+			size *= 2;
+			wchar_t *p = (wchar_t*)realloc(b, sizeof(wchar_t) * size);
+			if (p == NULL) {
+				free(b);
+				error = ERROR_NOT_ENOUGH_MEMORY;
+				break;
+			}
+			b = p;
+		}
+    }
+
+	// error
+	free(b);
+error_return:
+	*buf = NULL;
+	return error;
+}
+
+/**
+ *	GetPrivateProfileStringW() \x82̓\xAE\x93I\x83o\x83b\x83t\x83@\x94\xC5
+ *
+ *	@param str	\x95\xB6\x8E\x9A\x97\xF1\x82\xF0\x8Ai\x94[\x82\xB7\x82\xE9\x83o\x83b\x83t\x83@
+ *				\x95s\x97v\x82ɂȂ\xC1\x82\xBD\x82\xE7free()\x82\xB7\x82\xE9
+ *	@return	\x83G\x83\x89\x81[\x83R\x81[\x83h,0(=NO_ERROR)\x82̂Ƃ\xAB\x83G\x83\x89\x81[\x82Ȃ\xB5
+ */
+DWORD hGetPrivateProfileStringW(const wchar_t *section, const wchar_t *key, const wchar_t *def, const wchar_t *ini, wchar_t **str)
+{
+	size_t size = 256;
+	wchar_t *b = (wchar_t*)malloc(sizeof(wchar_t) * size);
+	DWORD error;
+	if (b == NULL) {
+		error = ERROR_NOT_ENOUGH_MEMORY;
+		goto error_return;
+	}
+	b[0] = 0;
+	for(;;) {
+		DWORD r = GetPrivateProfileStringW(section, key, def, b, (DWORD)size, ini);
+		if (r == 0 || b[0] == L'\0') {
+			// \x8E\x9F\x82̏ꍇ\x82\xB1\x82\xB1\x82ɓ\xFC\x82\xE9
+			//   ini\x82\xC9'key='\x82ƋL\x8Fq ("="\x82̌\xE3\x82ɉ\xBD\x82\xE0\x8F\x91\x82\xA2\x82Ă\xA2\x82Ȃ\xA2)
+			//   ini\x82\xC9'key=...' \x82\xAA\x91\xB6\x8D݂\xB5\x82Ȃ\xA2 \x82\xA9\x82\xC2 def=NULL
+			free(b);
+			*str = NULL;
+			return NO_ERROR;
+		} else if (r < size - 2) {
+			size = r + 1;
+			b = (wchar_t *)realloc(b, sizeof(wchar_t) * size);
+			*str = b;
+			return NO_ERROR;
+		} else {
+			wchar_t *p;
+			size *= 2;
+			p = (wchar_t*)realloc(b, sizeof(wchar_t) * size);
+			if (p == NULL) {
+				free(b);
+				error = ERROR_NOT_ENOUGH_MEMORY;
+				break;
+			}
+			b = p;
+		}
+	}
+
+	// error
+	free(b);
+error_return:
+	*str = NULL;
+	return error;
+}
+
+/**
+ *	GetFullPathNameW() \x82̓\xAE\x93I\x83o\x83b\x83t\x83@\x94\xC5
+ *
+ *	@param fullpath		fullpath\x82\xF0\x8Ai\x94[\x82\xB7\x82\xE9\x83o\x83b\x83t\x83@
+ *						\x95s\x97v\x82ɂȂ\xC1\x82\xBD\x82\xE7free()\x82\xB7\x82\xE9
+ *	@return	\x83G\x83\x89\x81[\x83R\x81[\x83h,0(=NO_ERROR)\x82̂Ƃ\xAB\x83G\x83\x89\x81[\x82Ȃ\xB5
+ */
+DWORD hGetFullPathNameW(const wchar_t *lpFileName, wchar_t **fullpath, wchar_t **filepart)
+{
+	size_t len = GetFullPathNameW(lpFileName, 0, NULL, NULL);		// include L'\0'
+	if (len == 0) {
+		*fullpath = NULL;
+		*filepart = NULL;
+		return GetLastError();
+	}
+	wchar_t *path = (wchar_t *)malloc(sizeof(wchar_t) * len);
+	wchar_t *file;
+	len = GetFullPathNameW(lpFileName, (DWORD)len, path, &file);
+	if (len == 0) {
+		free(path);
+		return GetLastError();
+	}
+	if (filepart != NULL) {
+		*filepart = file;
+	}
+	*fullpath = path;
+	return NO_ERROR;
+}

Added: trunk/teraterm/common/win32helper.h
===================================================================
--- trunk/teraterm/common/win32helper.h	                        (rev 0)
+++ trunk/teraterm/common/win32helper.h	2021-06-12 15:29:23 UTC (rev 9305)
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2021- TeraTerm Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <windows.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+DWORD hGetModuleFileNameW(HMODULE hModule, wchar_t **buf);
+DWORD hGetPrivateProfileStringW(const wchar_t *section, const wchar_t *key, const wchar_t *def, const wchar_t *ini, wchar_t **str);
+DWORD hGetFullPathNameW(const wchar_t *lpFileName, wchar_t **fullpath, wchar_t **filepart);
+
+#ifdef __cplusplus
+}
+#endif


Ttssh2-commit メーリングリストの案内
アーカイブの一覧に戻る