- magic_t magic_cookie = magic_open(MAGIC_MIME);
- if (magic_cookie) {
- if (magic_load(magic_cookie, NULL) != 0) {
- LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
- << "\tCouldn't load magic database - "
- << magic_error(magic_cookie));
- } else {
- char const * result = magic_file(magic_cookie,
- filename.toFilesystemEncoding().c_str());
- string mime;
- if (result)
- mime = token(result, ';', 0);
- else {
- LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
- << "\tCouldn't query magic database - "
- << magic_error(magic_cookie));
- }
- // our own detection is better for binary files (can be anything)
- // and different plain text formats
- if (!mime.empty() && mime != "application/octet-stream" &&
- mime != "text/plain") {
- Formats::const_iterator cit =
- find_if(formatlist.begin(), formatlist.end(),
- FormatMimeEqual(mime));
- if (cit != formatlist.end()) {
- LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
- << mime << " -> " << cit->name());
- // See special eps/ps handling below
- if (mime == "application/postscript")
- psformat = cit->name();
- else
- format = cit->name();
- }
- }
+ // one instance of Magic that will be reused for next calls
+ // This avoids to read the magic file everytime
+ // If libmagic is not available, Magic::file returns an empty string.
+ static Magic magic;
+ string const result = magic.file(filename.toFilesystemEncoding());
+ string const mime = token(result, ';', 0);
+ // our own detection is better for binary files (can be anything)
+ // and different plain text formats
+ if (!mime.empty() && mime != "application/octet-stream" &&
+ mime != "text/plain") {
+ Formats::const_iterator cit =
+ find_if(formatlist_.begin(), formatlist_.end(),
+ FormatMimeEqual(mime));
+ if (cit != formatlist_.end()) {
+ LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
+ << mime << " -> " << cit->name());
+ // See special eps/ps handling below
+ if (mime == "application/postscript")
+ psformat = cit->name();
+ else
+ format = cit->name();