2010/8/5 Jiří Techet <[email protected]>:
>
> This is used by the patch that writes relative paths to the project
> file. I put it to utils because I use it in gproject as well and
> might be useful for others.
>
> Signed-off-by: Jiří Techet <[email protected]>
> ---
> plugins/geanyfunctions.h | 2 ++
> src/plugindata.h | 2 +-
> src/plugins.c | 3 ++-
> src/utils.c | 41 +++++++++++++++++++++++++++++++++++++++++
> src/utils.h | 2 ++
> 5 files changed, 48 insertions(+), 2 deletions(-)
>
Ah, this patch is leaky as hell. Please use the attached patch instead.
Jiri
diff --git a/plugins/geanyfunctions.h b/plugins/geanyfunctions.h
index b17b9b3..a5fb1f6 100644
--- a/plugins/geanyfunctions.h
+++ b/plugins/geanyfunctions.h
@@ -244,6 +244,8 @@
geany_functions->p_utils->utils_get_file_list_full
#define utils_copy_environment \
geany_functions->p_utils->utils_copy_environment
+#define utils_relpath \
+ geany_functions->p_utils->utils_relpath
#define ui_dialog_vbox_new \
geany_functions->p_ui->ui_dialog_vbox_new
#define ui_frame_new_with_alignment \
diff --git a/src/plugindata.h b/src/plugindata.h
index 4f8584a..47006ca 100644
--- a/src/plugindata.h
+++ b/src/plugindata.h
@@ -430,7 +430,7 @@ typedef struct UtilsFuncs
GSList* (*utils_get_file_list_full)(const gchar *path, gboolean full_path, gboolean sort,
GError **error);
gchar** (*utils_copy_environment)(const gchar **exclude_vars, const gchar *first_varname, ...);
-
+ gchar* (*utils_relpath)(const gchar *origin, const gchar *dest);
}
UtilsFuncs;
diff --git a/src/plugins.c b/src/plugins.c
index fb5e3ca..9391021 100644
--- a/src/plugins.c
+++ b/src/plugins.c
@@ -220,7 +220,8 @@ static UtilsFuncs utils_funcs = {
&utils_str_middle_truncate,
&utils_str_remove_chars,
&utils_get_file_list_full,
- &utils_copy_environment
+ &utils_copy_environment,
+ &utils_relpath
};
static UIUtilsFuncs uiutils_funcs = {
diff --git a/src/utils.c b/src/utils.c
index 27dbe5d..6a2eff9 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -2036,3 +2036,53 @@ gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_va
}
+/**
+ * Generates a relative path from the origin directory to the destination directory.
+ *
+ * @param origin_dir The origin directory.
+ * @param dest_dir The destination directory.
+ *
+ * @return The relative path or NULL when not successful.
+ **/
+gchar *utils_relpath(const gchar *origin_dir, const gchar *dest_dir)
+{
+ gchar *origin, *dest;
+ gchar **originv, **destv;
+ gchar *ret = NULL;
+ guint i, j;
+
+ origin = tm_get_real_path(origin_dir);
+ dest = tm_get_real_path(dest_dir);
+
+ if (!NZV(origin) || !NZV(dest) || origin[0] != dest[0])
+ {
+ g_free(origin);
+ g_free(dest);
+ return NULL;
+ }
+
+ originv = g_strsplit_set(g_path_skip_root(origin), "/\\", -1);
+ destv = g_strsplit_set(g_path_skip_root(dest), "/\\", -1);
+
+ for (i = 0; originv[i] != NULL && destv[i] != NULL; i++)
+ if (g_strcmp0(originv[i], destv[i]) != 0)
+ break;
+
+ ret = g_strdup("");
+
+ for (j = i; originv[j] != NULL; j++)
+ setptr(ret, g_build_filename(ret, "..", NULL));
+
+ for (j = i; destv[j] != NULL; j++)
+ setptr(ret, g_build_filename(ret, destv[j], NULL));
+
+ if (strlen(ret) == 0)
+ setptr(ret, g_strdup("./"));
+
+ g_free(origin);
+ g_free(dest);
+ g_strfreev(originv);
+ g_strfreev(destv);
+
+ return ret;
+}
diff --git a/src/utils.h b/src/utils.h
index 9e4c24d..c8f4a13 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -242,4 +242,6 @@ gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...) G_GNUC_NULL_TERMINATED;
+gchar *utils_relpath(const gchar *origin, const gchar *dest);
+
#endif
_______________________________________________
Geany-devel mailing list
[email protected]
http://lists.uvena.de/cgi-bin/mailman/listinfo/geany-devel