From: Hourier Date: Sun, 1 Mar 2020 12:38:08 +0000 (+0900) Subject: [Refactor] #39962 Separated read-pref-file.c/h from files.c/h X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;ds=sidebyside;h=bc0217b30d72c504f031bf9689d56330d2d83598;p=hengband%2Fhengband.git [Refactor] #39962 Separated read-pref-file.c/h from files.c/h --- diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj b/Hengband_vcs2017/Hengband/Hengband.vcxproj index bd540991a..a51e2b4cb 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj @@ -195,6 +195,7 @@ + @@ -342,6 +343,7 @@ + diff --git a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters index 4c699ddb4..1f34f04ea 100644 --- a/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters +++ b/Hengband_vcs2017/Hengband/Hengband.vcxproj.filters @@ -469,6 +469,9 @@ core + + io + @@ -920,6 +923,9 @@ core + + io + diff --git a/src/Makefile.am b/src/Makefile.am index d5d1f2741..62ca14638 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -30,6 +30,7 @@ hengband_SOURCES = \ io/gf-descriptions.c io/gf-descriptions.h \ io/tokenizer.c io/tokenizer.h \ io/process-pref-file.c io/process-pref-file.h \ + io/read-pref-file.c io/read-pref-file.h \ io/exit-panic.c io/exit-panic.h \ signal-handlers.c signal-handlers.h uid-checker.c uid-checker.h \ character-dump.c character-dump.h core.c core.h files.c files.h \ diff --git a/src/autopick.c b/src/autopick.c index 12ac66ce1..5cd168948 100644 --- a/src/autopick.c +++ b/src/autopick.c @@ -18,6 +18,7 @@ #include "core.h" #include "core/show-file.h" #include "cmd/cmd-save.h" +#include "io/read-pref-file.h" #include "mind.h" diff --git a/src/birth.c b/src/birth.c index a165883c9..a860be44b 100644 --- a/src/birth.c +++ b/src/birth.c @@ -16,6 +16,7 @@ #include "bldg.h" #include "core.h" #include "core/show-file.h" +#include "io/read-pref-file.h" #include "term.h" #include "object-ego.h" diff --git a/src/cmd/cmd-dump.c b/src/cmd/cmd-dump.c index f73dad023..a428bc52e 100644 --- a/src/cmd/cmd-dump.c +++ b/src/cmd/cmd-dump.c @@ -44,6 +44,7 @@ #include "term.h" #include "core.h" #include "core/show-file.h" +#include "io/read-pref-file.h" #include "chuukei.h" #include "autopick.h" diff --git a/src/core.c b/src/core.c index d899b6f45..b7a9c8cad 100644 --- a/src/core.c +++ b/src/core.c @@ -38,7 +38,7 @@ #include "cmd/cmd-zaprod.h" #include "cmd/cmd-zapwand.h" #include "cmd/cmd-pet.h" -#include "cmd-basic.h" +#include "cmd/cmd-basic.h" #include "racial.h" #include "snipe.h" #include "dungeon.h" @@ -89,6 +89,7 @@ #include "dungeon-file.h" #include "uid-checker.h" #include "player/process-death.h" +#include "io/read-pref-file.h" #include "files.h" #include "scores.h" #include "autopick.h" diff --git a/src/files.c b/src/files.c index db1763b99..07ad1b3ba 100644 --- a/src/files.c +++ b/src/files.c @@ -19,13 +19,10 @@ #include "character-dump.h" #include "world.h" -#include "view-mainwindow.h" -#include "autopick.h" +#include "view-mainwindow.h" // 暫定。後で消す. #include "io/process-pref-file.h" // 暫定。依存性の向きがこれで良いか要確認. - -#define PREF_TYPE_NORMAL 0 -#define PREF_TYPE_AUTOPICK 1 -#define PREF_TYPE_HISTPREF 2 +#include "io/process-pref-file.h" +#include "io/read-pref-file.h" concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms @@ -296,137 +293,6 @@ concptr process_pref_file_expr(player_type *creature_ptr, char **sp, char *fp) /*! - * @brief process_pref_fileのサブルーチン / - * Open the "user pref file" and parse it. - * @param creature_ptr プレーヤーへの参照ポインタ - * @param name 読み込むファイル名 - * @param preftype prefファイルのタイプ - * @return エラーコード - * @details - *
- * Input:
- *   v: output buffer array
- *   f: final character
- * Output:
- *   result
- * 
- */ -static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype) -{ - FILE *fp; - fp = my_fopen(name, "r"); - if (!fp) return -1; - - char buf[1024]; - char old[1024]; - int line = -1; - errr err = 0; - bool bypass = FALSE; - while (my_fgets(fp, buf, sizeof(buf)) == 0) - { - line++; - if (!buf[0]) continue; - -#ifdef JP - if (!iskanji(buf[0])) -#endif - if (iswspace(buf[0])) continue; - - if (buf[0] == '#') continue; - strcpy(old, buf); - - /* Process "?:" */ - if ((buf[0] == '?') && (buf[1] == ':')) - { - char f; - char *s; - s = buf + 2; - concptr v = process_pref_file_expr(creature_ptr, &s, &f); - bypass = streq(v, "0"); - continue; - } - - if (bypass) continue; - - /* Process "%:" */ - if (buf[0] == '%') - { - static int depth_count = 0; - if (depth_count > 20) continue; - - depth_count++; - switch (preftype) - { - case PREF_TYPE_AUTOPICK: - (void)process_autopick_file(creature_ptr, buf + 2); - break; - case PREF_TYPE_HISTPREF: - (void)process_histpref_file(creature_ptr, buf + 2); - break; - default: - (void)process_pref_file(creature_ptr, buf + 2); - break; - } - - depth_count--; - continue; - } - - err = process_pref_file_command(creature_ptr, buf); - if (err != 0) - { - if (preftype != PREF_TYPE_AUTOPICK) - break; - err = process_autopick_file_command(buf); - } - } - - if (err != 0) - { - /* Print error message */ - /* ToDo: Add better error messages */ - msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."), - _(name, err), line, _(err, name)); - msg_format(_("('%s'を解析中)", "Parsing '%s'"), old); - msg_print(NULL); - } - - my_fclose(fp); - return (err); -} - - -/*! - * @brief pref設定ファイルを読み込み設定を反映させる / - * Process the "user pref file" with the given name - * @param creature_ptr プレーヤーへの参照ポインタ - * @param name 読み込むファイル名 - * @return エラーコード - * @details - *
- * See the functions above for a list of legal "commands".
- * We also accept the special "?" and "%" directives, which
- * allow conditional evaluation and filename inclusion.
- * 
- */ -errr process_pref_file(player_type *creature_ptr, concptr name) -{ - char buf[1024]; - path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name); - - errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL); - 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); - if (err2 < 0 && !err1) - return -2; - - return err2; -} - - -/*! * @brief プレイヤーステータスをファイルダンプ出力する * Hack -- Dump a character description file * @param creature_ptr プレーヤーへの参照ポインタ @@ -579,43 +445,6 @@ errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count) /*! - * @brief 自動拾いファイルを読み込む / - * @param creature_ptr プレーヤーへの参照ポインタ - * @param name ファイル名 - * @details - */ -errr process_autopick_file(player_type *creature_ptr, concptr name) -{ - char buf[1024]; - path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); - errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK); - return err; -} - - -/*! - * @brief プレイヤーの生い立ちファイルを読み込む / - * Process file for player's history editor. - * @param creature_ptr プレーヤーへの参照ポインタ - * @param name ファイル名 - * @return エラーコード - * @details - */ -errr process_histpref_file(player_type *creature_ptr, concptr name) -{ - bool old_character_xtra = current_world_ptr->character_xtra; - char buf[1024]; - path_build(buf, sizeof(buf), ANGBAND_DIR_USER, 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); - current_world_ptr->character_xtra = old_character_xtra; - return err; -} - - -/*! * @brief ファイル位置をシーク / * @param creature_ptr プレーヤーへの参照ポインタ * @param fd ファイルディスクリプタ diff --git a/src/files.h b/src/files.h index 9b58cf698..427ad2948 100644 --- a/src/files.h +++ b/src/files.h @@ -22,9 +22,6 @@ typedef void(*display_player_pf)(player_type*, int, map_name_pf); extern errr file_character(player_type *creature_ptr, concptr name, display_player_pf display_player, map_name_pf map_name); extern concptr process_pref_file_expr(player_type *creature_ptr, char **sp, char *fp); -extern errr process_pref_file(player_type *creature_ptr, concptr name); -extern errr process_autopick_file(player_type *creature_ptr, concptr name); -extern errr process_histpref_file(player_type *creature_ptr, concptr name); extern errr get_rnd_line(concptr file_name, int entry, char *output); void read_dead_file(char* buf, size_t buf_size); diff --git a/src/init.c b/src/init.c index 286499c98..4146c76d7 100644 --- a/src/init.c +++ b/src/init.c @@ -35,6 +35,7 @@ #include "uid-checker.h" #include "files.h" #include "core.h" +#include "io/read-pref-file.h" #include "term.h" #include "artifact.h" diff --git a/src/io/read-pref-file.c b/src/io/read-pref-file.c new file mode 100644 index 000000000..f3185425b --- /dev/null +++ b/src/io/read-pref-file.c @@ -0,0 +1,170 @@ +#include "io/read-pref-file.h" +#include "io/process-pref-file.h" +#include "autopick.h" +#include "files.h" // 暫定。コールバック化して後で消す. +#include "world.h" + +// todo コールバック関数に変更するので、いずれ消す. +#define PREF_TYPE_NORMAL 0 +#define PREF_TYPE_AUTOPICK 1 +#define PREF_TYPE_HISTPREF 2 + +/*! + * todo 関数名を変更する + * @brief process_pref_fileのサブルーチン / + * Open the "user pref file" and parse it. + * @param creature_ptr プレーヤーへの参照ポインタ + * @param name 読み込むファイル名 + * @param preftype prefファイルのタイプ + * @return エラーコード + */ +static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype) +{ + FILE *fp; + fp = my_fopen(name, "r"); + if (!fp) return -1; + + char buf[1024]; + char old[1024]; + int line = -1; + errr err = 0; + bool bypass = FALSE; + while (my_fgets(fp, buf, sizeof(buf)) == 0) + { + line++; + if (!buf[0]) continue; + +#ifdef JP + if (!iskanji(buf[0])) +#endif + if (iswspace(buf[0])) continue; + + if (buf[0] == '#') continue; + strcpy(old, buf); + + /* Process "?:" */ + if ((buf[0] == '?') && (buf[1] == ':')) + { + char f; + char *s; + s = buf + 2; + concptr v = process_pref_file_expr(creature_ptr, &s, &f); + bypass = streq(v, "0"); + continue; + } + + if (bypass) continue; + + /* Process "%:" */ + if (buf[0] == '%') + { + static int depth_count = 0; + if (depth_count > 20) continue; + + depth_count++; + switch (preftype) + { + case PREF_TYPE_AUTOPICK: + (void)process_autopick_file(creature_ptr, buf + 2); + break; + case PREF_TYPE_HISTPREF: + (void)process_histpref_file(creature_ptr, buf + 2); + break; + default: + (void)process_pref_file(creature_ptr, buf + 2); + break; + } + + depth_count--; + continue; + } + + err = process_pref_file_command(creature_ptr, buf); + if (err != 0) + { + if (preftype != PREF_TYPE_AUTOPICK) + break; + err = process_autopick_file_command(buf); + } + } + + if (err != 0) + { + /* Print error message */ + /* ToDo: Add better error messages */ + msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."), + _(name, err), line, _(err, name)); + msg_format(_("('%s'を解析中)", "Parsing '%s'"), old); + msg_print(NULL); + } + + my_fclose(fp); + return (err); +} + + +/*! + * @brief pref設定ファイルを読み込み設定を反映させる / + * Process the "user pref file" with the given name + * @param creature_ptr プレーヤーへの参照ポインタ + * @param name 読み込むファイル名 + * @return エラーコード + * @details + *
+ * See the functions above for a list of legal "commands".
+ * We also accept the special "?" and "%" directives, which
+ * allow conditional evaluation and filename inclusion.
+ * 
+ */ +errr process_pref_file(player_type *creature_ptr, concptr name) +{ + char buf[1024]; + path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name); + + errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL); + 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); + if (err2 < 0 && !err1) + return -2; + + return err2; +} + + +/*! + * @brief 自動拾いファイルを読み込む / + * @param creature_ptr プレーヤーへの参照ポインタ + * @param name ファイル名 + * @details + */ +errr process_autopick_file(player_type *creature_ptr, concptr name) +{ + char buf[1024]; + path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name); + errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK); + return err; +} + + +/*! + * @brief プレイヤーの生い立ちファイルを読み込む / + * Process file for player's history editor. + * @param creature_ptr プレーヤーへの参照ポインタ + * @param name ファイル名 + * @return エラーコード + * @details + */ +errr process_histpref_file(player_type *creature_ptr, concptr name) +{ + bool old_character_xtra = current_world_ptr->character_xtra; + char buf[1024]; + path_build(buf, sizeof(buf), ANGBAND_DIR_USER, 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); + current_world_ptr->character_xtra = old_character_xtra; + return err; +} diff --git a/src/io/read-pref-file.h b/src/io/read-pref-file.h new file mode 100644 index 000000000..03aa85662 --- /dev/null +++ b/src/io/read-pref-file.h @@ -0,0 +1,7 @@ +#pragma once + +#include "angband.h" + +extern errr process_pref_file(player_type *creature_ptr, concptr name); +extern errr process_autopick_file(player_type *creature_ptr, concptr name); +extern errr process_histpref_file(player_type *creature_ptr, concptr name); diff --git a/src/object1.c b/src/object1.c index bff109c9c..c82f2e973 100644 --- a/src/object1.c +++ b/src/object1.c @@ -13,6 +13,7 @@ #include "angband.h" #include "core.h" +#include "io/read-pref-file.h" #include "util.h" #include "artifact.h"