2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2020-04-02 15:01:27 +02:00
|
|
|
|
2023-03-06 14:25:19 +01:00
|
|
|
#include "Names.hxx"
|
2020-04-02 15:01:27 +02:00
|
|
|
|
2020-04-24 20:38:26 +02:00
|
|
|
#include <cstdlib>
|
2020-04-02 15:01:27 +02:00
|
|
|
#include <map>
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
This program generates an optimized parser for tag names, by doign
|
|
|
|
switch() on the first character. This reduces the number of
|
|
|
|
strcmp() calls.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if (argc != 2)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
FILE *out = fopen(argv[1], "w");
|
|
|
|
|
|
|
|
std::map<std::string_view, TagType> names;
|
|
|
|
for (unsigned i = 0; i < unsigned(TAG_NUM_OF_ITEM_TYPES); ++i)
|
|
|
|
names[tag_item_names[i]] = TagType(i);
|
|
|
|
|
|
|
|
fprintf(out,
|
|
|
|
"#include \"ParseName.hxx\"\n"
|
2023-03-06 15:17:41 +01:00
|
|
|
"#include \"Type.hxx\"\n"
|
2020-04-02 15:01:27 +02:00
|
|
|
"\n"
|
|
|
|
"#include <assert.h>\n"
|
|
|
|
"#include <string.h>\n"
|
|
|
|
"\n"
|
|
|
|
"TagType\n"
|
|
|
|
"tag_name_parse(const char *name) noexcept\n"
|
|
|
|
"{\n"
|
|
|
|
" assert(name != nullptr);\n"
|
|
|
|
"\n"
|
|
|
|
" switch (*name) {\n");
|
|
|
|
|
|
|
|
char first = 0;
|
|
|
|
|
2020-10-22 10:36:13 +02:00
|
|
|
for (const auto &[name, tag] : names) {
|
2020-04-02 15:01:27 +02:00
|
|
|
if (name.front() != first) {
|
|
|
|
if (first != 0)
|
|
|
|
fprintf(out, " break;\n\n");
|
|
|
|
first = name.front();
|
|
|
|
fprintf(out, " case '%c':\n", first);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out,
|
|
|
|
" if (strcmp(name + 1, \"%.*s\") == 0) return TagType(%u);\n",
|
|
|
|
int(name.size() - 1), name.data() + 1, unsigned(tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(out, " break;\n\n");
|
|
|
|
|
|
|
|
fprintf(out,
|
|
|
|
" }\n"
|
|
|
|
"\n"
|
|
|
|
" return TAG_NUM_OF_ITEM_TYPES;\n"
|
|
|
|
"}\n");
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|