filetype.cpp

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 #include <magic.h>
00028 
00029 namespace fawkes {
00030 
00031 /** Get filetype of file.
00032  * Returns a long decriptive string of the filetype, similar to the file
00033  * console utility.
00034  * @param filename path to the file whose type should be determined
00035  * @return descriptive string
00036  */
00037 std::string
00038 filetype_file(const char *filename)
00039 {
00040   std::string rv;
00041 
00042   magic_t m = magic_open( MAGIC_ERROR );
00043   magic_load( m, NULL );
00044 
00045   const char * res = magic_file( m, filename );
00046   if ( res == NULL ) {
00047     fawkes::Exception e("Failed to determine file type of %s: %s", filename, magic_error(m));
00048     magic_close(m);
00049     throw e;
00050   }
00051 
00052   rv = res;
00053   magic_close( m );
00054 
00055   return rv;
00056 }
00057 
00058 
00059 /** Get mime-type of file.
00060  * This function gives a brief mime-type for the given file.
00061  * @param filename path to the file whose type should be determined
00062  * @return descriptive string
00063  * @param filename 
00064  */
00065 std::string
00066 mimetype_file(const char *filename)
00067 {
00068   std::string rv;
00069 
00070 #ifdef MAGIC_MIME_TYPE
00071   magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME_TYPE );
00072 #else
00073   magic_t m = magic_open( MAGIC_ERROR | MAGIC_MIME );
00074 #endif
00075   magic_load( m, NULL );
00076 
00077   const char * res = magic_file( m, filename );
00078   if ( res == NULL ) {
00079     fawkes::Exception e("Failed to determine mime type of %s: %s", filename, magic_error(m));
00080     magic_close(m);
00081     throw e;
00082   }
00083 
00084   rv = res;
00085 #ifndef MAGIC_MIME_TYPE
00086   rv = rv.substr(0, rv.find(","));
00087 #endif
00088   magic_close(m);
00089   return rv;
00090 }
00091 
00092 } // end namespace fawkes
00093 

Generated on Tue Feb 22 13:31:28 2011 for Fawkes API by  doxygen 1.4.7