Prefer non-private mime-types in case of ambiguity.

GitOrigin-RevId: 6d44c83405e3fc0c9b386c26ca0f84474349a044
This commit is contained in:
levlam 2019-06-17 20:52:45 +03:00
parent 10e2fd28c0
commit 69814c72cb

View File

@ -15,7 +15,7 @@
#include <utility>
#include <vector>
std::pair<std::string, std::string> split(std::string s, char delimiter = ' ') {
static std::pair<std::string, std::string> split(std::string s, char delimiter = ' ') {
auto delimiter_pos = s.find(delimiter);
if (delimiter_pos == std::string::npos) {
return {std::move(s), ""};
@ -26,7 +26,7 @@ std::pair<std::string, std::string> split(std::string s, char delimiter = ' ') {
}
}
bool generate(const char *file_name, const char *from_name, const char *to_name,
static bool generate(const char *file_name, const char *from_name, const char *to_name,
const std::map<std::string, std::string> &map) {
// binary mode is needed for MSYS2 gperf
std::ofstream out(file_name, std::ios_base::trunc | std::ios_base::binary);
@ -71,6 +71,10 @@ bool generate(const char *file_name, const char *from_name, const char *to_name,
return true;
}
static bool is_private_mime_type(const std::string &mime_type) {
return mime_type.find("/x-") != std::string::npos;
}
int main(int argc, char *argv[]) {
if (argc != 4) {
std::cerr << "Wrong number of arguments supplied. Expected 'generate_mime_types_gperf <mime_types.txt> "
@ -132,7 +136,13 @@ int main(int argc, char *argv[]) {
for (auto &extension : extensions) {
if (!extension_to_mime_type.emplace(extension, mime_type).second) {
std::cerr << "Extension \"" << extension << "\" matches more than one type" << std::endl;
if (is_private_mime_type(extension_to_mime_type[extension]) == is_private_mime_type(mime_type)) {
std::cerr << "Extension \"" << extension << "\" matches more than one type" << std::endl;
} else {
if (!is_private_mime_type(mime_type)) {
extension_to_mime_type[extension] = mime_type;
}
}
}
}
}