OSDN Git Service

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