summaryrefslogtreecommitdiff
path: root/examples/vm_power_manager/parse.c
blob: 5467035994c53e37d8569d6161191caa2b47b9b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2010-2014 Intel Corporation.
 * Copyright(c) 2014 6WIND S.A.
 */

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include <rte_log.h>

#include "parse.h"

/*
 * Parse elem, the elem could be single number/range or group
 * 1) A single number elem, it's just a simple digit. e.g. 9
 * 2) A single range elem, two digits with a '-' between. e.g. 2-6
 * 3) A group elem, combines multiple 1) or 2) e.g 0,2-4,6
 *    Within group, '-' used for a range separator;
 *                       ',' used for a single number.
 */
int
parse_set(const char *input, uint16_t set[], unsigned int num)
{
	unsigned int idx;
	const char *str = input;
	char *end = NULL;
	unsigned int min, max;

	memset(set, 0, num * sizeof(uint16_t));

	while (isblank(*str))
		str++;

	/* only digit or left bracket is qualify for start point */
	if (!isdigit(*str) || *str == '\0')
		return -1;

	while (isblank(*str))
		str++;
	if (*str == '\0')
		return -1;

	min = num;
	do {

		/* go ahead to the first digit */
		while (isblank(*str))
			str++;
		if (!isdigit(*str))
			return -1;

		/* get the digit value */
		errno = 0;
		idx = strtoul(str, &end, 10);
		if (errno || end == NULL || idx >= num)
			return -1;

		/* go ahead to separator '-' and ',' */
		while (isblank(*end))
			end++;
		if (*end == '-') {
			if (min == num)
				min = idx;
			else /* avoid continuous '-' */
				return -1;
		} else if ((*end == ',') || (*end == ':') || (*end == '\0')) {
			max = idx;

			if (min == num)
				min = idx;

			for (idx = RTE_MIN(min, max);
					idx <= RTE_MAX(min, max); idx++) {
				set[idx] = 1;
			}
			min = num;
		} else
			return -1;

		str = end + 1;
	} while ((*end != '\0') && (*end != ':'));

	return str - input;
}

int
parse_branch_ratio(const char *input, float *branch_ratio)
{
	const char *str = input;
	char *end = NULL;

	while (isblank(*str))
		str++;

	if (*str == '\0')
		return -1;

	/* Go straight to the ':' separator if present */
	while ((*str != '\0') && (*str != ':'))
		str++;

	/* Branch ratio not specified in args so leave it at default setting */
	if (*str == '\0')
		return 0;

	/* Confirm ':' separator present */
	if (*str != ':')
		return -1;

	str++;
	errno = 0;
	*branch_ratio = strtof(str, &end);
	if (errno || end == NULL)
		return -1;

	if (*end != '\0')
		return -1;

	str = end + 1;

	return str - input;
}