OSDN Git Service

[Refactor] #39964 Removed the dependency from read-pref-file to autopick-pref-processor
[hengband/hengband.git] / src / io / read-pref-file.c
index f318542..bf62852 100644 (file)
@@ -1,6 +1,24 @@
-#include "io/read-pref-file.h"
-#include "io/process-pref-file.h"
-#include "autopick.h"
+/*
+ * @file cmd-dump.c
+ * @brief プレイヤーのインターフェイスに関するコマンドの実装 / Interface commands
+ * @date 2020/03/01
+ * @author Mogami & Hourier
+ * -Mogami-
+ * remove_auto_dump(orig_file, mark)
+ *     Remove the old automatic dump of type "mark".
+ * auto_dump_printf(fmt, ...)
+ *     Dump a formatted string using fprintf().
+ * open_auto_dump(buf, mark)
+ *     Open a file, remove old dump, and add new header.
+ * close_auto_dump(void)
+ *     Add a footer, and close the file.
+ */
+
+#include "angband.h"
+#include "io/dump-remover.h"
+#include "io/read-pref-file.h"
+#include "io/interpret-pref-file.h"
+#include "autopick/autopick-pref-processor.h"
 #include "files.h" // 暫定。コールバック化して後で消す.
 #include "world.h"
 
@@ -9,6 +27,14 @@
 #define PREF_TYPE_AUTOPICK 1
 #define PREF_TYPE_HISTPREF 2
 
+char auto_dump_header[] = "# vvvvvvv== %s ==vvvvvvv";
+char auto_dump_footer[] = "# ^^^^^^^== %s ==^^^^^^^";
+
+// Mark strings for auto dump
+
+// Variables for auto dump
+static int auto_dump_line_num;
+
 /*!
  * todo 関数名を変更する
  * @brief process_pref_fileのサブルーチン /
@@ -18,7 +44,7 @@
  * @param preftype prefファイルのタイプ
  * @return エラーコード
  */
-static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype)
+static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype, void(*process_autopick_file_command)(char*))
 {
        FILE *fp;
        fp = my_fopen(name, "r");
@@ -65,13 +91,13 @@ static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int p
                        switch (preftype)
                        {
                        case PREF_TYPE_AUTOPICK:
-                               (void)process_autopick_file(creature_ptr, buf + 2);
+                               (void)process_autopick_file(creature_ptr, buf + 2, process_autopick_file_command);
                                break;
                        case PREF_TYPE_HISTPREF:
-                               (void)process_histpref_file(creature_ptr, buf + 2);
+                               (void)process_histpref_file(creature_ptr, buf + 2, process_autopick_file_command);
                                break;
                        default:
-                               (void)process_pref_file(creature_ptr, buf + 2);
+                               (void)process_pref_file(creature_ptr, buf + 2, process_autopick_file_command);
                                break;
                        }
 
@@ -79,12 +105,14 @@ static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int p
                        continue;
                }
 
-               err = process_pref_file_command(creature_ptr, buf);
+               err = interpret_pref_file(creature_ptr, buf);
                if (err != 0)
                {
                        if (preftype != PREF_TYPE_AUTOPICK)
                                break;
-                       err = process_autopick_file_command(buf);
+                       
+                       (*process_autopick_file_command)(buf);
+                       err = 0;
                }
        }
 
@@ -116,16 +144,16 @@ static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int p
  * allow conditional evaluation and filename inclusion.
  * </pre>
  */
-errr process_pref_file(player_type *creature_ptr, concptr name)
+errr process_pref_file(player_type *creature_ptr, concptr name, void(*process_autopick_file_command)(char*))
 {
        char buf[1024];
        path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name);
 
-       errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
+       errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL, process_autopick_file_command);
        if (err1 > 0) return err1;
 
        path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
-       errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
+       errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL, process_autopick_file_command);
        if (err2 < 0 && !err1)
                return -2;
 
@@ -139,11 +167,11 @@ errr process_pref_file(player_type *creature_ptr, concptr name)
  * @param name ファイル名
  * @details
  */
-errr process_autopick_file(player_type *creature_ptr, concptr name)
+errr process_autopick_file(player_type *creature_ptr, concptr name, void(*process_autopick_file_command)(char*))
 {
        char buf[1024];
        path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
-       errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK);
+       errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK, process_autopick_file_command);
        return err;
 }
 
@@ -156,7 +184,7 @@ errr process_autopick_file(player_type *creature_ptr, concptr name)
  * @return エラーコード
  * @details
  */
-errr process_histpref_file(player_type *creature_ptr, concptr name)
+errr process_histpref_file(player_type *creature_ptr, concptr name, void(*process_autopick_file_command)(char*))
 {
        bool old_character_xtra = current_world_ptr->character_xtra;
        char buf[1024];
@@ -164,7 +192,76 @@ errr process_histpref_file(player_type *creature_ptr, concptr name)
 
        /* Hack -- prevent modification birth options in this file */
        current_world_ptr->character_xtra = TRUE;
-       errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF);
+       errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF, process_autopick_file_command);
        current_world_ptr->character_xtra = old_character_xtra;
        return err;
 }
+
+
+/*!
+ * @brief prfファイルのフォーマットに従った内容を出力する /
+ * Dump a formatted line, using "vstrnfmt()".
+ * @param fmt 出力内容
+ */
+void auto_dump_printf(FILE *auto_dump_stream, concptr fmt, ...)
+{
+       va_list vp;
+       char buf[1024];
+       va_start(vp, fmt);
+       (void)vstrnfmt(buf, sizeof(buf), fmt, vp);
+       va_end(vp);
+       for (concptr p = buf; *p; p++)
+       {
+               if (*p == '\n') auto_dump_line_num++;
+       }
+
+       fprintf(auto_dump_stream, "%s", buf);
+}
+
+
+/*!
+ * @brief prfファイルをファイルオープンする /
+ * Open file to append auto dump.
+ * @param buf ファイル名
+ * @param mark 出力するヘッダマーク
+ * @return ファイルポインタを取得できたらTRUEを返す
+ */
+bool open_auto_dump(FILE **fpp, concptr buf, concptr mark)
+{
+       char header_mark_str[80];
+       concptr auto_dump_mark = mark;
+       sprintf(header_mark_str, auto_dump_header, auto_dump_mark);
+       remove_auto_dump(buf, mark);
+       *fpp = my_fopen(buf, "a");
+       if (!fpp)
+       {
+               msg_format(_("%s を開くことができませんでした。", "Failed to open %s."), buf);
+               msg_print(NULL);
+               return FALSE;
+       }
+
+       fprintf(*fpp, "%s\n", header_mark_str);
+       auto_dump_line_num = 0;
+       auto_dump_printf(*fpp, _("# *警告!!* 以降の行は自動生成されたものです。\n",
+               "# *Warning!*  The lines below are an automatic dump.\n"));
+       auto_dump_printf(*fpp, _("# *警告!!* 後で自動的に削除されるので編集しないでください。\n",
+               "# Don't edit them; changes will be deleted and replaced automatically.\n"));
+       return TRUE;
+}
+
+/*!
+ * @brief prfファイルをファイルクローズする /
+ * Append foot part and close auto dump.
+ * @return なし
+ */
+void close_auto_dump(FILE **fpp, concptr auto_dump_mark)
+{
+       char footer_mark_str[80];
+       sprintf(footer_mark_str, auto_dump_footer, auto_dump_mark);
+       auto_dump_printf(*fpp, _("# *警告!!* 以降の行は自動生成されたものです。\n",
+               "# *Warning!*  The lines below are an automatic dump.\n"));
+       auto_dump_printf(*fpp, _("# *警告!!* 後で自動的に削除されるので編集しないでください。\n",
+               "# Don't edit them; changes will be deleted and replaced automatically.\n"));
+       fprintf(*fpp, "%s (%d)\n", footer_mark_str, auto_dump_line_num);
+       my_fclose(*fpp);
+}