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 #include <string_view>
17 * @brief Load an autopick preference file
19 void autopick_load_pref(PlayerType *player_ptr, bool disp_mes)
23 angband_strcpy(buf, pickpref_filename(player_ptr, PT_WITH_PNAME).data(), sizeof(buf));
24 errr err = process_autopick_file(player_ptr, buf);
25 if (err == 0 && disp_mes) {
26 msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
30 angband_strcpy(buf, pickpref_filename(player_ptr, PT_DEFAULT).data(), sizeof(buf));
31 err = process_autopick_file(player_ptr, buf);
32 if (err == 0 && disp_mes) {
33 msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
37 if (err && disp_mes) {
38 msg_print(_("自動拾い設定ファイルの読み込みに失敗しました。", "Failed to reload autopick preference."));
43 * @brief Get file name for autopick preference
45 std::string pickpref_filename(PlayerType *player_ptr, int filename_mode)
47 static const char namebase[] = _("picktype", "pickpref");
49 switch (filename_mode) {
51 return format("%s.prf", namebase);
54 return format("%s-%s.prf", namebase, player_ptr->base_name);
57 throw std::invalid_argument(format("The value of argument 'filename_mode' is invalid: %d", filename_mode));
62 * @brief Read whole lines of a file to memory
64 static std::vector<concptr> read_text_lines(std::string_view filename)
66 const auto &path = path_build(ANGBAND_DIR_USER, filename);
67 auto *fff = angband_fopen(path, FileOpenMode::READ);
73 std::vector<concptr> lines_list(MAX_LINES);
75 while (angband_fgets(fff, buf, sizeof(buf)) == 0) {
76 lines_list[lines++] = string_make(buf);
77 if (is_greater_autopick_max_line(lines)) {
83 lines_list[0] = string_make("");
91 * @brief Copy the default autopick file to the user directory
93 static void prepare_default_pickpref(PlayerType *player_ptr)
95 const std::vector<std::string> messages = { _("あなたは「自動拾いエディタ」を初めて起動しました。", "You have activated the Auto-Picker Editor for the first time."),
96 _("自動拾いのユーザー設定ファイルがまだ書かれていないので、", "Since user pref file for autopick is not yet created,"),
97 _("基本的な自動拾い設定ファイルをlib/pref/picktype.prfからコピーします。", "the default setting is loaded from lib/pref/pickpref.prf .") };
99 const auto filename = pickpref_filename(player_ptr, PT_DEFAULT);
100 for (const auto &message : messages) {
105 const auto &path_user = path_build(ANGBAND_DIR_USER, filename);
106 auto *user_fp = angband_fopen(path_user, FileOpenMode::WRITE);
111 fprintf(user_fp, "#***\n");
112 for (const auto &message : messages) {
113 fprintf(user_fp, "#*** %s\n", message.data());
116 fprintf(user_fp, "#***\n\n\n");
117 const auto &path_pref = path_build(ANGBAND_DIR_PREF, filename);
118 auto *pref_fp = angband_fopen(path_pref, FileOpenMode::READ);
120 angband_fclose(user_fp);
125 while (!angband_fgets(pref_fp, buf, sizeof(buf))) {
126 fprintf(user_fp, "%s\n", buf);
129 angband_fclose(user_fp);
130 angband_fclose(pref_fp);
134 * @brief Read an autopick prefence file to memory
135 * Prepare default if no user file is found
137 std::vector<concptr> read_pickpref_text_lines(PlayerType *player_ptr, int *filename_mode_p)
139 /* Try a filename with player name */
140 *filename_mode_p = PT_WITH_PNAME;
141 auto filename = pickpref_filename(player_ptr, *filename_mode_p);
142 std::vector<concptr> lines_list = read_text_lines(filename);
144 if (lines_list.empty()) {
145 *filename_mode_p = PT_DEFAULT;
146 filename = pickpref_filename(player_ptr, *filename_mode_p);
147 lines_list = read_text_lines(filename);
150 if (lines_list.empty()) {
151 prepare_default_pickpref(player_ptr);
152 lines_list = read_text_lines(filename);
155 if (lines_list.empty()) {
156 lines_list.resize(MAX_LINES);
157 lines_list[0] = string_make("");
164 * @brief Write whole lines of memory to a file.
166 bool write_text_lines(std::string_view filename, const std::vector<concptr> &lines)
168 const auto &path = path_build(ANGBAND_DIR_USER, filename);
169 auto *fff = angband_fopen(path, FileOpenMode::WRITE);
174 for (const auto *line : lines) {
175 if (line == nullptr) {
179 angband_fputs(fff, line, 1024);