[ttssh2-commit] [9570] ttermpro.exe 起動時に ini ファイルの文字コードを変換

Back to archive index
scmno****@osdn***** scmno****@osdn*****
2021年 12月 5日 (日) 22:08:19 JST


Revision: 9570
          https://osdn.net/projects/ttssh2/scm/svn/commits/9570
Author:   zmatsuo
Date:     2021-12-05 22:08:19 +0900 (Sun, 05 Dec 2021)
Log Message:
-----------
ttermpro.exe 起動時に ini ファイルの文字コードを変換

- Windows2000以降でACPだった場合、UTF16LEへ変換
- Windows2000より前でUTF16LEだった場合、ACPへ変換
- 変換ファイル
  - TERATERM.INI
  - KEYBOARD.CNF
- 変換時、バックアップファイルを作成
- TERATERM.INI 保存時も変換
  - ttermpro.exe起動後、次の場合も考慮
    - iniファイルが入れ替えられる
    - iniファイルがなくなっている
  - 保存時バックアップファイルも作成
- imgrate_to_5.html の説明を修正

Modified Paths:
--------------
    trunk/doc/ja/html/usage/imgrate_to_5.html
    trunk/teraterm/common/ttlib.h
    trunk/teraterm/common/ttlib_static_cpp.cpp
    trunk/teraterm/teraterm/vtwin.cpp
    trunk/teraterm/ttpcmn/ttcmn.c

-------------- next part --------------
Modified: trunk/doc/ja/html/usage/imgrate_to_5.html
===================================================================
--- trunk/doc/ja/html/usage/imgrate_to_5.html	2021-12-05 13:08:06 UTC (rev 9569)
+++ trunk/doc/ja/html/usage/imgrate_to_5.html	2021-12-05 13:08:19 UTC (rev 9570)
@@ -9,37 +9,46 @@
   </head>
 
   <body>
+    <h1>Tera Term 5 \x82ɂ‚\xA2\x82\xC4</h1>
+    <ul>
+      <li>Tera Term 5 \x82\xCD Tera Term 4 \x82\xCC Unicode\x89\xBB\x82\xF0\x90i\x82߂\xBD\x8E\x9F\x82̃o\x81[\x83W\x83\x87\x83\x93\x82ł\xB7\x81B</li>
+      <ul>
+        <li>\x83^\x81[\x83~\x83i\x83\x8B(\x93\xE0\x95\x94\x83o\x83b\x83t\x83@\x81A\x95`\x89\xE6\x81A\x83L\x81[\x93\xFC\x97\xCD)\x82\xCCUnicode\x89\xBB
+        <li>Windows UI(\x83\x81\x83j\x83\x85\x81[\x81A\x83_\x83C\x83A\x83\x8D\x83O)\x82\xCCUnicode\x89\xBB
+        <li>\x82\xBB\x82̑\xBCWindows API\x82\xCCUnicode\x94łւ̐؂\xE8\x91ւ\xA6(\x83t\x83@\x83C\x83\x8B\x96\xBC\x82Ȃ\xC7)
+      </ul>
+      <li>Windows 10, 11\x82\xF0\x83^\x81[\x83Q\x83b\x83g\x82ɂ\xB5\x82Ă\xA2\x82܂\xB7\x81B</li>
+      <li>\x93\xC1\x92\xA5\x82Ȃǂ\xCD<a href="https://osdn.net/projects/ttssh2/wiki/UNICODE">Unicode\x89\xBB\x8A\xEE\x96{\x95\xFB\x90j</a>\x82\xF0\x8EQ\x8FƂ\xAD\x82\xBE\x82\xB3\x82\xA2\x81B</li>
+    </ul>
+
     <h1>Tera Term 5 \x82ֈڍs</h1>
 
     <ul>
-      <li>Tera Term 5 \x82\xCD Tera Term 4 \x82\xCC Unicode\x89\xBB\x82\xF0\x90i\x82߂\xBD\x8E\x9F\x82̃o\x81[\x83W\x83\x87\x83\x93\x82ł\xB7
+      <li>Tera Term 4 \x82Ɠ\xAF\x8B\x8F\x82\xB7\x82邱\x82Ƃ\xAA\x82ł\xAB\x82܂\xB7\x81B</li>
+      <li>\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xCDTera Term 4\x97p\x82\xF0\x82\xBB\x82̂܂܎g\x97p\x82ł\xAB\x82܂\xB7\x81B</li>
       <ul>
-        <li>Windows 10, 11\x82\xF0\x83^\x81[\x83Q\x83b\x83g\x82ɂ\xB5\x82Ă\xA2\x82܂\xB7</li>
-        <li>\x93\xC1\x92\xA5\x82Ȃǂ\xCD<a href="https://osdn.net/projects/ttssh2/wiki/UNICODE">Unicode\x89\xBB\x8A\xEE\x96{\x95\xFB\x90j</a>\x82\xF0\x8EQ\x8FƂ\xAD\x82\xBE\x82\xB3\x82\xA2</li>
-      </ul>
-      <li>Tera Term 4 \x82Ɠ\xAF\x8B\x8F\x82\xB7\x82邱\x82Ƃ\xAA\x82ł\xAB\x82܂\xB7</li>
-      <li>\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xCDTera Term 4\x97p\x82\xF0\x82\xBB\x82̂܂܎g\x97p\x82ł\xAB\x82܂\xB7</li>
-      <ul>
-        <li>TERATERM.INI,KEYBOARD.CFG\x82\xCDUnicode\x91Ή\x9E\x82ƂȂ\xE8\x82܂\xB5\x82\xBD</li>
-        <li>Unicode\x82ɕϊ\xB7\x82\xB5\x82Ă\xA8\x82\xAD\x82ƁAUnicode\x83p\x83X,\x83t\x83@\x83C\x83\x8B\x96\xBC\x82Ȃǂ̐ݒ\xE8\x93\xE0\x97e\x82\xAA\x90\xB3\x82\xB5\x82\xAD\x95ۑ\xB6\x82ł\xAB\x82܂\xB7</li>
+        <li>TERATERM.INI,KEYBOARD.CFG\x82\xCDUnicode\x91Ή\x9E\x82ƂȂ\xE8\x82܂\xB5\x82\xBD\x81B</li>
         <ul>
-          <li>Tera Term 4 \x82ł̓p\x83X\x96\xBC\x82\xC9Shift_JIS\x82ŕ\\x8C\xBB\x82ł\xAB\x82Ȃ\xA2\x95\xB6\x8E\x9A\x82\xAA\x8A܂܂\xEA\x82Ă\xA2\x82\xE9\x82Ƃ\xBB\x82̍\x80\x96ڂ͐\xB3\x82\xB5\x82\xAD\x95ۑ\xB6\x82\xB3\x82\xEA\x82܂\xB9\x82\xF1\x82ł\xB5\x82\xBD</li>
+          <li>cygterm.cfg\x82͑Ή\x9E\x82\xB5\x82Ă\xA2\x82܂\xB9\x82\xF1\x81B</li>
         </ul>
-        <li>\x95ϊ\xB7\x82\xB5\x82Ȃ\xAD\x82Ă\xE0\x82\xBB\x82̂܂܎g\x97p\x82ł\xAB\x82܂\xB7</li>
-        <li>Unicode\x82̕\xB6\x8E\x9A\x83R\x81[\x83h\x82\xCDUTF-16 (with LE BOM)\x82ł\xB7</li>
-        <li>Windows\x82̃\x81\x83\x82\x92\xA0\x82ŊJ\x82\xA2\x82ĕ\xB6\x8E\x9A\x83R\x81[\x83h\x82\xF0UTF-16LE\x82ɐݒ肵\x82ď㏑\x82\xAB\x95ۑ\xB6\x82\xB7\x82\xE9\x95ϊ\xB7\x82ł\xAB\x82܂\xB7</li>
+        <li>Windows 10,11(2000\x82\xA9\x82\xE7?)\x82ł\xCDini\x83t\x83@\x83C\x83\x8B\x82\xF0Unicode\x89\xBB\x82\xB7\x82邱\x82Ƃ\xAA\x82ł\xAB\x82܂\xB7\x81B</li>
+        <li>Unicode\x89\xBB\x82\xB5\x82\xBDini\x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h\x82\xCDUTF-16 (with LE BOM)\x82ł\xB7\x81B</li>
+        <li>Tera Term 4 \x82ł\xCDUnicode\x94\xC5API\x82\xF0\x8Eg\x97p\x82\xB5\x82Ă\xA2\x82Ȃ\xA2\x82\xBD\x82߁AShift_JIS\x82ŕ\\x8C\xBB\x82ł\xAB\x82Ȃ\xA2\x95\xB6\x8E\x9A\x82\xAA\x8A܂܂\xEA\x82Ă\xA2\x82\xE9\x82ƁA\x82\xBB\x82̍\x80\x96ڂ͐\xB3\x82\xB5\x82\xAD\x88\xB5\x82\xA6\x82܂\xB9\x82\xF1\x82ł\xB5\x82\xBD\x81B</li>
+        <li>Tera Term 5 \x82͋N\x93\xAE\x8E\x9E\x82\xC9ini\x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h\x82\xF0\x8E\xA9\x93\xAE\x93I\x82\xC9Unicode\x82ɕϊ\xB7\x82\xB5\x82܂\xB7\x81B</li>
+        <li>\x95ϊ\xB7\x91O\x82̃t\x83@\x83C\x83\x8B\x82̓o\x83b\x83N\x83A\x83b\x83v\x82\xB3\x82\xEA\x82Ă\xA2\x82܂\xB7\x81B</li>
       </ul>
-      <li>\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82̃f\x83t\x83H\x83\x8B\x83g\x83t\x83H\x83\x8B\x83_\x82\xAA\x95ύX\x82\xB3\x82\xEA\x82܂\xB5\x82\xBD</li>
+      <li>\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82̃f\x83t\x83H\x83\x8B\x83g\x83t\x83H\x83\x8B\x83_\x82\xAA\x95ύX\x82\xB3\x82\xEA\x82܂\xB5\x82\xBD\x81B</li>
       <ul>
         <li>%APPDATA%\teraterm5\</li>
-        <li>Tera Term 4 \x82̐ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xF0\x83R\x83s\x81[\x82\xB5\x82Ă\xAD\x82\xBE\x82\xB3\x82\xA2</li>
+        <li>Tera Term 4 \x82̐ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xF0\x83R\x83s\x81[\x82\xB5\x82Ă\xAD\x82\xBE\x82\xB3\x82\xA2\x81B</li>
         <ul>
           <li>TERATERM.INI</li>
           <li>KEYBOARD.CNF</li>
           <li>cygterm.cfg</li>
         </ul>
-        <li>\x83t\x83@\x83C\x83\x8B\x82\xAA\x82Ȃ\xA2\x8Fꍇ\x81Attermpro.exe\x8BN\x93\xAE\x8E\x9E\x82Ƀf\x83t\x83H\x83\x8B\x83g\x82̐ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xAA\x83R\x83s\x81[\x82\xB3\x82\xEA\x82܂\xB7</li>
-        <li>4\x82̂Ƃ\xAB\x82͎\xC0\x8Ds\x83t\x83@\x83C\x83\x8B\x82Ɠ\xAF\x82\xB6\x83t\x83H\x83\x8B\x83_(\x82܂\xBD\x82\xCDVirtual Store)\x82ł\xB5\x82\xBD</li>
+        <li>\x83t\x83@\x83C\x83\x8B\x82\xAA\x82Ȃ\xA2\x8Fꍇ\x81Attermpro.exe\x8BN\x93\xAE\x8E\x9E\x82Ƀf\x83t\x83H\x83\x8B\x83g\x82̐ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xAA\x83R\x83s\x81[\x82\xB3\x82\xEA\x82܂\xB7\x81B</li>
+        <li>4\x82̂Ƃ\xAB\x82͎\xC0\x8Ds\x83t\x83@\x83C\x83\x8B\x82Ɠ\xAF\x82\xB6\x83t\x83H\x83\x8B\x83_(\x82܂\xBD\x82\xCDVirtual Store)\x82ł\xB5\x82\xBD\x81B</li>
+        <li><a href="../setup/folder.md">\x83t\x83H\x83\x8B\x83_\x82ɂ‚\xA2\x82\xC4 (\x8C\xBB\x8D݂\xCD Windows 10(11)\x82œ\xAE\x8D\xEC\x83e\x83X\x83g\x82\xB5\x82Ă\xA2\x82܂\xB7)</a></li>
       </ul>
     </ul>
 
@@ -46,21 +55,27 @@
     <h1>Tera Term 5 \x82ֈڍs(plugin)</h1>
 
     <ul>
-      <li>compat_w95.h\x82͕s\x97v\x82ƂȂ\xE8\x82܂\xB5\x82\xBD</li>
+      <li>compat_w95.h\x82͕s\x97v\x82ƂȂ\xE8\x82܂\xB5\x82\xBD\x81B</li>
       <ul>
         <li><a href="https://osdn.net/projects/ttssh2/scm/svn/commits/9129">r9239</a></li>
       </ul>
-      <li>plugin API\x82\xF0\x95ύX\x82\xB5\x82܂\xB5\x82\xBD</li>
+      <li>plugin API\x82\xF0\x95ύX\x82\xB5\x82܂\xB5\x82\xBD\x81B</li>
       <ul>
         <li>\x83t\x83@\x83C\x83\x8B\x96\xBC\x82\xAAUnicode (\x8C^\x82\xAA char \x82\xA9\x82\xE7 wchar_t) \x82ɂȂ\xE8\x82܂\xB5\x82\xBD\x81B</li>
         <li><a href="https://osdn.net/projects/ttssh2/svn/view/trunk/teraterm/teraterm/ttsetup.h?view=markup&root=ttssh2#l44">trunk/teraterm/ttsetup.h</a></li>
         <li><a href="https://osdn.net/projects/ttssh2/svn/view/branches/4-stable/teraterm/teraterm/ttsetup.h?view=markup&root=ttssh2#l39">4-stable/teraterm/ttsetup.h</a></li>
       </ul>
-      <li>ini\x83t\x83@\x83C\x83\x8B\x93ǂݏ\x91\x82\xAB\x8B\xA4\x92ʊ֐\x94\x82\xF0\x92lj\xC1\x82\xB5\x82܂\xB5\x82\xBD</li>
+      <li>ini\x83t\x83@\x83C\x83\x8B\x93ǂݏ\x91\x82\xAB\x8B\xA4\x92ʊ֐\x94\x82\xF0\x92lj\xC1\x82\xB5\x82܂\xB5\x82\xBD\x81B</li>
       <ul>
-        <li>\x83t\x83@\x83C\x83\x8B\x96\xBC\x82\xBE\x82\xAF\x82\xF0Unicode\x89\xBB\x82\xB5\x82ď]\x97\x88\x82Ɠ\xAF\x82\xB6\x93\xAE\x8D\xEC\x82\xF0\x82\xB7\x82\xE9\x8A֐\x94\x82\xF0\x8F\x80\x94\xF5</li>
+        <li>\x83t\x83@\x83C\x83\x8B\x96\xBC\x82\xBE\x82\xAF\x82\xF0Unicode\x89\xBB\x82\xB5\x82ď]\x97\x88\x82Ɠ\xAF\x82\xB6\x93\xAE\x8D\xEC\x82\xF0\x82\xB7\x82\xE9\x8A֐\x94\x82\xF0\x8F\x80\x94\xF5\x81B</li>
         <li><a href="https://osdn.net/projects/ttssh2/svn/view/trunk/teraterm/common/inifile_com.h?view=markup&root=ttssh2">trunk/teraterm/common/inifile_com.h</a></li>
       </ul>
+      <li>struct tttset (tttypes.h) \x82̃\x81\x83\x93\x83o\x82\xF0\x95ύX\x82\xB5\x82܂\xB5\x82\xBD\x81B</li>
+      <ul>
+        <li>\x83t\x83@\x83C\x83\x8B\x96\xBC\x81A\x83p\x83X\x81A\x95\xB6\x8E\x9A\x97\xF1\x82̃\x81\x83\x93\x83o\x82ɂ́AW\x82\xAA\x82‚\xA2\x82\xBDUnicode\x94ł̃\x81\x83\x93\x83o\x82\xAA\x92lj\xC1\x81B</li>
+        <li>\x83t\x83@\x83C\x83\x8B\x96\xBC\x81A\x83p\x83X\x82̕\xB6\x8E\x9A\x90\x94\x82\xCDMAX_PATH\x95\xB6\x8E\x9A\x88ȏ\xE3\x82ɂȂ邱\x82Ƃ\xF0\x8Dl\x97\xB6\x81B</li>
+        <li>\x82܂\xBE\x95ω\xBB\x82\xB7\x82\xE9\x97\\x92\xE8(\x83\x81\x83\x93\x83o\x96\xBC\x81A\x8F\x87\x8F\x98\x81A\x83T\x83C\x83Y\x81A\x8C^\x82ȂǂȂ\xC7)\x81B</li>
+      </ul>
     </ul>
 
   </body>

Modified: trunk/teraterm/common/ttlib.h
===================================================================
--- trunk/teraterm/common/ttlib.h	2021-12-05 13:08:06 UTC (rev 9569)
+++ trunk/teraterm/common/ttlib.h	2021-12-05 13:08:19 UTC (rev 9570)
@@ -198,6 +198,8 @@
 BOOL IsRelativePathW(const wchar_t *path);
 DWORD TTWinExec(const wchar_t *command);
 DWORD TTWinExecA(const char *commandA);
+void CreateBakupFile(const wchar_t *fname, const wchar_t *prev_str);
+BOOL ConvertIniFileCharCode(const wchar_t *fname,  const wchar_t *bak_str);
 
 #ifdef __cplusplus
 }

Modified: trunk/teraterm/common/ttlib_static_cpp.cpp
===================================================================
--- trunk/teraterm/common/ttlib_static_cpp.cpp	2021-12-05 13:08:06 UTC (rev 9569)
+++ trunk/teraterm/common/ttlib_static_cpp.cpp	2021-12-05 13:08:19 UTC (rev 9570)
@@ -26,6 +26,9 @@
  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* C99\x95\x97\x82ɋL\x8Fq\x82ł\xAB\x82\xE9\x82悤\x82\xC9cpp\x82Ƃ\xB5\x82\xBD */
+/* Visual Studio 2005 \x82\xAA C89 */
+
 #include <windows.h>
 #include <stdio.h>
 #include <string.h>
@@ -38,6 +41,7 @@
 #include <wchar.h>
 #include <shlobj.h>
 #include <malloc.h>
+#include <time.h>
 
 #include "i18n.h"
 #include "asprintf.h"
@@ -44,6 +48,7 @@
 #include "win32helper.h"
 #include "codeconv.h"
 #include "compat_win.h"
+#include "fileread.h"
 
 #include "ttlib.h"
 
@@ -1425,3 +1430,121 @@
 	free(commandW);
 	return e;
 }
+
+/**
+ *	\x83o\x83b\x83N\x83A\x83b\x83v\x83t\x83@\x83C\x83\x8B\x82\xF0\x8D쐬\x82\xB7\x82\xE9
+ *
+ *	@param[in]	fname		\x83t\x83@\x83C\x83\x8B\x96\xBC(\x83t\x83\x8B\x83p\x83X)
+ *	@param[in]	prev_str	\x83t\x83@\x83C\x83\x8B\x96\xBC\x82̑O\x82ɒlj\xC1\x82\xB7\x82镶\x8E\x9A\x97\xF1
+ *							NULL\x82̏ꍇ\x82́A\x8C\xBB\x8Dݓ\x{239E576}\x8E\x9A\x97\xF1\x82ƂȂ\xE9
+ *	\x97\xE1
+ *		frame
+ *			"C:\Users\user\AppData\Roaming\teraterm5\TERATERM.INI"
+ *		prev_str
+ *			NULL
+ *		\x8D쐬\x82\xB3\x82\xEA\x82\xE9\x83o\x83b\x83N\x83A\x83b\x83v\x83t\x83@\x83C\x83\x8B
+ *			"C:\Users\user\AppData\Roaming\teraterm5\211204_231234_TERATERM.INI"
+ */
+void CreateBakupFile(const wchar_t *fname, const wchar_t *prev_str)
+{
+	DWORD attr = GetFileAttributesW(fname);
+	if (attr == INVALID_FILE_ATTRIBUTES) {
+		// \x83t\x83@\x83C\x83\x8B\x82\xAA\x82Ȃ\xA2
+		return;
+	}
+
+	wchar_t dete_str[32];
+	const wchar_t *prev_str_ptr = prev_str;
+	if (prev_str == NULL) {
+		time_t now;
+		time(&now);
+		struct tm *now_tm;
+		now_tm = localtime(&now);
+		wcsftime(dete_str, _countof(dete_str), L"%y%m%d_%H%M%S_", now_tm);
+		prev_str_ptr = dete_str;
+	}
+
+	wchar_t *dup = _wcsdup(fname);
+	wchar_t *p = wcsrchr(dup, L'\\') + 1;
+	wchar_t *name = _wcsdup(p);
+	*p = 0;
+	const wchar_t *path = dup;
+
+	wchar_t *bak_fname = NULL;
+	awcscats(&bak_fname, path, prev_str_ptr, name, NULL);
+	free(dup);
+	free(name);
+
+	MoveFileW(fname, bak_fname);
+	CopyFileW(bak_fname, fname, TRUE);
+	free(bak_fname);
+}
+
+// common_static.lib \x92P\x91̂Ńr\x83\x8B\x83h\x82ł\xAB\x82\xE9\x82悤\x92lj\xC1
+// TODO \x82\xB7\x82ׂ\xC4common_static\x88ړ\xAE\x82\xB7\x82\xE9?
+static BOOL _IsWindows2000OrLater(void)
+{
+	return IsWindowsVerOrLater(5, 0);
+}
+
+/**
+ *	ini\x83t\x83@\x83C\x83\x8B\x82\xF0\x8C\xBB\x8D݂̊‹\xAB\x82ɍ\x87\x82킹\x82ĕϊ\xB7\x82\xB7\x82\xE9
+ *
+ *	@param	fname	\x83t\x83\x8B\x83p\x83X\x83t\x83@\x83C\x83\x8B\x96\xBC
+ *	@param	bak_str	\x95ϊ\xB7\x8E\x9E\x82Ɍ\xB3\x83t\x83@\x83C\x83\x8B\x82\xF0\x83o\x83b\x83N\x83A\x83b\x83v\x82\xB7\x82\xE9\x82Ƃ\xAB\x82ɒlj\xC1\x82\xB7\x82镶\x8E\x9A\x97\xF1
+ *	@retval	TRUE	\x95ϊ\xB7\x82\xB5\x82\xBD(\x83o\x83b\x83N\x83A\x83b\x83v\x82\xF0\x8D쐬\x82\xB5\x82\xBD)
+ *	@retval	FALSE	\x95ϊ\xB7\x82\xB5\x82Ȃ\xA9\x82\xC1\x82\xBD(\x83t\x83@\x83C\x83\x8B\x82\xAA\x91\xB6\x8D݂\xB5\x82Ȃ\xA2)
+ *
+ *	ini\x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h
+ *		NT\x8Cn\x82ł\xCDANSI\x82܂\xBD\x82\xCDUnicode,UTF16BE BOM)\x82\xF0\x8Eg\x97p\x82ł\xAB\x82\xE9
+ *		9x\x8Cn\x82ł\xCDANSI\x82̂\xDD
+ */
+BOOL ConvertIniFileCharCode(const wchar_t *fname,  const wchar_t *bak_str)
+{
+	FILE *fp;
+	_wfopen_s(&fp, fname, L"rb");
+	if (fp == NULL) {
+		// \x83t\x83@\x83C\x83\x8B\x82\xAA\x91\xB6\x8D݂\xB5\x82Ȃ\xA2
+		return FALSE;
+	}
+
+	// \x83t\x83@\x83C\x83\x8B\x82\xF0\x93ǂݍ\x9E\x82\xDE
+	LoadFileCode code;
+	size_t len;
+	char *ini_u8 = LoadFileU8C(fp, &len, &code);
+	fclose(fp);
+
+	BOOL converted = FALSE;
+	if(_IsWindows2000OrLater()) {
+		// UTF16 LE
+		if (code != FILE_CODE_UTF16LE) {
+			CreateBakupFile(fname, bak_str);
+
+			wchar_t *ini_u16 = ToWcharU8(ini_u8);
+			_wfopen_s(&fp, fname, L"wb");
+			if (fp != NULL) {
+				fwrite("\xff\xfe", 2, 1, fp);	// BOM
+				fwrite(ini_u16, wcslen(ini_u16), sizeof(wchar_t), fp);
+				fclose(fp);
+				converted = TRUE;
+			}
+		}
+	}
+	else {
+		// ACP
+		if (code != FILE_CODE_ACP) {
+			CreateBakupFile(fname, bak_str);
+
+			char *ini_acp = ToCharU8(ini_u8);
+			_wfopen_s(&fp, fname, L"wb");
+			if (fp != NULL) {
+				fwrite(ini_acp, strlen(ini_acp), sizeof(char), fp);
+				fclose(fp);
+				converted = TRUE;
+			}
+		}
+	}
+	free(ini_u8);
+
+	return converted;
+}

Modified: trunk/teraterm/teraterm/vtwin.cpp
===================================================================
--- trunk/teraterm/teraterm/vtwin.cpp	2021-12-05 13:08:06 UTC (rev 9569)
+++ trunk/teraterm/teraterm/vtwin.cpp	2021-12-05 13:08:19 UTC (rev 9570)
@@ -4748,7 +4748,32 @@
 		}
 #endif
 
-		CopyFileW(PrevSetupFNW, ts.SetupFNameW, TRUE);
+		if (wcscmp(PrevSetupFNW, ts.SetupFNameW) == 0) {
+			// \x93\xAF\x96\xBC\x83t\x83@\x83C\x83\x8B\x82֏\x91\x82\xAB\x8D\x9E\x82\xDD(\x8F㏑\x82\xAB)
+			// \x83o\x83b\x83N\x83A\x83b\x83v\x82\xF0\x8D쐬
+			CreateBakupFile(ts.SetupFNameW, NULL);
+		}
+		else {
+			// \x88قȂ\xE9\x83t\x83@\x83C\x83\x8B\x82֏\x91\x82\xAB\x8D\x9E\x82\xDD
+			CopyFileW(PrevSetupFNW, ts.SetupFNameW, TRUE);
+		}
+
+		if (GetFileAttributesW(ts.SetupFNameW) == INVALID_FILE_ATTRIBUTES) {
+			// \x83t\x83@\x83C\x83\x8B\x82\xAA\x82Ȃ\xA2
+			// UTF16LE BOM\x82\xBE\x82\xAF\x82\xCCini\x83t\x83@\x83C\x83\x8B\x82\xF0\x8D쐬
+			FILE *fp;
+			_wfopen_s(&fp, ts.SetupFNameW, L"wb");
+			if (fp != NULL) {
+				fwrite("\xff\xfe", 2, 1, fp);	// BOM
+				fclose(fp);
+			}
+		}
+		else {
+			// \x83t\x83@\x83C\x83\x8B\x82\xAA\x91\xB6\x8D݂\xB7\x82\xE9
+			// ini\x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h\x82\xF0\x95ύX\x82\xB7\x82\xE9
+			ConvertIniFileCharCode(ts.SetupFNameW, NULL);
+		}
+
 		/* write current setup values to file */
 		(*WriteIniFile)(ts.SetupFNameW, &ts);
 		/* copy host list */

Modified: trunk/teraterm/ttpcmn/ttcmn.c
===================================================================
--- trunk/teraterm/ttpcmn/ttcmn.c	2021-12-05 13:08:06 UTC (rev 9569)
+++ trunk/teraterm/ttpcmn/ttcmn.c	2021-12-05 13:08:19 UTC (rev 9570)
@@ -36,6 +36,7 @@
 #include <htmlhelp.h>
 #include <assert.h>
 #include <crtdbg.h>
+#include <time.h>
 
 #define DllExport __declspec(dllexport)
 #include "language.h"
@@ -49,6 +50,7 @@
 #include "compat_win.h"
 #include "win32helper.h"
 #include "asprintf.h"
+#include "fileread.h"
 
 #include "ttcmn_dup.h"
 
@@ -163,6 +165,21 @@
 	}
 }
 
+static void ConvertIniFiles(const wchar_t *filelist[],  const wchar_t *dir, const wchar_t *date_str)
+{
+	while(1) {
+		if (*filelist == NULL) {
+			break;
+		}
+
+		wchar_t *fname = NULL;
+		awcscats(&fname, dir, L"\\", *filelist, NULL);
+		ConvertIniFileCharCode(fname, date_str);
+		free(fname);
+		filelist++;
+	}
+}
+
 BOOL WINAPI StartTeraTerm(PTTSet ts)
 {
 	if (FirstInstance) {
@@ -202,10 +219,9 @@
 	awcscats(&ts->KeyCnfFNW, ts->HomeDirW, L"\\KEYBOARD.CNF", NULL);
 	WideCharToACP_t(ts->KeyCnfFNW, ts->KeyCnfFN, _countof(ts->KeyCnfFN));
 
-	// TERATERM.INI \x82\xAA\x91\xB6\x8D݂\xB7\x82\xE9?
+	// TERATERM.INI \x82\xAA\x91\xB6\x8D݂\xB5\x82Ȃ\xA2\x82Ƃ\xAB
+	// \x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xF0\x8Cl\x83t\x83H\x83\x8B\x83_\x82փR\x83s\x81[\x82\xB7\x82\xE9
 	if (GetFileAttributesW(ts->SetupFNameW) == INVALID_FILE_ATTRIBUTES) {
-		// \x91\xB6\x8D݂\xB5\x82Ȃ\xA2\x8Fꍇ\x81A\x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xF0\x8Cl\x83t\x83H\x83\x8B\x83_\x82փR\x83s\x81[\x82\xB7\x82\xE9
-		// TODO \x90ݒ\xE8\x83t\x83@\x83C\x83\x8B\x82\xCC unicode, ansi\x95ϊ\xB7
 		static const wchar_t *filelist[] = {
 			L"TERATERM.INI",
 			L"KEYBOARD.CNF",
@@ -228,6 +244,26 @@
 		CopyFiles(filelist, ts->ExeDirW, ts->HomeDirW);
 	}
 
+	// ini\x83t\x83@\x83C\x83\x8B\x82̕\xB6\x8E\x9A\x83R\x81[\x83h\x82\xF0\x95ϊ\xB7\x82\xB7\x82\xE9
+	{
+		static const wchar_t *filelist[] = {
+			L"TERATERM.INI",
+			L"KEYBOARD.CNF",
+			NULL,
+		};
+
+		// backup \x83t\x83@\x83C\x83\x8B\x82ɂ‚\xAF\x82\xE9\x93\xFA\x95t\x95\xB6\x8E\x9A\x97\xF1
+		time_t now;
+		time(&now);
+		struct tm *now_tm;
+		now_tm = localtime(&now);
+		wchar_t dete_str[256];
+		wcsftime(dete_str, _countof(dete_str), L"%y%m%d_%H%M%S_", now_tm);
+
+		// ini\x83t\x83@\x83C\x83\x8B\x82\xF0\x95ϊ\xB7\x82\xB7\x82\xE9
+		ConvertIniFiles(filelist, ts->HomeDirW, dete_str);
+	}
+
 	if (FirstInstance) {
 		FirstInstance = FALSE;
 		return TRUE;


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