OSDN Git Service

[Refactor] struct player_type を class PlayerType に置換。
[hengbandforosx/hengbandosx.git] / src / autopick / autopick-reader-writer.cpp
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"
11
12 /*!
13  * @brief Load an autopick preference file
14  */
15 void autopick_load_pref(PlayerType *player_ptr, bool disp_mes)
16 {
17     GAME_TEXT buf[80];
18     init_autopick();
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);
23     }
24
25     if (err < 0) {
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);
30         }
31     }
32
33     if (err && disp_mes) {
34         msg_print(_("自動拾い設定ファイルの読み込みに失敗しました。", "Failed to reload autopick preference."));
35     }
36 }
37
38 /*!
39  * @brief Get file name for autopick preference
40  */
41 concptr pickpref_filename(PlayerType *player_ptr, int filename_mode)
42 {
43     static const char namebase[] = _("picktype", "pickpref");
44
45     switch (filename_mode) {
46     case PT_DEFAULT:
47         return format("%s.prf", namebase);
48
49     case PT_WITH_PNAME:
50         return format("%s-%s.prf", namebase, player_ptr->base_name);
51
52     default:
53         return nullptr;
54     }
55 }
56
57 /*!
58  * @brief Read whole lines of a file to memory
59  */
60 static std::vector<concptr> read_text_lines(concptr filename)
61 {
62     FILE *fff;
63
64     int lines = 0;
65     char buf[1024];
66
67     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
68     fff = angband_fopen(buf, "r");
69     if (!fff)
70         return {};
71
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))
76             break;
77     }
78
79     if (lines == 0)
80         lines_list[0] = string_make("");
81
82     angband_fclose(fff);
83     return lines_list;
84 }
85
86 /*!
87  * @brief Copy the default autopick file to the user directory
88  */
89 static void prepare_default_pickpref(PlayerType *player_ptr)
90 {
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 };
94
95     concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
96     for (int i = 0; messages[i]; i++) {
97         msg_print(messages[i]);
98     }
99
100     msg_print(nullptr);
101     char buf[1024];
102     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
103     FILE *user_fp;
104     user_fp = angband_fopen(buf, "w");
105     if (!user_fp)
106         return;
107
108     fprintf(user_fp, "#***\n");
109     for (int i = 0; messages[i]; i++) {
110         fprintf(user_fp, "#***  %s\n", messages[i]);
111     }
112
113     fprintf(user_fp, "#***\n\n\n");
114     path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
115     FILE *pref_fp;
116     pref_fp = angband_fopen(buf, "r");
117
118     if (!pref_fp) {
119         angband_fclose(user_fp);
120         return;
121     }
122
123     while (!angband_fgets(pref_fp, buf, sizeof(buf))) {
124         fprintf(user_fp, "%s\n", buf);
125     }
126
127     angband_fclose(user_fp);
128     angband_fclose(pref_fp);
129 }
130
131 /*!
132  * @brief Read an autopick prefence file to memory
133  * Prepare default if no user file is found
134  */
135 std::vector<concptr> read_pickpref_text_lines(PlayerType *player_ptr, int *filename_mode_p)
136 {
137     /* Try a filename with player name */
138     *filename_mode_p = PT_WITH_PNAME;
139     char buf[1024];
140     strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
141     std::vector<concptr> lines_list = read_text_lines(buf);
142
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);
147     }
148
149     if (lines_list.empty()) {
150         prepare_default_pickpref(player_ptr);
151         lines_list = read_text_lines(buf);
152     }
153
154     if (lines_list.empty()) {
155         lines_list.resize(MAX_LINES);
156         lines_list[0] = string_make("");
157     }
158
159     return lines_list;
160 }
161
162 /*!
163  * @brief Write whole lines of memory to a file.
164  */
165 bool write_text_lines(concptr filename, concptr *lines_list)
166 {
167     char buf[1024];
168     path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
169     FILE *fff;
170     fff = angband_fopen(buf, "w");
171     if (!fff)
172         return false;
173
174     for (int lines = 0; lines_list[lines]; lines++) {
175         angband_fputs(fff, lines_list[lines], 1024);
176     }
177
178     angband_fclose(fff);
179     return true;
180 }