[gimp] libgimpconfig: add gimp_config_path_unexpand()



commit 6b391156bee37b11c38f5e65ddff5208bf3ca260
Author: Michael Natterer <mitch gimp org>
Date:   Thu Sep 1 00:36:46 2016 +0200

    libgimpconfig: add gimp_config_path_unexpand()
    
    which is the opposite of gimp_config_path_expand() and replaces all
    occurances of well-known gimprc paths with their respective
    identifiers such as ${gimp_data_directory}, ${gimp_directory} etc.

 libgimpconfig/gimpconfig-path.c |  125 +++++++++++++++++++++++++++++++++++----
 libgimpconfig/gimpconfig-path.h |    4 +
 libgimpconfig/gimpconfig.def    |    1 +
 3 files changed, 118 insertions(+), 12 deletions(-)
---
diff --git a/libgimpconfig/gimpconfig-path.c b/libgimpconfig/gimpconfig-path.c
index 6a5bd88..1aff673 100644
--- a/libgimpconfig/gimpconfig-path.c
+++ b/libgimpconfig/gimpconfig-path.c
@@ -190,6 +190,7 @@ gimp_param_spec_config_path_type (GParamSpec *pspec)
 static gchar        * gimp_config_path_expand_only   (const gchar  *path,
                                                       GError      **error) G_GNUC_MALLOC;
 static inline gchar * gimp_config_path_extract_token (const gchar **str);
+static gchar        * gimp_config_path_unexpand_only (const gchar  *path) G_GNUC_MALLOC;
 
 
 /**
@@ -269,17 +270,19 @@ gimp_config_build_writable_path (const gchar *name)
 
 /**
  * gimp_config_path_expand:
- * @path: a NUL-terminated string in UTF-8 encoding
+ * @path:   a NUL-terminated string in UTF-8 encoding
  * @recode: whether to convert to the filesystem's encoding
- * @error: return location for errors
+ * @error:  return location for errors
  *
- * Paths as stored in the gimprc have to be treated special.  The
- * string may contain special identifiers such as for example
- * ${gimp_dir} that have to be substituted before use. Also the user's
- * filesystem may be in a different encoding than UTF-8 (which is what
- * is used for the gimprc). This function does the variable
- * substitution for you and can also attempt to convert to the
- * filesystem encoding.
+ * Paths as stored in gimprc and other config files have to be treated
+ * special.  The string may contain special identifiers such as for
+ * example ${gimp_dir} that have to be substituted before use. Also
+ * the user's filesystem may be in a different encoding than UTF-8
+ * (which is what is used for the gimprc). This function does the
+ * variable substitution for you and can also attempt to convert to
+ * the filesystem encoding.
+ *
+ * To reverse the expansion, use gimp_config_path_unexpand().
  *
  * Return value: a newly allocated NUL-terminated string
  *
@@ -313,7 +316,7 @@ gimp_config_path_expand (const gchar  *path,
 
 /**
  * gimp_config_path_expand_to_files:
- * @path: a NUL-terminated string in UTF-8 encoding
+ * @path:  a NUL-terminated string in UTF-8 encoding
  * @error: return location for errors
  *
  * Paths as stored in the gimprc have to be treated special. The
@@ -323,8 +326,8 @@ gimp_config_path_expand (const gchar  *path,
  * is used for the gimprc).
  *
  * This function runs @path through gimp_config_path_expand() and
- * gimp_path_parse(), then turns the filenames returned by gimp_path_parse()
- * into GFile using g_file_new_for_path().
+ * gimp_path_parse(), then turns the filenames returned by
+ * gimp_path_parse() into GFile using g_file_new_for_path().
  *
  * Return value: a #GList of newly allocated #GFile objects.
  *
@@ -361,6 +364,51 @@ gimp_config_path_expand_to_files (const gchar  *path,
   return files;
 }
 
+/**
+ * gimp_config_path_unexpand:
+ * @path:   a NUL-terminated string
+ * @recode: whether @path is in filesystem encoding or UTF-8
+ * @error:  return location for errors
+ *
+ * The inverse operation of gimp_config_path_expand()
+ *
+ * This function takes a @path and tries to substitute the first
+ * elements by well-known special identifiers such as for example
+ * ${gimp_dir}. The unexpanded path can then be stored in gimprc and
+ * other config files.
+ *
+ * If @recode is %TRUE then @path is in local filesystem encoding,
+ * if @recode is %FALSE then @path is assumed to be UTF-8.
+ *
+ * Return value: a newly allocated NUL-terminated UTF-8 string
+ *
+ * Since: 2.10
+ **/
+gchar *
+gimp_config_path_unexpand (const gchar  *path,
+                           gboolean      recode,
+                           GError      **error)
+{
+  g_return_val_if_fail (path != NULL, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  if (recode)
+    {
+      gchar *retval;
+      gchar *utf8 = g_filename_to_utf8 (path, -1, NULL, NULL, error);
+
+      if (! utf8)
+        return NULL;
+
+      retval = gimp_config_path_unexpand_only (utf8);
+
+      g_free (utf8);
+
+      return retval;
+    }
+
+  return gimp_config_path_unexpand_only (path);
+}
 
 #define SUBSTS_ALLOC 4
 
@@ -534,3 +582,56 @@ gimp_config_path_extract_token (const gchar **str)
 
   return token;
 }
+
+static gchar *
+gimp_config_path_unexpand_only (const gchar *path)
+{
+  const struct
+  {
+    const gchar *id;
+    const gchar *prefix;
+  }
+  identifiers[] =
+  {
+    { "${gimp_plug_in_dir}",      gimp_plug_in_directory () },
+    { "${gimp_data_dir}",         gimp_data_directory () },
+    { "${gimp_sysconf_dir}",      gimp_sysconf_directory () },
+    { "${gimp_installation_dir}", gimp_installation_directory () },
+    { "${gimp_dir}",              gimp_directory () }
+  };
+
+  GList *files;
+  GList *list;
+  gchar *unexpanded;
+
+  files = gimp_path_parse (path, 256, FALSE, NULL);
+
+  for (list = files; list; list = g_list_next (list))
+    {
+      gchar *dir = list->data;
+      gint   i;
+
+      for (i = 0; i < G_N_ELEMENTS (identifiers); i++)
+        {
+          if (g_str_has_prefix (dir, identifiers[i].prefix))
+            {
+              gchar *tmp = g_strconcat (identifiers[i].id,
+                                        dir + strlen (identifiers[i].prefix),
+                                        NULL);
+
+              g_printerr ("old: %s\nnew: %s\n", dir, tmp);
+
+              g_free (dir);
+              list->data = tmp;
+
+              break;
+            }
+        }
+    }
+
+  unexpanded = gimp_path_to_str (files);
+
+  gimp_path_free (files);
+
+  return unexpanded;
+}
diff --git a/libgimpconfig/gimpconfig-path.h b/libgimpconfig/gimpconfig-path.h
index 9139d81..783124a 100644
--- a/libgimpconfig/gimpconfig-path.h
+++ b/libgimpconfig/gimpconfig-path.h
@@ -80,6 +80,10 @@ gchar             * gimp_config_path_expand          (const gchar  *path,
 GList             * gimp_config_path_expand_to_files (const gchar  *path,
                                                       GError      **error) G_GNUC_MALLOC;
 
+gchar             * gimp_config_path_unexpand        (const gchar  *path,
+                                                      gboolean      recode,
+                                                      GError      **error) G_GNUC_MALLOC;
+
 gchar             * gimp_config_build_data_path      (const gchar  *name) G_GNUC_MALLOC;
 gchar             * gimp_config_build_writable_path  (const gchar  *name) G_GNUC_MALLOC;
 gchar             * gimp_config_build_plug_in_path   (const gchar  *name) G_GNUC_MALLOC;
diff --git a/libgimpconfig/gimpconfig.def b/libgimpconfig/gimpconfig.def
index c9e74c6..f4c4946 100644
--- a/libgimpconfig/gimpconfig.def
+++ b/libgimpconfig/gimpconfig.def
@@ -36,6 +36,7 @@ EXPORTS
        gimp_config_path_expand
        gimp_config_path_expand_to_files
        gimp_config_path_get_type
+       gimp_config_path_unexpand
        gimp_config_reset
        gimp_config_reset_properties
        gimp_config_reset_property


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]