Esempio n. 1
0
static int
parse_property(const char *line, struct block *block)
{
	char *equal = strchr(line, '=');
	const char *property, *value;

	if (!equal) {
		berror(block, "malformated property, should be of the form 'key=value'");
		return 1;
	}

	/* split property and value */
	*equal = '\0';
	property = line;
	value = equal + 1;

#define PARSE(_name, _size, _type) \
	if (strcmp(property, #_name) == 0) { \
		strncpy(block->_name, value, _size - 1); \
		goto parsed; \
	} \

#define PARSE_NUM(_name) \
	if (strcmp(property, #_name) == 0) { \
		block->_name = atoi(value); \
		goto parsed; \
	} \

	PROTOCOL_KEYS(PARSE);
	PARSE(command, sizeof(block->command), _);
	PARSE_NUM(interval);
	PARSE_NUM(signal);
	/* TODO some better check for numbers and boolean */

#undef PARSE_NUM
#undef PARSE

	berror(block, "unknown property: \"%s\"", property);
	return 1;

parsed:
	bdebug(block, "set property %s to \"%s\"", property, value);
	return 0;
}
Esempio n. 2
0
int kvsscanf(const char *buffer, const char *format, va_list args)
{
	const char *buf = buffer;
	int count = 0;
	while (*format)
	{
		if (kisspace(*format))
		{
			while (kisspace(*buf))
				buf++;
		}
		else if (*format == '%')
		{
			format++;
			switch (*format++)
			{
			case '%':
			{
				if (*buffer++ != '%')
					return count;
			}

			case 'n':
			{
				int *out = va_arg(args, int *);
				*out = buf - buffer;
			}

			case 'd':
			{
				int32_t *out = va_arg(args, int32_t *);
				PARSE_NUM(buf, out, int32_t, uint32_t);
			}

			case 'u':
			{
				uint32_t *out = va_arg(args, uint32_t *);
				PARSE_NUM(buf, out, uint32_t, uint32_t);
			}
			}
		}
		else
		{
			if (*buffer++ != *format++)