OSDN Git Service

Merge pull request #2321 from habu1010/feature/refactor-died-from-to-string
[hengbandforosx/hengbandosx.git] / src / load / load-util.cpp
1 #include "load/load-util.h"
2 #include "locale/japanese.h"
3 #include "term/screen-processor.h"
4
5 FILE *loading_savefile;
6 uint32_t loading_savefile_version;
7 byte load_xor_byte; // Old "encryption" byte.
8 uint32_t v_check = 0L; // Simple "checksum" on the actual values.
9 uint32_t x_check = 0L; // Simple "checksum" on the encoded bytes.
10
11 /*
12  * Japanese Kanji code
13  * 0: Unknown
14  * 1: ASCII
15  * 2: EUC
16  * 3: SJIS
17  */
18 byte kanji_code = 0;
19
20 /*!
21  * @brief ゲームスクリーンにメッセージを表示する / Hack -- Show information on the screen, one line at a time.
22  * @param msg 表示文字列
23  * @details
24  * Avoid the top two lines, to avoid interference with "msg_print()".
25  */
26 void load_note(concptr msg)
27 {
28     static TERM_LEN y = 2;
29     prt(msg, y, 0);
30     if (++y >= 24) {
31         y = 2;
32     }
33
34     term_fresh();
35 }
36
37 /*!
38  * @brief ロードファイルポインタから1バイトを読み込む
39  * @return 読み込んだバイト値
40  * @details
41  * The following functions are used to load the basic building blocks
42  * of savefiles.  They also maintain the "checksum" info for 2.7.0+
43  */
44 byte sf_get(void)
45 {
46     byte c = getc(loading_savefile) & 0xFF;
47     byte v = c ^ load_xor_byte;
48     load_xor_byte = c;
49
50     v_check += v;
51     x_check += load_xor_byte;
52     return v;
53 }
54
55 /*!
56  * @brief ロードファイルポインタから1バイトを読み込む
57  */
58 byte rd_byte()
59 {
60     return sf_get();
61 }
62
63 /*!
64  * @brief ロードファイルポインタから符号なし16bit値を読み込む
65  */
66 uint16_t rd_u16b()
67 {
68     uint16_t val = sf_get();
69     val |= (static_cast<uint16_t>(sf_get()) << 8);
70
71     return val;
72 }
73
74 /*!
75  * @brief ロードファイルポインタから符号つき16bit値を読み込む
76  */
77 int16_t rd_s16b()
78 {
79     return static_cast<int16_t>(rd_u16b());
80 }
81
82 /*!
83  * @brief ロードファイルポインタから符号なし32bit値を読み込む
84  */
85 uint32_t rd_u32b()
86 {
87     uint32_t val = sf_get();
88     val |= (static_cast<uint32_t>(sf_get()) << 8);
89     val |= (static_cast<uint32_t>(sf_get()) << 16);
90     val |= (static_cast<uint32_t>(sf_get()) << 24);
91
92     return val;
93 }
94
95 /*!
96  * @brief ロードファイルポインタから符号つき32bit値を読み込む
97  */
98 int32_t rd_s32b()
99 {
100     return static_cast<int32_t>(rd_u32b());
101 }
102
103 /*!
104  * @brief ロードファイルポインタから文字列を読み込んでポインタに渡す / Hack -- read a string
105  * @param str 読み込みポインタ
106  * @param max 最大読み取りバイト数
107  */
108 void rd_string(char *str, int max)
109 {
110     for (int i = 0; true; i++) {
111         auto tmp8u = rd_byte();
112         if (i < max) {
113             str[i] = tmp8u;
114         }
115
116         if (!tmp8u) {
117             break;
118         }
119     }
120
121     str[max - 1] = '\0';
122 #ifdef JP
123     switch (kanji_code) {
124 #ifdef SJIS
125     case 2:
126         euc2sjis(str);
127         break;
128 #endif
129
130 #ifdef EUC
131     case 3:
132         sjis2euc(str);
133         break;
134 #endif
135
136     case 0: {
137         byte code = codeconv(str);
138
139         /* 漢字コードが判明したら、それを記録 */
140         if (code) {
141             kanji_code = code;
142         }
143
144         break;
145     }
146     default:
147         break;
148     }
149 #endif
150 }
151
152 /*!
153  * @brief ロードファイルポインタから文字列を読み込んで std::string オブジェクトに格納する
154  * @param str std::string オブジェクトへの参照
155  * @param max 最大読み取りバイト数
156  */
157 void rd_string(std::string &str, int max)
158 {
159     std::vector<char> buf(max);
160     rd_string(buf.data(), max);
161     str = buf.data();
162 }
163
164 /*!
165  * @brief ロードファイルポインタを指定バイト分飛ばして進める / Hack -- strip some bytes
166  * @param n スキップバイト数
167  */
168 void strip_bytes(int n)
169 {
170     while (n-- > 0) {
171         (void)rd_byte();
172     }
173 }
174
175 /**
176  * @brief ロード中のセーブファイルのバージョンが引数で指定したバージョンと比較して古いかどうか調べる
177  *
178  * @param version 比較するセーブファイルのバージョン
179  * @return bool ロード中のセーブファイルのバージョンが version より古いなら true
180  *              version と等しいかより新しいなら false
181  */
182 bool loading_savefile_version_is_older_than(uint32_t version)
183 {
184     return loading_savefile_version < version;
185 }