OSDN Git Service

[Refactor] #39964 Removed the dependency from read-pref-file to autopick-pref-processor
[hengband/hengband.git] / src / autopick / autopick-reader-writer.c
1 #include "angband.h"
2 #include "autopick/autopick-reader-writer.h"
3 #include "autopick/autopick-initializer.h"
4 #include "autopick/autopick-pref-processor.h"
5 #include "files.h"
6 #include "io/read-pref-file.h"
7
8 /*
9  * Load an autopick preference file
10  */
11 void autopick_load_pref(player_type *player_ptr, bool disp_mes)
12 {
13         GAME_TEXT buf[80];
14         init_autopick();
15         my_strcpy(buf, pickpref_filename(player_ptr, PT_WITH_PNAME), sizeof(buf));
16         errr err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
17         if (err == 0 && disp_mes)
18         {
19                 msg_format(_("%s\82ð\93Ç\82Ý\8d\9e\82Ý\82Ü\82µ\82½\81B", "Loaded '%s'."), buf);
20         }
21
22         if (err < 0)
23         {
24                 my_strcpy(buf, pickpref_filename(player_ptr, PT_DEFAULT), sizeof(buf));
25                 err = process_autopick_file(player_ptr, buf, process_autopick_file_command);
26                 if (err == 0 && disp_mes)
27                 {
28                         msg_format(_("%s\82ð\93Ç\82Ý\8d\9e\82Ý\82Ü\82µ\82½\81B", "Loaded '%s'."), buf);
29                 }
30         }
31
32         if (err && disp_mes)
33         {
34                 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."));
35         }
36 }
37
38
39 /*
40  *  Get file name for autopick preference
41  */
42 concptr pickpref_filename(player_type *player_ptr, int filename_mode)
43 {
44         static const char namebase[] = _("picktype", "pickpref");
45
46         switch (filename_mode)
47         {
48         case PT_DEFAULT:
49                 return format("%s.prf", namebase);
50
51         case PT_WITH_PNAME:
52                 return format("%s-%s.prf", namebase, player_ptr->base_name);
53
54         default:
55                 return NULL;
56         }
57 }
58
59
60 /*
61  * Read whole lines of a file to memory
62  */
63 static concptr *read_text_lines(concptr filename)
64 {
65         concptr *lines_list = NULL;
66         FILE *fff;
67
68         int lines = 0;
69         char buf[1024];
70
71         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
72         fff = my_fopen(buf, "r");
73         if (!fff) return NULL;
74
75         C_MAKE(lines_list, MAX_LINES, concptr);
76         while (my_fgets(fff, buf, sizeof(buf)) == 0)
77         {
78                 lines_list[lines++] = string_make(buf);
79                 if (lines >= MAX_LINES - 1) break;
80         }
81
82         if (lines == 0)
83                 lines_list[0] = string_make("");
84
85         my_fclose(fff);
86         return lines_list;
87 }
88
89
90 /*
91  * Copy the default autopick file to the user directory
92  */
93 static void prepare_default_pickpref(player_type *player_ptr)
94 {
95         const concptr messages[] = {
96                 _("\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."),
97                 _("\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,"),
98                 _("\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 ."),
99                 NULL
100         };
101
102         concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
103         for (int i = 0; messages[i]; i++)
104         {
105                 msg_print(messages[i]);
106         }
107
108         msg_print(NULL);
109         char buf[1024];
110         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
111         FILE *user_fp;
112         user_fp = my_fopen(buf, "w");
113         if (!user_fp) return;
114
115         fprintf(user_fp, "#***\n");
116         for (int i = 0; messages[i]; i++)
117         {
118                 fprintf(user_fp, "#***  %s\n", messages[i]);
119         }
120
121         fprintf(user_fp, "#***\n\n\n");
122         path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
123         FILE *pref_fp;
124         pref_fp = my_fopen(buf, "r");
125
126         if (!pref_fp)
127         {
128                 my_fclose(user_fp);
129                 return;
130         }
131
132         while (!my_fgets(pref_fp, buf, sizeof(buf)))
133         {
134                 fprintf(user_fp, "%s\n", buf);
135         }
136
137         my_fclose(user_fp);
138         my_fclose(pref_fp);
139 }
140
141 /*
142  * Read an autopick prefence file to memory
143  * Prepare default if no user file is found
144  */
145 concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
146 {
147         /* Try a filename with player name */
148         *filename_mode_p = PT_WITH_PNAME;
149         char buf[1024];
150         strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
151         concptr *lines_list;
152         lines_list = read_text_lines(buf);
153
154         if (!lines_list)
155         {
156                 *filename_mode_p = PT_DEFAULT;
157                 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
158                 lines_list = read_text_lines(buf);
159         }
160
161         if (!lines_list)
162         {
163                 prepare_default_pickpref(player_ptr);
164                 lines_list = read_text_lines(buf);
165         }
166
167         if (!lines_list)
168         {
169                 C_MAKE(lines_list, MAX_LINES, concptr);
170                 lines_list[0] = string_make("");
171         }
172
173         return lines_list;
174 }
175
176
177 /*
178  * Write whole lines of memory to a file.
179  */
180 bool write_text_lines(concptr filename, concptr *lines_list)
181 {
182         char buf[1024];
183         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
184         FILE *fff;
185         fff = my_fopen(buf, "w");
186         if (!fff) return FALSE;
187
188         for (int lines = 0; lines_list[lines]; lines++)
189         {
190                 my_fputs(fff, lines_list[lines], 1024);
191         }
192
193         my_fclose(fff);
194         return TRUE;
195 }