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 "term/z-form.h"
25 #include "util/angband-files.h"
26 #include "view/display-messages.h"
28 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
29 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
30 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
31 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
32 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
33 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
34 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
35 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
36 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
37 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
38 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
39 concptr ANGBAND_DIR_DEBUG_SAVE; //*< Savefiles for debug data
40 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
41 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
44 * Buffer to hold the current savefile name
45 * 'savefile' holds full path name. 'savefile_base' holds only base name.
48 char savefile_base[40];
49 char debug_savefile[1024];
52 * @brief プレイヤーステータスをファイルダンプ出力する
53 * Hack -- Dump a character description file
54 * @param player_ptr プレイヤーへの参照ポインタ
58 * Allow the "full" flag to dump additional info,
59 * and trigger its usage from various places in the code.
61 errr file_character(PlayerType *player_ptr, concptr name)
64 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
65 int fd = fd_open(buf, O_RDONLY);
67 std::string query = _("現存するファイル ", "Replace existing file ");
68 query.append(buf).append(_(" に上書きしますか? ", "? "));
70 if (get_check_strict(player_ptr, query, CHECK_NO_HISTORY)) {
77 fff = angband_fopen(buf, "w");
81 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
86 make_character_dump(player_ptr, fff);
88 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
94 * @brief ファイルからランダムに行を一つ取得する
95 * @param file_name ファイル名
96 * @param entry 特定条件時のN:タグヘッダID
97 * @return ファイルから取得した行 (但しファイルがなかったり異常値ならばnullopt)
99 std::optional<std::string> get_random_line(concptr file_name, int entry)
102 path_build(filename, sizeof(filename), ANGBAND_DIR_FILE, file_name);
103 auto *fp = angband_fopen(filename, "r");
112 if (angband_fgets(fp, buf, sizeof(buf)) != 0) {
118 if ((buf[0] != 'N') || (buf[1] != ':')) {
127 if (monraces_info[i2enum<MonsterRaceId>(entry)].flags1 & RF1_MALE) {
130 } else if (buf[2] == 'F') {
131 if (monraces_info[i2enum<MonsterRaceId>(entry)].flags1 & RF1_FEMALE) {
134 } else if (sscanf(&(buf[2]), "%d", &test) != EOF) {
139 msg_format("Error in line %d of %s!", line_num, file_name);
150 if (angband_fgets(fp, buf, sizeof(buf))) {
154 if ((buf[0] == 'N') && (buf[1] == ':')) {
167 if (one_in_(counter + 1)) {
184 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ)
185 * @param file_name ファイル名
186 * @param entry 特定条件時のN:タグヘッダID
188 * @return ファイルから取得した行 (但しファイルがなかったり異常値ならばnullopt)
191 std::optional<std::string> get_random_line_ja_only(concptr file_name, int entry, int count)
193 std::optional<std::string> line;
194 for (auto i = 0; i < count; i++) {
195 line = get_random_line(file_name, entry);
196 if (!line.has_value()) {
200 auto is_kanji = false;
201 for (auto c = line.value().data(); *c != '\0'; c++) {
202 is_kanji |= iskanji(*c);
215 * @brief ファイル位置をシーク /
216 * @param player_ptr プレイヤーへの参照ポインタ
217 * @param fd ファイルディスクリプタ
218 * @param where ファイルバイト位置
219 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
223 static errr counts_seek(PlayerType *player_ptr, int fd, uint32_t where, bool flag)
225 char temp1[128]{}, temp2[128]{};
226 auto short_pclass = enum2i(player_ptr->pclass);
227 #ifdef SAVEFILE_USE_UID
228 strnfmt(temp1, sizeof(temp1), "%d.%s.%d%d%d", player_ptr->player_uid, savefile_base, short_pclass, player_ptr->ppersonality, player_ptr->age);
230 strnfmt(temp1, sizeof(temp1), "%s.%d%d%d", savefile_base, short_pclass, player_ptr->ppersonality, player_ptr->age);
232 for (int i = 0; temp1[i]; i++) {
233 temp1[i] ^= (i + 1) * 63;
237 uint32_t zero_header[3] = { 0L, 0L, 0L };
239 if (fd_seek(fd, seekpoint + 3 * sizeof(uint32_t))) {
242 if (fd_read(fd, (char *)(temp2), sizeof(temp2))) {
247 fd_seek(fd, seekpoint);
248 fd_write(fd, (char *)zero_header, 3 * sizeof(uint32_t));
249 fd_write(fd, (char *)(temp1), sizeof(temp1));
253 if (strcmp(temp1, temp2) == 0) {
257 seekpoint += 128 + 3 * sizeof(uint32_t);
260 return fd_seek(fd, seekpoint + where * sizeof(uint32_t));
265 * @param player_ptr プレイヤーへの参照ポインタ
266 * @param where ファイルバイト位置
270 uint32_t counts_read(PlayerType *player_ptr, int where)
273 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
274 int fd = fd_open(buf, O_RDONLY);
277 if (counts_seek(player_ptr, fd, where, false) || fd_read(fd, (char *)(&count), sizeof(uint32_t))) {
287 * @brief ファイル位置に書き込む /
288 * @param player_ptr プレイヤーへの参照ポインタ
289 * @param where ファイルバイト位置
294 errr counts_write(PlayerType *player_ptr, int where, uint32_t count)
297 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
299 safe_setuid_grab(player_ptr);
300 int fd = fd_open(buf, O_RDWR);
303 safe_setuid_grab(player_ptr);
304 fd = fd_make(buf, 0644);
308 safe_setuid_grab(player_ptr);
309 errr err = fd_lock(fd, F_WRLCK);
315 counts_seek(player_ptr, fd, where, true);
316 fd_write(fd, (char *)(&count), sizeof(uint32_t));
317 safe_setuid_grab(player_ptr);
318 err = fd_lock(fd, F_UNLCK);
330 * @brief 墓のアスキーアートテンプレを読み込む
331 * @param buf テンプレへのバッファ
332 * @param buf_size バッファの長さ
334 void read_dead_file(char *buf, size_t buf_size)
336 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
339 fp = angband_fopen(buf, "r");
345 while (angband_fgets(fp, buf, buf_size) == 0) {
346 put_str(buf, i++, 0);