OSDN Git Service

Replace sprintf() simply. Does part of the work to resolve https://github.com/hengba...
[hengbandforosx/hengbandosx.git] / src / cmd-io / cmd-macro.cpp
index fb57c5c..e16fe5c 100644 (file)
@@ -11,6 +11,7 @@
 #include "system/player-type-definition.h"
 #include "term/screen-processor.h"
 #include "term/term-color-types.h"
+#include "term/z-form.h"
 #include "util/angband-files.h"
 #include "util/int-char-converter.h"
 #include "util/string-processor.h"
@@ -25,15 +26,16 @@ static void macro_dump(FILE **fpp, concptr fname)
     static concptr mark = "Macro Dump";
     char buf[1024];
     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
-    if (!open_auto_dump(fpp, buf, mark))
+    if (!open_auto_dump(fpp, buf, mark)) {
         return;
+    }
 
     auto_dump_printf(*fpp, _("\n# 自動マクロセーブ\n\n", "\n# Automatic macro dump\n\n"));
 
     for (int i = 0; i < macro__num; i++) {
-        ascii_to_text(buf, macro__act[i]);
+        ascii_to_text(buf, macro__act[i], sizeof(buf));
         auto_dump_printf(*fpp, "A:%s\n", buf);
-        ascii_to_text(buf, macro__pat[i]);
+        ascii_to_text(buf, macro__pat[i], sizeof(buf));
         auto_dump_printf(*fpp, "P:%s\n", buf);
         auto_dump_printf(*fpp, "\n");
     }
@@ -55,20 +57,20 @@ static void macro_dump(FILE **fpp, concptr fname)
 static void do_cmd_macro_aux(char *buf)
 {
     flush();
-    inkey_base = TRUE;
+    inkey_base = true;
     char i = inkey();
     int n = 0;
     while (i) {
         buf[n++] = i;
-        inkey_base = TRUE;
-        inkey_scan = TRUE;
+        inkey_base = true;
+        inkey_scan = true;
         i = inkey();
     }
 
     buf[n] = '\0';
     flush();
     char tmp[1024];
-    ascii_to_text(tmp, buf);
+    ascii_to_text(tmp, buf, sizeof(tmp));
     term_addstr(-1, TERM_WHITE, tmp);
 }
 
@@ -88,7 +90,7 @@ static void do_cmd_macro_aux_keymap(char *buf)
     flush();
     buf[0] = inkey();
     buf[1] = '\0';
-    ascii_to_text(tmp, buf);
+    ascii_to_text(tmp, buf, sizeof(tmp));
     term_addstr(-1, TERM_WHITE, tmp);
     flush();
 }
@@ -114,20 +116,22 @@ static errr keymap_dump(concptr fname)
     }
 
     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, fname);
-    if (!open_auto_dump(&auto_dump_stream, buf, mark))
+    if (!open_auto_dump(&auto_dump_stream, buf, mark)) {
         return -1;
+    }
 
     auto_dump_printf(auto_dump_stream, _("\n# 自動キー配置セーブ\n\n", "\n# Automatic keymap dump\n\n"));
     for (int i = 0; i < 256; i++) {
         concptr act;
         act = keymap_act[mode][i];
-        if (!act)
+        if (!act) {
             continue;
+        }
 
         buf[0] = (char)i;
         buf[1] = '\0';
-        ascii_to_text(key, buf);
-        ascii_to_text(buf, act);
+        ascii_to_text(key, buf, sizeof(key));
+        ascii_to_text(buf, act, sizeof(buf));
         auto_dump_printf(auto_dump_stream, "A:%s\n", buf);
         auto_dump_printf(auto_dump_stream, "C:%d:%s\n", mode, key);
     }
@@ -146,16 +150,17 @@ static errr keymap_dump(concptr fname)
  * Could use some helpful instructions on this page.
  * </pre>
  */
-void do_cmd_macros(player_type *creature_ptr)
+void do_cmd_macros(PlayerType *player_ptr)
 {
     char tmp[1024];
     char buf[1024];
+    static char macro_buf[1024];
     FILE *auto_dump_stream;
     BIT_FLAGS mode = rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG;
     screen_save();
+    term_clear();
 
     auto print_macro_menu = [] {
-        term_clear();
         prt(_("[ マクロの設定 ]", "Interact with Macros"), 2, 0);
         prt(_("(1) ユーザー設定ファイルのロード", "(1) Load a user pref file"), 4, 5);
         prt(_("(2) ファイルにマクロを追加", "(2) Append macros to a file"), 5, 5);
@@ -173,31 +178,35 @@ void do_cmd_macros(player_type *creature_ptr)
     while (true) {
         msg_print(_("コマンド: ", "Command: "));
         const int key = inkey();
-        if (key == ESCAPE)
+        if (key == ESCAPE) {
             break;
-        msg_erase();
+        }
+        clear_from(1);
         print_macro_menu();
 
         if (key == '1') {
             prt(_("コマンド: ユーザー設定ファイルのロード", "Command: Load a user pref file"), 16, 0);
             prt(_("ファイル: ", "File: "), 18, 0);
-            sprintf(tmp, "%s.prf", creature_ptr->base_name);
-            if (!askfor(tmp, 80))
+            strnfmt(tmp, sizeof(tmp), "%s.prf", player_ptr->base_name);
+            if (!askfor(tmp, 80)) {
                 continue;
+            }
 
-            errr err = process_pref_file(creature_ptr, tmp);
-            if (-2 == err)
+            errr err = process_pref_file(player_ptr, tmp, true);
+            if (-2 == err) {
                 msg_format(_("標準の設定ファイル'%s'を読み込みました。", "Loaded default '%s'."), tmp);
-            else if (err)
+            } else if (err) {
                 msg_format(_("'%s'の読み込みに失敗しました!", "Failed to load '%s'!"), tmp);
-            else
+            } else {
                 msg_format(_("'%s'を読み込みました。", "Loaded '%s'."), tmp);
+            }
         } else if (key == '2') {
             prt(_("コマンド: マクロをファイルに追加する", "Command: Append macros to a file"), 16, 0);
             prt(_("ファイル: ", "File: "), 18, 0);
-            sprintf(tmp, "%s.prf", creature_ptr->base_name);
-            if (!askfor(tmp, 80))
+            strnfmt(tmp, sizeof(tmp), "%s.prf", player_ptr->base_name);
+            if (!askfor(tmp, 80)) {
                 continue;
+            }
 
             macro_dump(&auto_dump_stream, tmp);
             msg_print(_("マクロを追加しました。", "Appended macros."));
@@ -210,10 +219,12 @@ void do_cmd_macros(player_type *creature_ptr)
             if (k < 0) {
                 msg_print(_("そのキーにはマクロは定義されていません。", "Found no macro."));
             } else {
+                // マクロの作成時に参照するためmacro_bufにコピーする
+                strncpy(macro_buf, macro__act[k].data(), sizeof(macro_buf) - 1);
                 // too long macro must die
-                strncpy(tmp, macro__act[k], 80);
+                strncpy(tmp, macro_buf, 80);
                 tmp[80] = '\0';
-                ascii_to_text(buf, tmp);
+                ascii_to_text(buf, tmp, sizeof(buf));
                 prt(buf, 22, 0);
                 msg_print(_("マクロを確認しました。", "Found a macro."));
             }
@@ -226,10 +237,12 @@ void do_cmd_macros(player_type *creature_ptr)
                     "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
                 22, 0);
             prt(_("マクロ行動: ", "Action: "), 20, 0);
-            tmp[0] = '\0';
+            // 最後に参照したマクロデータを元に作成する(コピーを行えるように)
+            macro_buf[80] = '\0';
+            ascii_to_text(tmp, macro_buf, sizeof(tmp));
             if (askfor(tmp, 80)) {
-                text_to_ascii(macro__buf, tmp);
-                macro_add(buf, macro__buf);
+                text_to_ascii(macro_buf, tmp, sizeof(macro_buf));
+                macro_add(buf, macro_buf);
                 msg_print(_("マクロを追加しました。", "Added a macro."));
             }
         } else if (key == '5') {
@@ -241,9 +254,10 @@ void do_cmd_macros(player_type *creature_ptr)
         } else if (key == '6') {
             prt(_("コマンド: キー配置をファイルに追加する", "Command: Append keymaps to a file"), 16, 0);
             prt(_("ファイル: ", "File: "), 18, 0);
-            sprintf(tmp, "%s.prf", creature_ptr->base_name);
-            if (!askfor(tmp, 80))
+            strnfmt(tmp, sizeof(tmp), "%s.prf", player_ptr->base_name);
+            if (!askfor(tmp, 80)) {
                 continue;
+            }
 
             (void)keymap_dump(tmp);
             msg_print(_("キー配置を追加しました。", "Appended keymaps."));
@@ -256,10 +270,12 @@ void do_cmd_macros(player_type *creature_ptr)
             if (!act) {
                 msg_print(_("キー配置は定義されていません。", "Found no keymap."));
             } else {
+                // マクロの作成時に参照するためmacro_bufにコピーする
+                strncpy(macro_buf, act, sizeof(macro_buf) - 1);
                 // too long macro must die
-                strncpy(tmp, act, 80);
+                strncpy(tmp, macro_buf, 80);
                 tmp[80] = '\0';
-                ascii_to_text(buf, tmp);
+                ascii_to_text(buf, tmp, sizeof(buf));
                 prt(buf, 22, 0);
                 msg_print(_("キー配置を確認しました。", "Found a keymap."));
             }
@@ -272,11 +288,13 @@ void do_cmd_macros(player_type *creature_ptr)
                     "Press Left/Right arrow keys to move cursor. Backspace/Delete to delete a char."),
                 22, 0);
             prt(_("行動: ", "Action: "), 20, 0);
-            tmp[0] = '\0';
+            // 最後に参照したマクロデータを元に作成する(コピーを行えるように)
+            macro_buf[80] = '\0';
+            ascii_to_text(tmp, macro_buf, sizeof(tmp));
             if (askfor(tmp, 80)) {
-                text_to_ascii(macro__buf, tmp);
+                text_to_ascii(macro_buf, tmp, sizeof(macro_buf));
                 string_free(keymap_act[mode][(byte)(buf[0])]);
-                keymap_act[mode][(byte)(buf[0])] = string_make(macro__buf);
+                keymap_act[mode][(byte)(buf[0])] = string_make(macro_buf);
                 msg_print(_("キー配置を追加しました。", "Added a keymap."));
             }
         } else if (key == '9') {
@@ -284,7 +302,7 @@ void do_cmd_macros(player_type *creature_ptr)
             prt(_("押すキー: ", "Keypress: "), 18, 0);
             do_cmd_macro_aux_keymap(buf);
             string_free(keymap_act[mode][(byte)(buf[0])]);
-            keymap_act[mode][(byte)(buf[0])] = NULL;
+            keymap_act[mode][(byte)(buf[0])] = nullptr;
             msg_print(_("キー配置を削除しました。", "Removed a keymap."));
         } else if (key == '0') {
             prt(_("コマンド: マクロ行動の入力", "Command: Enter a new action"), 16, 0);
@@ -294,13 +312,16 @@ void do_cmd_macros(player_type *creature_ptr)
                 22, 0);
             prt(_("マクロ行動: ", "Action: "), 20, 0);
             buf[0] = '\0';
-            if (!askfor(buf, 80))
+            if (!askfor(buf, 80)) {
                 continue;
+            }
 
-            text_to_ascii(macro__buf, buf);
+            text_to_ascii(macro_buf, buf, sizeof(macro_buf));
         } else {
             bell();
         }
+
+        msg_erase();
     }
 
     screen_load();