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 "util/string-processor.h"
8 #include "view/display-messages.h"
11 * Load an autopick preference file
13 void autopick_load_pref(player_type *player_ptr, bool disp_mes)
17 angband_strcpy(buf, pickpref_filename(player_ptr, PT_WITH_PNAME), sizeof(buf));
18 errr err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
19 if (err == 0 && disp_mes)
21 msg_format(_("%s
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
\82Ü
\82µ
\82½
\81B", "Loaded '%s'."), buf);
26 angband_strcpy(buf, pickpref_filename(player_ptr, PT_DEFAULT), sizeof(buf));
27 err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
28 if (err == 0 && disp_mes)
30 msg_format(_("%s
\82ð
\93Ç
\82Ý
\8d\9e\82Ý
\82Ü
\82µ
\82½
\81B", "Loaded '%s'."), buf);
36 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."));
42 * Get file name for autopick preference
44 concptr pickpref_filename(player_type *player_ptr, int filename_mode)
46 static const char namebase[] = _("picktype", "pickpref");
48 switch (filename_mode)
51 return format("%s.prf", namebase);
54 return format("%s-%s.prf", namebase, player_ptr->base_name);
63 * Read whole lines of a file to memory
65 static concptr *read_text_lines(concptr filename)
67 concptr *lines_list = NULL;
73 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
74 fff = angband_fopen(buf, "r");
75 if (!fff) return NULL;
77 C_MAKE(lines_list, MAX_LINES, concptr);
78 while (angband_fgets(fff, buf, sizeof(buf)) == 0)
80 lines_list[lines++] = string_make(buf);
81 if (lines >= MAX_LINES - 1) break;
85 lines_list[0] = string_make("");
93 * Copy the default autopick file to the user directory
95 static void prepare_default_pickpref(player_type *player_ptr)
97 const concptr messages[] = {
98 _("
\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."),
99 _("
\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,"),
100 _("
\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 ."),
104 concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
105 for (int i = 0; messages[i]; i++)
107 msg_print(messages[i]);
112 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
114 user_fp = angband_fopen(buf, "w");
115 if (!user_fp) return;
117 fprintf(user_fp, "#***\n");
118 for (int i = 0; messages[i]; i++)
120 fprintf(user_fp, "#*** %s\n", messages[i]);
123 fprintf(user_fp, "#***\n\n\n");
124 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
126 pref_fp = angband_fopen(buf, "r");
130 angband_fclose(user_fp);
134 while (!angband_fgets(pref_fp, buf, sizeof(buf)))
136 fprintf(user_fp, "%s\n", buf);
139 angband_fclose(user_fp);
140 angband_fclose(pref_fp);
144 * Read an autopick prefence file to memory
145 * Prepare default if no user file is found
147 concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
149 /* Try a filename with player name */
150 *filename_mode_p = PT_WITH_PNAME;
152 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
154 lines_list = read_text_lines(buf);
158 *filename_mode_p = PT_DEFAULT;
159 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
160 lines_list = read_text_lines(buf);
165 prepare_default_pickpref(player_ptr);
166 lines_list = read_text_lines(buf);
171 C_MAKE(lines_list, MAX_LINES, concptr);
172 lines_list[0] = string_make("");
180 * Write whole lines of memory to a file.
182 bool write_text_lines(concptr filename, concptr *lines_list)
185 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
187 fff = angband_fopen(buf, "w");
188 if (!fff) return FALSE;
190 for (int lines = 0; lines_list[lines]; lines++)
192 angband_fputs(fff, lines_list[lines], 1024);