OSDN Git Service

Corrected English spelling mistake.
[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 "util/string-processor.h"
8 #include "view/display-messages.h"
9
10 /*
11  * Load an autopick preference file
12  */
13 void autopick_load_pref(player_type *player_ptr, bool disp_mes)
14 {
15         GAME_TEXT buf[80];
16         init_autopick();
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)
20         {
21                 msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
22         }
23
24         if (err < 0)
25         {
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)
29                 {
30                         msg_format(_("%sを読み込みました。", "Loaded '%s'."), buf);
31                 }
32         }
33
34         if (err && disp_mes)
35         {
36                 msg_print(_("自動拾い設定ファイルの読み込みに失敗しました。", "Failed to reload autopick preference."));
37         }
38 }
39
40
41 /*
42  *  Get file name for autopick preference
43  */
44 concptr pickpref_filename(player_type *player_ptr, int filename_mode)
45 {
46         static const char namebase[] = _("picktype", "pickpref");
47
48         switch (filename_mode)
49         {
50         case PT_DEFAULT:
51                 return format("%s.prf", namebase);
52
53         case PT_WITH_PNAME:
54                 return format("%s-%s.prf", namebase, player_ptr->base_name);
55
56         default:
57                 return NULL;
58         }
59 }
60
61
62 /*
63  * Read whole lines of a file to memory
64  */
65 static concptr *read_text_lines(concptr filename)
66 {
67         concptr *lines_list = NULL;
68         FILE *fff;
69
70         int lines = 0;
71         char buf[1024];
72
73         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
74         fff = angband_fopen(buf, "r");
75         if (!fff) return NULL;
76
77         C_MAKE(lines_list, MAX_LINES, concptr);
78         while (angband_fgets(fff, buf, sizeof(buf)) == 0)
79         {
80                 lines_list[lines++] = string_make(buf);
81                 if (lines >= MAX_LINES - 1) break;
82         }
83
84         if (lines == 0)
85                 lines_list[0] = string_make("");
86
87         angband_fclose(fff);
88         return lines_list;
89 }
90
91
92 /*
93  * Copy the default autopick file to the user directory
94  */
95 static void prepare_default_pickpref(player_type *player_ptr)
96 {
97         const concptr messages[] = {
98                 _("あなたは「自動拾いエディタ」を初めて起動しました。", "You have activated the Auto-Picker Editor for the first time."),
99                 _("自動拾いのユーザー設定ファイルがまだ書かれていないので、", "Since user pref file for autopick is not yet created,"),
100                 _("基本的な自動拾い設定ファイルをlib/pref/picktype.prfからコピーします。", "the default setting is loaded from lib/pref/pickpref.prf ."),
101                 NULL
102         };
103
104         concptr filename = pickpref_filename(player_ptr, PT_DEFAULT);
105         for (int i = 0; messages[i]; i++)
106         {
107                 msg_print(messages[i]);
108         }
109
110         msg_print(NULL);
111         char buf[1024];
112         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
113         FILE *user_fp;
114         user_fp = angband_fopen(buf, "w");
115         if (!user_fp) return;
116
117         fprintf(user_fp, "#***\n");
118         for (int i = 0; messages[i]; i++)
119         {
120                 fprintf(user_fp, "#***  %s\n", messages[i]);
121         }
122
123         fprintf(user_fp, "#***\n\n\n");
124         path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
125         FILE *pref_fp;
126         pref_fp = angband_fopen(buf, "r");
127
128         if (!pref_fp)
129         {
130                 angband_fclose(user_fp);
131                 return;
132         }
133
134         while (!angband_fgets(pref_fp, buf, sizeof(buf)))
135         {
136                 fprintf(user_fp, "%s\n", buf);
137         }
138
139         angband_fclose(user_fp);
140         angband_fclose(pref_fp);
141 }
142
143 /*
144  * Read an autopick prefence file to memory
145  * Prepare default if no user file is found
146  */
147 concptr *read_pickpref_text_lines(player_type *player_ptr, int *filename_mode_p)
148 {
149         /* Try a filename with player name */
150         *filename_mode_p = PT_WITH_PNAME;
151         char buf[1024];
152         strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
153         concptr *lines_list;
154         lines_list = read_text_lines(buf);
155
156         if (!lines_list)
157         {
158                 *filename_mode_p = PT_DEFAULT;
159                 strcpy(buf, pickpref_filename(player_ptr, *filename_mode_p));
160                 lines_list = read_text_lines(buf);
161         }
162
163         if (!lines_list)
164         {
165                 prepare_default_pickpref(player_ptr);
166                 lines_list = read_text_lines(buf);
167         }
168
169         if (!lines_list)
170         {
171                 C_MAKE(lines_list, MAX_LINES, concptr);
172                 lines_list[0] = string_make("");
173         }
174
175         return lines_list;
176 }
177
178
179 /*
180  * Write whole lines of memory to a file.
181  */
182 bool write_text_lines(concptr filename, concptr *lines_list)
183 {
184         char buf[1024];
185         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
186         FILE *fff;
187         fff = angband_fopen(buf, "w");
188         if (!fff) return FALSE;
189
190         for (int lines = 0; lines_list[lines]; lines++)
191         {
192                 angband_fputs(fff, lines_list[lines], 1024);
193         }
194
195         angband_fclose(fff);
196         return TRUE;
197 }