2 * @brief ファイル入出力管理 / Purpose: code dealing with files (and death)
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
7 * This software may be copied and distributed for educational, research,
8 * and not for profit purposes provided that this copyright and statement
9 * are included in all such copies. Other copyrights may also apply.
10 * 2014 Deskull rearranged comment for Doxygen.\n
14 #include "io/files-util.h"
15 #include "core/asking-player.h"
16 #include "io-dump/character-dump.h"
17 #include "io/input-key-acceptor.h"
18 #include "io/uid-checker.h"
19 #include "monster-race/monster-race.h"
20 #include "monster-race/race-flags1.h"
21 #include "term/screen-processor.h"
22 #include "util/angband-files.h"
23 #include "view/display-messages.h"
25 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
26 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
27 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
28 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
29 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
30 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
31 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
32 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
33 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
34 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
35 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
36 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
37 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
40 * Buffer to hold the current savefile name
41 * 'savefile' holds full path name. 'savefile_base' holds only base name.
44 char savefile_base[40];
47 * @brief プレイヤーステータスをファイルダンプ出力する
48 * Hack -- Dump a character description file
49 * @param creature_ptr プレーヤーへの参照ポインタ
53 * Allow the "full" flag to dump additional info,
54 * and trigger its usage from various places in the code.
56 errr file_character(player_type *creature_ptr, concptr name, update_playtime_pf update_playtime, display_player_pf display_player)
59 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
60 int fd = fd_open(buf, O_RDONLY);
64 (void)sprintf(out_val, _("現存するファイル %s に上書きしますか? ", "Replace existing file %s? "), buf);
65 if (get_check_strict(creature_ptr, out_val, CHECK_NO_HISTORY))
71 fff = angband_fopen(buf, "w");
74 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
79 make_character_dump(creature_ptr, fff, update_playtime, display_player);
81 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
87 * @brief ファイルからランダムに行を一つ取得する /
88 * Get a random line from a file
89 * @param file_name ファイル名
90 * @param entry 特定条件時のN:タグヘッダID
91 * @param output 出力先の文字列参照ポインタ
95 * Based on the monster speech patch by Matt Graham,
98 errr get_rnd_line(concptr file_name, int entry, char *output)
101 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, file_name);
103 fp = angband_fopen(buf, "r");
110 if (angband_fgets(fp, buf, sizeof(buf)) != 0) {
116 if ((buf[0] != 'N') || (buf[1] != ':'))
121 } else if (buf[2] == 'M') {
122 if (r_info[entry].flags1 & RF1_MALE)
124 } else if (buf[2] == 'F') {
125 if (r_info[entry].flags1 & RF1_FEMALE)
127 } else if (sscanf(&(buf[2]), "%d", &test) != EOF) {
131 msg_format("Error in line %d of %s!", line_num, file_name);
138 for (counter = 0;; counter++) {
140 test = angband_fgets(fp, buf, sizeof(buf));
142 /* Ignore lines starting with 'N:' */
143 if ((buf[0] == 'N') && (buf[1] == ':'))
155 if (one_in_(counter + 1))
160 return counter ? 0 : -1;
165 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ) /
166 * @param file_name ファイル名
167 * @param entry 特定条件時のN:タグヘッダID
168 * @param output 出力先の文字列参照ポインタ
173 errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count)
176 for (int i = 0; i < count; i++) {
177 result = get_rnd_line(file_name, entry, output);
181 for (int j = 0; output[j]; j++)
182 kanji |= iskanji(output[j]);
192 * @brief ファイル位置をシーク /
193 * @param creature_ptr プレーヤーへの参照ポインタ
194 * @param fd ファイルディスクリプタ
195 * @param where ファイルバイト位置
196 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
200 static errr counts_seek(player_type *creature_ptr, int fd, u32b where, bool flag)
202 char temp1[128], temp2[128];
203 #ifdef SAVEFILE_USE_UID
204 (void)sprintf(temp1, "%d.%s.%d%d%d", creature_ptr->player_uid, savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
206 (void)sprintf(temp1, "%s.%d%d%d", savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
208 for (int i = 0; temp1[i]; i++)
209 temp1[i] ^= (i + 1) * 63;
212 u32b zero_header[3] = { 0L, 0L, 0L };
214 if (fd_seek(fd, seekpoint + 3 * sizeof(u32b)))
216 if (fd_read(fd, (char *)(temp2), sizeof(temp2))) {
220 fd_seek(fd, seekpoint);
221 fd_write(fd, (char *)zero_header, 3 * sizeof(u32b));
222 fd_write(fd, (char *)(temp1), sizeof(temp1));
226 if (strcmp(temp1, temp2) == 0)
229 seekpoint += 128 + 3 * sizeof(u32b);
232 return fd_seek(fd, seekpoint + where * sizeof(u32b));
237 * @param creature_ptr プレーヤーへの参照ポインタ
238 * @param where ファイルバイト位置
242 u32b counts_read(player_type *creature_ptr, int where)
245 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
246 int fd = fd_open(buf, O_RDONLY);
249 if (counts_seek(creature_ptr, fd, where, FALSE) || fd_read(fd, (char *)(&count), sizeof(u32b)))
258 * @brief ファイル位置に書き込む /
259 * @param creature_ptr プレーヤーへの参照ポインタ
260 * @param where ファイルバイト位置
265 errr counts_write(player_type *creature_ptr, int where, u32b count)
268 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
270 safe_setuid_grab(creature_ptr);
271 int fd = fd_open(buf, O_RDWR);
274 safe_setuid_grab(creature_ptr);
275 fd = fd_make(buf, 0644);
279 safe_setuid_grab(creature_ptr);
280 errr err = fd_lock(fd, F_WRLCK);
285 counts_seek(creature_ptr, fd, where, TRUE);
286 fd_write(fd, (char *)(&count), sizeof(u32b));
287 safe_setuid_grab(creature_ptr);
288 err = fd_lock(fd, F_UNLCK);
299 * @brief 墓のアスキーアートテンプレを読み込む
300 * @param buf テンプレへのバッファ
301 * @param buf_size バッファの長さ
304 void read_dead_file(char *buf, size_t buf_size)
306 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
309 fp = angband_fopen(buf, "r");
314 while (angband_fgets(fp, buf, buf_size) == 0) {
315 put_str(buf, i++, 0);