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 | |
---|
24 | #include <config.h> |
---|
25 | #include <bonobo-activation/bonobo-activation.h> |
---|
26 | #include <libgnomevfs/gnome-vfs-init.h> |
---|
27 | #include <libgnomevfs/gnome-vfs-mime-magic.h> |
---|
28 | #include <libgnomevfs/gnome-vfs-mime-utils.h> |
---|
29 | #include <libgnomevfs/gnome-vfs-mime-info.h> |
---|
30 | #include <libgnomevfs/gnome-vfs-mime.h> |
---|
31 | #include <libgnomevfs/gnome-vfs-utils.h> |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <string.h> |
---|
35 | |
---|
36 | #include <sys/stat.h> |
---|
37 | |
---|
38 | #include <glib/gstrfuncs.h> |
---|
39 | #include <glib/gutils.h> |
---|
40 | |
---|
41 | static gboolean |
---|
42 | is_good_scheme_char (char c) |
---|
43 | { |
---|
44 | return g_ascii_isalnum (c) || c == '+' || c == '-' || c == '.'; |
---|
45 | } |
---|
46 | |
---|
47 | static gboolean |
---|
48 | is_uri (const char *str) |
---|
49 | { |
---|
50 | const char *p; |
---|
51 | |
---|
52 | if (! g_ascii_isalpha (*str)) { |
---|
53 | return FALSE; |
---|
54 | } |
---|
55 | |
---|
56 | p = str + 1; |
---|
57 | while (is_good_scheme_char (*p)) { |
---|
58 | p++; |
---|
59 | } |
---|
60 | return *p == ':' && strchr (p, '/') != NULL; |
---|
61 | } |
---|
62 | |
---|
63 | int |
---|
64 | main (int argc, char **argv) |
---|
65 | { |
---|
66 | GnomeVFSURI *uri; |
---|
67 | gboolean magic_only; |
---|
68 | gboolean suffix_only; |
---|
69 | gboolean dump_table; |
---|
70 | gboolean speed_test; |
---|
71 | const char *result; |
---|
72 | const char *table_path; |
---|
73 | char *uri_string; |
---|
74 | char *curdir; |
---|
75 | char *path; |
---|
76 | struct stat tmp; |
---|
77 | GTimer *timer; |
---|
78 | int i; |
---|
79 | |
---|
80 | table_path = NULL; |
---|
81 | magic_only = FALSE; |
---|
82 | dump_table = FALSE; |
---|
83 | speed_test = FALSE; |
---|
84 | suffix_only = FALSE; |
---|
85 | |
---|
86 | if (!gnome_vfs_init ()) { |
---|
87 | fprintf (stderr, "Cannot initialize gnome-vfs.\n"); |
---|
88 | return 1; |
---|
89 | } |
---|
90 | |
---|
91 | if (argc == 1 || strcmp (argv[1], "--help") == 0) { |
---|
92 | fprintf (stderr, "Usage: %s [--magicOnly | --suffixOnly] [--dumpTable] " |
---|
93 | " [--loadTable <table path>] fileToCheck1 [fileToCheck2 ...] \n", *argv); |
---|
94 | return 1; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | ++argv; |
---|
99 | for (; *argv; argv++) { |
---|
100 | if (strcmp (*argv, "--magicOnly") == 0) { |
---|
101 | magic_only = TRUE; |
---|
102 | } else if (strcmp (*argv, "--suffixOnly") == 0) { |
---|
103 | suffix_only = TRUE; |
---|
104 | } else if (strcmp (*argv, "--dumpTable") == 0) { |
---|
105 | dump_table = TRUE; |
---|
106 | } else if (strcmp (*argv, "--speedTest") == 0) { |
---|
107 | speed_test = TRUE; |
---|
108 | } else if (strcmp (*argv, "--loadTable") == 0) { |
---|
109 | ++argv; |
---|
110 | if (!*argv) { |
---|
111 | fprintf (stderr, "Table path expected.\n"); |
---|
112 | return 1; |
---|
113 | } |
---|
114 | table_path = *argv; |
---|
115 | if (stat(table_path, &tmp) != 0) { |
---|
116 | fprintf (stderr, "Table path %s not found.\n", table_path); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | } else { |
---|
120 | break; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | if (speed_test) { |
---|
126 | timer = g_timer_new (); |
---|
127 | g_timer_start (timer); |
---|
128 | for (i = 0; i < 100; i++) { |
---|
129 | gnome_vfs_mime_info_reload (); |
---|
130 | } |
---|
131 | fprintf (stderr, "Mime reload took %g(ms)\n", |
---|
132 | g_timer_elapsed (timer, NULL) * 10.0); |
---|
133 | } |
---|
134 | |
---|
135 | for (; *argv != NULL; argv++) { |
---|
136 | uri_string = g_strdup (*argv); |
---|
137 | if (is_uri (uri_string)) { |
---|
138 | uri = gnome_vfs_uri_new (*argv); |
---|
139 | } else { |
---|
140 | uri = NULL; |
---|
141 | } |
---|
142 | if (uri == NULL) { |
---|
143 | if (uri_string[0] == '/') { |
---|
144 | path = uri_string; |
---|
145 | } else { |
---|
146 | curdir = g_get_current_dir (); |
---|
147 | path = g_strconcat (curdir, "/", uri_string, NULL); |
---|
148 | g_free (uri_string); |
---|
149 | g_free (curdir); |
---|
150 | } |
---|
151 | uri_string = gnome_vfs_get_uri_from_local_path (path); |
---|
152 | g_free (path); |
---|
153 | uri = gnome_vfs_uri_new (uri_string); |
---|
154 | } |
---|
155 | if (uri == NULL) { |
---|
156 | printf ("%s is neither a full URI nor an absolute filename\n", *argv); |
---|
157 | continue; |
---|
158 | } |
---|
159 | |
---|
160 | if (magic_only) { |
---|
161 | result = gnome_vfs_get_mime_type_from_file_data (uri); |
---|
162 | } else if (suffix_only) { |
---|
163 | result = gnome_vfs_get_mime_type_from_uri (uri); |
---|
164 | } else { |
---|
165 | result = gnome_vfs_get_mime_type (uri_string); |
---|
166 | } |
---|
167 | |
---|
168 | printf ("looks like %s is %s\n", *argv, result); |
---|
169 | gnome_vfs_uri_unref (uri); |
---|
170 | } |
---|
171 | |
---|
172 | return bonobo_activation_debug_shutdown (); |
---|
173 | } |
---|