Split a line into tokens using space, comma or equals as separators. Note strings with spaces still work, they need to be closed in quotes, which are removed upon tokenizing.
| 167 | // Split a line into tokens using space, comma or equals as separators. |
| 168 | // Note strings with spaces still work, they need to be closed in quotes, which are removed upon tokenizing. |
| 169 | void TFE_Parser::tokenizeLine(const char* line, TokenList& tokens) |
| 170 | { |
| 171 | tokens.clear(); |
| 172 | |
| 173 | const size_t len = strlen(line); |
| 174 | // first move past leading whitespace and ending white space. |
| 175 | size_t start = 0, end = 0; |
| 176 | for (size_t c = 0; c < len; c++) |
| 177 | { |
| 178 | if (!isWhitespace(line[c])) |
| 179 | { |
| 180 | if (start == 0 && end == 0) { start = c; } |
| 181 | end = c + 1; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // next start reading tokens. |
| 186 | bool inQuote = false; |
| 187 | char curToken[1024]; |
| 188 | size_t curTokenPos = 0; |
| 189 | // TODO: Add an option to allow white space in tokens when not in quotes, but still remove trailing/ending whitespace. |
| 190 | // This is useful for names. |
| 191 | for (size_t c = start; c < end; c++) |
| 192 | { |
| 193 | if (line[c] == '"') |
| 194 | { |
| 195 | if (inQuote && curTokenPos == 0) |
| 196 | { |
| 197 | tokens.push_back(""); |
| 198 | } |
| 199 | inQuote = !inQuote; |
| 200 | } |
| 201 | else if (!inQuote && (isWhitespace(line[c]) || isSeparator(line[c]))) |
| 202 | { |
| 203 | curToken[curTokenPos] = 0; |
| 204 | if (curTokenPos) |
| 205 | { |
| 206 | tokens.push_back(curToken); |
| 207 | } |
| 208 | curTokenPos = 0; |
| 209 | curToken[0] = 0; |
| 210 | } |
| 211 | else if (!inQuote && m_enableColonSeperator && line[c] == ':') |
| 212 | { |
| 213 | curToken[curTokenPos] = 0; |
| 214 | if (curTokenPos) |
| 215 | { |
| 216 | tokens.push_back(curToken); |
| 217 | } |
| 218 | curTokenPos = 0; |
| 219 | curToken[0] = 0; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | curToken[curTokenPos++] = line[c]; |
| 224 | } |
| 225 | } |
| 226 |
no test coverage detected