1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2016, Rob Norris <rw_norris@hotmail.com>
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "file_magic.h"
36 * @magic_string: Type of file (this part matched against the magic library response)
37 * @extension: Optional extension to try when Magic Library is not available
39 * Returns: Whether the file is of the specified type.
40 * Uses the Magic library if available to determine the file against the supplied type.
41 * Otherwise uses a rudimentary file extension check.
43 gboolean file_magic_check ( const gchar *filename, const gchar *magic_string, const gchar *extension )
45 gboolean is_requested_file_type = FALSE;
48 // Or magic_version() if available - probably need libmagic 5.18 or so
49 // (can't determine exactly which version the versioning became available)
50 g_debug ("%s: magic version: %d", __FUNCTION__, MAGIC_VERSION );
52 magic_t myt = magic_open ( MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME );
55 // We have to 'package' the magic database ourselves :(
56 // --> %PROGRAM FILES%\Viking\magic.mgc
57 int ml = magic_load ( myt, ".\\magic.mgc" );
60 int ml = magic_load ( myt, NULL );
63 const char* magic = magic_file ( myt, filename );
64 g_debug ("%s: magic output: %s", __FUNCTION__, magic );
66 if ( g_ascii_strncasecmp ( magic, magic_string, strlen(magic_string) ) == 0 )
67 is_requested_file_type = TRUE;
70 g_critical ("%s: magic load database failure", __FUNCTION__ );
77 is_requested_file_type = a_file_check_ext ( filename, extension );
79 return is_requested_file_type;