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 "system/monster-race-info.h"
22 #include "system/player-type-definition.h"
23 #include "term/screen-processor.h"
24 #include "util/angband-files.h"
25 #include "view/display-messages.h"
27 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
28 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
29 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
30 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
31 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
32 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
33 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
34 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
35 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
36 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
37 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
38 concptr ANGBAND_DIR_DEBUG_SAVE; //*< Savefiles for debug data
39 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
40 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
43 * Buffer to hold the current savefile name
44 * 'savefile' holds full path name. 'savefile_base' holds only base name.
47 char savefile_base[40];
48 char debug_savefile[1024];
51 * @brief プレイヤーステータスをファイルダンプ出力する
52 * Hack -- Dump a character description file
53 * @param player_ptr プレイヤーへの参照ポインタ
57 * Allow the "full" flag to dump additional info,
58 * and trigger its usage from various places in the code.
60 errr file_character(PlayerType *player_ptr, concptr name)
63 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
64 int fd = fd_open(buf, O_RDONLY);
66 char out_val[sizeof(buf) + 128];
68 (void)sprintf(out_val, _("現存するファイル %s に上書きしますか? ", "Replace existing file %s? "), buf);
69 if (get_check_strict(player_ptr, out_val, CHECK_NO_HISTORY)) {
76 fff = angband_fopen(buf, "w");
80 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
85 make_character_dump(player_ptr, fff);
87 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
93 * @brief ファイルからランダムに行を一つ取得する /
94 * Get a random line from a file
95 * @param file_name ファイル名
96 * @param entry 特定条件時のN:タグヘッダID
97 * @param output 出力先の文字列参照ポインタ
101 * Based on the monster speech patch by Matt Graham,
104 errr get_rnd_line(concptr file_name, int entry, char *output)
107 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, file_name);
109 fp = angband_fopen(buf, "r");
117 if (angband_fgets(fp, buf, sizeof(buf)) != 0) {
123 if ((buf[0] != 'N') || (buf[1] != ':')) {
129 } else if (buf[2] == 'M') {
130 if (monraces_info[i2enum<MonsterRaceId>(entry)].flags1 & RF1_MALE) {
133 } else if (buf[2] == 'F') {
134 if (monraces_info[i2enum<MonsterRaceId>(entry)].flags1 & RF1_FEMALE) {
137 } else if (sscanf(&(buf[2]), "%d", &test) != EOF) {
142 msg_format("Error in line %d of %s!", line_num, file_name);
149 for (counter = 0;; counter++) {
151 test = angband_fgets(fp, buf, sizeof(buf));
153 /* Ignore lines starting with 'N:' */
154 if ((buf[0] == 'N') && (buf[1] == ':')) {
170 if (one_in_(counter + 1)) {
176 return counter ? 0 : -1;
181 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ) /
182 * @param file_name ファイル名
183 * @param entry 特定条件時のN:タグヘッダID
184 * @param output 出力先の文字列参照ポインタ
189 errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count)
192 for (int i = 0; i < count; i++) {
193 result = get_rnd_line(file_name, entry, output);
198 for (int j = 0; output[j]; j++) {
199 kanji |= iskanji(output[j]);
211 * @brief ファイル位置をシーク /
212 * @param player_ptr プレイヤーへの参照ポインタ
213 * @param fd ファイルディスクリプタ
214 * @param where ファイルバイト位置
215 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
219 static errr counts_seek(PlayerType *player_ptr, int fd, uint32_t where, bool flag)
221 char temp1[128], temp2[128];
222 auto short_pclass = enum2i(player_ptr->pclass);
223 #ifdef SAVEFILE_USE_UID
224 (void)sprintf(temp1, "%d.%s.%d%d%d", player_ptr->player_uid, savefile_base, short_pclass, player_ptr->ppersonality, player_ptr->age);
226 (void)sprintf(temp1, "%s.%d%d%d", savefile_base, short_pclass, player_ptr->ppersonality, player_ptr->age);
228 for (int i = 0; temp1[i]; i++) {
229 temp1[i] ^= (i + 1) * 63;
233 uint32_t zero_header[3] = { 0L, 0L, 0L };
235 if (fd_seek(fd, seekpoint + 3 * sizeof(uint32_t))) {
238 if (fd_read(fd, (char *)(temp2), sizeof(temp2))) {
243 fd_seek(fd, seekpoint);
244 fd_write(fd, (char *)zero_header, 3 * sizeof(uint32_t));
245 fd_write(fd, (char *)(temp1), sizeof(temp1));
249 if (strcmp(temp1, temp2) == 0) {
253 seekpoint += 128 + 3 * sizeof(uint32_t);
256 return fd_seek(fd, seekpoint + where * sizeof(uint32_t));
261 * @param player_ptr プレイヤーへの参照ポインタ
262 * @param where ファイルバイト位置
266 uint32_t counts_read(PlayerType *player_ptr, int where)
269 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
270 int fd = fd_open(buf, O_RDONLY);
273 if (counts_seek(player_ptr, fd, where, false) || fd_read(fd, (char *)(&count), sizeof(uint32_t))) {
283 * @brief ファイル位置に書き込む /
284 * @param player_ptr プレイヤーへの参照ポインタ
285 * @param where ファイルバイト位置
290 errr counts_write(PlayerType *player_ptr, int where, uint32_t count)
293 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
295 safe_setuid_grab(player_ptr);
296 int fd = fd_open(buf, O_RDWR);
299 safe_setuid_grab(player_ptr);
300 fd = fd_make(buf, 0644);
304 safe_setuid_grab(player_ptr);
305 errr err = fd_lock(fd, F_WRLCK);
311 counts_seek(player_ptr, fd, where, true);
312 fd_write(fd, (char *)(&count), sizeof(uint32_t));
313 safe_setuid_grab(player_ptr);
314 err = fd_lock(fd, F_UNLCK);
326 * @brief 墓のアスキーアートテンプレを読み込む
327 * @param buf テンプレへのバッファ
328 * @param buf_size バッファの長さ
330 void read_dead_file(char *buf, size_t buf_size)
332 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
335 fp = angband_fopen(buf, "r");
341 while (angband_fgets(fp, buf, buf_size) == 0) {
342 put_str(buf, i++, 0);