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"
39 static void g_dummy_file_file_iface_init (GFileIface
*iface
);
45 int port
; /* -1 => not in uri */
53 GObject parent_instance
;
55 GDecodedUri
*decoded_uri
;
59 #define g_dummy_file_get_type _g_dummy_file_get_type
60 G_DEFINE_TYPE_WITH_CODE (GDummyFile
, g_dummy_file
, G_TYPE_OBJECT
,
61 G_IMPLEMENT_INTERFACE (G_TYPE_FILE
,
62 g_dummy_file_file_iface_init
))
64 #define SUB_DELIM_CHARS "!$&'()*+,;="
66 static char * _g_encode_uri (GDecodedUri
*decoded
);
67 static void _g_decoded_uri_free (GDecodedUri
*decoded
);
68 static GDecodedUri
*_g_decode_uri (const char *uri
);
69 static GDecodedUri
*_g_decoded_uri_new (void);
71 static char * unescape_string (const gchar
*escaped_string
,
72 const gchar
*escaped_string_end
,
73 const gchar
*illegal_characters
);
75 static void g_string_append_encoded (GString
*string
,
77 const char *reserved_chars_allowed
);
80 g_dummy_file_finalize (GObject
*object
)
84 dummy
= G_DUMMY_FILE (object
);
86 if (dummy
->decoded_uri
)
87 _g_decoded_uri_free (dummy
->decoded_uri
);
89 g_free (dummy
->text_uri
);
91 if (G_OBJECT_CLASS (g_dummy_file_parent_class
)->finalize
)
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
)
178 dirname
= g_path_get_dirname (dummy
->decoded_uri
->path
);
180 if (strcmp (dirname
, ".") == 0)
186 new_decoded_uri
= *dummy
->decoded_uri
;
187 new_decoded_uri
.path
= dirname
;
188 uri
= _g_encode_uri (&new_decoded_uri
);
191 parent
= _g_dummy_file_new (uri
);
198 g_dummy_file_dup (GFile
*file
)
200 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
202 return _g_dummy_file_new (dummy
->text_uri
);
206 g_dummy_file_hash (GFile
*file
)
208 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
210 return g_str_hash (dummy
->text_uri
);
214 g_dummy_file_equal (GFile
*file1
,
217 GDummyFile
*dummy1
= G_DUMMY_FILE (file1
);
218 GDummyFile
*dummy2
= G_DUMMY_FILE (file2
);
220 return g_str_equal (dummy1
->text_uri
, dummy2
->text_uri
);
224 safe_strcmp (const char *a
,
232 return strcmp (a
, b
);
236 uri_same_except_path (GDecodedUri
*a
,
239 if (safe_strcmp (a
->scheme
, b
->scheme
) != 0)
241 if (safe_strcmp (a
->userinfo
, b
->userinfo
) != 0)
243 if (safe_strcmp (a
->host
, b
->host
) != 0)
245 if (a
->port
!= b
->port
)
252 match_prefix (const char *path
,
257 prefix_len
= strlen (prefix
);
258 if (strncmp (path
, prefix
, prefix_len
) != 0)
260 return path
+ prefix_len
;
264 g_dummy_file_prefix_matches (GFile
*parent
, GFile
*descendant
)
266 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
267 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
268 const char *remainder
;
270 if (parent_dummy
->decoded_uri
!= NULL
&&
271 descendant_dummy
->decoded_uri
!= NULL
)
273 if (uri_same_except_path (parent_dummy
->decoded_uri
,
274 descendant_dummy
->decoded_uri
))
276 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
277 parent_dummy
->decoded_uri
->path
);
278 if (remainder
!= NULL
&& *remainder
== '/')
280 while (*remainder
== '/')
289 remainder
= match_prefix (descendant_dummy
->text_uri
,
290 parent_dummy
->text_uri
);
291 if (remainder
!= NULL
&& *remainder
== '/')
293 while (*remainder
== '/')
304 g_dummy_file_get_relative_path (GFile
*parent
,
307 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
308 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
309 const char *remainder
;
311 if (parent_dummy
->decoded_uri
!= NULL
&&
312 descendant_dummy
->decoded_uri
!= NULL
)
314 if (uri_same_except_path (parent_dummy
->decoded_uri
,
315 descendant_dummy
->decoded_uri
))
317 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
318 parent_dummy
->decoded_uri
->path
);
319 if (remainder
!= NULL
&& *remainder
== '/')
321 while (*remainder
== '/')
324 return g_strdup (remainder
);
330 remainder
= match_prefix (descendant_dummy
->text_uri
,
331 parent_dummy
->text_uri
);
332 if (remainder
!= NULL
&& *remainder
== '/')
334 while (*remainder
== '/')
337 return unescape_string (remainder
, NULL
, "/");
346 g_dummy_file_resolve_relative_path (GFile
*file
,
347 const char *relative_path
)
349 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
352 GDecodedUri new_decoded_uri
;
355 if (dummy
->decoded_uri
== NULL
)
357 str
= g_string_new (dummy
->text_uri
);
358 g_string_append (str
, "/");
359 g_string_append_encoded (str
, relative_path
, SUB_DELIM_CHARS
":@/");
360 child
= _g_dummy_file_new (str
->str
);
361 g_string_free (str
, TRUE
);
365 new_decoded_uri
= *dummy
->decoded_uri
;
367 if (g_path_is_absolute (relative_path
))
368 new_decoded_uri
.path
= g_strdup (relative_path
);
370 new_decoded_uri
.path
= g_build_filename (new_decoded_uri
.path
, relative_path
, NULL
);
372 uri
= _g_encode_uri (&new_decoded_uri
);
373 g_free (new_decoded_uri
.path
);
375 child
= _g_dummy_file_new (uri
);
383 g_dummy_file_get_child_for_display_name (GFile
*file
,
384 const char *display_name
,
387 return g_file_get_child (file
, display_name
);
391 g_dummy_file_has_uri_scheme (GFile
*file
,
392 const char *uri_scheme
)
394 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
396 if (dummy
->decoded_uri
)
397 return g_ascii_strcasecmp (uri_scheme
, dummy
->decoded_uri
->scheme
) == 0;
402 g_dummy_file_get_uri_scheme (GFile
*file
)
404 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
406 if (dummy
->decoded_uri
)
407 return g_strdup (dummy
->decoded_uri
->scheme
);
414 g_dummy_file_file_iface_init (GFileIface
*iface
)
416 iface
->dup
= g_dummy_file_dup
;
417 iface
->hash
= g_dummy_file_hash
;
418 iface
->equal
= g_dummy_file_equal
;
419 iface
->is_native
= g_dummy_file_is_native
;
420 iface
->has_uri_scheme
= g_dummy_file_has_uri_scheme
;
421 iface
->get_uri_scheme
= g_dummy_file_get_uri_scheme
;
422 iface
->get_basename
= g_dummy_file_get_basename
;
423 iface
->get_path
= g_dummy_file_get_path
;
424 iface
->get_uri
= g_dummy_file_get_uri
;
425 iface
->get_parse_name
= g_dummy_file_get_parse_name
;
426 iface
->get_parent
= g_dummy_file_get_parent
;
427 iface
->prefix_matches
= g_dummy_file_prefix_matches
;
428 iface
->get_relative_path
= g_dummy_file_get_relative_path
;
429 iface
->resolve_relative_path
= g_dummy_file_resolve_relative_path
;
430 iface
->get_child_for_display_name
= g_dummy_file_get_child_for_display_name
;
433 /* Uri handling helper functions: */
436 unescape_character (const char *scanner
)
441 first_digit
= g_ascii_xdigit_value (*scanner
++);
445 second_digit
= g_ascii_xdigit_value (*scanner
++);
446 if (second_digit
< 0)
449 return (first_digit
<< 4) | second_digit
;
453 unescape_string (const gchar
*escaped_string
,
454 const gchar
*escaped_string_end
,
455 const gchar
*illegal_characters
)
461 if (escaped_string
== NULL
)
464 if (escaped_string_end
== NULL
)
465 escaped_string_end
= escaped_string
+ strlen (escaped_string
);
467 result
= g_malloc (escaped_string_end
- escaped_string
+ 1);
470 for (in
= escaped_string
; in
< escaped_string_end
; in
++)
476 if (escaped_string_end
- in
< 2)
482 character
= unescape_character (in
);
484 /* Check for an illegal character. We consider '\0' illegal here. */
485 if (character
<= 0 ||
486 (illegal_characters
!= NULL
&&
487 strchr (illegal_characters
, (char)character
) != NULL
))
492 in
++; /* The other char will be eaten in the loop header */
494 *out
++ = (char)character
;
498 g_warn_if_fail (out
- result
<= strlen (escaped_string
));
503 _g_decoded_uri_free (GDecodedUri
*decoded
)
508 g_free (decoded
->scheme
);
509 g_free (decoded
->query
);
510 g_free (decoded
->fragment
);
511 g_free (decoded
->userinfo
);
512 g_free (decoded
->host
);
513 g_free (decoded
->path
);
518 _g_decoded_uri_new (void)
522 uri
= g_new0 (GDecodedUri
, 1);
529 _g_decode_uri (const char *uri
)
531 GDecodedUri
*decoded
;
532 const char *p
, *in
, *hier_part_start
, *hier_part_end
, *query_start
, *fragment_start
;
536 /* From RFC 3986 Decodes:
537 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
543 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
546 if (!g_ascii_isalpha (*p
))
556 if (!(g_ascii_isalnum(c
) ||
563 decoded
= _g_decoded_uri_new ();
565 decoded
->scheme
= g_malloc (p
- uri
);
566 out
= decoded
->scheme
;
567 for (in
= uri
; in
< p
- 1; in
++)
568 *out
++ = g_ascii_tolower (*in
);
573 query_start
= strchr (p
, '?');
576 hier_part_end
= query_start
++;
577 fragment_start
= strchr (query_start
, '#');
580 decoded
->query
= g_strndup (query_start
, fragment_start
- query_start
);
581 decoded
->fragment
= g_strdup (fragment_start
+1);
585 decoded
->query
= g_strdup (query_start
);
586 decoded
->fragment
= NULL
;
592 decoded
->query
= NULL
;
593 fragment_start
= strchr (p
, '#');
596 hier_part_end
= fragment_start
++;
597 decoded
->fragment
= g_strdup (fragment_start
);
601 hier_part_end
= p
+ strlen (p
);
602 decoded
->fragment
= NULL
;
607 hier-part = "//" authority path-abempty
614 if (hier_part_start
[0] == '/' &&
615 hier_part_start
[1] == '/')
617 const char *authority_start
, *authority_end
;
618 const char *userinfo_start
, *userinfo_end
;
619 const char *host_start
, *host_end
;
620 const char *port_start
;
622 authority_start
= hier_part_start
+ 2;
623 /* authority is always followed by / or nothing */
624 authority_end
= memchr (authority_start
, '/', hier_part_end
- authority_start
);
625 if (authority_end
== NULL
)
626 authority_end
= hier_part_end
;
629 authority = [ userinfo "@" ] host [ ":" port ]
632 userinfo_end
= memchr (authority_start
, '@', authority_end
- authority_start
);
635 userinfo_start
= authority_start
;
636 decoded
->userinfo
= unescape_string (userinfo_start
, userinfo_end
, NULL
);
637 if (decoded
->userinfo
== NULL
)
639 _g_decoded_uri_free (decoded
);
642 host_start
= userinfo_end
+ 1;
645 host_start
= authority_start
;
647 port_start
= memchr (host_start
, ':', authority_end
- host_start
);
650 host_end
= port_start
++;
652 decoded
->port
= atoi(port_start
);
656 host_end
= authority_end
;
660 decoded
->host
= g_strndup (host_start
, host_end
- host_start
);
662 hier_part_start
= authority_end
;
665 decoded
->path
= unescape_string (hier_part_start
, hier_part_end
, "/");
667 if (decoded
->path
== NULL
)
669 _g_decoded_uri_free (decoded
);
677 is_valid (char c
, const char *reserved_chars_allowed
)
679 if (g_ascii_isalnum (c
) ||
686 if (reserved_chars_allowed
&&
687 strchr (reserved_chars_allowed
, c
) != NULL
)
694 g_string_append_encoded (GString
*string
,
696 const char *reserved_chars_allowed
)
700 static const gchar hex
[16] = "0123456789ABCDEF";
702 end
= encoded
+ strlen (encoded
);
704 while ((c
= *encoded
) != 0)
706 if (is_valid (c
, reserved_chars_allowed
))
708 g_string_append_c (string
, c
);
713 g_string_append_c (string
, '%');
714 g_string_append_c (string
, hex
[((guchar
)c
) >> 4]);
715 g_string_append_c (string
, hex
[((guchar
)c
) & 0xf]);
722 _g_encode_uri (GDecodedUri
*decoded
)
726 uri
= g_string_new (NULL
);
728 g_string_append (uri
, decoded
->scheme
);
729 g_string_append (uri
, "://");
731 if (decoded
->host
!= NULL
)
733 if (decoded
->userinfo
)
735 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
736 g_string_append_encoded (uri
, decoded
->userinfo
, SUB_DELIM_CHARS
":");
737 g_string_append_c (uri
, '@');
740 g_string_append (uri
, decoded
->host
);
742 if (decoded
->port
!= -1)
744 g_string_append_c (uri
, ':');
745 g_string_append_printf (uri
, "%d", decoded
->port
);
749 g_string_append_encoded (uri
, decoded
->path
, SUB_DELIM_CHARS
":@/");
753 g_string_append_c (uri
, '?');
754 g_string_append (uri
, decoded
->query
);
757 if (decoded
->fragment
)
759 g_string_append_c (uri
, '#');
760 g_string_append (uri
, decoded
->fragment
);
763 return g_string_free (uri
, FALSE
);