]> git.lyx.org Git - lyx.git/blob - src/support/lyxmagic.h
Outliner: distinguish non-active refs from broken refs
[lyx.git] / src / support / lyxmagic.h
1 // -*- C++ -*-
2 /**
3  * \file lyxmagic.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Jean-Marc Lasgouttes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_MAGIC_H
13 #define LYX_MAGIC_H
14
15 namespace lyx {
16
17 #ifdef HAVE_MAGIC_H
18
19 #include "support/debug.h"
20
21 #include <magic.h>
22
23 class Magic {
24 public:
25         Magic() : ok_(false) {
26                 cookie_ = magic_open(MAGIC_MIME);
27                 if (cookie_) {
28                         if (magic_load(cookie_, NULL) != 0)
29                                 LYXERR(Debug::GRAPHICS, "Magic: couldn't load magic database - "
30                                         << magic_error(cookie_));
31                         else
32                                 ok_ = true;
33                 }
34         }
35
36         ~Magic() {
37                 if(cookie_)
38                         magic_close(cookie_);
39         }
40
41         // returns a string of the form "mime-type;encoding", or an empty string on error.
42         std::string file(std::string const & name) const {
43                 if (!ok_)
44                         return std::string();
45
46                 char const * result = magic_file(cookie_, name.c_str());
47                 if (result)
48                         return result;
49                 else
50                         LYXERR(Debug::GRAPHICS, "Magic: couldn't query magic database - "
51                                    << magic_error(cookie_));
52
53                 return std::string();
54         }
55
56 private:
57         magic_t cookie_;
58         bool ok_;
59 };
60
61 #else // !HAVE_MAGIC_T
62
63 // A dummy Magic class that always returns an empty result
64 class Magic {
65 public:
66         Magic() {
67                 LYXERR(Debug::GRAPHICS, "Magic: libmagic support not configured");
68         }
69
70         std::string file(std::string const & ) const { return empty_string(); }
71 };
72
73 #endif // HAVE_MAGIC_T
74
75 }
76
77 #endif // LYX_MAGIC_H