OSDN Git Service

[Refactor] #39962 Separated read-pref-file.c/h from files.c/h
[hengband/hengband.git] / src / io / read-pref-file.c
1 #include "io/read-pref-file.h"
2 #include "io/process-pref-file.h"
3 #include "autopick.h"
4 #include "files.h" // 暫定。コールバック化して後で消す.
5 #include "world.h"
6
7 // todo コールバック関数に変更するので、いずれ消す.
8 #define PREF_TYPE_NORMAL   0
9 #define PREF_TYPE_AUTOPICK 1
10 #define PREF_TYPE_HISTPREF 2
11
12 /*!
13  * todo 関数名を変更する
14  * @brief process_pref_fileのサブルーチン /
15  * Open the "user pref file" and parse it.
16  * @param creature_ptr プレーヤーへの参照ポインタ
17  * @param name 読み込むファイル名
18  * @param preftype prefファイルのタイプ
19  * @return エラーコード
20  */
21 static errr process_pref_file_aux(player_type *creature_ptr, concptr name, int preftype)
22 {
23         FILE *fp;
24         fp = my_fopen(name, "r");
25         if (!fp) return -1;
26
27         char buf[1024];
28         char old[1024];
29         int line = -1;
30         errr err = 0;
31         bool bypass = FALSE;
32         while (my_fgets(fp, buf, sizeof(buf)) == 0)
33         {
34                 line++;
35                 if (!buf[0]) continue;
36
37 #ifdef JP
38                 if (!iskanji(buf[0]))
39 #endif
40                         if (iswspace(buf[0])) continue;
41
42                 if (buf[0] == '#') continue;
43                 strcpy(old, buf);
44
45                 /* Process "?:<expr>" */
46                 if ((buf[0] == '?') && (buf[1] == ':'))
47                 {
48                         char f;
49                         char *s;
50                         s = buf + 2;
51                         concptr v = process_pref_file_expr(creature_ptr, &s, &f);
52                         bypass = streq(v, "0");
53                         continue;
54                 }
55
56                 if (bypass) continue;
57
58                 /* Process "%:<file>" */
59                 if (buf[0] == '%')
60                 {
61                         static int depth_count = 0;
62                         if (depth_count > 20) continue;
63
64                         depth_count++;
65                         switch (preftype)
66                         {
67                         case PREF_TYPE_AUTOPICK:
68                                 (void)process_autopick_file(creature_ptr, buf + 2);
69                                 break;
70                         case PREF_TYPE_HISTPREF:
71                                 (void)process_histpref_file(creature_ptr, buf + 2);
72                                 break;
73                         default:
74                                 (void)process_pref_file(creature_ptr, buf + 2);
75                                 break;
76                         }
77
78                         depth_count--;
79                         continue;
80                 }
81
82                 err = process_pref_file_command(creature_ptr, buf);
83                 if (err != 0)
84                 {
85                         if (preftype != PREF_TYPE_AUTOPICK)
86                                 break;
87                         err = process_autopick_file_command(buf);
88                 }
89         }
90
91         if (err != 0)
92         {
93                 /* Print error message */
94                 /* ToDo: Add better error messages */
95                 msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."),
96                         _(name, err), line, _(err, name));
97                 msg_format(_("('%s'を解析中)", "Parsing '%s'"), old);
98                 msg_print(NULL);
99         }
100
101         my_fclose(fp);
102         return (err);
103 }
104
105
106 /*!
107  * @brief pref設定ファイルを読み込み設定を反映させる /
108  * Process the "user pref file" with the given name
109  * @param creature_ptr プレーヤーへの参照ポインタ
110  * @param name 読み込むファイル名
111  * @return エラーコード
112  * @details
113  * <pre>
114  * See the functions above for a list of legal "commands".
115  * We also accept the special "?" and "%" directives, which
116  * allow conditional evaluation and filename inclusion.
117  * </pre>
118  */
119 errr process_pref_file(player_type *creature_ptr, concptr name)
120 {
121         char buf[1024];
122         path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name);
123
124         errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
125         if (err1 > 0) return err1;
126
127         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
128         errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
129         if (err2 < 0 && !err1)
130                 return -2;
131
132         return err2;
133 }
134
135
136 /*!
137  * @brief 自動拾いファイルを読み込む /
138  * @param creature_ptr プレーヤーへの参照ポインタ
139  * @param name ファイル名
140  * @details
141  */
142 errr process_autopick_file(player_type *creature_ptr, concptr name)
143 {
144         char buf[1024];
145         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
146         errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK);
147         return err;
148 }
149
150
151 /*!
152  * @brief プレイヤーの生い立ちファイルを読み込む /
153  * Process file for player's history editor.
154  * @param creature_ptr プレーヤーへの参照ポインタ
155  * @param name ファイル名
156  * @return エラーコード
157  * @details
158  */
159 errr process_histpref_file(player_type *creature_ptr, concptr name)
160 {
161         bool old_character_xtra = current_world_ptr->character_xtra;
162         char buf[1024];
163         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
164
165         /* Hack -- prevent modification birth options in this file */
166         current_world_ptr->character_xtra = TRUE;
167         errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF);
168         current_world_ptr->character_xtra = old_character_xtra;
169         return err;
170 }