Code

Filter effects dialog:
[inkscape.git] / src / extract-uri.cpp
1 #include <string.h>
2 #include <glib.h>
4 // FIXME: kill this ugliness when we have a proper CSS parser
6 // Functions as per 4.3.4 of CSS 2.1
7 // http://www.w3.org/TR/CSS21/syndata.html#uri
8 gchar *extract_uri(gchar const *s)
9 {
10     if (!s)
11         return NULL;
13     gchar* result = NULL;
14     gchar const *sb = s;
15     if ( strlen(sb) < 4 || strncmp(sb, "url", 3) != 0 ) {
16         return NULL;
17     }
19     sb += 3;
21     // This first whitespace technically is not allowed.
22     // Just left in for now for legacy behavior.
23     while ( ( *sb == ' ' ) ||
24             ( *sb == '\t' ) )
25     {
26         sb++;
27     }
29     if ( *sb == '(' ) {
30         sb++;
31         while ( ( *sb == ' ' ) ||
32                 ( *sb == '\t' ) )
33         {
34             sb++;
35         }
37         gchar delim = ')';
38         if ( (*sb == '\'' || *sb == '"') ) {
39             delim = *sb;
40             sb++;
41         }
42         gchar const* se = sb + 1;
43         while ( *se && (*se != delim) ) {
44             se++;
45         }
47         // we found the delimiter
48         if ( *se ) {
49             if ( delim == ')' ) {
50                 // back up for any trailing whitespace
51                 se--;
52                 while ( ( se[-1] == ' ' ) ||
53                         ( se[-1] == '\t' ) )
54                 {
55                     se--;
56                 }
57                 result = g_strndup(sb, se - sb + 1);
58             } else {
59                 gchar const* tail = se + 1;
60                 while ( ( *tail == ' ' ) ||
61                         ( *tail == '\t' ) )
62                 {
63                     tail++;
64                 }
65                 if ( *tail == ')' ) {
66                     result = g_strndup(sb, se - sb);
67                 }
68             }
69         }
70     }
72     return result;
73 }
75 /*
76   Local Variables:
77   mode:c++
78   c-file-style:"stroustrup"
79   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
80   indent-tabs-mode:nil
81   fill-column:99
82   End:
83 */
84 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :