0001 #include <stdlib.h>
0002 #include <stdint.h>
0003 #include <string.h>
0004 #include <stdio.h>
0005
0006 #include "util/debug.h"
0007 #include "util/parse-sublevel-options.h"
0008
0009 static int parse_one_sublevel_option(const char *str,
0010 struct sublevel_option *opts)
0011 {
0012 struct sublevel_option *opt = opts;
0013 char *vstr, *s = strdup(str);
0014 int v = 1;
0015
0016 if (!s) {
0017 pr_err("no memory\n");
0018 return -1;
0019 }
0020
0021 vstr = strchr(s, '=');
0022 if (vstr)
0023 *vstr++ = 0;
0024
0025 while (opt->name) {
0026 if (!strcmp(s, opt->name))
0027 break;
0028 opt++;
0029 }
0030
0031 if (!opt->name) {
0032 pr_err("Unknown option name '%s'\n", s);
0033 free(s);
0034 return -1;
0035 }
0036
0037 if (vstr)
0038 v = atoi(vstr);
0039
0040 *opt->value_ptr = v;
0041 free(s);
0042 return 0;
0043 }
0044
0045
0046 int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts)
0047 {
0048 char *s = strdup(str);
0049 char *p = NULL;
0050 int ret;
0051
0052 if (!s) {
0053 pr_err("no memory\n");
0054 return -1;
0055 }
0056
0057 p = strtok(s, ",");
0058 while (p) {
0059 ret = parse_one_sublevel_option(p, opts);
0060 if (ret) {
0061 free(s);
0062 return ret;
0063 }
0064
0065 p = strtok(NULL, ",");
0066 }
0067
0068 free(s);
0069 return 0;
0070 }