1 #include "autopick/autopick-reader-writer.h"
2 #include "autopick/autopick-initializer.h"
3 #include "autopick/autopick-pref-processor.h"
4 #include "autopick/autopick-util.h"
5 #include "io/files-util.h"
6 #include "io/read-pref-file.h"
7 #include "system/player-type-definition.h"
8 #include "util/angband-files.h"
9 #include "util/string-processor.h"
10 #include "view/display-messages.h"
13 * @brief Load an autopick preference file
15 void autopick_load_pref(PlayerType *player_ptr, bool disp_mes)
19 angband_strcpy(buf, pickpref_filename(player_ptr, PT_WITH_PNAME), sizeof(buf));
20 errr err = process_autopick_file(player_ptr, buf);
21 if (err == 0 && disp_mes) {
22 msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
26 angband_strcpy(buf, pickpref_filename(player_ptr, PT_DEFAULT), sizeof(buf));
27 err = process_autopick_file(player_ptr, buf);
28 if (err == 0 && disp_mes) {
29 msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
33 if (err && disp_mes) {
34 msg_print(_("自動拾い設定ファイルの読み込みに失敗しました。", "Failed to reload autopick preference."));
39 * @brief Get file name for autopick preference
41 concptr pickpref_filename(PlayerType *player_ptr, int filename_mode)
43 static const char namebase[] = _("picktype", "pickpref");
45 switch (filename_mode) {
47 return format("%s.prf", namebase);
50 return format("%s-%s.prf", namebase, player_ptr->base_name);
58 * @brief Read whole lines of a file to memory
60 static std::vector<concptr> read_text_lines(concptr filename)
67 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
68 fff = angband_fopen(buf, "r");
72 std::vector<concptr> lines_list(MAX_LINES);
73 while (angband_fgets(fff, buf, sizeof(buf)) == 0) {
74 lines_list[lines++] = string_make(buf);
75 if (is_greater_autopick_max_line(lines))
80 lines_list[0] = string_make("");
87 * @brief Copy the default autopick file to the user directory
89 static void prepare_default_pickpref(PlayerType *player_ptr)
91 const concptr messages[] = { _("あなたは「自動拾いエディタ」を初めて起動しました。", "You have activated the Auto-Picker Editor for the first time."),
92 _("自動拾いのユーザー設定ファイルがまだ書かれていないので、", "Since user pref file for autopick is not yet created,"),
93 _("基本的な自動拾い設定ファイルをlib/pref/picktype.prfからコピーします。", "the default setting is loaded from lib/pref/pickpref.prf ."), nullptr };
95 concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
96 for (int i = 0; messages[i]; i++) {
97 msg_print(messages[i]);
102 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
104 user_fp = angband_fopen(buf, "w");
108 fprintf(user_fp, "#***\n");
109 for (int i = 0; messages[i]; i++) {
110 fprintf(user_fp, "#*** %s\n", messages[i]);
113 fprintf(user_fp, "#***\n\n\n");
114 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
116 pref_fp = angband_fopen(buf, "r");
119 angband_fclose(user_fp);
123 while (!angband_fgets(pref_fp, buf, sizeof(buf))) {
124 fprintf(user_fp, "%s\n", buf);
127 angband_fclose(user_fp);
128 angband_fclose(pref_fp);
132 * @brief Read an autopick prefence file to memory
133 * Prepare default if no user file is found
135 std::vector<concptr> read_pickpref_text_lines(PlayerType *player_ptr, int *filename_mode_p)
137 /* Try a filename with player name */
138 *filename_mode_p = PT_WITH_PNAME;
140 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
141 std::vector<concptr> lines_list = read_text_lines(buf);
143 if (lines_list.empty()) {
144 *filename_mode_p = PT_DEFAULT;
145 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
146 lines_list = read_text_lines(buf);
149 if (lines_list.empty()) {
150 prepare_default_pickpref(player_ptr);
151 lines_list = read_text_lines(buf);
154 if (lines_list.empty()) {
155 lines_list.resize(MAX_LINES);
156 lines_list[0] = string_make("");
163 * @brief Write whole lines of memory to a file.
165 bool write_text_lines(concptr filename, concptr *lines_list)
168 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
170 fff = angband_fopen(buf, "w");
174 for (int lines = 0; lines_list[lines]; lines++) {
175 angband_fputs(fff, lines_list[lines], 1024);