我正在尝试使用 libmagic 和以下代码来确定几种类型的文件的 mime 类型:
auto handle = ::magic_open(MAGIC_MIME_TYPE);
::magic_load(handle, NULL);
// Both of these fail with the same error
// file_path being a const char* with the path to the file.
auto type2 = ::magic_file(handle, file_path);
// svg_content being an std::vector<char> with the contents of the file.
//auto type2 = ::magic_buffer(handle, svg_content.data(), svg_content.size());
if(!type2)
{
std::cout << magic_error(handle) << std::endl;
}
::magic_close(handle);
但是对于我尝试的任何文件或缓冲区,我都会收到正则表达式错误,或者是或类似于:46: regex error 17 for `(dryad-bibo/v)[0-9].[0-9]', (match failed)
例如这个 .svg 文件:
<svg xmlns="http://www.w3.org/2000/svg" id="flag-icon-css-no" viewBox="0 0 640 480">
<path fill="#ed2939" d="M0 0h640v480H0z"/>
<path fill="#fff" d="M180 0h120v480H180z"/>
<path fill="#fff" d="M0 180h640v120H0z"/>
<path fill="#002664" d="M210 0h60v480h-60z"/>
<path fill="#002664" d="M0 210h640v60H0z"/>
</svg>
到目前为止我已经尝试过:我正在链接本地构建的 libmagic 版本,在构建时我会遗漏什么吗?是否有任何调用不正确或有什么我遗漏的?
尝试运行在本地编译的相关文件二进制文件时出现类似错误。而当我使用默认可用的文件命令时,我会得到 image/svg+xml 作为输出。
编辑
为了构建 libmagic(适用于 macOS 和 Ubuntu),我按照以下步骤操作:
更新
看起来这个文件底部的正则表达式导致了问题(至少对于 svg):
https://github.com/file/file/blob/b56b58d499dbe58f2bed28e6b3c297fe7add992e/magic/Magdir/dataone
更新 2
发生了一些奇怪的事情;在我让它工作的系统上,
magic_version()
正如预期的那样,报告 540。但在出现此错误的系统上,magic_version()
报告 538。这对我来说意义不大,因为我无法在系统本身的任何地方以及运行
./file --version
时找到该版本。在构建库中,它报告 file-5.40
.
最佳答案
非常不满意的答案,但它链接到 GoogleTest以某种方式导致此错误,甚至没有运行任何测试,只是链接它。
我改用 Catch2相反,问题得到了解决。
关于c++ - 46 : regex error 17 for `(dryad-bibo/v)[0-9].[0-9]' ,(匹配失败),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69222631/