OSDN Git Service

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