Remove relative/untidy path elements when opening documents (closes

#2823998).



git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@3998 ea778897-0a13-0410-b9d1-a72fbfd435f5
This commit is contained in:
Nick Treleaven 2009-07-20 15:37:02 +00:00
parent 84f8db733d
commit 3c8a24d0fd
4 changed files with 54 additions and 0 deletions

View File

@ -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>

View File

@ -1183,6 +1183,9 @@ GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename
#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);

View File

@ -1710,3 +1710,49 @@ gboolean utils_is_remote_path(const gchar *path)
}
/* 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);
}

View File

@ -101,6 +101,8 @@ const gchar *utils_get_eol_name(gint eol_mode);
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);