[Ttssh2-commit] [7466] 新しいMBCPToUTF32()追加、従来の関数は MBCP_UTF32() にリネーム

scmno****@osdn***** scmno****@osdn*****
2019年 3月 10日 (日) 02:33:27 JST


Revision: 7466
          http://sourceforge.jp/projects/ttssh2/scm/svn/commits/7466
Author:   zmatsuo
Date:     2019-03-10 02:33:27 +0900 (Sun, 10 Mar 2019)
Log Message:
-----------
新しいMBCPToUTF32()追加、従来の関数は MBCP_UTF32() にリネーム

Modified Paths:
--------------
    trunk/teraterm/common/codeconv.cpp
    trunk/teraterm/common/codeconv.h
    trunk/teraterm/ttpcmn/ttcmn.c

-------------- next part --------------
Modified: trunk/teraterm/common/codeconv.cpp
===================================================================
--- trunk/teraterm/common/codeconv.cpp	2019-03-09 17:33:16 UTC (rev 7465)
+++ trunk/teraterm/common/codeconv.cpp	2019-03-09 17:33:27 UTC (rev 7466)
@@ -96,7 +96,7 @@
 /**
  * CP932\x95\xB6\x8E\x9A(Shift_JIS) 1\x95\xB6\x8E\x9A\x82\xA9\x82\xE7UTF-32\x82֕ϊ\xB7\x82\xB7\x82\xE9
  * @param[in]		cp932		CP932\x95\xB6\x8E\x9A
- * @retval			\x95ϊ\xB7\x82\xB5\x82\xBDUTF-32\x95\xB6\x8E\x9A\x90\x94
+ * @retval			\x95ϊ\xB7\x82\xB5\x82\xBDUTF-32\x95\xB6\x8E\x9A
  *					0=\x83G\x83\x89\x81[(\x95ϊ\xB7\x82ł\xAB\x82Ȃ\xA9\x82\xC1\x82\xBD)
  */
 unsigned int CP932ToUTF32(unsigned short cp932)
@@ -161,30 +161,33 @@
 
 /**
  *	code page \x82\xCC mulit byte \x95\xB6\x8E\x9A\x82\xF0 UTF-32\x82֕ϊ\xB7\x82\xB7\x82\xE9
- *	@param KCode	\x83}\x83\x8B\x83`\x83o\x83C\x83g\x82̕\xB6\x8E\x9A\x83R\x81[\x83h(0x0000-0xffff)
- *	@param CoePage	\x83}\x83\x8B\x83`\x83o\x83C\x83g\x82̃R\x81[\x83h\x83y\x81[\x83W
- *	@retval			unicode(UTF-32\x95\xB6\x8E\x9A\x83R\x81[\x83h)
+ *	@param mb_code		\x83}\x83\x8B\x83`\x83o\x83C\x83g\x82̕\xB6\x8E\x9A\x83R\x81[\x83h(0x0000-0xffff)
+ *	@param code_page	\x83}\x83\x8B\x83`\x83o\x83C\x83g\x82̃R\x81[\x83h\x83y\x81[\x83W
+ *	@retval				unicode(UTF-32\x95\xB6\x8E\x9A\x83R\x81[\x83h)
  */
-unsigned int MBCPToUTF32(unsigned short KCode, int CodePage)
+unsigned int MBCP_UTF32(unsigned short mb_code, int code_page)
 {
 	unsigned int c;
 
-	if (CodePage == 932) {
-		c = CP932ToUTF32(KCode);
+	if (code_page == CP_ACP) {
+		code_page = (int)GetACP();
+	}
+	if (code_page == 932) {
+		c = CP932ToUTF32(mb_code);
 	} else {
-		char buf[3];
+		char buf[2];
 		wchar_t wchar;
 		int ret;
 		int len = 0;
-		if (KCode < 0x100) {
-			buf[0] = KCode & 0xff;
+		if (mb_code < 0x100) {
+			buf[0] = mb_code & 0xff;
 			len = 1;
 		} else {
-			buf[0] = KCode >> 8;
-			buf[1] = KCode & 0xff;
+			buf[0] = mb_code >> 8;
+			buf[1] = mb_code & 0xff;
 			len = 2;
 		}
-		ret = MultiByteToWideChar(CodePage, MB_ERR_INVALID_CHARS, buf, len, &wchar, 1);
+		ret = MultiByteToWideChar(code_page, MB_ERR_INVALID_CHARS, buf, len, &wchar, 1);
 		if (ret <= 0) {
 			c = 0;
 		} else {
@@ -386,6 +389,51 @@
 }
 
 /**
+ *	\x83}\x83\x8B\x83`\x83o\x83C\x83g\x95\xB6\x8E\x9A(code_page) \x82\xA9\x82\xE7unicode(UTF-32)\x82\xF01\x95\xB6\x8E\x9A\x8E\xE6\x82\xE8\x8Fo\x82\xB7
+ *	@retval	0	\x95\xB6\x8E\x9A\x82Ƃ\xB5\x82Ĉ\xB5\x82\xA6\x82Ȃ\xA2(\x95\xB6\x8E\x9A\x83R\x81[\x83h\x82\xAA\x82\xA8\x82\xA9\x82\xB5\x82\xA2)
+ *	@retval	1	1\x83L\x83\x83\x83\x89\x83N\x83^\x82\xC51\x95\xB6\x8E\x9A\x82Ƃ\xB5\x82Ĉ\xB5\x82\xA6\x82\xE9
+ *	@retval	2	2\x83L\x83\x83\x83\x89\x83N\x83^\x82\xC51\x95\xB6\x8E\x9A\x82Ƃ\xB5\x82Ĉ\xB5\x82\xA6\x82\xE9
+ */
+size_t MBCPToUTF32(const char *mb_ptr, size_t mb_len, int code_page, unsigned int *u32)
+{
+	size_t input_len;
+	wchar_t u16_str[2];
+	size_t u16_len;
+
+	assert(mb_ptr != NULL);
+	if (mb_len == 0) {
+		*u32 = 0;
+		return 0;
+	}
+	if (code_page == CP_ACP) {
+		code_page = (int)GetACP();
+	}
+
+	input_len = 1;
+	while(1) {
+		u16_len = ::MultiByteToWideChar(code_page, MB_ERR_INVALID_CHARS,
+										mb_ptr, (int)input_len,
+										u16_str, 2);
+		if (u16_len != 0) {
+			size_t r = UTF16ToUTF32(u16_str, u16_len, u32);
+			assert(r != 0);
+			if (r == 0) {
+				// \x82Ȃ\xA2\x82͂\xB8
+				return 0;
+			} else {
+				return input_len;
+			}
+		}
+
+		input_len++;
+		if (input_len > mb_len) {
+			*u32 = 0;
+			return 0;
+		}
+	}
+}
+
+/**
  * UTF-32\x95\xB6\x8E\x9A \x82\xA9\x82\xE7 UTF-8 \x82֕ϊ\xB7\x82\xB7\x82\xE9
  * @param[in]		u32		\x95ϊ\xB7\x82\xB7\x82\xE9UTF-32
  * @param[in,out]	u8_ptr	\x95ϊ\xB7\x8C\xE3UTF-8\x95\xB6\x8E\x9A\x97\xF1\x8Fo\x97͐\xE6(NULL\x82̂Ƃ\xAB\x8Fo\x97͂\xB5\x82Ȃ\xA2)
@@ -397,7 +445,7 @@
 {
 	size_t out_len = 0;
 	uint8_t *u8_ptr = (uint8_t *)u8_ptr_;
-	if (u8_ptr != NULL) {
+	if (u8_ptr == NULL) {
 		u8_len = 4;
 	}
 

Modified: trunk/teraterm/common/codeconv.h
===================================================================
--- trunk/teraterm/common/codeconv.h	2019-03-09 17:33:16 UTC (rev 7465)
+++ trunk/teraterm/common/codeconv.h	2019-03-09 17:33:27 UTC (rev 7466)
@@ -34,16 +34,17 @@
 extern "C" {
 #endif
 
-
 // simple code convert
 unsigned int CP932ToUTF32(unsigned short cp932);
 unsigned short UTF32ToDecSp(unsigned int u32);
-unsigned int MBCPToUTF32(unsigned short KCode, int CodePage);
+unsigned int MBCP_UTF32(unsigned short mb_code, int code_page);
 unsigned short UTF32_CP932(unsigned int u32);
 
 // 1char ToUTF32
 size_t UTF8ToUTF32(const char *u8_ptr_, size_t u8_len, unsigned int *u32_);
 size_t UTF16ToUTF32(const wchar_t *wstr_ptr, size_t wstr_len, unsigned int *u32);
+size_t MBCPToUTF32(const char *mb_ptr, size_t mb_len, int code_page, unsigned int *u32);
+
 // 1char UTF32To
 size_t UTF32ToUTF16(unsigned int u32, wchar_t *wstr_ptr, size_t wstr_len);
 size_t UTF32ToUTF8(unsigned int u32, char *u8_ptr, size_t u8_len);

Modified: trunk/teraterm/ttpcmn/ttcmn.c
===================================================================
--- trunk/teraterm/ttpcmn/ttcmn.c	2019-03-09 17:33:16 UTC (rev 7465)
+++ trunk/teraterm/ttpcmn/ttcmn.c	2019-03-09 17:33:27 UTC (rev 7466)
@@ -1589,7 +1589,7 @@
 	unsigned int code;
 	int outlen;
 
-	code = MBCPToUTF32(K, CodePage);
+	code = MBCP_UTF32(K, CodePage);
 	if (code == 0) {
 		// \x95ϊ\xB7\x8E\xB8\x94s
 		code = 0xfffd; // U+FFFD: Replacement Character


Ttssh2-commit メーリングリストの案内