ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-mime.c
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 (table_path != NULL) {
126                 gnome_vfs_mime_test_get_magic_table (table_path);
127         }
128
129         if (dump_table) {
130                 gnome_vfs_mime_dump_magic_table ();
131         }
132
133         if (speed_test) {
134                 timer = g_timer_new ();
135                 g_timer_start (timer);
136                 for (i = 0; i < 100; i++) {
137                         gnome_vfs_mime_info_reload ();
138                 }
139                 fprintf (stderr, "Mime reload took %g(ms)\n",
140                          g_timer_elapsed (timer, NULL) * 10.0);
141         }
142
143         for (; *argv != NULL; argv++) {
144                 uri_string = g_strdup (*argv);
145                 if (is_uri (uri_string)) {
146                         uri = gnome_vfs_uri_new (*argv);
147                 } else {
148                         uri = NULL;
149                 }
150                 if (uri == NULL) {
151                         if (uri_string[0] == '/') {
152                                 path = uri_string;
153                         } else {
154                                 curdir = g_get_current_dir ();
155                                 path = g_strconcat (curdir, "/", uri_string, NULL);
156                                 g_free (uri_string);
157                                 g_free (curdir);
158                         }
159                         uri_string = gnome_vfs_get_uri_from_local_path (path);
160                         g_free (path);
161                         uri = gnome_vfs_uri_new (uri_string);
162                 }
163                 if (uri == NULL) {
164                         printf ("%s is neither a full URI nor an absolute filename\n", *argv);
165                         continue;
166                 }
167
168                 if (magic_only) {
169                         result = gnome_vfs_get_mime_type_from_file_data (uri);
170                 } else if (suffix_only) {
171                         result = gnome_vfs_get_mime_type_from_uri (uri);
172                 } else {
173                         result = gnome_vfs_get_mime_type (uri_string);
174                 }
175         
176                 printf ("looks like %s is %s\n", *argv, result);
177                 gnome_vfs_uri_unref (uri);
178         }
179         
180         return bonobo_activation_debug_shutdown ();
181 }