Tokenizer.cxx 3.94 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "StringUtil.hxx"
23 24
#include "Error.hxx"
#include "Domain.hxx"
25

26 27
#include <glib.h>

28 29 30
#include <assert.h>
#include <string.h>

31
static constexpr Domain tokenizer_domain("tokenizer");
32 33 34 35 36 37 38 39 40 41 42 43 44 45

static inline bool
valid_word_first_char(char ch)
{
	return g_ascii_isalpha(ch);
}

static inline bool
valid_word_char(char ch)
{
	return g_ascii_isalnum(ch) || ch == '_';
}

char *
46
Tokenizer::NextWord(Error &error)
47
{
48
	char *const word = input;
49 50

	if (*input == 0)
51
		return nullptr;
52 53 54 55

	/* check the first character */

	if (!valid_word_first_char(*input)) {
56
		error.Set(tokenizer_domain, "Letter expected");
57
		return nullptr;
58 59 60 61 62 63 64 65 66 67
	}

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

	while (*++input != 0) {
		if (g_ascii_isspace(*input)) {
			/* a whitespace: the word ends here */
			*input = 0;
			/* skip all following spaces, too */
68
			input = strchug_fast(input + 1);
69 70 71 72
			break;
		}

		if (!valid_word_char(*input)) {
73
			error.Set(tokenizer_domain, "Invalid word character");
74
			return nullptr;
75 76 77 78 79 80 81 82 83
		}
	}

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

	return word;
}

84 85 86 87 88 89 90
static inline bool
valid_unquoted_char(char ch)
{
	return (unsigned char)ch > 0x20 && ch != '"' && ch != '\'';
}

char *
91
Tokenizer::NextUnquoted(Error &error)
92
{
93
	char *const word = input;
94 95

	if (*input == 0)
96
		return nullptr;
97 98 99 100

	/* check the first character */

	if (!valid_unquoted_char(*input)) {
101
		error.Set(tokenizer_domain, "Invalid unquoted character");
102
		return nullptr;
103 104 105 106 107 108 109 110 111 112
	}

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

	while (*++input != 0) {
		if (g_ascii_isspace(*input)) {
			/* a whitespace: the word ends here */
			*input = 0;
			/* skip all following spaces, too */
113
			input = strchug_fast(input + 1);
114 115 116 117
			break;
		}

		if (!valid_unquoted_char(*input)) {
118 119
			error.Set(tokenizer_domain,
				  "Invalid unquoted character");
120
			return nullptr;
121 122 123 124 125 126 127 128 129
		}
	}

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

	return word;
}

130
char *
131
Tokenizer::NextString(Error &error)
132
{
133
	char *const word = input, *dest = input;
134 135 136

	if (*input == 0)
		/* end of line */
137
		return nullptr;
138 139 140 141

	/* check for the opening " */

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

	++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" */
160
			--input;
161
			error.Set(tokenizer_domain, "Missing closing '\"'");
162
			return nullptr;
163 164 165 166 167 168 169 170 171 172 173
		}

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

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

	++input;
	if (*input != 0 && !g_ascii_isspace(*input)) {
174 175
		error.Set(tokenizer_domain,
			  "Space expected after closing '\"'");
176
		return nullptr;
177 178 179 180 181
	}

	/* finish the string and return it */

	*dest = 0;
182
	input = strchug_fast(input);
183 184 185 186
	return word;
}

char *
187
Tokenizer::NextParam(Error &error)
188
{
189
	if (*input == '"')
190
		return NextString(error);
191
	else
192
		return NextUnquoted(error);
193
}