OSDN Git Service

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