SF.net SVN: geany:[3998] trunk

ntrel at users.sourceforge.net ntrel at xxxxx
Mon Jul 20 15:37:02 UTC 2009


Revision: 3998
          http://geany.svn.sourceforge.net/geany/?rev=3998&view=rev
Author:   ntrel
Date:     2009-07-20 15:37:02 +0000 (Mon, 20 Jul 2009)

Log Message:
-----------
Remove relative/untidy path elements when opening documents (closes
#2823998).

Modified Paths:
--------------
    trunk/ChangeLog
    trunk/src/document.c
    trunk/src/utils.c
    trunk/src/utils.h

Modified: trunk/ChangeLog
===================================================================
--- trunk/ChangeLog	2009-07-20 12:25:24 UTC (rev 3997)
+++ trunk/ChangeLog	2009-07-20 15:37:02 UTC (rev 3998)
@@ -10,6 +10,9 @@
    implementation, as this is more useful potentially than a gpointer*
    argument, and more straightforward.
    Add foreach_c_array(), foreach_ptr_array() to API.
+ * src/utils.c, src/utils.h, src/document.c:
+   Remove relative/untidy path elements when opening documents (closes
+   #2823998).
 
 
 2009-07-19  Enrico Tröger  <enrico(dot)troeger(at)uvena(dot)de>

Modified: trunk/src/document.c
===================================================================
--- trunk/src/document.c	2009-07-20 12:25:24 UTC (rev 3997)
+++ trunk/src/document.c	2009-07-20 15:37:02 UTC (rev 3998)
@@ -1183,6 +1183,9 @@
 #else
 		locale_filename = g_strdup(filename);
 #endif
+		/* remove relative junk */
+		utils_tidy_path(locale_filename);
+
 		/* try to get the UTF-8 equivalent for the filename, fallback to filename if error */
 		utf8_filename = utils_get_utf8_from_locale(locale_filename);
 

Modified: trunk/src/utils.c
===================================================================
--- trunk/src/utils.c	2009-07-20 12:25:24 UTC (rev 3997)
+++ trunk/src/utils.c	2009-07-20 15:37:02 UTC (rev 3998)
@@ -1710,3 +1710,49 @@
 }
 
 
+/* Remove all relative and untidy elements from the path of @a filename.
+ * @param filename must be a valid absolute path.
+ * @see tm_get_real_path() - also resolves links. */
+void utils_tidy_path(gchar *filename)
+{
+	GString *str = g_string_new(filename);
+	const gchar *c, *needle;
+	gchar *tmp;
+	gssize pos;
+
+	g_return_if_fail(g_path_is_absolute(filename));
+
+	/* replace "/./" and "//" */
+	utils_string_replace_all(str, G_DIR_SEPARATOR_S "." G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
+	utils_string_replace_all(str, G_DIR_SEPARATOR_S G_DIR_SEPARATOR_S, G_DIR_SEPARATOR_S);
+
+	/* replace "/.." */
+	needle = G_DIR_SEPARATOR_S "..";
+	while (1)
+	{
+		c = strstr(str->str, needle);
+		if (c == NULL)
+			break;
+		else
+		{
+			pos = c - str->str;
+			if (pos <= 3)
+				break;	/* bad path */
+
+			g_string_erase(str, pos, strlen(needle));	/* erase "/.." */
+
+			tmp = g_strndup(str->str, pos);	/* path up to "/.." */
+			c = g_strrstr(tmp, G_DIR_SEPARATOR_S);
+			g_return_if_fail(c);
+
+			pos = c - tmp;	/* position of previous "/" */
+			g_string_erase(str, pos, strlen(c));
+			g_free(tmp);
+		}
+	}
+	g_return_if_fail(strlen(str->str) <= strlen(filename));
+	strcpy(filename, str->str);
+	g_string_free(str, TRUE);
+}
+
+

Modified: trunk/src/utils.h
===================================================================
--- trunk/src/utils.h	2009-07-20 12:25:24 UTC (rev 3997)
+++ trunk/src/utils.h	2009-07-20 15:37:02 UTC (rev 3998)
@@ -101,6 +101,8 @@
 
 gboolean utils_atob(const gchar *str);
 
+void utils_tidy_path(gchar *filename);
+
 gboolean utils_is_absolute_path(const gchar *path);
 
 const gchar *utils_path_skip_root(const gchar *path);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.



More information about the Commits mailing list