OSDN Git Service

Merge remote-tracking branch 'remotes/origin/For2.2.2-Fix-Hourier' into For2.2.2...
[hengband/hengband.git] / src / io / read-pref-file.c
1 #include "io/read-pref-file.h"
2 #include "io/interpret-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 = interpret_pref_file(creature_ptr, buf);
83                 if (err != 0)
84                 {
85                         if (preftype != PREF_TYPE_AUTOPICK)
86                                 break;
87                         
88                         process_autopick_file_command(buf);
89                         err = 0;
90                 }
91         }
92
93         if (err != 0)
94         {
95                 /* Print error message */
96                 /* ToDo: Add better error messages */
97                 msg_format(_("ファイル'%s'の%d行でエラー番号%dのエラー。", "Error %d in line %d of file '%s'."),
98                         _(name, err), line, _(err, name));
99                 msg_format(_("('%s'を解析中)", "Parsing '%s'"), old);
100                 msg_print(NULL);
101         }
102
103         my_fclose(fp);
104         return (err);
105 }
106
107
108 /*!
109  * @brief pref設定ファイルを読み込み設定を反映させる /
110  * Process the "user pref file" with the given name
111  * @param creature_ptr プレーヤーへの参照ポインタ
112  * @param name 読み込むファイル名
113  * @return エラーコード
114  * @details
115  * <pre>
116  * See the functions above for a list of legal "commands".
117  * We also accept the special "?" and "%" directives, which
118  * allow conditional evaluation and filename inclusion.
119  * </pre>
120  */
121 errr process_pref_file(player_type *creature_ptr, concptr name)
122 {
123         char buf[1024];
124         path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, name);
125
126         errr err1 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
127         if (err1 > 0) return err1;
128
129         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
130         errr err2 = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_NORMAL);
131         if (err2 < 0 && !err1)
132                 return -2;
133
134         return err2;
135 }
136
137
138 /*!
139  * @brief 自動拾いファイルを読み込む /
140  * @param creature_ptr プレーヤーへの参照ポインタ
141  * @param name ファイル名
142  * @details
143  */
144 errr process_autopick_file(player_type *creature_ptr, concptr name)
145 {
146         char buf[1024];
147         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
148         errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_AUTOPICK);
149         return err;
150 }
151
152
153 /*!
154  * @brief プレイヤーの生い立ちファイルを読み込む /
155  * Process file for player's history editor.
156  * @param creature_ptr プレーヤーへの参照ポインタ
157  * @param name ファイル名
158  * @return エラーコード
159  * @details
160  */
161 errr process_histpref_file(player_type *creature_ptr, concptr name)
162 {
163         bool old_character_xtra = current_world_ptr->character_xtra;
164         char buf[1024];
165         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
166
167         /* Hack -- prevent modification birth options in this file */
168         current_world_ptr->character_xtra = TRUE;
169         errr err = process_pref_file_aux(creature_ptr, buf, PREF_TYPE_HISTPREF);
170         current_world_ptr->character_xtra = old_character_xtra;
171         return err;
172 }