OSDN Git Service

[Refactor] #40635 Moved angband_header from info-reader-util.c/h to init.c/h
[hengband/hengband.git] / src / info-reader / info-reader-util.c
1 #include "info-reader/info-reader-util.h"
2 #include "main/angband-headers.h"
3 #include "object-enchant/activation-info-table.h"
4 #include "view/display-messages.h"
5
6 /*!
7  * @brief データの可変文字列情報をテキストとして保管する /
8  * Add a text to the text-storage and store offset to it.
9  * @param offset 文字列保管ポインタからのオフセット
10  * @param head テキスト保管ヘッダ情報の構造体参照ポインタ
11  * @param buf 保管文字列
12  * @param normal_text テキストの正規化を行う
13  * @return
14  * 無事保管ができたらTRUEを返す。
15  * Returns FALSE when there isn't enough space available to store
16  * the text.
17  */
18 bool add_text(u32b *offset, angband_header *head, concptr buf, bool normal_text)
19 {
20     if (head->text_size + strlen(buf) + 8 > FAKE_TEXT_SIZE)
21         return FALSE;
22
23     if (*offset == 0) {
24         *offset = ++head->text_size;
25     } else if (normal_text) {
26         /*
27          * If neither the end of the last line nor
28          * the beginning of current line is not a space,
29          * fill up a space as a correct separator of two words.
30          */
31         if (head->text_size > 0 &&
32 #ifdef JP
33             (*(head->text_ptr + head->text_size - 1) != ' ') && ((head->text_size == 1) || !iskanji(*(head->text_ptr + head->text_size - 2))) && (buf[0] != ' ')
34             && !iskanji(buf[0])
35 #else
36             (*(head->text_ptr + head->text_size - 1) != ' ') && (buf[0] != ' ')
37 #endif
38         ) {
39             *(head->text_ptr + head->text_size) = ' ';
40             head->text_size++;
41         }
42     }
43
44     strcpy(head->text_ptr + head->text_size, buf);
45     head->text_size += strlen(buf);
46     return TRUE;
47 }
48
49 /*!
50  * @brief データの可変文字列情報を名前として保管する /
51  * Add a name to the name-storage and return an offset to it.
52  * @param offset 文字列保管ポインタからのオフセット
53  * @param head テキスト保管ヘッダ情報の構造体参照ポインタ
54  * @param buf 保管文字列
55  * @return
56  * 無事保管ができたらTRUEを返す。
57  * Returns FALSE when there isn't enough space available to store
58  * the text.
59  */
60 bool add_name(u32b *offset, angband_header *head, concptr buf)
61 {
62     if (head->name_size + strlen(buf) + 8 > FAKE_NAME_SIZE)
63         return FALSE;
64
65     if (*offset == 0) {
66         *offset = ++head->name_size;
67     }
68
69     strcpy(head->name_ptr + head->name_size, buf);
70     head->name_size += strlen(buf);
71     return TRUE;
72 }
73
74 /*!
75  * @brief データの可変文字列情報をタグとして保管する /
76  * Add a tag to the tag-storage and return an offset to it.
77  * @param offset 文字列保管ポインタからのオフセット
78  * @param head テキスト保管ヘッダ情報の構造体参照ポインタ
79  * @param buf 保管文字列
80  * @return
81  * 無事保管ができたらTRUEを返す。
82  * Returns FALSE when there isn't enough space available to store
83  * the text.
84  */
85 bool add_tag(STR_OFFSET *offset, angband_header *head, concptr buf)
86 {
87     u32b i;
88     for (i = 1; i < head->tag_size; i += strlen(&head->tag_ptr[i]) + 1) {
89         if (streq(&head->tag_ptr[i], buf))
90             break;
91     }
92
93     if (i >= head->tag_size) {
94         if (head->tag_size + strlen(buf) + 8 > FAKE_TAG_SIZE)
95             return FALSE;
96
97         strcpy(head->tag_ptr + head->tag_size, buf);
98         i = head->tag_size;
99         head->tag_size += strlen(buf) + 1;
100     }
101
102     *offset = (s16b)i;
103     return TRUE;
104 }
105
106 /*!
107  * @brief テキストトークンを走査してフラグを一つ得る(汎用) /
108  * Grab one flag from a textual string
109  * @param flags ビットフラグを追加する先の参照ポインタ
110  * @param names トークン定義配列
111  * @param what 参照元の文字列ポインタ
112  * @return エラーコード
113  */
114 errr grab_one_flag(u32b *flags, concptr names[], concptr what)
115 {
116     for (int i = 0; i < 32; i++) {
117         if (streq(what, names[i])) {
118             *flags |= (1L << i);
119             return 0;
120         }
121     }
122
123     return -1;
124 }
125
126 /*!
127  * @brief テキストトークンを走査してフラグを一つ得る(発動能力用) /
128  * Grab one activation index flag
129  * @param what 参照元の文字列ポインタ
130  * @return 発動能力ID
131  */
132 byte grab_one_activation_flag(concptr what)
133 {
134     for (int i = 0;; i++) {
135         if (activation_info[i].flag == NULL)
136             break;
137
138         if (streq(what, activation_info[i].flag)) {
139             return activation_info[i].index;
140         }
141     }
142
143     int j = atoi(what);
144     if (j > 0) {
145         return ((byte)j);
146     }
147
148     msg_format(_("未知の発動・フラグ '%s'。", "Unknown activation flag '%s'."), what);
149     return 0;
150 }