1 #include "autopick/autopick-reader-writer.h"
2 #include "autopick/autopick-initializer.h"
3 #include "autopick/autopick-pref-processor.h"
4 #include "io/files-util.h"
5 #include "io/read-pref-file.h"
6 #include "util/angband-files.h"
7 #include "view/display-messages.h"
10 * Load an autopick preference file
12 void autopick_load_pref(player_type *player_ptr, bool disp_mes)
16 angband_strcpy(buf, pickpref_filename(player_ptr, PT_WITH_PNAME), sizeof(buf));
17 errr err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
18 if (err == 0 && disp_mes)
20 msg_format(_("%s
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
\82Ü
\82µ
\82½
\81B", "Loaded '%s'."), buf);
25 angband_strcpy(buf, pickpref_filename(player_ptr, PT_DEFAULT), sizeof(buf));
26 err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
27 if (err == 0 && disp_mes)
29 msg_format(_("%s
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
\82Ü
\82µ
\82½
\81B", "Loaded '%s'."), buf);
35 msg_print(_("
\8e©
\93®
\8fE
\82¢
\90Ý
\92è
\83t
\83@
\83C
\83\8b\82Ì
\93Ç
\82Ý
\8d\9e\82Ý
\82É
\8e¸
\94s
\82µ
\82Ü
\82µ
\82½
\81B", "Failed to reload autopick preference."));
41 * Get file name for autopick preference
43 concptr pickpref_filename(player_type *player_ptr, int filename_mode)
45 static const char namebase[] = _("picktype", "pickpref");
47 switch (filename_mode)
50 return format("%s.prf", namebase);
53 return format("%s-%s.prf", namebase, player_ptr->base_name);
62 * Read whole lines of a file to memory
64 static concptr *read_text_lines(concptr filename)
66 concptr *lines_list = NULL;
72 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
73 fff = angband_fopen(buf, "r");
74 if (!fff) return NULL;
76 C_MAKE(lines_list, MAX_LINES, concptr);
77 while (angband_fgets(fff, buf, sizeof(buf)) == 0)
79 lines_list[lines++] = string_make(buf);
80 if (lines >= MAX_LINES - 1) break;
84 lines_list[0] = string_make("");
92 * Copy the default autopick file to the user directory
94 static void prepare_default_pickpref(player_type *player_ptr)
96 const concptr messages[] = {
97 _("
\82 \82È
\82½
\82Í
\81u
\8e©
\93®
\8fE
\82¢
\83G
\83f
\83B
\83^
\81v
\82ð
\8f\89\82ß
\82Ä
\8bN
\93®
\82µ
\82Ü
\82µ
\82½
\81B", "You have activated the Auto-Picker Editor for the first time."),
98 _("
\8e©
\93®
\8fE
\82¢
\82Ì
\83\86\81[
\83U
\81[
\90Ý
\92è
\83t
\83@
\83C
\83\8b\82ª
\82Ü
\82¾
\8f\91\82©
\82ê
\82Ä
\82¢
\82È
\82¢
\82Ì
\82Å
\81A", "Since user pref file for autopick is not yet created,"),
99 _("
\8aî
\96{
\93I
\82È
\8e©
\93®
\8fE
\82¢
\90Ý
\92è
\83t
\83@
\83C
\83\8b\82ðlib/pref/picktype.prf
\82©
\82ç
\83R
\83s
\81[
\82µ
\82Ü
\82·
\81B", "the default setting is loaded from lib/pref/pickpref.prf ."),
103 concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
104 for (int i = 0; messages[i]; i++)
106 msg_print(messages[i]);
111 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
113 user_fp = angband_fopen(buf, "w");
114 if (!user_fp) return;
116 fprintf(user_fp, "#***\n");
117 for (int i = 0; messages[i]; i++)
119 fprintf(user_fp, "#*** %s\n", messages[i]);
122 fprintf(user_fp, "#***\n\n\n");
123 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
125 pref_fp = angband_fopen(buf, "r");
129 angband_fclose(user_fp);
133 while (!angband_fgets(pref_fp, buf, sizeof(buf)))
135 fprintf(user_fp, "%s\n", buf);
138 angband_fclose(user_fp);
139 angband_fclose(pref_fp);
143 * Read an autopick prefence file to memory
144 * Prepare default if no user file is found
146 concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
148 /* Try a filename with player name */
149 *filename_mode_p = PT_WITH_PNAME;
151 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
153 lines_list = read_text_lines(buf);
157 *filename_mode_p = PT_DEFAULT;
158 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
159 lines_list = read_text_lines(buf);
164 prepare_default_pickpref(player_ptr);
165 lines_list = read_text_lines(buf);
170 C_MAKE(lines_list, MAX_LINES, concptr);
171 lines_list[0] = string_make("");
179 * Write whole lines of memory to a file.
181 bool write_text_lines(concptr filename, concptr *lines_list)
184 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
186 fff = angband_fopen(buf, "w");
187 if (!fff) return FALSE;
189 for (int lines = 0; lines_list[lines]; lines++)
191 angband_fputs(fff, lines_list[lines], 1024);