OSDN Git Service

[Refactor] wiz_spell_stat をstatic constexprに変更した
[hengbandforosx/hengbandosx.git] / src / io / tokenizer.cpp
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 int16_t tokenize(char *buf, int16_t num, char **tokens, BIT_FLAGS mode)
22 {
23     int16_t i = 0;
24     char *s = buf;
25     while (i < num - 1) {
26         char *t;
27         for (t = s; *t; t++) {
28             if ((*t == ':') || (*t == '/')) {
29                 break;
30             }
31
32             if ((mode & TOKENIZE_CHECKQUOTE) && (*t == '\'')) {
33                 t++;
34                 if (*t == '\\') {
35                     t++;
36                 }
37                 if (!*t) {
38                     break;
39                 }
40
41                 t++;
42                 if (*t != '\'') {
43                     *t = '\'';
44                 }
45             }
46
47             if (*t == '\\') {
48                 t++;
49             }
50         }
51
52         if (!*t) {
53             break;
54         }
55
56         *t++ = '\0';
57         tokens[i++] = s;
58         s = t;
59     }
60
61     tokens[i++] = s;
62     return i;
63 }