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
)
151 g_dummy_file_get_uri (GFile
*file
)
153 return g_strdup (G_DUMMY_FILE (file
)->text_uri
);
157 g_dummy_file_get_parse_name (GFile
*file
)
159 return g_strdup (G_DUMMY_FILE (file
)->text_uri
);
163 g_dummy_file_get_parent (GFile
*file
)
165 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
169 GDecodedUri new_decoded_uri
;
171 if (dummy
->decoded_uri
== NULL
||
172 g_strcmp0 (dummy
->decoded_uri
->path
, "/") == 0)
175 dirname
= g_path_get_dirname (dummy
->decoded_uri
->path
);
177 if (strcmp (dirname
, ".") == 0)
183 new_decoded_uri
= *dummy
->decoded_uri
;
184 new_decoded_uri
.path
= dirname
;
185 uri
= _g_encode_uri (&new_decoded_uri
);
188 parent
= _g_dummy_file_new (uri
);
195 g_dummy_file_dup (GFile
*file
)
197 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
199 return _g_dummy_file_new (dummy
->text_uri
);
203 g_dummy_file_hash (GFile
*file
)
205 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
207 return g_str_hash (dummy
->text_uri
);
211 g_dummy_file_equal (GFile
*file1
,
214 GDummyFile
*dummy1
= G_DUMMY_FILE (file1
);
215 GDummyFile
*dummy2
= G_DUMMY_FILE (file2
);
217 return g_str_equal (dummy1
->text_uri
, dummy2
->text_uri
);
221 safe_strcmp (const char *a
,
229 return strcmp (a
, b
);
233 uri_same_except_path (GDecodedUri
*a
,
236 if (safe_strcmp (a
->scheme
, b
->scheme
) != 0)
238 if (safe_strcmp (a
->userinfo
, b
->userinfo
) != 0)
240 if (safe_strcmp (a
->host
, b
->host
) != 0)
242 if (a
->port
!= b
->port
)
249 match_prefix (const char *path
,
254 prefix_len
= strlen (prefix
);
255 if (strncmp (path
, prefix
, prefix_len
) != 0)
257 return path
+ prefix_len
;
261 g_dummy_file_prefix_matches (GFile
*parent
, GFile
*descendant
)
263 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
264 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
265 const char *remainder
;
267 if (parent_dummy
->decoded_uri
!= NULL
&&
268 descendant_dummy
->decoded_uri
!= NULL
)
270 if (uri_same_except_path (parent_dummy
->decoded_uri
,
271 descendant_dummy
->decoded_uri
))
273 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
274 parent_dummy
->decoded_uri
->path
);
275 if (remainder
!= NULL
&& *remainder
== '/')
277 while (*remainder
== '/')
286 remainder
= match_prefix (descendant_dummy
->text_uri
,
287 parent_dummy
->text_uri
);
288 if (remainder
!= NULL
&& *remainder
== '/')
290 while (*remainder
== '/')
301 g_dummy_file_get_relative_path (GFile
*parent
,
304 GDummyFile
*parent_dummy
= G_DUMMY_FILE (parent
);
305 GDummyFile
*descendant_dummy
= G_DUMMY_FILE (descendant
);
306 const char *remainder
;
308 if (parent_dummy
->decoded_uri
!= NULL
&&
309 descendant_dummy
->decoded_uri
!= NULL
)
311 if (uri_same_except_path (parent_dummy
->decoded_uri
,
312 descendant_dummy
->decoded_uri
))
314 remainder
= match_prefix (descendant_dummy
->decoded_uri
->path
,
315 parent_dummy
->decoded_uri
->path
);
316 if (remainder
!= NULL
&& *remainder
== '/')
318 while (*remainder
== '/')
321 return g_strdup (remainder
);
327 remainder
= match_prefix (descendant_dummy
->text_uri
,
328 parent_dummy
->text_uri
);
329 if (remainder
!= NULL
&& *remainder
== '/')
331 while (*remainder
== '/')
334 return unescape_string (remainder
, NULL
, "/");
343 g_dummy_file_resolve_relative_path (GFile
*file
,
344 const char *relative_path
)
346 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
349 GDecodedUri new_decoded_uri
;
352 if (dummy
->decoded_uri
== NULL
)
354 str
= g_string_new (dummy
->text_uri
);
355 g_string_append (str
, "/");
356 g_string_append_encoded (str
, relative_path
, SUB_DELIM_CHARS
":@/");
357 child
= _g_dummy_file_new (str
->str
);
358 g_string_free (str
, TRUE
);
362 new_decoded_uri
= *dummy
->decoded_uri
;
364 if (g_path_is_absolute (relative_path
))
365 new_decoded_uri
.path
= g_strdup (relative_path
);
367 new_decoded_uri
.path
= g_build_filename (new_decoded_uri
.path
, relative_path
, NULL
);
369 uri
= _g_encode_uri (&new_decoded_uri
);
370 g_free (new_decoded_uri
.path
);
372 child
= _g_dummy_file_new (uri
);
380 g_dummy_file_get_child_for_display_name (GFile
*file
,
381 const char *display_name
,
384 return g_file_get_child (file
, display_name
);
388 g_dummy_file_has_uri_scheme (GFile
*file
,
389 const char *uri_scheme
)
391 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
393 if (dummy
->decoded_uri
)
394 return g_ascii_strcasecmp (uri_scheme
, dummy
->decoded_uri
->scheme
) == 0;
399 g_dummy_file_get_uri_scheme (GFile
*file
)
401 GDummyFile
*dummy
= G_DUMMY_FILE (file
);
403 if (dummy
->decoded_uri
)
404 return g_strdup (dummy
->decoded_uri
->scheme
);
411 g_dummy_file_file_iface_init (GFileIface
*iface
)
413 iface
->dup
= g_dummy_file_dup
;
414 iface
->hash
= g_dummy_file_hash
;
415 iface
->equal
= g_dummy_file_equal
;
416 iface
->is_native
= g_dummy_file_is_native
;
417 iface
->has_uri_scheme
= g_dummy_file_has_uri_scheme
;
418 iface
->get_uri_scheme
= g_dummy_file_get_uri_scheme
;
419 iface
->get_basename
= g_dummy_file_get_basename
;
420 iface
->get_path
= g_dummy_file_get_path
;
421 iface
->get_uri
= g_dummy_file_get_uri
;
422 iface
->get_parse_name
= g_dummy_file_get_parse_name
;
423 iface
->get_parent
= g_dummy_file_get_parent
;
424 iface
->prefix_matches
= g_dummy_file_prefix_matches
;
425 iface
->get_relative_path
= g_dummy_file_get_relative_path
;
426 iface
->resolve_relative_path
= g_dummy_file_resolve_relative_path
;
427 iface
->get_child_for_display_name
= g_dummy_file_get_child_for_display_name
;
429 iface
->supports_thread_contexts
= TRUE
;
432 /* Uri handling helper functions: */
435 unescape_character (const char *scanner
)
440 first_digit
= g_ascii_xdigit_value (*scanner
++);
444 second_digit
= g_ascii_xdigit_value (*scanner
++);
445 if (second_digit
< 0)
448 return (first_digit
<< 4) | second_digit
;
452 unescape_string (const gchar
*escaped_string
,
453 const gchar
*escaped_string_end
,
454 const gchar
*illegal_characters
)
460 if (escaped_string
== NULL
)
463 if (escaped_string_end
== NULL
)
464 escaped_string_end
= escaped_string
+ strlen (escaped_string
);
466 result
= g_malloc (escaped_string_end
- escaped_string
+ 1);
469 for (in
= escaped_string
; in
< escaped_string_end
; in
++)
475 if (escaped_string_end
- in
< 2)
481 character
= unescape_character (in
);
483 /* Check for an illegal character. We consider '\0' illegal here. */
484 if (character
<= 0 ||
485 (illegal_characters
!= NULL
&&
486 strchr (illegal_characters
, (char)character
) != NULL
))
491 in
++; /* The other char will be eaten in the loop header */
493 *out
++ = (char)character
;
497 g_warn_if_fail (out
- result
<= strlen (escaped_string
));
502 _g_decoded_uri_free (GDecodedUri
*decoded
)
507 g_free (decoded
->scheme
);
508 g_free (decoded
->query
);
509 g_free (decoded
->fragment
);
510 g_free (decoded
->userinfo
);
511 g_free (decoded
->host
);
512 g_free (decoded
->path
);
517 _g_decoded_uri_new (void)
521 uri
= g_new0 (GDecodedUri
, 1);
528 _g_decode_uri (const char *uri
)
530 GDecodedUri
*decoded
;
531 const char *p
, *in
, *hier_part_start
, *hier_part_end
, *query_start
, *fragment_start
;
535 /* From RFC 3986 Decodes:
536 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
542 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
545 if (!g_ascii_isalpha (*p
))
555 if (!(g_ascii_isalnum(c
) ||
562 decoded
= _g_decoded_uri_new ();
564 decoded
->scheme
= g_malloc (p
- uri
);
565 out
= decoded
->scheme
;
566 for (in
= uri
; in
< p
- 1; in
++)
567 *out
++ = g_ascii_tolower (*in
);
572 query_start
= strchr (p
, '?');
575 hier_part_end
= query_start
++;
576 fragment_start
= strchr (query_start
, '#');
579 decoded
->query
= g_strndup (query_start
, fragment_start
- query_start
);
580 decoded
->fragment
= g_strdup (fragment_start
+1);
584 decoded
->query
= g_strdup (query_start
);
585 decoded
->fragment
= NULL
;
591 decoded
->query
= NULL
;
592 fragment_start
= strchr (p
, '#');
595 hier_part_end
= fragment_start
++;
596 decoded
->fragment
= g_strdup (fragment_start
);
600 hier_part_end
= p
+ strlen (p
);
601 decoded
->fragment
= NULL
;
606 hier-part = "//" authority path-abempty
613 if (hier_part_start
[0] == '/' &&
614 hier_part_start
[1] == '/')
616 const char *authority_start
, *authority_end
;
617 const char *userinfo_start
, *userinfo_end
;
618 const char *host_start
, *host_end
;
619 const char *port_start
;
621 authority_start
= hier_part_start
+ 2;
622 /* authority is always followed by / or nothing */
623 authority_end
= memchr (authority_start
, '/', hier_part_end
- authority_start
);
624 if (authority_end
== NULL
)
625 authority_end
= hier_part_end
;
628 authority = [ userinfo "@" ] host [ ":" port ]
631 userinfo_end
= memchr (authority_start
, '@', authority_end
- authority_start
);
634 userinfo_start
= authority_start
;
635 decoded
->userinfo
= unescape_string (userinfo_start
, userinfo_end
, NULL
);
636 if (decoded
->userinfo
== NULL
)
638 _g_decoded_uri_free (decoded
);
641 host_start
= userinfo_end
+ 1;
644 host_start
= authority_start
;
646 port_start
= memchr (host_start
, ':', authority_end
- host_start
);
649 host_end
= port_start
++;
651 decoded
->port
= atoi(port_start
);
655 host_end
= authority_end
;
659 decoded
->host
= g_strndup (host_start
, host_end
- host_start
);
661 hier_part_start
= authority_end
;
664 decoded
->path
= unescape_string (hier_part_start
, hier_part_end
, "/");
666 if (decoded
->path
== NULL
)
668 _g_decoded_uri_free (decoded
);
676 is_valid (char c
, const char *reserved_chars_allowed
)
678 if (g_ascii_isalnum (c
) ||
685 if (reserved_chars_allowed
&&
686 strchr (reserved_chars_allowed
, c
) != NULL
)
693 g_string_append_encoded (GString
*string
,
695 const char *reserved_chars_allowed
)
699 static const gchar hex
[16] = "0123456789ABCDEF";
701 end
= encoded
+ strlen (encoded
);
703 while ((c
= *encoded
) != 0)
705 if (is_valid (c
, reserved_chars_allowed
))
707 g_string_append_c (string
, c
);
712 g_string_append_c (string
, '%');
713 g_string_append_c (string
, hex
[((guchar
)c
) >> 4]);
714 g_string_append_c (string
, hex
[((guchar
)c
) & 0xf]);
721 _g_encode_uri (GDecodedUri
*decoded
)
725 uri
= g_string_new (NULL
);
727 g_string_append (uri
, decoded
->scheme
);
728 g_string_append (uri
, "://");
730 if (decoded
->host
!= NULL
)
732 if (decoded
->userinfo
)
734 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
735 g_string_append_encoded (uri
, decoded
->userinfo
, SUB_DELIM_CHARS
":");
736 g_string_append_c (uri
, '@');
739 g_string_append (uri
, decoded
->host
);
741 if (decoded
->port
!= -1)
743 g_string_append_c (uri
, ':');
744 g_string_append_printf (uri
, "%d", decoded
->port
);
748 g_string_append_encoded (uri
, decoded
->path
, SUB_DELIM_CHARS
":@/");
752 g_string_append_c (uri
, '?');
753 g_string_append (uri
, decoded
->query
);
756 if (decoded
->fragment
)
758 g_string_append_c (uri
, '#');
759 g_string_append (uri
, decoded
->fragment
);
762 return g_string_free (uri
, FALSE
);