1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
4
* String substitution utilities for config files
5
* Copyright (C) 2001 Sven Neumann <sven@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#include <glib-object.h>
29
#include "libgimpbase/gimpbase.h"
31
#include "gimpconfig-path.h"
33
#include "gimp-intl.h"
36
#define SUBSTS_ALLOC 4
38
static gchar * gimp_config_path_expand_only (const gchar *path,
40
static inline gchar * extract_token (const gchar **str);
43
extract_token (const gchar **str)
48
if (strncmp (*str, "${", 2))
53
while (*p && (*p != '}'))
54
p = g_utf8_next_char (p);
59
token = g_strndup (*str + 2, g_utf8_pointer_to_offset (*str + 2, p));
61
*str = p + 1; /* after the closing bracket */
67
* gimp_config_path_expand:
68
* @path: a %NUL-terminated string in UTF-8 encoding
69
* @recode: whether to convert to the filesystem's encoding
70
* @error: return location for errors
72
* Paths as stored in the gimprc have to be treated special. The
73
* string may contain special identifiers such as for example
74
* ${gimp_dir} that have to be substituted before use. Also the user's
75
* filesystem may be in a different encoding than UTF-8 (which is what
76
* is used for the gimprc). This function does the variable
77
* substitution for you and can also attempt to convert to the
78
* filesystem encoding.
80
* Return value: a newly allocated %NUL-terminated string
83
gimp_config_path_expand (const gchar *path,
87
g_return_val_if_fail (path != NULL, NULL);
88
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
93
gchar *expanded = gimp_config_path_expand_only (path, error);
98
retval = g_filename_from_utf8 (expanded, -1, NULL, NULL, error);
105
return gimp_config_path_expand_only (path, error);
109
gimp_config_path_expand_only (const gchar *path,
116
gchar *filename = NULL;
117
gchar *expanded = NULL;
118
gchar **substs = NULL;
131
length += strlen (gimp_filename_to_utf8 (g_get_home_dir ()));
135
#endif /* G_OS_WIN32 */
137
if ((token = extract_token (&p)) != NULL)
139
for (i = 0; i < n_substs; i++)
140
if (strcmp (substs[2*i], token) == 0)
151
if (strcmp (token, "gimp_dir") == 0)
152
s = gimp_directory ();
153
else if (strcmp (token, "gimp_data_dir") == 0)
154
s = gimp_data_directory ();
155
else if (strcmp (token, "gimp_plug_in_dir") == 0 ||
156
strcmp (token, "gimp_plugin_dir") == 0)
157
s = gimp_plug_in_directory ();
158
else if (strcmp (token, "gimp_sysconf_dir") == 0)
159
s = gimp_sysconf_directory ();
162
s = g_getenv (token);
165
/* The default user gimprc on Windows references
166
* ${TEMP}, but not all Windows installations have that
167
* environment variable, even if it should be kinda
168
* standard. So special-case it.
170
if (!s && strcmp (token, "TEMP") == 0)
171
s = g_get_tmp_dir ();
172
#endif /* G_OS_WIN32 */
177
g_set_error (error, 0, 0, _("Cannot expand ${%s}"), token);
182
if (n_substs % SUBSTS_ALLOC == 0)
183
substs = g_renew (gchar *, substs, 2 * (n_substs + SUBSTS_ALLOC));
185
substs[2*n_substs] = token;
186
substs[2*n_substs + 1] = (gchar *) gimp_filename_to_utf8 (s);
188
length += strlen (substs[2*n_substs + 1]);
194
length += g_utf8_skip[(const guchar) *p];
195
p = g_utf8_next_char (p);
200
return g_strdup (path);
202
expanded = g_new (gchar, length + 1);
214
strcat (n, gimp_filename_to_utf8 (g_get_home_dir ()));
215
n += strlen (gimp_filename_to_utf8 (g_get_home_dir ()));
219
#endif /* G_OS_WIN32 */
221
if ((token = extract_token (&p)) != NULL)
223
for (i = 0; i < n_substs; i++)
225
if (strcmp (substs[2*i], token) == 0)
248
for (i = 0; i < n_substs; i++)
249
g_free (substs[2*i]);