1 | /* test-mime.c - Test for the mime type sniffing features of GNOME |
---|
2 | Virtual File System Library |
---|
3 | |
---|
4 | Copyright (C) 2000 Eazel |
---|
5 | |
---|
6 | The Gnome Library is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU Library General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | The Gnome Library is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | Library General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU Library General Public |
---|
17 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
18 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Pavel Cisler <pavel@eazel.com> |
---|
22 | */ |
---|
23 | #ifdef HAVE_CONFIG_H |
---|
24 | #include <config.h> |
---|
25 | #endif |
---|
26 | |
---|
27 | #include "gnome-vfs.h" |
---|
28 | #include "gnome-vfs-mime.h" |
---|
29 | #include "gnome-vfs-mime-magic.h" |
---|
30 | |
---|
31 | #include <stdio.h> |
---|
32 | #include <string.h> |
---|
33 | |
---|
34 | int |
---|
35 | main (int argc, char **argv) |
---|
36 | { |
---|
37 | GnomeVFSURI *uri; |
---|
38 | gboolean magic_only; |
---|
39 | gboolean suffix_only; |
---|
40 | gboolean dump_table; |
---|
41 | const char *result; |
---|
42 | const char *table_path; |
---|
43 | char *uri_string; |
---|
44 | struct stat tmp; |
---|
45 | |
---|
46 | table_path = NULL; |
---|
47 | magic_only = FALSE; |
---|
48 | dump_table = FALSE; |
---|
49 | suffix_only = FALSE; |
---|
50 | |
---|
51 | if (!gnome_vfs_init ()) { |
---|
52 | fprintf (stderr, "Cannot initialize gnome-vfs.\n"); |
---|
53 | return 1; |
---|
54 | } |
---|
55 | |
---|
56 | if (argc == 1 || strcmp (argv[1], "--help") == 0) { |
---|
57 | fprintf (stderr, "Usage: %s [--magicOnly | --suffixOnly] [--dumpTable] " |
---|
58 | " [--loadTable <table path>] fileToCheck1 [fileToCheck2 ...] \n", *argv); |
---|
59 | return 1; |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | ++argv; |
---|
64 | for (; *argv; argv++) { |
---|
65 | if (strcmp (*argv, "--magicOnly") == 0) { |
---|
66 | magic_only = TRUE; |
---|
67 | } else if (strcmp (*argv, "--suffixOnly") == 0) { |
---|
68 | suffix_only = TRUE; |
---|
69 | } else if (strcmp (*argv, "--dumpTable") == 0) { |
---|
70 | dump_table = TRUE; |
---|
71 | } else if (strcmp (*argv, "--loadTable") == 0) { |
---|
72 | ++argv; |
---|
73 | if (!*argv) { |
---|
74 | fprintf (stderr, "Table path expected.\n"); |
---|
75 | return 1; |
---|
76 | } |
---|
77 | table_path = *argv; |
---|
78 | if (stat(table_path, &tmp) != 0) { |
---|
79 | fprintf (stderr, "Table path %s not found.\n", table_path); |
---|
80 | return 1; |
---|
81 | } |
---|
82 | } else { |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | |
---|
88 | if (table_path != NULL) { |
---|
89 | gnome_vfs_mime_test_get_magic_table (table_path); |
---|
90 | } |
---|
91 | |
---|
92 | if (dump_table) { |
---|
93 | gnome_vfs_mime_dump_magic_table (); |
---|
94 | } |
---|
95 | |
---|
96 | for (; *argv != NULL; argv++) { |
---|
97 | uri = gnome_vfs_uri_new (*argv); |
---|
98 | if (uri == NULL) { |
---|
99 | uri_string = gnome_vfs_get_uri_from_local_path (*argv); |
---|
100 | uri = gnome_vfs_uri_new (uri_string); |
---|
101 | g_free (uri_string); |
---|
102 | } |
---|
103 | |
---|
104 | if (uri == NULL) { |
---|
105 | printf ("%s is not a valid uri or file name\n", *argv); |
---|
106 | return 1; |
---|
107 | } |
---|
108 | |
---|
109 | if (magic_only) { |
---|
110 | result = gnome_vfs_get_mime_type_from_file_data (uri); |
---|
111 | } else if (suffix_only) { |
---|
112 | result = gnome_vfs_get_mime_type_from_uri (uri); |
---|
113 | } else { |
---|
114 | result = gnome_vfs_get_mime_type (uri); |
---|
115 | } |
---|
116 | |
---|
117 | printf ("looks like %s is %s\n", *argv, result); |
---|
118 | gnome_vfs_uri_unref (uri); |
---|
119 | } |
---|
120 | |
---|
121 | return 0; |
---|
122 | } |
---|