Skip to content

Commit

Permalink
toml: Keep available manifest parsers in list
Browse files Browse the repository at this point in the history
This approach allows to easily expand parsers list in future,
without harming code readability.

Signed-off-by: Karol Trzcinski <karolx.trzcinski@linux.intel.com>
  • Loading branch information
ktrzcinx authored and lgirdwood committed Oct 19, 2020
1 parent c98a29d commit 8249abf
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/adsp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1562,13 +1562,35 @@ static int parse_version(toml_table_t *toml, int64_t version[2])
return 0;
}

static inline bool check_config_version(int major, int minor, const int64_t *version)
struct config_parser {
int major;
int minor;
int (*parse)(const toml_table_t *toml, struct adsp *out, bool verbose);
};

static const struct config_parser *find_config_parser(int64_t version[2])
{
return version[0] == major && version[1] == minor;
/* list of supported configuration version with handler to parser */
static const struct config_parser parsers[] = {
{1, 0, parse_adsp_config_v1_0},
{1, 5, parse_adsp_config_v1_5},
{1, 8, parse_adsp_config_v1_8},
{2, 5, parse_adsp_config_v2_5},
};
int i;

for (i = 0; i < ARRAY_SIZE(parsers); ++i) {
if (parsers[i].major == version[0] &&
parsers[i].minor == version[1]) {
return &parsers[i];
}
}
return NULL;
}

static int adsp_parse_config_fd(FILE *fd, struct adsp *out, bool verbose)
{
const struct config_parser *parser;
int64_t manifest_version[2];
toml_table_t *toml;
char errbuf[256];
Expand All @@ -1584,21 +1606,16 @@ static int adsp_parse_config_fd(FILE *fd, struct adsp *out, bool verbose)
if (ret < 0)
goto error;

/* parsing function depends on manifest_version */
if (check_config_version(1, 0, manifest_version)) {
ret = parse_adsp_config_v1_0(toml, out, verbose);
} else if (check_config_version(1, 5, manifest_version)) {
ret = parse_adsp_config_v1_5(toml, out, verbose);
} else if (check_config_version(1, 8, manifest_version)) {
ret = parse_adsp_config_v1_8(toml, out, verbose);
} else if (check_config_version(2, 5, manifest_version)) {
ret = parse_adsp_config_v2_5(toml, out, verbose);
} else {
/* find parser compatible with manifest version */
parser = find_config_parser(manifest_version);
if (!parser) {
ret = log_err(-EINVAL, "error: Unsupported config version %d.%d\n",
manifest_version[0], manifest_version[1]);
goto error;
}

/* run dedicated parser */
ret = parser->parse(toml, out, verbose);
error:
toml_free(toml);
return ret;
Expand Down

0 comments on commit 8249abf

Please sign in to comment.