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>
33 #include "gdummyfile.h"
37 static void g_dummy_file_file_iface_init (GFileIface *iface);
43 int port; /* -1 => not in uri */
51 GObject parent_instance;
53 GDecodedUri *decoded_uri;
57 G_DEFINE_TYPE_WITH_CODE (GDummyFile, g_dummy_file, G_TYPE_OBJECT,
58 G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
59 g_dummy_file_file_iface_init))
61 #define SUB_DELIM_CHARS "!$&'()*+,;="
63 static char * _g_encode_uri (GDecodedUri *decoded);
64 static void _g_decoded_uri_free (GDecodedUri *decoded);
65 static GDecodedUri *_g_decode_uri (const char *uri);
66 static GDecodedUri *_g_decoded_uri_new (void);
68 static char * unescape_string (const gchar *escaped_string,
69 const gchar *escaped_string_end,
70 const gchar *illegal_characters);
72 static void g_string_append_encoded (GString *string, const char *encoded,
73 const char *reserved_chars_allowed);
76 g_dummy_file_finalize (GObject *object)
80 dummy = G_DUMMY_FILE (object);
82 if (dummy->decoded_uri)
83 _g_decoded_uri_free (dummy->decoded_uri);
85 g_free (dummy->text_uri);
87 if (G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize)
88 (*G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize) (object);
92 g_dummy_file_class_init (GDummyFileClass *klass)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = g_dummy_file_finalize;
100 g_dummy_file_init (GDummyFile *dummy)
106 * @uri: Universal Resource Identifier for the dummy file object.
108 * Returns: a new #GFile.
111 g_dummy_file_new (const char *uri)
115 g_return_val_if_fail (uri != NULL, NULL);
117 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
118 dummy->text_uri = g_strdup (uri);
119 dummy->decoded_uri = _g_decode_uri (uri);
121 return G_FILE (dummy);
125 g_dummy_file_is_native (GFile *file)
131 g_dummy_file_get_basename (GFile *file)
133 GDummyFile *dummy = G_DUMMY_FILE (file);
135 if (dummy->decoded_uri)
136 return g_path_get_basename (dummy->decoded_uri->path);
137 return g_strdup (dummy->text_uri);
141 g_dummy_file_get_path (GFile *file)
143 GDummyFile *dummy = G_DUMMY_FILE (file);
145 if (dummy->decoded_uri)
146 return g_strdup (dummy->decoded_uri->path);
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)
174 dirname = g_path_get_dirname (dummy->decoded_uri->path);
176 if (strcmp (dirname, ".") == 0)
182 new_decoded_uri = *dummy->decoded_uri;
183 new_decoded_uri.path = dirname;
184 uri = _g_encode_uri (&new_decoded_uri);
187 parent = g_dummy_file_new (uri);
194 g_dummy_file_dup (GFile *file)
196 GDummyFile *dummy = G_DUMMY_FILE (file);
198 return g_dummy_file_new (dummy->text_uri);
202 g_dummy_file_hash (GFile *file)
204 GDummyFile *dummy = G_DUMMY_FILE (file);
206 return g_str_hash (dummy->text_uri);
210 g_dummy_file_equal (GFile *file1,
213 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
214 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
216 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
220 safe_strcmp (const char *a, const char *b)
227 return strcmp (a, b);
231 uri_same_except_path (GDecodedUri *a,
234 if (safe_strcmp (a->scheme, b->scheme) != 0)
236 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
238 if (safe_strcmp (a->host, b->host) != 0)
240 if (a->port != b->port)
247 match_prefix (const char *path, const char *prefix)
251 prefix_len = strlen (prefix);
252 if (strncmp (path, prefix, prefix_len) != 0)
254 return path + prefix_len;
258 g_dummy_file_contains_file (GFile *parent,
261 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
262 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
263 const char *remainder;
265 if (parent_dummy->decoded_uri != NULL &&
266 descendant_dummy->decoded_uri != NULL)
268 if (uri_same_except_path (parent_dummy->decoded_uri,
269 descendant_dummy->decoded_uri)) {
270 remainder = match_prefix (descendant_dummy->decoded_uri->path,
271 parent_dummy->decoded_uri->path);
272 if (remainder != NULL && *remainder == '/')
274 while (*remainder == '/')
283 remainder = match_prefix (descendant_dummy->text_uri,
284 parent_dummy->text_uri);
285 if (remainder != NULL && *remainder == '/')
287 while (*remainder == '/')
298 g_dummy_file_get_relative_path (GFile *parent,
301 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
302 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
303 const char *remainder;
305 if (parent_dummy->decoded_uri != NULL &&
306 descendant_dummy->decoded_uri != NULL)
308 if (uri_same_except_path (parent_dummy->decoded_uri,
309 descendant_dummy->decoded_uri)) {
310 remainder = match_prefix (descendant_dummy->decoded_uri->path,
311 parent_dummy->decoded_uri->path);
312 if (remainder != NULL && *remainder == '/')
314 while (*remainder == '/')
317 return g_strdup (remainder);
323 remainder = match_prefix (descendant_dummy->text_uri,
324 parent_dummy->text_uri);
325 if (remainder != NULL && *remainder == '/')
327 while (*remainder == '/')
330 return unescape_string (remainder, NULL, "/");
339 g_dummy_file_resolve_relative_path (GFile *file,
340 const char *relative_path)
342 GDummyFile *dummy = G_DUMMY_FILE (file);
345 GDecodedUri new_decoded_uri;
348 if (dummy->decoded_uri == NULL)
350 str = g_string_new (dummy->text_uri);
351 g_string_append (str, "/");
352 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
353 child = g_dummy_file_new (str->str);
354 g_string_free (str, TRUE);
358 new_decoded_uri = *dummy->decoded_uri;
360 if (g_path_is_absolute (relative_path))
361 new_decoded_uri.path = g_strdup (relative_path);
363 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
365 uri = _g_encode_uri (&new_decoded_uri);
366 g_free (new_decoded_uri.path);
368 child = g_dummy_file_new (uri);
376 g_dummy_file_get_child_for_display_name (GFile *file,
377 const char *display_name,
380 return g_file_get_child (file, display_name);
384 g_dummy_file_has_uri_scheme (GFile *file,
385 const char *uri_scheme)
387 GDummyFile *dummy = G_DUMMY_FILE (file);
389 if (dummy->decoded_uri)
390 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
395 g_dummy_file_get_uri_scheme (GFile *file)
397 GDummyFile *dummy = G_DUMMY_FILE (file);
399 if (dummy->decoded_uri)
400 return g_strdup (dummy->decoded_uri->scheme);
407 g_dummy_file_file_iface_init (GFileIface *iface)
409 iface->dup = g_dummy_file_dup;
410 iface->hash = g_dummy_file_hash;
411 iface->equal = g_dummy_file_equal;
412 iface->is_native = g_dummy_file_is_native;
413 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
414 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
415 iface->get_basename = g_dummy_file_get_basename;
416 iface->get_path = g_dummy_file_get_path;
417 iface->get_uri = g_dummy_file_get_uri;
418 iface->get_parse_name = g_dummy_file_get_parse_name;
419 iface->get_parent = g_dummy_file_get_parent;
420 iface->contains_file = g_dummy_file_contains_file;
421 iface->get_relative_path = g_dummy_file_get_relative_path;
422 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
423 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
426 /* Uri handling helper functions: */
429 unescape_character (const char *scanner)
434 first_digit = g_ascii_xdigit_value (*scanner++);
438 second_digit = g_ascii_xdigit_value (*scanner++);
439 if (second_digit < 0)
442 return (first_digit << 4) | second_digit;
446 unescape_string (const gchar *escaped_string,
447 const gchar *escaped_string_end,
448 const gchar *illegal_characters)
454 if (escaped_string == NULL)
457 if (escaped_string_end == NULL)
458 escaped_string_end = escaped_string + strlen (escaped_string);
460 result = g_malloc (escaped_string_end - escaped_string + 1);
463 for (in = escaped_string; in < escaped_string_end; in++) {
467 if (escaped_string_end - in < 2)
473 character = unescape_character (in);
475 /* Check for an illegal character. We consider '\0' illegal here. */
476 if (character <= 0 ||
477 (illegal_characters != NULL &&
478 strchr (illegal_characters, (char)character) != NULL))
483 in++; /* The other char will be eaten in the loop header */
485 *out++ = (char)character;
489 g_assert (out - result <= strlen (escaped_string));
494 _g_decoded_uri_free (GDecodedUri *decoded)
499 g_free (decoded->scheme);
500 g_free (decoded->query);
501 g_free (decoded->fragment);
502 g_free (decoded->userinfo);
503 g_free (decoded->host);
504 g_free (decoded->path);
509 _g_decoded_uri_new (void)
513 uri = g_new0 (GDecodedUri, 1);
520 _g_decode_uri (const char *uri)
522 GDecodedUri *decoded;
523 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
527 /* From RFC 3986 Decodes:
528 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
534 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
537 if (!g_ascii_isalpha (*p))
547 if (!(g_ascii_isalnum(c) ||
554 decoded = _g_decoded_uri_new ();
556 decoded->scheme = g_malloc (p - uri);
557 out = decoded->scheme;
558 for (in = uri; in < p - 1; in++)
559 *out++ = g_ascii_tolower (*in);
564 query_start = strchr (p, '?');
567 hier_part_end = query_start++;
568 fragment_start = strchr (query_start, '#');
571 decoded->query = g_strndup (query_start, fragment_start - query_start);
572 decoded->fragment = g_strdup (fragment_start+1);
576 decoded->query = g_strdup (query_start);
577 decoded->fragment = NULL;
583 decoded->query = NULL;
584 fragment_start = strchr (p, '#');
587 hier_part_end = fragment_start++;
588 decoded->fragment = g_strdup (fragment_start);
592 hier_part_end = p + strlen (p);
593 decoded->fragment = NULL;
598 hier-part = "//" authority path-abempty
605 if (hier_part_start[0] == '/' &&
606 hier_part_start[1] == '/')
608 const char *authority_start, *authority_end;
609 const char *userinfo_start, *userinfo_end;
610 const char *host_start, *host_end;
611 const char *port_start;
613 authority_start = hier_part_start + 2;
614 /* authority is always followed by / or nothing */
615 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
616 if (authority_end == NULL)
617 authority_end = hier_part_end;
620 authority = [ userinfo "@" ] host [ ":" port ]
623 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
626 userinfo_start = authority_start;
627 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
628 if (decoded->userinfo == NULL)
630 _g_decoded_uri_free (decoded);
633 host_start = userinfo_end + 1;
636 host_start = authority_start;
638 port_start = memchr (host_start, ':', authority_end - host_start);
641 host_end = port_start++;
643 decoded->port = atoi(port_start);
647 host_end = authority_end;
651 decoded->host = g_strndup (host_start, host_end - host_start);
653 hier_part_start = authority_end;
656 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
658 if (decoded->path == NULL)
660 _g_decoded_uri_free (decoded);
668 is_valid (char c, const char *reserved_chars_allowed)
670 if (g_ascii_isalnum (c) ||
677 if (reserved_chars_allowed &&
678 strchr (reserved_chars_allowed, c) != NULL)
685 g_string_append_encoded (GString *string, const char *encoded,
686 const char *reserved_chars_allowed)
690 static const gchar hex[16] = "0123456789ABCDEF";
692 end = encoded + strlen (encoded);
694 while ((c = *encoded) != 0)
696 if (is_valid (c, reserved_chars_allowed))
698 g_string_append_c (string, c);
703 g_string_append_c (string, '%');
704 g_string_append_c (string, hex[((guchar)c) >> 4]);
705 g_string_append_c (string, hex[((guchar)c) & 0xf]);
712 _g_encode_uri (GDecodedUri *decoded)
716 uri = g_string_new (NULL);
718 g_string_append (uri, decoded->scheme);
719 g_string_append (uri, "://");
721 if (decoded->host != NULL)
723 if (decoded->userinfo)
725 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
726 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
727 g_string_append_c (uri, '@');
730 g_string_append (uri, decoded->host);
732 if (decoded->port != -1)
734 g_string_append_c (uri, ':');
735 g_string_append_printf (uri, "%d", decoded->port);
739 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
743 g_string_append_c (uri, '?');
744 g_string_append (uri, decoded->query);
747 if (decoded->fragment)
749 g_string_append_c (uri, '#');
750 g_string_append (uri, decoded->fragment);
753 return g_string_free (uri, FALSE);