1 | #include <stdlib.h> |
2 | #include <stdint.h> |
3 | #include <string.h> |
4 | #include <stdio.h> |
5 | |
6 | #include "util/debug.h" |
7 | #include "util/parse-sublevel-options.h" |
8 | |
9 | static int parse_one_sublevel_option(const char *str, |
10 | struct sublevel_option *opts) |
11 | { |
12 | struct sublevel_option *opt = opts; |
13 | char *vstr, *s = strdup(str); |
14 | int v = 1; |
15 | |
16 | if (!s) { |
17 | pr_err("no memory\n" ); |
18 | return -1; |
19 | } |
20 | |
21 | vstr = strchr(s, '='); |
22 | if (vstr) |
23 | *vstr++ = 0; |
24 | |
25 | while (opt->name) { |
26 | if (!strcmp(s, opt->name)) |
27 | break; |
28 | opt++; |
29 | } |
30 | |
31 | if (!opt->name) { |
32 | pr_err("Unknown option name '%s'\n" , s); |
33 | free(s); |
34 | return -1; |
35 | } |
36 | |
37 | if (vstr) |
38 | v = atoi(vstr); |
39 | |
40 | *opt->value_ptr = v; |
41 | free(s); |
42 | return 0; |
43 | } |
44 | |
45 | /* parse options like --foo a=<n>,b,c... */ |
46 | int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts) |
47 | { |
48 | char *s = strdup(str); |
49 | char *p = NULL; |
50 | int ret; |
51 | |
52 | if (!s) { |
53 | pr_err("no memory\n" ); |
54 | return -1; |
55 | } |
56 | |
57 | p = strtok(s, "," ); |
58 | while (p) { |
59 | ret = parse_one_sublevel_option(str: p, opts); |
60 | if (ret) { |
61 | free(s); |
62 | return ret; |
63 | } |
64 | |
65 | p = strtok(NULL, "," ); |
66 | } |
67 | |
68 | free(s); |
69 | return 0; |
70 | } |
71 | |