Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
filetype.cpp
1 
2 /***************************************************************************
3  * filetype.cpp - little utility to decide on filetype
4  *
5  * Generated: Sun Oct 26 10:52:59 2008 (split off cpp file)
6  * Copyright 2005-2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <utils/system/filetype.h>
25 #include <core/exception.h>
26 
27 #ifdef HAVE_LIBMAGIC
28 # include <magic.h>
29 #endif
30 
31 namespace fawkes {
32 
33 /** Get filetype of file.
34  * Returns a long decriptive string of the filetype, similar to the file
35  * console utility.
36  * @param filename path to the file whose type should be determined
37  * @return descriptive string
38  */
39 std::string
40 filetype_file(const char *filename)
41 {
42  std::string rv;
43 
44 #ifdef HAVE_LIBMAGIC
45  magic_t m = magic_open( MAGIC_ERROR );
46  magic_load( m, NULL );
47 
48  const char * res = magic_file( m, filename );
49  if ( res == NULL ) {
50  fawkes::Exception e("Failed to determine file type of %s: %s", filename, magic_error(m));
51  magic_close(m);
52  throw e;
53  }
54 
55  rv = res;
56  magic_close( m );
57 #else
58  throw fawkes::Exception("Failed to determine file type of %s "
59  "(libmagic not available at compile time)",
60  filename);
61 #endif
62 
63  return rv;
64 }
65 
66 
67 /** Get mime-type of file.
68  * This function gives a brief mime-type for the given file.
69  * @param filename path to the file whose type should be determined
70  * @return descriptive string
71  * @param filename
72  */
73 std::string
74 mimetype_file(const char *filename)
75 {
76  std::string rv;
77 
78 #ifdef HAVE_LIBMAGIC
79 # ifdef MAGIC_MIME_TYPE
80  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME_TYPE );
81 # else
82  magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME );
83 # endif
84  magic_load( m, NULL );
85 
86  const char * res = magic_file( m, filename );
87  if ( res == NULL ) {
88  fawkes::Exception e("Failed to determine mime type of %s: %s", filename, magic_error(m));
89  magic_close(m);
90  throw e;
91  }
92 
93  rv = res;
94 # ifndef MAGIC_MIME_TYPE
95  rv = rv.substr(0, rv.find(","));
96 # endif
97  magic_close(m);
98 #else
99  throw fawkes::Exception("Failed to determine file type of %s "
100  "(libmagic not available at compile time)",
101  filename);
102 #endif
103  return rv;
104 }
105 
106 } // end namespace fawkes
107