summaryrefslogtreecommitdiffstats
path: root/src/helper/gnome-utils.cpp
blob: 3d2b333a281419de6af84d87b695c26f840cfc47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * Helpers
 *
 * Authors:
 *   Mitsuru Oka
 *   Lauris Kaplinski <lauris@kaplinski.com>
 *
 * Copyright (C) 2002 authors
 *
 * Released under GNU GPL, read the file 'COPYING' for more information
 */

#include <string.h>
#include <ctype.h>
#include <glib.h>

#include "gnome-utils.h"

/**
 * gnome_uri_list_extract_uris:
 * @uri_list: an uri-list in the standard format.
 *
 * Returns a GList containing strings allocated with g_malloc
 * that have been splitted from @uri-list.
 */
GList *gnome_uri_list_extract_uris(const gchar *uri_list)
{
    const gchar *p, *q;
    gchar *retval;
    GList *result = NULL;

    g_return_val_if_fail(uri_list != NULL, NULL);

    p = uri_list;

    /* We don't actually try to validate the URI according to RFC
     * 2396, or even check for allowed characters - we just ignore
     * comments and trim whitespace off the ends.  We also
     * allow LF delimination as well as the specified CRLF.
     */
    while (p) {
        if (*p != '#') {
            while (isspace(*p))
                p++;

            q = p;
            while (*q && (*q != '\n') && (*q != '\r'))
                q++;

            if (q > p) {
                q--;
                while (q > p && isspace(*q))
                    q--;

                retval = (gchar *)g_malloc(q - p + 2);
                strncpy(retval, p, q - p + 1);
                retval[q - p + 1] = '\0';

                result = g_list_prepend(result, retval);
            }
        }
        p = strchr(p, '\n');
        if (p)
            p++;
    }

    return g_list_reverse(result);
}

/**
 * gnome_uri_list_extract_filenames:
 * @uri_list: an uri-list in the standard format
 *
 * Returns a GList containing strings allocated with g_malloc
 * that contain the filenames in the uri-list.
 *
 * Note that unlike gnome_uri_list_extract_uris() function, this
 * will discard any non-file uri from the result value.
 */
GList *gnome_uri_list_extract_filenames(const gchar *uri_list)
{
    g_return_val_if_fail(uri_list != NULL, NULL);

    GList *result = gnome_uri_list_extract_uris(uri_list);

    GList *tmp_list = result;
    while (tmp_list) {
        gchar *s = (gchar *)tmp_list->data;

        GList *node = tmp_list;
        tmp_list = tmp_list->next;

        if (!strncmp(s, "file:", 5)) {
            node->data = g_filename_from_uri(s, NULL, NULL);
            /* not sure if this fallback is useful at all */
            if (!node->data)
                node->data = g_strdup(s + 5);
        } else {
            result = g_list_remove_link(result, node);
            g_list_free_1(node);
        }
        g_free(s);
    }
    return result;
}

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8 :