- magic_t magic_cookie = magic_open(MAGIC_MIME);
- if (magic_cookie) {
- string format;
- if (magic_load(magic_cookie, NULL) != 0) {
- LYXERR(Debug::GRAPHICS, "Formats::getFormatFromFile\n"
- << "\tCouldn't load magic database - "
- << magic_error(magic_cookie));
- } else {
- string mime = magic_file(magic_cookie,
- filename.toFilesystemEncoding().c_str());
- mime = token(mime, ';', 0);
- // we need our own ps/eps detection
- if ((mime != "application/postscript") && (mime != "text/plain")) {
- Formats::const_iterator cit =
- find_if(formatlist.begin(), formatlist.end(),
- FormatMimeEqual(mime));
- if (cit != formats.end()) {
- LYXERR(Debug::GRAPHICS, "\tgot format from MIME type: "
- << mime << " -> " << cit->name());
- 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();