glib/tests: Fix non-debug build of slice test
[glib.git] / gio / gdummyfile.c
blob8f22715381c1f7687760d874884ac1e2a0b3a5b7
1 /* GIO - GLib Input, Output and Streaming Library
2 *
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>
23 #include "config.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
32 #include "gdummyfile.h"
33 #include "gfile.h"
36 static void g_dummy_file_file_iface_init (GFileIface *iface);
38 typedef struct {
39 char *scheme;
40 char *userinfo;
41 char *host;
42 int port; /* -1 => not in uri */
43 char *path;
44 char *query;
45 char *fragment;
46 } GDecodedUri;
48 struct _GDummyFile
50 GObject parent_instance;
52 GDecodedUri *decoded_uri;
53 char *text_uri;
56 #define g_dummy_file_get_type _g_dummy_file_get_type
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,
73 const char *encoded,
74 const char *reserved_chars_allowed);
76 static void
77 g_dummy_file_finalize (GObject *object)
79 GDummyFile *dummy;
81 dummy = G_DUMMY_FILE (object);
83 if (dummy->decoded_uri)
84 _g_decoded_uri_free (dummy->decoded_uri);
86 g_free (dummy->text_uri);
88 G_OBJECT_CLASS (g_dummy_file_parent_class)->finalize (object);
91 static void
92 g_dummy_file_class_init (GDummyFileClass *klass)
94 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
96 gobject_class->finalize = g_dummy_file_finalize;
99 static void
100 g_dummy_file_init (GDummyFile *dummy)
104 GFile *
105 _g_dummy_file_new (const char *uri)
107 GDummyFile *dummy;
109 g_return_val_if_fail (uri != NULL, NULL);
111 dummy = g_object_new (G_TYPE_DUMMY_FILE, NULL);
112 dummy->text_uri = g_strdup (uri);
113 dummy->decoded_uri = _g_decode_uri (uri);
115 return G_FILE (dummy);
118 static gboolean
119 g_dummy_file_is_native (GFile *file)
121 return FALSE;
124 static char *
125 g_dummy_file_get_basename (GFile *file)
127 GDummyFile *dummy = G_DUMMY_FILE (file);
129 if (dummy->decoded_uri)
130 return g_path_get_basename (dummy->decoded_uri->path);
131 return g_strdup (dummy->text_uri);
134 static char *
135 g_dummy_file_get_path (GFile *file)
137 return NULL;
140 static char *
141 g_dummy_file_get_uri (GFile *file)
143 return g_strdup (G_DUMMY_FILE (file)->text_uri);
146 static char *
147 g_dummy_file_get_parse_name (GFile *file)
149 return g_strdup (G_DUMMY_FILE (file)->text_uri);
152 static GFile *
153 g_dummy_file_get_parent (GFile *file)
155 GDummyFile *dummy = G_DUMMY_FILE (file);
156 GFile *parent;
157 char *dirname;
158 char *uri;
159 GDecodedUri new_decoded_uri;
161 if (dummy->decoded_uri == NULL ||
162 g_strcmp0 (dummy->decoded_uri->path, "/") == 0)
163 return NULL;
165 dirname = g_path_get_dirname (dummy->decoded_uri->path);
167 if (strcmp (dirname, ".") == 0)
169 g_free (dirname);
170 return NULL;
173 new_decoded_uri = *dummy->decoded_uri;
174 new_decoded_uri.path = dirname;
175 uri = _g_encode_uri (&new_decoded_uri);
176 g_free (dirname);
178 parent = _g_dummy_file_new (uri);
179 g_free (uri);
181 return parent;
184 static GFile *
185 g_dummy_file_dup (GFile *file)
187 GDummyFile *dummy = G_DUMMY_FILE (file);
189 return _g_dummy_file_new (dummy->text_uri);
192 static guint
193 g_dummy_file_hash (GFile *file)
195 GDummyFile *dummy = G_DUMMY_FILE (file);
197 return g_str_hash (dummy->text_uri);
200 static gboolean
201 g_dummy_file_equal (GFile *file1,
202 GFile *file2)
204 GDummyFile *dummy1 = G_DUMMY_FILE (file1);
205 GDummyFile *dummy2 = G_DUMMY_FILE (file2);
207 return g_str_equal (dummy1->text_uri, dummy2->text_uri);
210 static int
211 safe_strcmp (const char *a,
212 const char *b)
214 if (a == NULL)
215 a = "";
216 if (b == NULL)
217 b = "";
219 return strcmp (a, b);
222 static gboolean
223 uri_same_except_path (GDecodedUri *a,
224 GDecodedUri *b)
226 if (safe_strcmp (a->scheme, b->scheme) != 0)
227 return FALSE;
228 if (safe_strcmp (a->userinfo, b->userinfo) != 0)
229 return FALSE;
230 if (safe_strcmp (a->host, b->host) != 0)
231 return FALSE;
232 if (a->port != b->port)
233 return FALSE;
235 return TRUE;
238 static const char *
239 match_prefix (const char *path,
240 const char *prefix)
242 int prefix_len;
244 prefix_len = strlen (prefix);
245 if (strncmp (path, prefix, prefix_len) != 0)
246 return NULL;
247 return path + prefix_len;
250 static gboolean
251 g_dummy_file_prefix_matches (GFile *parent, GFile *descendant)
253 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
254 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
255 const char *remainder;
257 if (parent_dummy->decoded_uri != NULL &&
258 descendant_dummy->decoded_uri != NULL)
260 if (uri_same_except_path (parent_dummy->decoded_uri,
261 descendant_dummy->decoded_uri))
263 remainder = match_prefix (descendant_dummy->decoded_uri->path,
264 parent_dummy->decoded_uri->path);
265 if (remainder != NULL && *remainder == '/')
267 while (*remainder == '/')
268 remainder++;
269 if (*remainder != 0)
270 return TRUE;
274 else
276 remainder = match_prefix (descendant_dummy->text_uri,
277 parent_dummy->text_uri);
278 if (remainder != NULL && *remainder == '/')
280 while (*remainder == '/')
281 remainder++;
282 if (*remainder != 0)
283 return TRUE;
287 return FALSE;
290 static char *
291 g_dummy_file_get_relative_path (GFile *parent,
292 GFile *descendant)
294 GDummyFile *parent_dummy = G_DUMMY_FILE (parent);
295 GDummyFile *descendant_dummy = G_DUMMY_FILE (descendant);
296 const char *remainder;
298 if (parent_dummy->decoded_uri != NULL &&
299 descendant_dummy->decoded_uri != NULL)
301 if (uri_same_except_path (parent_dummy->decoded_uri,
302 descendant_dummy->decoded_uri))
304 remainder = match_prefix (descendant_dummy->decoded_uri->path,
305 parent_dummy->decoded_uri->path);
306 if (remainder != NULL && *remainder == '/')
308 while (*remainder == '/')
309 remainder++;
310 if (*remainder != 0)
311 return g_strdup (remainder);
315 else
317 remainder = match_prefix (descendant_dummy->text_uri,
318 parent_dummy->text_uri);
319 if (remainder != NULL && *remainder == '/')
321 while (*remainder == '/')
322 remainder++;
323 if (*remainder != 0)
324 return unescape_string (remainder, NULL, "/");
328 return NULL;
332 static GFile *
333 g_dummy_file_resolve_relative_path (GFile *file,
334 const char *relative_path)
336 GDummyFile *dummy = G_DUMMY_FILE (file);
337 GFile *child;
338 char *uri;
339 GDecodedUri new_decoded_uri;
340 GString *str;
342 if (dummy->decoded_uri == NULL)
344 str = g_string_new (dummy->text_uri);
345 g_string_append (str, "/");
346 g_string_append_encoded (str, relative_path, SUB_DELIM_CHARS ":@/");
347 child = _g_dummy_file_new (str->str);
348 g_string_free (str, TRUE);
350 else
352 new_decoded_uri = *dummy->decoded_uri;
354 if (g_path_is_absolute (relative_path))
355 new_decoded_uri.path = g_strdup (relative_path);
356 else
357 new_decoded_uri.path = g_build_filename (new_decoded_uri.path, relative_path, NULL);
359 uri = _g_encode_uri (&new_decoded_uri);
360 g_free (new_decoded_uri.path);
362 child = _g_dummy_file_new (uri);
363 g_free (uri);
366 return child;
369 static GFile *
370 g_dummy_file_get_child_for_display_name (GFile *file,
371 const char *display_name,
372 GError **error)
374 return g_file_get_child (file, display_name);
377 static gboolean
378 g_dummy_file_has_uri_scheme (GFile *file,
379 const char *uri_scheme)
381 GDummyFile *dummy = G_DUMMY_FILE (file);
383 if (dummy->decoded_uri)
384 return g_ascii_strcasecmp (uri_scheme, dummy->decoded_uri->scheme) == 0;
385 return FALSE;
388 static char *
389 g_dummy_file_get_uri_scheme (GFile *file)
391 GDummyFile *dummy = G_DUMMY_FILE (file);
393 if (dummy->decoded_uri)
394 return g_strdup (dummy->decoded_uri->scheme);
396 return NULL;
400 static void
401 g_dummy_file_file_iface_init (GFileIface *iface)
403 iface->dup = g_dummy_file_dup;
404 iface->hash = g_dummy_file_hash;
405 iface->equal = g_dummy_file_equal;
406 iface->is_native = g_dummy_file_is_native;
407 iface->has_uri_scheme = g_dummy_file_has_uri_scheme;
408 iface->get_uri_scheme = g_dummy_file_get_uri_scheme;
409 iface->get_basename = g_dummy_file_get_basename;
410 iface->get_path = g_dummy_file_get_path;
411 iface->get_uri = g_dummy_file_get_uri;
412 iface->get_parse_name = g_dummy_file_get_parse_name;
413 iface->get_parent = g_dummy_file_get_parent;
414 iface->prefix_matches = g_dummy_file_prefix_matches;
415 iface->get_relative_path = g_dummy_file_get_relative_path;
416 iface->resolve_relative_path = g_dummy_file_resolve_relative_path;
417 iface->get_child_for_display_name = g_dummy_file_get_child_for_display_name;
419 iface->supports_thread_contexts = TRUE;
422 /* Uri handling helper functions: */
424 static int
425 unescape_character (const char *scanner)
427 int first_digit;
428 int second_digit;
430 first_digit = g_ascii_xdigit_value (*scanner++);
431 if (first_digit < 0)
432 return -1;
434 second_digit = g_ascii_xdigit_value (*scanner++);
435 if (second_digit < 0)
436 return -1;
438 return (first_digit << 4) | second_digit;
441 static char *
442 unescape_string (const gchar *escaped_string,
443 const gchar *escaped_string_end,
444 const gchar *illegal_characters)
446 const gchar *in;
447 gchar *out, *result;
448 gint character;
450 if (escaped_string == NULL)
451 return NULL;
453 if (escaped_string_end == NULL)
454 escaped_string_end = escaped_string + strlen (escaped_string);
456 result = g_malloc (escaped_string_end - escaped_string + 1);
458 out = result;
459 for (in = escaped_string; in < escaped_string_end; in++)
461 character = *in;
462 if (*in == '%')
464 in++;
465 if (escaped_string_end - in < 2)
467 g_free (result);
468 return NULL;
471 character = unescape_character (in);
473 /* Check for an illegal character. We consider '\0' illegal here. */
474 if (character <= 0 ||
475 (illegal_characters != NULL &&
476 strchr (illegal_characters, (char)character) != NULL))
478 g_free (result);
479 return NULL;
481 in++; /* The other char will be eaten in the loop header */
483 *out++ = (char)character;
486 *out = '\0';
487 g_warn_if_fail (out - result <= strlen (escaped_string));
488 return result;
491 void
492 _g_decoded_uri_free (GDecodedUri *decoded)
494 if (decoded == NULL)
495 return;
497 g_free (decoded->scheme);
498 g_free (decoded->query);
499 g_free (decoded->fragment);
500 g_free (decoded->userinfo);
501 g_free (decoded->host);
502 g_free (decoded->path);
503 g_free (decoded);
506 GDecodedUri *
507 _g_decoded_uri_new (void)
509 GDecodedUri *uri;
511 uri = g_new0 (GDecodedUri, 1);
512 uri->port = -1;
514 return uri;
517 GDecodedUri *
518 _g_decode_uri (const char *uri)
520 GDecodedUri *decoded;
521 const char *p, *in, *hier_part_start, *hier_part_end, *query_start, *fragment_start;
522 char *out;
523 char c;
525 /* From RFC 3986 Decodes:
526 * URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
529 p = uri;
531 /* Decode scheme:
532 scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
535 if (!g_ascii_isalpha (*p))
536 return NULL;
538 while (1)
540 c = *p++;
542 if (c == ':')
543 break;
545 if (!(g_ascii_isalnum(c) ||
546 c == '+' ||
547 c == '-' ||
548 c == '.'))
549 return NULL;
552 decoded = _g_decoded_uri_new ();
554 decoded->scheme = g_malloc (p - uri);
555 out = decoded->scheme;
556 for (in = uri; in < p - 1; in++)
557 *out++ = g_ascii_tolower (*in);
558 *out = 0;
560 hier_part_start = p;
562 query_start = strchr (p, '?');
563 if (query_start)
565 hier_part_end = query_start++;
566 fragment_start = strchr (query_start, '#');
567 if (fragment_start)
569 decoded->query = g_strndup (query_start, fragment_start - query_start);
570 decoded->fragment = g_strdup (fragment_start+1);
572 else
574 decoded->query = g_strdup (query_start);
575 decoded->fragment = NULL;
578 else
580 /* No query */
581 decoded->query = NULL;
582 fragment_start = strchr (p, '#');
583 if (fragment_start)
585 hier_part_end = fragment_start++;
586 decoded->fragment = g_strdup (fragment_start);
588 else
590 hier_part_end = p + strlen (p);
591 decoded->fragment = NULL;
595 /* 3:
596 hier-part = "//" authority path-abempty
597 / path-absolute
598 / path-rootless
599 / path-empty
603 if (hier_part_start[0] == '/' &&
604 hier_part_start[1] == '/')
606 const char *authority_start, *authority_end;
607 const char *userinfo_start, *userinfo_end;
608 const char *host_start, *host_end;
609 const char *port_start;
611 authority_start = hier_part_start + 2;
612 /* authority is always followed by / or nothing */
613 authority_end = memchr (authority_start, '/', hier_part_end - authority_start);
614 if (authority_end == NULL)
615 authority_end = hier_part_end;
617 /* 3.2:
618 authority = [ userinfo "@" ] host [ ":" port ]
621 userinfo_end = memchr (authority_start, '@', authority_end - authority_start);
622 if (userinfo_end)
624 userinfo_start = authority_start;
625 decoded->userinfo = unescape_string (userinfo_start, userinfo_end, NULL);
626 if (decoded->userinfo == NULL)
628 _g_decoded_uri_free (decoded);
629 return NULL;
631 host_start = userinfo_end + 1;
633 else
634 host_start = authority_start;
636 port_start = memchr (host_start, ':', authority_end - host_start);
637 if (port_start)
639 host_end = port_start++;
641 decoded->port = atoi(port_start);
643 else
645 host_end = authority_end;
646 decoded->port = -1;
649 decoded->host = g_strndup (host_start, host_end - host_start);
651 hier_part_start = authority_end;
654 decoded->path = unescape_string (hier_part_start, hier_part_end, "/");
656 if (decoded->path == NULL)
658 _g_decoded_uri_free (decoded);
659 return NULL;
662 return decoded;
665 static gboolean
666 is_valid (char c, const char *reserved_chars_allowed)
668 if (g_ascii_isalnum (c) ||
669 c == '-' ||
670 c == '.' ||
671 c == '_' ||
672 c == '~')
673 return TRUE;
675 if (reserved_chars_allowed &&
676 strchr (reserved_chars_allowed, c) != NULL)
677 return TRUE;
679 return FALSE;
682 static void
683 g_string_append_encoded (GString *string,
684 const char *encoded,
685 const char *reserved_chars_allowed)
687 unsigned char c;
688 static const gchar hex[16] = "0123456789ABCDEF";
690 while ((c = *encoded) != 0)
692 if (is_valid (c, reserved_chars_allowed))
694 g_string_append_c (string, c);
695 encoded++;
697 else
699 g_string_append_c (string, '%');
700 g_string_append_c (string, hex[((guchar)c) >> 4]);
701 g_string_append_c (string, hex[((guchar)c) & 0xf]);
702 encoded++;
707 static char *
708 _g_encode_uri (GDecodedUri *decoded)
710 GString *uri;
712 uri = g_string_new (NULL);
714 g_string_append (uri, decoded->scheme);
715 g_string_append (uri, "://");
717 if (decoded->host != NULL)
719 if (decoded->userinfo)
721 /* userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) */
722 g_string_append_encoded (uri, decoded->userinfo, SUB_DELIM_CHARS ":");
723 g_string_append_c (uri, '@');
726 g_string_append (uri, decoded->host);
728 if (decoded->port != -1)
730 g_string_append_c (uri, ':');
731 g_string_append_printf (uri, "%d", decoded->port);
735 g_string_append_encoded (uri, decoded->path, SUB_DELIM_CHARS ":@/");
737 if (decoded->query)
739 g_string_append_c (uri, '?');
740 g_string_append (uri, decoded->query);
743 if (decoded->fragment)
745 g_string_append_c (uri, '#');
746 g_string_append (uri, decoded->fragment);
749 return g_string_free (uri, FALSE);