OSDN Git Service

[Refactor] #39964 Separated autopick-reader-writer.c/h from autopick.c
authorHourier <hourier@users.sourceforge.jp>
Sat, 25 Apr 2020 13:18:57 +0000 (22:18 +0900)
committerHourier <hourier@users.sourceforge.jp>
Sat, 25 Apr 2020 13:18:57 +0000 (22:18 +0900)
Hengband_vcs2017/Hengband/Hengband.vcxproj
Hengband_vcs2017/Hengband/Hengband.vcxproj.filters
src/Makefile.am
src/autopick/autopick-reader-writer.c [new file with mode: 0644]
src/autopick/autopick-reader-writer.h [new file with mode: 0644]
src/autopick/autopick.c

index 7b8bf81..585efdf 100644 (file)
     <ClCompile Include="..\..\src\autopick\autopick-initializer.c" />\r
     <ClCompile Include="..\..\src\autopick\autopick-matcher.c" />\r
     <ClCompile Include="..\..\src\autopick\autopick-menu-data-table.c" />\r
+    <ClCompile Include="..\..\src\autopick\autopick-reader-writer.c" />\r
     <ClCompile Include="..\..\src\autopick\autopick-util.c" />\r
     <ClCompile Include="..\..\src\autopick\autopick.c" />\r
     <ClCompile Include="..\..\src\avatar.c" />\r
     <ClInclude Include="..\..\src\autopick\autopick-matcher.h" />\r
     <ClInclude Include="..\..\src\autopick\autopick-menu-data-table.h" />\r
     <ClInclude Include="..\..\src\autopick\autopick-methods-table.h" />\r
+    <ClInclude Include="..\..\src\autopick\autopick-reader-writer.h" />\r
     <ClInclude Include="..\..\src\autopick\autopick-util.h" />\r
     <ClInclude Include="..\..\src\autopick\autopick.h" />\r
     <ClInclude Include="..\..\src\avatar.h" />\r
index 8cfebcf..a6221ce 100644 (file)
     <ClCompile Include="..\..\src\autopick\autopick-destroyer.c">
       <Filter>autopick</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\src\autopick\autopick-reader-writer.c">
+      <Filter>autopick</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\gamevalue.h" />
     <ClInclude Include="..\..\src\autopick\autopick-destroyer.h">
       <Filter>autopick</Filter>
     </ClInclude>
+    <ClInclude Include="..\..\src\autopick\autopick-reader-writer.h">
+      <Filter>autopick</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="..\..\src\wall.bmp" />
index 899dff6..ee57b71 100644 (file)
@@ -21,6 +21,7 @@ hengband_SOURCES = \
        autopick/autopick-matcher.c autopick/autopick-matcher.h \
        autopick-describer.c autopick-describer.h \
        autopick-destroyer.c autopick-destroyer.h \
+       autopick/autopick-reader-writer.c autopick/autopick-reader-writer.h \
        \
        avatar.h avatar.c birth.c birth.h \
        \
diff --git a/src/autopick/autopick-reader-writer.c b/src/autopick/autopick-reader-writer.c
new file mode 100644 (file)
index 0000000..bba89fc
--- /dev/null
@@ -0,0 +1,182 @@
+#include "angband.h"
+#include "autopick/autopick-util.h"
+#include "autopick/autopick-reader-writer.h"
+#include "files.h"
+
+/*
+ *  Get file name for autopick preference
+ */
+concptr pickpref_filename(player_type *player_ptr, int filename_mode)
+{
+       static const char namebase[] = _("picktype", "pickpref");
+
+       switch (filename_mode)
+       {
+       case PT_DEFAULT:
+               return format("%s.prf", namebase);
+
+       case PT_WITH_PNAME:
+               return format("%s-%s.prf", namebase, player_ptr->base_name);
+
+       default:
+               return NULL;
+       }
+}
+
+
+/*
+ * Read whole lines of a file to memory
+ */
+static concptr *read_text_lines(concptr filename)
+{
+       concptr *lines_list = NULL;
+       FILE *fff;
+
+       int lines = 0;
+       char buf[1024];
+
+       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
+       fff = my_fopen(buf, "r");
+       if (!fff) return NULL;
+
+       C_MAKE(lines_list, MAX_LINES, concptr);
+       while (my_fgets(fff, buf, sizeof(buf)) == 0)
+       {
+               lines_list[lines++] = string_make(buf);
+               if (lines >= MAX_LINES - 1) break;
+       }
+
+       if (lines == 0)
+               lines_list[0] = string_make("");
+
+       my_fclose(fff);
+       return lines_list;
+}
+
+
+/*
+ * Add one line to autopick_list[]
+ */
+void add_autopick_list(autopick_type *entry)
+{
+       if (max_autopick >= max_max_autopick)
+       {
+               int old_max_max_autopick = max_max_autopick;
+               autopick_type *old_autopick_list = autopick_list;
+               max_max_autopick += MAX_AUTOPICK_DEFAULT;
+               C_MAKE(autopick_list, max_max_autopick, autopick_type);
+               (void)C_COPY(autopick_list, old_autopick_list, old_max_max_autopick, autopick_type);
+               C_KILL(old_autopick_list, old_max_max_autopick, autopick_type);
+       }
+
+       autopick_list[max_autopick] = *entry;
+       max_autopick++;
+}
+
+
+/*
+ * Copy the default autopick file to the user directory
+ */
+static void prepare_default_pickpref(player_type *player_ptr)
+{
+       const concptr messages[] = {
+               _("\82 \82È\82½\82Í\81u\8e©\93®\8fE\82¢\83G\83f\83B\83^\81v\82ð\8f\89\82ß\82Ä\8bN\93®\82µ\82Ü\82µ\82½\81B", "You have activated the Auto-Picker Editor for the first time."),
+               _("\8e©\93®\8fE\82¢\82Ì\83\86\81[\83U\81[\90Ý\92è\83t\83@\83C\83\8b\82ª\82Ü\82¾\8f\91\82©\82ê\82Ä\82¢\82È\82¢\82Ì\82Å\81A", "Since user pref file for autopick is not yet created,"),
+               _("\8aî\96{\93I\82È\8e©\93®\8fE\82¢\90Ý\92è\83t\83@\83C\83\8b\82ðlib/pref/picktype.prf\82©\82ç\83R\83s\81[\82µ\82Ü\82·\81B", "the default setting is loaded from lib/pref/pickpref.prf ."),
+               NULL
+       };
+
+       concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
+       for (int i = 0; messages[i]; i++)
+       {
+               msg_print(messages[i]);
+       }
+
+       msg_print(NULL);
+       char buf[1024];
+       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
+       FILE *user_fp;
+       user_fp = my_fopen(buf, "w");
+       if (!user_fp) return;
+
+       fprintf(user_fp, "#***\n");
+       for (int i = 0; messages[i]; i++)
+       {
+               fprintf(user_fp, "#***  %s\n", messages[i]);
+       }
+
+       fprintf(user_fp, "#***\n\n\n");
+       path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
+       FILE *pref_fp;
+       pref_fp = my_fopen(buf, "r");
+
+       if (!pref_fp)
+       {
+               my_fclose(user_fp);
+               return;
+       }
+
+       while (!my_fgets(pref_fp, buf, sizeof(buf)))
+       {
+               fprintf(user_fp, "%s\n", buf);
+       }
+
+       my_fclose(user_fp);
+       my_fclose(pref_fp);
+}
+
+/*
+ * Read an autopick prefence file to memory
+ * Prepare default if no user file is found
+ */
+concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
+{
+       /* Try a filename with player name */
+       *filename_mode_p = PT_WITH_PNAME;
+       char buf[1024];
+       strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
+       concptr *lines_list;
+       lines_list = read_text_lines(buf);
+
+       if (!lines_list)
+       {
+               *filename_mode_p = PT_DEFAULT;
+               strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
+               lines_list = read_text_lines(buf);
+       }
+
+       if (!lines_list)
+       {
+               prepare_default_pickpref(player_ptr);
+               lines_list = read_text_lines(buf);
+       }
+
+       if (!lines_list)
+       {
+               C_MAKE(lines_list, MAX_LINES, concptr);
+               lines_list[0] = string_make("");
+       }
+
+       return lines_list;
+}
+
+
+/*
+ * Write whole lines of memory to a file.
+ */
+bool write_text_lines(concptr filename, concptr *lines_list)
+{
+       char buf[1024];
+       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
+       FILE *fff;
+       fff = my_fopen(buf, "w");
+       if (!fff) return FALSE;
+
+       for (int lines = 0; lines_list[lines]; lines++)
+       {
+               my_fputs(fff, lines_list[lines], 1024);
+       }
+
+       my_fclose(fff);
+       return TRUE;
+}
diff --git a/src/autopick/autopick-reader-writer.h b/src/autopick/autopick-reader-writer.h
new file mode 100644 (file)
index 0000000..24039c7
--- /dev/null
@@ -0,0 +1,6 @@
+#pragma once
+
+concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p);
+bool write_text_lines(concptr filename, concptr *lines_list);
+concptr pickpref_filename(player_type *player_ptr, int filename_mode);
+void add_autopick_list(autopick_type *entry);
index 1797ca1..73f88e2 100644 (file)
@@ -25,6 +25,7 @@
 #include "autopick/autopick-destroyer.h"
 #include "autopick/autopick-describer.h"
 #include "autopick/autopick-entry.h"
+#include "autopick/autopick-reader-writer.h"
 #include "gameterm.h"
 #include "autopick/autopick.h"
 #include "core.h"
 #include "player-class.h"
 #include "player-race.h"
 #include "player-inventory.h"
-#include "view/display-player.h"
+#include "view/display-player.h" // 暫定。後で消す.
 #include "object/object-kind.h"
 #include "object-ego.h"
 #include "object-flavor.h"
 #include "object-hook.h"
 
-#include "files.h"
 #include "floor.h"
 #include "world.h"
 #include "monster.h"
 #include "monsterrace.h"
-#include "view/display-main-window.h" // 暫定。後で消す
-
-/*
- *  Get file name for autopick preference
- */
-static concptr pickpref_filename(player_type *player_ptr, int filename_mode)
-{
-       static const char namebase[] = _("picktype", "pickpref");
-
-       switch (filename_mode)
-       {
-       case PT_DEFAULT:
-               return format("%s.prf", namebase);
-
-       case PT_WITH_PNAME:
-               return format("%s-%s.prf", namebase, player_ptr->base_name);
-
-       default:
-               return NULL;
-       }
-}
-
+#include "view/display-main-window.h" // 暫定。後で消す.
 
 /*
  * Load an autopick preference file
@@ -106,26 +85,6 @@ void autopick_load_pref(player_type *player_ptr, bool disp_mes)
 
 
 /*
- * Add one line to autopick_list[]
- */
-static void add_autopick_list(autopick_type *entry)
-{
-       if (max_autopick >= max_max_autopick)
-       {
-               int old_max_max_autopick = max_max_autopick;
-               autopick_type *old_autopick_list = autopick_list;
-               max_max_autopick += MAX_AUTOPICK_DEFAULT;
-               C_MAKE(autopick_list, max_max_autopick, autopick_type);
-               (void)C_COPY(autopick_list, old_autopick_list, old_max_max_autopick, autopick_type);
-               C_KILL(old_autopick_list, old_max_max_autopick, autopick_type);
-       }
-
-       autopick_list[max_autopick] = *entry;
-       max_autopick++;
-}
-
-
-/*
  *  Process line for auto picker/destroyer.
  */
 void process_autopick_file_command(char *buf)
@@ -528,144 +487,6 @@ bool autopick_autoregister(player_type *player_ptr, object_type *o_ptr)
 
 
 /*
- * Read whole lines of a file to memory
- */
-static concptr *read_text_lines(concptr filename)
-{
-       concptr *lines_list = NULL;
-       FILE *fff;
-
-       int lines = 0;
-       char buf[1024];
-
-       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
-       fff = my_fopen(buf, "r");
-       if (!fff) return NULL;
-
-       C_MAKE(lines_list, MAX_LINES, concptr);
-       while (my_fgets(fff, buf, sizeof(buf)) == 0)
-       {
-               lines_list[lines++] = string_make(buf);
-               if (lines >= MAX_LINES - 1) break;
-       }
-
-       if (lines == 0)
-               lines_list[0] = string_make("");
-
-       my_fclose(fff);
-       return lines_list;
-}
-
-
-/*
- * Copy the default autopick file to the user directory
- */
-static void prepare_default_pickpref(player_type *player_ptr)
-{
-       const concptr messages[] = {
-               _("あなたは「自動拾いエディタ」を初めて起動しました。", "You have activated the Auto-Picker Editor for the first time."),
-               _("自動拾いのユーザー設定ファイルがまだ書かれていないので、", "Since user pref file for autopick is not yet created,"),
-               _("基本的な自動拾い設定ファイルをlib/pref/picktype.prfからコピーします。", "the default setting is loaded from lib/pref/pickpref.prf ."),
-               NULL
-       };
-
-       concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
-       for (int i = 0; messages[i]; i++)
-       {
-               msg_print(messages[i]);
-       }
-
-       msg_print(NULL);
-       char buf[1024];
-       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
-       FILE *user_fp;
-       user_fp = my_fopen(buf, "w");
-       if (!user_fp) return;
-
-       fprintf(user_fp, "#***\n");
-       for (int i = 0; messages[i]; i++)
-       {
-               fprintf(user_fp, "#***  %s\n", messages[i]);
-       }
-
-       fprintf(user_fp, "#***\n\n\n");
-       path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
-       FILE *pref_fp;
-       pref_fp = my_fopen(buf, "r");
-
-       if (!pref_fp)
-       {
-               my_fclose(user_fp);
-               return;
-       }
-
-       while (!my_fgets(pref_fp, buf, sizeof(buf)))
-       {
-               fprintf(user_fp, "%s\n", buf);
-       }
-
-       my_fclose(user_fp);
-       my_fclose(pref_fp);
-}
-
-/*
- * Read an autopick prefence file to memory
- * Prepare default if no user file is found
- */
-static concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
-{
-       /* Try a filename with player name */
-       *filename_mode_p = PT_WITH_PNAME;
-       char buf[1024];
-       strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
-       concptr *lines_list;
-       lines_list = read_text_lines(buf);
-
-       if (!lines_list)
-       {
-               *filename_mode_p = PT_DEFAULT;
-               strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
-               lines_list = read_text_lines(buf);
-       }
-
-       if (!lines_list)
-       {
-               prepare_default_pickpref(player_ptr);
-               lines_list = read_text_lines(buf);
-       }
-
-       if (!lines_list)
-       {
-               C_MAKE(lines_list, MAX_LINES, concptr);
-               lines_list[0] = string_make("");
-       }
-
-       return lines_list;
-}
-
-
-/*
- * Write whole lines of memory to a file.
- */
-static bool write_text_lines(concptr filename, concptr *lines_list)
-{
-       char buf[1024];
-       path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
-       FILE *fff;
-       fff = my_fopen(buf, "w");
-       if (!fff) return FALSE;
-
-       for (int lines = 0; lines_list[lines]; lines++)
-       {
-               my_fputs(fff, lines_list[lines], 1024);
-       }
-
-       my_fclose(fff);
-       return TRUE;
-}
-
-
-/*
  * Delete or insert string
  */
 static void toggle_keyword(text_body_type *tb, BIT_FLAGS flg)