3 * @brief ファイル入出力管理 / Purpose: code dealing with files (and death)
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
8 * This software may be copied and distributed for educational, research,
9 * and not for profit purposes provided that this copyright and statement
10 * are included in all such copies. Other copyrights may also apply.
11 * 2014 Deskull rearranged comment for Doxygen.\n
16 #include "uid-checker.h"
18 #include "core.h" // 暫定。後で消す.
19 #include "character-dump.h"
21 concptr ANGBAND_DIR; //!< Path name: The main "lib" directory This variable is not actually used anywhere in the code
22 concptr ANGBAND_DIR_APEX; //!< High score files (binary) These files may be portable between platforms
23 concptr ANGBAND_DIR_BONE; //!< Bone files for player ghosts (ascii) These files are portable between platforms
24 concptr ANGBAND_DIR_DATA; //!< Binary image files for the "*_info" arrays (binary) These files are not portable between platforms
25 concptr ANGBAND_DIR_EDIT; //!< Textual template files for the "*_info" arrays (ascii) These files are portable between platforms
26 concptr ANGBAND_DIR_SCRIPT; //!< Script files These files are portable between platforms.
27 concptr ANGBAND_DIR_FILE; //!< Various extra files (ascii) These files may be portable between platforms
28 concptr ANGBAND_DIR_HELP; //!< Help files (normal) for the online help (ascii) These files are portable between platforms
29 concptr ANGBAND_DIR_INFO; //!< Help files (spoilers) for the online help (ascii) These files are portable between platforms
30 concptr ANGBAND_DIR_PREF; //!< Default user "preference" files (ascii) These files are rarely portable between platforms
31 concptr ANGBAND_DIR_SAVE; //!< Savefiles for current characters (binary)
32 concptr ANGBAND_DIR_USER; //!< User "preference" files (ascii) These files are rarely portable between platforms
33 concptr ANGBAND_DIR_XTRA; //!< Various extra files (binary) These files are rarely portable between platforms
36 * Buffer to hold the current savefile name
37 * 'savefile' holds full path name. 'savefile_base' holds only base name.
40 char savefile_base[40];
43 * todo サブルーチンとは言い難い。autopick.c から直接呼ばれている
44 * @brief process_pref_fileのサブルーチンとして条件分岐処理の解釈と結果を返す
45 * Helper function for "process_pref_file()"
46 * @param creature_ptr プレーヤーへの参照ポインタ
47 * @param sp テキスト文字列の参照ポインタ
48 * @param fp 再帰中のポインタ参照
53 * v: output buffer array
59 concptr process_pref_file_expr(player_type *creature_ptr, char **sp, char *fp)
63 while (iswspace(*s)) s++;
83 t = process_pref_file_expr(creature_ptr, &s, &f);
88 else if (streq(t, "IOR"))
91 while (*s && (f != b2))
93 t = process_pref_file_expr(creature_ptr, &s, &f);
94 if (*t && !streq(t, "0")) v = "1";
97 else if (streq(t, "AND"))
100 while (*s && (f != b2))
102 t = process_pref_file_expr(creature_ptr, &s, &f);
103 if (*t && streq(t, "0")) v = "0";
106 else if (streq(t, "NOT"))
109 while (*s && (f != b2))
111 t = process_pref_file_expr(creature_ptr, &s, &f);
112 if (*t && streq(t, "1")) v = "0";
115 else if (streq(t, "EQU"))
120 t = process_pref_file_expr(creature_ptr, &s, &f);
122 while (*s && (f != b2))
124 p = process_pref_file_expr(creature_ptr, &s, &f);
125 if (streq(t, p)) v = "1";
128 else if (streq(t, "LEQ"))
133 t = process_pref_file_expr(creature_ptr, &s, &f);
135 while (*s && (f != b2))
138 t = process_pref_file_expr(creature_ptr, &s, &f);
139 if (*t && atoi(p) > atoi(t)) v = "0";
142 else if (streq(t, "GEQ"))
147 t = process_pref_file_expr(creature_ptr, &s, &f);
149 while (*s && (f != b2))
152 t = process_pref_file_expr(creature_ptr, &s, &f);
153 if (*t && atoi(p) < atoi(t)) v = "0";
158 while (*s && (f != b2))
160 t = process_pref_file_expr(creature_ptr, &s, &f);
164 if (f != b2) v = "?x?x?";
166 if ((f = *s) != '\0') *s++ = '\0';
173 /* Accept all printables except spaces and brackets */
175 while (iskanji(*s) || (isprint(*s) && !my_strchr(" []", *s)))
177 if (iskanji(*s)) s++;
181 while (isprint(*s) && !my_strchr(" []", *s)) ++s;
184 if ((f = *s) != '\0') *s++ = '\0';
194 if (streq(b + 1, "SYS"))
198 else if (streq(b + 1, "KEYBOARD"))
200 v = ANGBAND_KEYBOARD;
202 else if (streq(b + 1, "GRAF"))
206 else if (streq(b + 1, "MONOCHROME"))
213 else if (streq(b + 1, "RACE"))
221 else if (streq(b + 1, "CLASS"))
229 else if (streq(b + 1, "PLAYER"))
231 static char tmp_player_name[32];
233 for (pn = creature_ptr->name, tpn = tmp_player_name; *pn; pn++, tpn++)
243 *tpn = my_strchr(" []", *pn) ? '_' : *pn;
249 else if (streq(b + 1, "REALM1"))
252 v = E_realm_names[creature_ptr->realm1];
254 v = realm_names[creature_ptr->realm1];
257 else if (streq(b + 1, "REALM2"))
260 v = E_realm_names[creature_ptr->realm2];
262 v = realm_names[creature_ptr->realm2];
265 else if (streq(b + 1, "LEVEL"))
267 sprintf(tmp, "%02d", creature_ptr->lev);
270 else if (streq(b + 1, "AUTOREGISTER"))
272 if (creature_ptr->autopick_autoregister)
277 else if (streq(b + 1, "MONEY"))
279 sprintf(tmp, "%09ld", (long int)creature_ptr->au);
290 * @brief プレイヤーステータスをファイルダンプ出力する
291 * Hack -- Dump a character description file
292 * @param creature_ptr プレーヤーへの参照ポインタ
293 * @param name 出力ファイル名
296 * Allow the "full" flag to dump additional info,
297 * and trigger its usage from various places in the code.
299 errr file_character(player_type *creature_ptr, concptr name, update_playtime_pf update_playtime, display_player_pf display_player, map_name_pf map_name)
302 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, name);
304 FILE_TYPE(FILE_TYPE_TEXT);
306 int fd = fd_open(buf, O_RDONLY);
311 (void)sprintf(out_val, _("現存するファイル %s に上書きしますか? ", "Replace existing file %s? "), buf);
312 if (get_check_strict(out_val, CHECK_NO_HISTORY)) fd = -1;
316 if (fd < 0) fff = my_fopen(buf, "w");
320 prt(_("キャラクタ情報のファイルへの書き出しに失敗しました!", "Character dump failed!"), 0, 0);
325 make_character_dump(creature_ptr, fff, update_playtime, display_player, map_name);
327 msg_print(_("キャラクタ情報のファイルへの書き出しに成功しました。", "Character dump successful."));
334 * @brief ファイルからランダムに行を一つ取得する /
335 * Get a random line from a file
336 * @param file_name ファイル名
337 * @param entry 特定条件時のN:タグヘッダID
338 * @param output 出力先の文字列参照ポインタ
342 * Based on the monster speech patch by Matt Graham,
345 errr get_rnd_line(concptr file_name, int entry, char *output)
348 path_build(buf, sizeof(buf), ANGBAND_DIR_FILE, file_name);
350 fp = my_fopen(buf, "r");
357 if (my_fgets(fp, buf, sizeof(buf)) != 0)
364 if ((buf[0] != 'N') || (buf[1] != ':')) continue;
370 else if (buf[2] == 'M')
372 if (r_info[entry].flags1 & RF1_MALE) break;
374 else if (buf[2] == 'F')
376 if (r_info[entry].flags1 & RF1_FEMALE) break;
378 else if (sscanf(&(buf[2]), "%d", &test) != EOF)
380 if (test == entry) break;
383 msg_format("Error in line %d of %s!", line_num, file_name);
389 for (counter = 0; ; counter++)
393 test = my_fgets(fp, buf, sizeof(buf));
396 /* Ignore lines starting with 'N:' */
397 if ((buf[0] == 'N') && (buf[1] == ':')) continue;
399 if (buf[0] != '#') break;
406 if (one_in_(counter + 1)) strcpy(output, buf);
410 return counter ? 0 : -1;
416 * @brief ファイルからランダムに行を一つ取得する(日本語文字列のみ) /
417 * @param file_name ファイル名
418 * @param entry 特定条件時のN:タグヘッダID
419 * @param output 出力先の文字列参照ポインタ
424 errr get_rnd_line_jonly(concptr file_name, int entry, char *output, int count)
427 for (int i = 0; i < count; i++)
429 result = get_rnd_line(file_name, entry, output);
432 for (int j = 0; output[j]; j++) kanji |= iskanji(output[j]);
442 * @brief ファイル位置をシーク /
443 * @param creature_ptr プレーヤーへの参照ポインタ
444 * @param fd ファイルディスクリプタ
445 * @param where ファイルバイト位置
446 * @param flag FALSEならば現ファイルを超えた位置へシーク時エラー、TRUEなら足りない間を0で埋め尽くす
450 static errr counts_seek(player_type *creature_ptr, int fd, u32b where, bool flag)
452 char temp1[128], temp2[128];
453 #ifdef SAVEFILE_USE_UID
454 (void)sprintf(temp1, "%d.%s.%d%d%d", creature_ptr->player_uid, savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
456 (void)sprintf(temp1, "%s.%d%d%d", savefile_base, creature_ptr->pclass, creature_ptr->pseikaku, creature_ptr->age);
458 for (int i = 0; temp1[i]; i++)
459 temp1[i] ^= (i + 1) * 63;
462 u32b zero_header[3] = { 0L, 0L, 0L };
465 if (fd_seek(fd, seekpoint + 3 * sizeof(u32b)))
467 if (fd_read(fd, (char*)(temp2), sizeof(temp2)))
472 fd_seek(fd, seekpoint);
473 fd_write(fd, (char*)zero_header, 3 * sizeof(u32b));
474 fd_write(fd, (char*)(temp1), sizeof(temp1));
478 if (strcmp(temp1, temp2) == 0)
481 seekpoint += 128 + 3 * sizeof(u32b);
484 return fd_seek(fd, seekpoint + where * sizeof(u32b));
490 * @param creature_ptr プレーヤーへの参照ポインタ
491 * @param where ファイルバイト位置
495 u32b counts_read(player_type *creature_ptr, int where)
498 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
499 int fd = fd_open(buf, O_RDONLY);
502 if (counts_seek(creature_ptr, fd, where, FALSE) ||
503 fd_read(fd, (char*)(&count), sizeof(u32b)))
513 * @brief ファイル位置に書き込む /
514 * @param creature_ptr プレーヤーへの参照ポインタ
515 * @param where ファイルバイト位置
520 errr counts_write(player_type *creature_ptr, int where, u32b count)
523 path_build(buf, sizeof(buf), ANGBAND_DIR_DATA, _("z_info_j.raw", "z_info.raw"));
526 int fd = fd_open(buf, O_RDWR);
530 FILE_TYPE(FILE_TYPE_DATA);
532 fd = fd_make(buf, 0644);
537 errr err = fd_lock(fd, F_WRLCK);
541 counts_seek(creature_ptr, fd, where, TRUE);
542 fd_write(fd, (char*)(&count), sizeof(u32b));
544 err = fd_lock(fd, F_UNLCK);
555 * @brief 墓のアスキーアートテンプレを読み込む
556 * @param buf テンプレへのバッファ
557 * @param buf_size バッファの長さ
560 void read_dead_file(char *buf, size_t buf_size)
562 path_build(buf, buf_size, ANGBAND_DIR_FILE, _("dead_j.txt", "dead.txt"));
565 fp = my_fopen(buf, "r");
569 while (my_fgets(fp, buf, buf_size) == 0)
571 put_str(buf, i++, 0);