1 /* magic_db.hh
2  * This file is part of Worker, a file manager for UNIX/X11.
3  * Copyright (C) 2008-2020 Ralf Hoffmann.
4  * You can contact me at: ralf@boomerangsworld.de
5  *   or http://www.boomerangsworld.de/worker
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef MAGIC_DB_HH
23 #define MAGIC_DB_HH
24 
25 #include "wdefines.h"
26 #include "aguix/mutex.h"
27 #include <string>
28 
29 #ifdef HAVE_LIBMAGIC
30 #  include <magic.h>
31 #endif
32 
33 class MagicDB
34 {
35 public:
36     MagicDB();
37     ~MagicDB();
38 
39     static MagicDB &getInstance();
40 
41     static const int NO_FLAG = 0x0;
42     static const int DECOMPRESS = 0x1;
43     static const int USE_MIME_TYPE = 0x2;
44     static const int USE_MIME_ENCODING = 0x4;
45     static const int USE_MIME = USE_MIME_TYPE | USE_MIME_ENCODING;
46 
47     void init();
48     void fini();
49     std::string getInfo( const char *filename, int flags );
50     std::string getInfo( const std::string &filename, int flags );
51     std::string getInfo( const void *buffer, size_t len, int flags );
52 private:
53 #ifdef HAVE_LIBMAGIC
54     magic_t m_magic_cookie;
55 #endif
56     MutEx m_lock;
57     bool m_magic_init;
58 
59     static MagicDB *m_instance;
60 };
61 
62 #endif
63