OSDN Git Service

[Implement] 一週間以上前のデバッグログの自動削除
[hengband/hengband.git] / src / io / tokenizer.c
1 #include "io/tokenizer.h"
2
3 /*!
4  * @brief 各種データテキストをトークン単位に分解する / Extract the first few "tokens" from a buffer
5  * @param buf データテキストの参照ポインタ
6  * @param num トークンの数
7  * @param tokens トークンを保管する文字列参照ポインタ配列
8  * @param mode オプション
9  * @return 解釈した文字列数
10  * @details
11  * <pre>
12  * This function uses "colon" and "slash" as the delimeter characters.
13  * We never extract more than "num" tokens.  The "last" token may include
14  * "delimeter" characters, allowing the buffer to include a "string" token.
15  * We save pointers to the tokens in "tokens", and return the number found.
16  * Hack -- Attempt to handle the 'c' character formalism
17  * Hack -- An empty buffer, or a final delimeter, yields an "empty" token.
18  * Hack -- We will always extract at least one token
19  * </pre>
20  */
21 s16b tokenize(char *buf, s16b num, char **tokens, BIT_FLAGS mode)
22 {
23         s16b i = 0;
24         char *s = buf;
25         while (i < num - 1)
26         {
27                 char *t;
28                 for (t = s; *t; t++)
29                 {
30                         if ((*t == ':') || (*t == '/')) break;
31
32                         if ((mode & TOKENIZE_CHECKQUOTE) && (*t == '\''))
33                         {
34                                 t++;
35                                 if (*t == '\\') t++;
36                                 if (!*t) break;
37
38                                 t++;
39                                 if (*t != '\'') *t = '\'';
40                         }
41
42                         if (*t == '\\') t++;
43                 }
44
45                 if (!*t) break;
46
47                 *t++ = '\0';
48                 tokens[i++] = s;
49                 s = t;
50         }
51
52         tokens[i++] = s;
53         return i;
54 }