From d6d1ae960c822409bfcd3c978902c68576b1cac0 Mon Sep 17 00:00:00 2001 From: Hourier Date: Sat, 25 Apr 2020 11:27:11 +0900 Subject: [PATCH 1/1] [Refactor] #40236 Unified duplicatied functions --- Hengband_vcs2017/Hengband/Hengband.vcxproj | 3 + Hengband_vcs2017/Hengband/Hengband.vcxproj.filters | 9 ++ src/Makefile.am | 4 +- src/cmd/cmd-dump.c | 97 +-------------------- src/io/dump-remover.c | 99 ++++++++++++++++++++++ src/io/dump-remover.h | 3 + src/io/read-pref-file.c | 99 +--------------------- 7 files changed, 119 insertions(+), 195 deletions(-) create mode 100644 src/io/dump-remover.c create mode 100644 src/io/dump-remover.h diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj b/Hengband_vcs2017/Hengband/Hengband.vcxproj index e704c7e02..871cf5e1c 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj @@ -151,12 +151,14 @@ + + @@ -304,6 +306,7 @@ + diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters index d4f720c7d..ea481285b 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters @@ -637,6 +637,12 @@ autopick + + autopick + + + io + @@ -1280,6 +1286,9 @@ autopick + + io + diff --git a/src/Makefile.am b/src/Makefile.am index ac49422fa..c33909fd8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,8 @@ hengband_SOURCES = \ autopick/autopick-editor-table.h autopick/autopick-flags-table.h \ autopick/autopick-key-flag-process.h autopick/autopick-key-table.h \ autopick/autopick-menu-data-table.c autopick/autopick-menu-data-table.h \ - autopick/autopick-methods-table.h autopick/autopick-util.h \ + autopick/autopick-methods-table.h \ + autopick/autopick-util.c autopick/autopick-util.h \ autopick/autopick-entry.c autopick/autopick-entry.h \ \ avatar.h avatar.c birth.c birth.h \ @@ -52,6 +53,7 @@ hengband_SOURCES = \ player/permanent-resistances.c player/permanent-resistances.h \ player/temporary-resistances.c player/temporary-resistances.h \ \ + io/dump-remover.c io/dump-remover.h \ io/gf-descriptions.c io/gf-descriptions.h \ io/tokenizer.c io/tokenizer.h \ io/interpret-pref-file.c io/interpret-pref-file.h \ diff --git a/src/cmd/cmd-dump.c b/src/cmd/cmd-dump.c index fb812de95..5dac742d3 100644 --- a/src/cmd/cmd-dump.c +++ b/src/cmd/cmd-dump.c @@ -35,6 +35,7 @@ #include "cmd/dump-util.h" #include "gameterm.h" #include "core.h" // 暫定。後で消す. +#include "io/dump-remover.h" #include "io/read-pref-file.h" #include "io/interpret-pref-file.h" @@ -49,102 +50,6 @@ #include "chuukei.h" -/*! - * @brief prefファイルを選択して処理する / - * Ask for a "user pref line" and process it - * @brief prf出力内容を消去する / - * Remove old lines automatically generated before. - * @param orig_file 消去を行うファイル名 - */ -static void remove_auto_dump(concptr orig_file, concptr auto_dump_mark) -{ - char buf[1024]; - bool between_mark = FALSE; - bool changed = FALSE; - int line_num = 0; - long header_location = 0; - 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; - - FILE *tmp_fff = NULL; - char tmp_file[FILE_NAME_SIZE]; - if (!open_temporary_file(&tmp_fff, tmp_file)) return; - - 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); -} - - #ifdef JP #else /*! diff --git a/src/io/dump-remover.c b/src/io/dump-remover.c new file mode 100644 index 000000000..298382ecb --- /dev/null +++ b/src/io/dump-remover.c @@ -0,0 +1,99 @@ +#include "angband.h" +#include "cmd/dump-util.h" +#include "io/dump-remover.h" +#include "io/read-pref-file.h" + +/*! + * @brief prefファイルを選択して処理する / + * Ask for a "user pref line" and process it + * @brief prf出力内容を消去する / + * Remove old lines automatically generated before. + * @param orig_file 消去を行うファイル名 + */ +void remove_auto_dump(concptr orig_file, concptr auto_dump_mark) +{ + char buf[1024]; + bool between_mark = FALSE; + bool changed = FALSE; + int line_num = 0; + long header_location = 0; + 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; + + FILE *tmp_fff = NULL; + char tmp_file[FILE_NAME_SIZE]; + if (!open_temporary_file(&tmp_fff, tmp_file)) return; + + 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); +} diff --git a/src/io/dump-remover.h b/src/io/dump-remover.h new file mode 100644 index 000000000..f72098546 --- /dev/null +++ b/src/io/dump-remover.h @@ -0,0 +1,3 @@ +#pragma once + +void remove_auto_dump(concptr orig_file, concptr auto_dump_mark); diff --git a/src/io/read-pref-file.c b/src/io/read-pref-file.c index 11d483036..9bda35814 100644 --- a/src/io/read-pref-file.c +++ b/src/io/read-pref-file.c @@ -15,6 +15,7 @@ */ #include "angband.h" +#include "io/dump-remover.h" #include "io/read-pref-file.h" #include "io/interpret-pref-file.h" #include "autopick/autopick.h" @@ -198,104 +199,6 @@ errr process_histpref_file(player_type *creature_ptr, concptr name) /*! - * @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 出力内容 -- 2.11.0