File: f-utils.c

package info (click to toggle)
f-spot 0.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 21,784 kB
  • ctags: 16,078
  • sloc: cs: 108,718; sh: 17,053; xml: 13,852; ansic: 3,187; makefile: 2,324
file content (105 lines) | stat: -rw-r--r-- 2,777 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* f-utils.h
 *
 * Copyright (C) 2003  Ettore Perazzoli
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli <ettore@ximian.com>
 */

#include <config.h>

#include "f-utils.h"

#include <glib.h>


char *
f_build_relative_path (const char *start_path,
		       const char *destination_path)
{
	const char *sp, *dp;
	GString *relative_path;
	gboolean need_separator;
	char *retval;

	g_return_val_if_fail (g_path_is_absolute (start_path), NULL);
	g_return_val_if_fail (g_path_is_absolute (destination_path), NULL);

	sp = start_path;
	dp = destination_path;

	/* Look for a common subpath at the beginning of the paths.  */

	while (*sp == *dp && *sp != 0) {
		sp ++;
		dp ++;
	}

	if (*sp == 0 && *dp == 0)
		return g_strdup ("");

	/* Roll back to the path element.  This is guarranteed to not run past
	   the beginning of the string because of the g_path_is_absolute()
	   checks above.  */

	while (*sp != G_DIR_SEPARATOR && *sp != 0)
		sp --;
	while (*dp != G_DIR_SEPARATOR && *dp != 0)
		dp --;

	g_assert (*dp == G_DIR_SEPARATOR || *dp == 0);
	g_assert (*sp == G_DIR_SEPARATOR || *sp == 0);

	/* Start constructing the string by adding one ".." for each path
	   component in the source path that is not in the destination
	   path.  */

	relative_path = g_string_new ("");

	need_separator = FALSE;
	while (*sp != 0) {
		sp ++;

		if (*sp == G_DIR_SEPARATOR || *sp == 0) {
			while (*sp == G_DIR_SEPARATOR)
				sp ++;

			if (need_separator)
				g_string_append (relative_path, G_DIR_SEPARATOR_S);
				
			g_string_append (relative_path, "..");
			need_separator = TRUE;
		}
	}

	/* Add the rest of the path elements, if any.  */

	if (*dp != 0) {
  		/*  (Notice that DP is guaranteed to point to either a null character
		    or to a separator so we don't need to add it ourselves.)  */
  		if (need_separator)
			g_string_append (relative_path, dp);
		else
			g_string_append (relative_path, dp + 1);
	}

	retval = relative_path->str;
	g_string_free (relative_path, FALSE);

	return retval;
}