Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * filetype.cpp - little utility to decide on filetype 00004 * 00005 * Generated: Sun Oct 26 10:52:59 2008 (split off cpp file) 00006 * Copyright 2005-2008 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <utils/system/filetype.h> 00025 #include <core/exception.h> 00026 00027 #ifdef HAVE_LIBMAGIC 00028 # include <magic.h> 00029 #endif 00030 00031 namespace fawkes { 00032 00033 /** Get filetype of file. 00034 * Returns a long decriptive string of the filetype, similar to the file 00035 * console utility. 00036 * @param filename path to the file whose type should be determined 00037 * @return descriptive string 00038 */ 00039 std::string 00040 filetype_file(const char *filename) 00041 { 00042 std::string rv; 00043 00044 #ifdef HAVE_LIBMAGIC 00045 magic_t m = magic_open( MAGIC_ERROR ); 00046 magic_load( m, NULL ); 00047 00048 const char * res = magic_file( m, filename ); 00049 if ( res == NULL ) { 00050 fawkes::Exception e("Failed to determine file type of %s: %s", filename, magic_error(m)); 00051 magic_close(m); 00052 throw e; 00053 } 00054 00055 rv = res; 00056 magic_close( m ); 00057 #else 00058 throw fawkes::Exception("Failed to determine file type of %s " 00059 "(libmagic not available at compile time)", 00060 filename); 00061 #endif 00062 00063 return rv; 00064 } 00065 00066 00067 /** Get mime-type of file. 00068 * This function gives a brief mime-type for the given file. 00069 * @param filename path to the file whose type should be determined 00070 * @return descriptive string 00071 * @param filename 00072 */ 00073 std::string 00074 mimetype_file(const char *filename) 00075 { 00076 std::string rv; 00077 00078 #ifdef HAVE_LIBMAGIC 00079 # ifdef MAGIC_MIME_TYPE 00080 magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME_TYPE ); 00081 # else 00082 magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME ); 00083 # endif 00084 magic_load( m, NULL ); 00085 00086 const char * res = magic_file( m, filename ); 00087 if ( res == NULL ) { 00088 fawkes::Exception e("Failed to determine mime type of %s: %s", filename, magic_error(m)); 00089 magic_close(m); 00090 throw e; 00091 } 00092 00093 rv = res; 00094 # ifndef MAGIC_MIME_TYPE 00095 rv = rv.substr(0, rv.find(",")); 00096 # endif 00097 magic_close(m); 00098 #else 00099 throw fawkes::Exception("Failed to determine file type of %s " 00100 "(libmagic not available at compile time)", 00101 filename); 00102 #endif 00103 return rv; 00104 } 00105 00106 } // end namespace fawkes 00107