Tokenizer.cxx 3.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
21
#include "Tokenizer.hxx"
22
#include "CharUtil.hxx"
23
#include "StringUtil.hxx"
24 25
#include "Error.hxx"
#include "Domain.hxx"
26

27
static constexpr Domain tokenizer_domain("tokenizer");
28 29 30 31

static inline bool
valid_word_first_char(char ch)
{
32
	return IsAlphaASCII(ch);
33 34 35 36 37
}

static inline bool
valid_word_char(char ch)
{
38
	return IsAlphaNumericASCII(ch) || ch == '_';
39 40 41
}

char *
42
Tokenizer::NextWord(Error &error)
43
{
44
	char *const word = input;
45 46

	if (*input == 0)
47
		return nullptr;
48 49 50 51

	/* check the first character */

	if (!valid_word_first_char(*input)) {
52
		error.Set(tokenizer_domain, "Letter expected");
53
		return nullptr;
54 55 56 57 58 59
	}

	/* now iterate over the other characters until we find a
	   whitespace or end-of-string */

	while (*++input != 0) {
60
		if (IsWhitespaceOrNull(*input)) {
61 62 63
			/* a whitespace: the word ends here */
			*input = 0;
			/* skip all following spaces, too */
64
			input = strchug_fast(input + 1);
65 66 67 68
			break;
		}

		if (!valid_word_char(*input)) {
69
			error.Set(tokenizer_domain, "Invalid word character");
70
			return nullptr;
71 72 73 74 75 76 77 78 79
		}
	}

	/* end of string: the string is already null-terminated
	   here */

	return word;
}

80 81 82 83 84 85 86
static inline bool
valid_unquoted_char(char ch)
{
	return (unsigned char)ch > 0x20 && ch != '"' && ch != '\'';
}

char *
87
Tokenizer::NextUnquoted(Error &error)
88
{
89
	char *const word = input;
90 91

	if (*input == 0)
92
		return nullptr;
93 94 95 96

	/* check the first character */

	if (!valid_unquoted_char(*input)) {
97
		error.Set(tokenizer_domain, "Invalid unquoted character");
98
		return nullptr;
99 100 101 102 103 104
	}

	/* now iterate over the other characters until we find a
	   whitespace or end-of-string */

	while (*++input != 0) {
105
		if (IsWhitespaceOrNull(*input)) {
106 107 108
			/* a whitespace: the word ends here */
			*input = 0;
			/* skip all following spaces, too */
109
			input = strchug_fast(input + 1);
110 111 112 113
			break;
		}

		if (!valid_unquoted_char(*input)) {
114 115
			error.Set(tokenizer_domain,
				  "Invalid unquoted character");
116
			return nullptr;
117 118 119 120 121 122 123 124 125
		}
	}

	/* end of string: the string is already null-terminated
	   here */

	return word;
}

126
char *
127
Tokenizer::NextString(Error &error)
128
{
129
	char *const word = input, *dest = input;
130 131 132

	if (*input == 0)
		/* end of line */
133
		return nullptr;
134 135 136 137

	/* check for the opening " */

	if (*input != '"') {
138
		error.Set(tokenizer_domain, "'\"' expected");
139
		return nullptr;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	}

	++input;

	/* copy all characters */

	while (*input != '"') {
		if (*input == '\\')
			/* the backslash escapes the following
			   character */
			++input;

		if (*input == 0) {
			/* return input-1 so the caller can see the
			   difference between "end of line" and
			   "error" */
156
			--input;
157
			error.Set(tokenizer_domain, "Missing closing '\"'");
158
			return nullptr;
159 160 161 162 163 164 165 166 167 168
		}

		/* copy one character */
		*dest++ = *input++;
	}

	/* the following character must be a whitespace (or end of
	   line) */

	++input;
169
	if (!IsWhitespaceOrNull(*input)) {
170 171
		error.Set(tokenizer_domain,
			  "Space expected after closing '\"'");
172
		return nullptr;
173 174 175 176 177
	}

	/* finish the string and return it */

	*dest = 0;
178
	input = strchug_fast(input);
179 180 181 182
	return word;
}

char *
183
Tokenizer::NextParam(Error &error)
184
{
185
	if (*input == '"')
186
		return NextString(error);
187
	else
188
		return NextUnquoted(error);
189
}