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 11d4830..bf62852 100644 (file)
  */
 
 #include "angband.h"
+#include "io/dump-remover.h"
 #include "io/read-pref-file.h"
 #include "io/interpret-pref-file.h"
-#include "autopick/autopick.h"
+#include "autopick/autopick-pref-processor.h"
 #include "files.h" // 暫定。コールバック化して後で消す.
 #include "world.h"
 
@@ -43,7 +44,7 @@ static int auto_dump_line_num;
  * @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");
@@ -90,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;
                        }
 
@@ -110,7 +111,7 @@ static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int p
                        if (preftype != PREF_TYPE_AUTOPICK)
                                break;
                        
-                       process_autopick_file_command(buf);
+                       (*process_autopick_file_command)(buf);
                        err = 0;
                }
        }
@@ -143,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;
 
@@ -166,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;
 }
 
@@ -183,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];
@@ -191,111 +192,13 @@ 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出力内容を消去する /
- * Remove old lines automatically generated before.
- * @param orig_file 消去を行うファイル名
- */
-static void remove_auto_dump(concptr orig_file, concptr auto_dump_mark)
-{
-       char header_mark_str[80];
-       char footer_mark_str[80];
-       sprintf(header_mark_str, auto_dump_header, auto_dump_mark);
-       sprintf(footer_mark_str, auto_dump_footer, auto_dump_mark);
-       size_t mark_len = strlen(footer_mark_str);
-       FILE *orig_fff;
-       orig_fff = my_fopen(orig_file, "r");
-       if (!orig_fff) return;
-
-       char tmp_file[1024];
-       FILE *tmp_fff;
-       tmp_fff = my_fopen_temp(tmp_file, 1024);
-       if (!tmp_fff)
-       {
-               msg_format(_("一時ファイル %s を作成できませんでした。", "Failed to create temporary file %s."), tmp_file);
-               msg_print(NULL);
-               return;
-       }
-
-       char buf[1024];
-       bool between_mark = FALSE;
-       bool changed = FALSE;
-       int line_num = 0;
-       long header_location = 0;
-       while (TRUE)
-       {
-               if (my_fgets(orig_fff, buf, sizeof(buf)))
-               {
-                       if (between_mark)
-                       {
-                               fseek(orig_fff, header_location, SEEK_SET);
-                               between_mark = FALSE;
-                               continue;
-                       }
-                       else
-                       {
-                               break;
-                       }
-               }
-
-               if (!between_mark)
-               {
-                       if (!strcmp(buf, header_mark_str))
-                       {
-                               header_location = ftell(orig_fff);
-                               line_num = 0;
-                               between_mark = TRUE;
-                               changed = TRUE;
-                       }
-                       else
-                       {
-                               fprintf(tmp_fff, "%s\n", buf);
-                       }
-
-                       continue;
-               }
-
-               if (!strncmp(buf, footer_mark_str, mark_len))
-               {
-                       int tmp;
-                       if (!sscanf(buf + mark_len, " (%d)", &tmp)
-                               || tmp != line_num)
-                       {
-                               fseek(orig_fff, header_location, SEEK_SET);
-                       }
-
-                       between_mark = FALSE;
-                       continue;
-               }
-
-               line_num++;
-       }
-
-       my_fclose(orig_fff);
-       my_fclose(tmp_fff);
-
-       if (changed)
-       {
-               tmp_fff = my_fopen(tmp_file, "r");
-               orig_fff = my_fopen(orig_file, "w");
-               while (!my_fgets(tmp_fff, buf, sizeof(buf)))
-                       fprintf(orig_fff, "%s\n", buf);
-
-               my_fclose(orig_fff);
-               my_fclose(tmp_fff);
-       }
-
-       fd_kill(tmp_file);
-}
-
-
-/*!
  * @brief prfファイルのフォーマットに従った内容を出力する /
  * Dump a formatted line, using "vstrnfmt()".
  * @param fmt 出力内容