1 #define __GNOME_UTILS_C__
3 /*
4 * Helpers
5 *
6 * Author:
7 * Mitsuru Oka
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 *
10 * Copyright (C) 2002 authors
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include <string.h>
16 #include <ctype.h>
17 #include <glib.h>
19 /**
20 * gnome_uri_list_extract_uris:
21 * @uri_list: an uri-list in the standard format.
22 *
23 * Returns a GList containing strings allocated with g_malloc
24 * that have been splitted from @uri-list.
25 */
26 GList*
27 gnome_uri_list_extract_uris (const gchar* uri_list)
28 {
29 const gchar *p, *q;
30 gchar *retval;
31 GList *result = NULL;
33 g_return_val_if_fail (uri_list != NULL, NULL);
35 p = uri_list;
37 /* We don't actually try to validate the URI according to RFC
38 * 2396, or even check for allowed characters - we just ignore
39 * comments and trim whitespace off the ends. We also
40 * allow LF delimination as well as the specified CRLF.
41 */
42 while (p) {
43 if (*p != '#') {
44 while (isspace(*p))
45 p++;
47 q = p;
48 while (*q && (*q != '\n') && (*q != '\r'))
49 q++;
51 if (q > p) {
52 q--;
53 while (q > p && isspace(*q))
54 q--;
56 retval = (gchar*)g_malloc (q - p + 2);
57 strncpy (retval, p, q - p + 1);
58 retval[q - p + 1] = '\0';
60 result = g_list_prepend (result, retval);
61 }
62 }
63 p = strchr (p, '\n');
64 if (p)
65 p++;
66 }
68 return g_list_reverse (result);
69 }
71 /**
72 * gnome_uri_list_extract_filenames:
73 * @uri_list: an uri-list in the standard format
74 *
75 * Returns a GList containing strings allocated with g_malloc
76 * that contain the filenames in the uri-list.
77 *
78 * Note that unlike gnome_uri_list_extract_uris() function, this
79 * will discard any non-file uri from the result value.
80 */
81 GList*
82 gnome_uri_list_extract_filenames (const gchar* uri_list)
83 {
84 GList *tmp_list, *node, *result;
86 g_return_val_if_fail (uri_list != NULL, NULL);
88 result = gnome_uri_list_extract_uris (uri_list);
90 tmp_list = result;
91 while (tmp_list) {
92 gchar *s = (gchar*)tmp_list->data;
94 node = tmp_list;
95 tmp_list = tmp_list->next;
97 if (!strncmp (s, "file:", 5)) {
98 node->data = g_filename_from_uri (s, NULL, NULL);
99 /* not sure if this fallback is useful at all */
100 if (!node->data) node->data = g_strdup (s+5);
101 } else {
102 result = g_list_remove_link(result, node);
103 g_list_free_1 (node);
104 }
105 g_free (s);
106 }
107 return result;
108 }