1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
35 #include "gdummyfile.h"
40 static void g_dummy_file_file_iface_init (GFileIface
*iface
);
46 int port
; /* -1 => not in uri */
54 GObject parent_instance
;
56 GDecodedUri
*decoded_uri
;
60 #define g_dummy_file_get_type _g_dummy_file_get_type
61 G_DEFINE_TYPE_WITH_CODE (GDummyFile
, g_dummy_file
, G_TYPE_OBJECT
,
62 G_IMPLEMENT_INTERFACE (G_TYPE_FILE
,
63 g_dummy_file_file_iface_init
))
65 #define SUB_DELIM_CHARS "!$&'()*+,;="
67 static char * _g_encode_uri (GDecodedUri
*decoded
);
68 static void _g_decoded_uri_free (GDecodedUri
*decoded
);
69 static GDecodedUri
*_g_decode_uri (const char *uri
);
70 static GDecodedUri
*_g_decoded_uri_new (void);
72 static char * unescape_string (const gchar
*escaped_string
,
73 const gchar
*escaped_string_end
,
74 const gchar
*illegal_characters
);
76 static void g_string_append_encoded (GString
*string
,
78 const char *reserved_chars_allowed
);
81 g_dummy_file_finalize (GObject
*object
)
85 dummy
= G_DUMMY_FILE (object
);
87 if (dummy
->decoded_uri
)
88 _g_decoded_uri_free (dummy
->decoded_uri
);
90 g_free (dummy
->text_uri
);
92 G_OBJECT_CLASS (g_dummy_file_parent_class
)->finalize (object
);
96 g_dummy_file_class_init (GDummyFileClass
*klass
)
98 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
100 gobject_class
->finalize
= g_dummy_file_finalize
;
104 g_dummy_file_init (GDummyFile
*dummy
)
110 * @uri: Universal Resource Identifier for the dummy file object.
112 * Returns: a new #GFile.
115 _g_dummy_file_new (const char *uri
)
119 g_return_val_if_fail (uri
!= NULL
, NULL
);
121 dummy
= g_object_new (G_TYPE_DUMMY_FILE
, NULL
);
122 dummy
->text_uri
= g_strdup (uri
);
123 dummy
->decoded_uri
= _g_decode_uri (uri
);
125 return G_FILE (dummy
);
129 g_dummy_file_is_native (GFile
*file
)
135 g_dummy_file_get_basename (GFile
*file
)
137 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
139 if (dummy
->decoded_uri
)
140 return g_path_get_basename (dummy
->decoded_uri
->path
);
141 return g_strdup (dummy
->text_uri
);
145 g_dummy_file_get_path (GFile
*file
)
147 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
149 if (dummy
->decoded_uri
)
150 return g_strdup (dummy
->decoded_uri
->path
);
155 g_dummy_file_get_uri (GFile
*file
)
157 return g_strdup (G_DUMMY_FILE (file
)->text_uri
);
161 g_dummy_file_get_parse_name (GFile
*file
)
163 return g_strdup (G_DUMMY_FILE (file
)->text_uri
);
167 g_dummy_file_get_parent (GFile
*file
)
169 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
173 GDecodedUri new_decoded_uri
;
175 if (dummy
->decoded_uri
== NULL
||
176 g_strcmp0 (dummy
->decoded_uri
->path
, "/") == 0)
179 dirname
= g_path_get_dirname (dummy
->decoded_uri
->path
);
181 if (strcmp (dirname
, ".") == 0)
187 new_decoded_uri
= *dummy
->decoded_uri
;
188 new_decoded_uri
.path
= dirname
;
189 uri
= _g_encode_uri (&new_decoded_uri
);
192 parent
= _g_dummy_file_new (uri
);
199 g_dummy_file_dup (GFile
*file
)
201 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
203 return _g_dummy_file_new (dummy
->text_uri
);
207 g_dummy_file_hash (GFile
*file
)
209 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
211 return g_str_hash (dummy
->text_uri
);
215 g_dummy_file_equal (GFile
*file1
,
218 GDummyFile
*dummy1
= G_DUMMY_FILE (file1
);
219 GDummyFile
*dummy2
= G_DUMMY_FILE (file2
);
221 return g_str_equal (dummy1
->text_uri
, dummy2
->text_uri
);
225 safe_strcmp (const char *a
,
233 return strcmp (a
, b
);
237 uri_same_except_path (GDecodedUri
*a
,
240 if (safe_strcmp (a
->scheme
, b
->scheme
) != 0)
242 if (safe_strcmp (a
->userinfo
, b
->userinfo
) != 0)
244 if (safe_strcmp (a
->host
, b
->host
) != 0)
246 if (a
->port
!= b
->port
)
253 match_prefix (const char *path
,
258 prefix_len
= strlen (prefix
);
259 if (strncmp (path
, prefix
, prefix_len
) != 0)
261 return path
+ prefix_len
;
265 g_dummy_file_prefix_matches (GFile
*parent
, GFile
*descendant
)
267 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
268 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
269 const char *remainder
;
271 if (parent_dummy
->decoded_uri
!= NULL
&&
272 descendant_dummy
->decoded_uri
!= NULL
)
274 if (uri_same_except_path (parent_dummy
->decoded_uri
,
275 descendant_dummy
->decoded_uri
))
277 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
278 parent_dummy
->decoded_uri
->path
);
279 if (remainder
!= NULL
&& *remainder
== '/')
281 while (*remainder
== '/')
290 remainder
= match_prefix (descendant_dummy
->text_uri
,
291 parent_dummy
->text_uri
);
292 if (remainder
!= NULL
&& *remainder
== '/')
294 while (*remainder
== '/')
305 g_dummy_file_get_relative_path (GFile
*parent
,
308 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
309 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
310 const char *remainder
;
312 if (parent_dummy
->decoded_uri
!= NULL
&&
313 descendant_dummy
->decoded_uri
!= NULL
)
315 if (uri_same_except_path (parent_dummy
->decoded_uri
,
316 descendant_dummy
->decoded_uri
))
318 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
319 parent_dummy
->decoded_uri
->path
);
320 if (remainder
!= NULL
&& *remainder
== '/')
322 while (*remainder
== '/')
325 return g_strdup (remainder
);
331 remainder
= match_prefix (descendant_dummy
->text_uri
,
332 parent_dummy
->text_uri
);
333 if (remainder
!= NULL
&& *remainder
== '/')
335 while (*remainder
== '/')
338 return unescape_string (remainder
, NULL
, "/");
347 g_dummy_file_resolve_relative_path (GFile
*file
,
348 const char *relative_path
)
350 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
353 GDecodedUri new_decoded_uri
;
356 if (dummy
->decoded_uri
== NULL
)
358 str
= g_string_new (dummy
->text_uri
);
359 g_string_append (str
, "/");
360 g_string_append_encoded (str
, relative_path
, SUB_DELIM_CHARS
":@/");
361 child
= _g_dummy_file_new (str
->str
);
362 g_string_free (str
, TRUE
);
366 new_decoded_uri
= *dummy
->decoded_uri
;
368 if (g_path_is_absolute (relative_path
))
369 new_decoded_uri
.path
= g_strdup (relative_path
);
371 new_decoded_uri
.path
= g_build_filename (new_decoded_uri
.path
, relative_path
, NULL
);
373 uri
= _g_encode_uri (&new_decoded_uri
);
374 g_free (new_decoded_uri
.path
);
376 child
= _g_dummy_file_new (uri
);
384 g_dummy_file_get_child_for_display_name (GFile
*file
,
385 const char *display_name
,
388 return g_file_get_child (file
, display_name
);
392 g_dummy_file_has_uri_scheme (GFile
*file
,
393 const char *uri_scheme
)
395 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
397 if (dummy
->decoded_uri
)
398 return g_ascii_strcasecmp (uri_scheme
, dummy
->decoded_uri
->scheme
) == 0;
403 g_dummy_file_get_uri_scheme (GFile
*file
)
405 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
407 if (dummy
->decoded_uri
)
408 return g_strdup (dummy
->decoded_uri
->scheme
);
415 g_dummy_file_file_iface_init (GFileIface
*iface
)
417 iface
->dup
= g_dummy_file_dup
;
418 iface
->hash
= g_dummy_file_hash
;
419 iface
->equal
= g_dummy_file_equal
;
420 iface
->is_native
= g_dummy_file_is_native
;
421 iface
->has_uri_scheme
= g_dummy_file_has_uri_scheme
;
422 iface
->get_uri_scheme
= g_dummy_file_get_uri_scheme
;
423 iface
->get_basename
= g_dummy_file_get_basename
;
424 iface
->get_path
= g_dummy_file_get_path
;
425 iface
->get_uri
= g_dummy_file_get_uri
;
426 iface
->get_parse_name
= g_dummy_file_get_parse_name
;
427 iface
->get_parent
= g_dummy_file_get_parent
;
428 iface
->prefix_matches
= g_dummy_file_prefix_matches
;
429 iface
->get_relative_path
= g_dummy_file_get_relative_path
;
430 iface
->resolve_relative_path
= g_dummy_file_resolve_relative_path
;
431 iface
->get_child_for_display_name
= g_dummy_file_get_child_for_display_name
;
434 /* Uri handling helper functions: */
437 unescape_character (const char *scanner
)
442 first_digit
= g_ascii_xdigit_value (*scanner
++);
446 second_digit
= g_ascii_xdigit_value (*scanner
++);
447 if (second_digit
< 0)
450 return (first_digit
<< 4) | second_digit
;
454 unescape_string (const gchar
*escaped_string
,
455 const gchar
*escaped_string_end
,
456 const gchar
*illegal_characters
)
462 if (escaped_string
== NULL
)
465 if (escaped_string_end
== NULL
)
466 escaped_string_end
= escaped_string
+ strlen (escaped_string
);
468 result
= g_malloc (escaped_string_end
- escaped_string
+ 1);
471 for (in
= escaped_string
; in
< escaped_string_end
; in
++)
477 if (escaped_string_end
- in
< 2)
483 character
= unescape_character (in
);
485 /* Check for an illegal character. We consider '\0' illegal here. */
486 if (character
<= 0 ||
487 (illegal_characters
!= NULL
&&
488 strchr (illegal_characters
, (char)character
) != NULL
))
493 in
++; /* The other char will be eaten in the loop header */
495 *out
++ = (char)character
;
499 g_warn_if_fail (out
- result
<= strlen (escaped_string
));
504 _g_decoded_uri_free (GDecodedUri
*decoded
)
509 g_free (decoded
->scheme
);
510 g_free (decoded
->query
);
511 g_free (decoded
->fragment
);
512 g_free (decoded
->userinfo
);
513 g_free (decoded
->host
);
514 g_free (decoded
->path
);
519 _g_decoded_uri_new (void)
523 uri
= g_new0 (GDecodedUri
, 1);
530 _g_decode_uri (const char *uri
)
532 GDecodedUri
*decoded
;
533 const char *p
, *in
, *hier_part_start
, *hier_part_end
, *query_start
, *fragment_start
;
537 /* From RFC 3986 Decodes:
538 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
544 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
547 if (!g_ascii_isalpha (*p
))
557 if (!(g_ascii_isalnum(c
) ||
564 decoded
= _g_decoded_uri_new ();
566 decoded
->scheme
= g_malloc (p
- uri
);
567 out
= decoded
->scheme
;
568 for (in
= uri
; in
< p
- 1; in
++)
569 *out
++ = g_ascii_tolower (*in
);
574 query_start
= strchr (p
, '?');
577 hier_part_end
= query_start
++;
578 fragment_start
= strchr (query_start
, '#');
581 decoded
->query
= g_strndup (query_start
, fragment_start
- query_start
);
582 decoded
->fragment
= g_strdup (fragment_start
+1);
586 decoded
->query
= g_strdup (query_start
);
587 decoded
->fragment
= NULL
;
593 decoded
->query
= NULL
;
594 fragment_start
= strchr (p
, '#');
597 hier_part_end
= fragment_start
++;
598 decoded
->fragment
= g_strdup (fragment_start
);
602 hier_part_end
= p
+ strlen (p
);
603 decoded
->fragment
= NULL
;
608 hier-part = "//" authority path-abempty
615 if (hier_part_start
[0] == '/' &&
616 hier_part_start
[1] == '/')
618 const char *authority_start
, *authority_end
;
619 const char *userinfo_start
, *userinfo_end
;
620 const char *host_start
, *host_end
;
621 const char *port_start
;
623 authority_start
= hier_part_start
+ 2;
624 /* authority is always followed by / or nothing */
625 authority_end
= memchr (authority_start
, '/', hier_part_end
- authority_start
);
626 if (authority_end
== NULL
)
627 authority_end
= hier_part_end
;
630 authority = [ userinfo "@" ] host [ ":" port ]
633 userinfo_end
= memchr (authority_start
, '@', authority_end
- authority_start
);
636 userinfo_start
= authority_start
;
637 decoded
->userinfo
= unescape_string (userinfo_start
, userinfo_end
, NULL
);
638 if (decoded
->userinfo
== NULL
)
640 _g_decoded_uri_free (decoded
);
643 host_start
= userinfo_end
+ 1;
646 host_start
= authority_start
;
648 port_start
= memchr (host_start
, ':', authority_end
- host_start
);
651 host_end
= port_start
++;
653 decoded
->port
= atoi(port_start
);
657 host_end
= authority_end
;
661 decoded
->host
= g_strndup (host_start
, host_end
- host_start
);
663 hier_part_start
= authority_end
;
666 decoded
->path
= unescape_string (hier_part_start
, hier_part_end
, "/");
668 if (decoded
->path
== NULL
)
670 _g_decoded_uri_free (decoded
);
678 is_valid (char c
, const char *reserved_chars_allowed
)
680 if (g_ascii_isalnum (c
) ||
687 if (reserved_chars_allowed
&&
688 strchr (reserved_chars_allowed
, c
) != NULL
)
695 g_string_append_encoded (GString
*string
,
697 const char *reserved_chars_allowed
)
701 static const gchar hex
[16] = "0123456789ABCDEF";
703 end
= encoded
+ strlen (encoded
);
705 while ((c
= *encoded
) != 0)
707 if (is_valid (c
, reserved_chars_allowed
))
709 g_string_append_c (string
, c
);
714 g_string_append_c (string
, '%');
715 g_string_append_c (string
, hex
[((guchar
)c
) >> 4]);
716 g_string_append_c (string
, hex
[((guchar
)c
) & 0xf]);
723 _g_encode_uri (GDecodedUri
*decoded
)
727 uri
= g_string_new (NULL
);
729 g_string_append (uri
, decoded
->scheme
);
730 g_string_append (uri
, "://");
732 if (decoded
->host
!= NULL
)
734 if (decoded
->userinfo
)
736 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
737 g_string_append_encoded (uri
, decoded
->userinfo
, SUB_DELIM_CHARS
":");
738 g_string_append_c (uri
, '@');
741 g_string_append (uri
, decoded
->host
);
743 if (decoded
->port
!= -1)
745 g_string_append_c (uri
, ':');
746 g_string_append_printf (uri
, "%d", decoded
->port
);
750 g_string_append_encoded (uri
, decoded
->path
, SUB_DELIM_CHARS
":@/");
754 g_string_append_c (uri
, '?');
755 g_string_append (uri
, decoded
->query
);
758 if (decoded
->fragment
)
760 g_string_append_c (uri
, '#');
761 g_string_append (uri
, decoded
->fragment
);
764 return g_string_free (uri
, FALSE
);