2 * Copyright 2008 Yura Siamashka <yurand2@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include "plugindata.h"
24 #include "pluginmacros.h"
27 extern GeanyData
*geany_data
;
31 find_file_path(const gchar
* dir
, const gchar
* filename
)
36 gchar
*base_prev
= g_strdup(":");
40 while (strcmp(base
, base_prev
) != 0)
42 gitdir
= g_build_path("/", base
, filename
, NULL
);
43 ret
= g_file_test(gitdir
, G_FILE_TEST_IS_REGULAR
);
53 base
= g_path_get_dirname(base
);
61 /* Normalize a pathname. This collapses redundant separators and up-level references so that A//B, A/./B
62 * and A/foo/../B all become A/B. It does not normalize the case. On Windows, it converts forward
63 * slashes to backward slashes. It should be understood that this may change the meaning of the
64 * path if it contains symbolic links!
67 normpath(const gchar
* filename
)
75 if (!filename
|| strlen(filename
) == 0)
77 v
= g_strsplit_set(filename
, "/\\", -1);
78 if (!g_strv_length(v
))
81 out
= g_malloc0(sizeof(gchar
*) * (g_strv_length(v
) + 2));
84 if (filename
[0] == '.' && strcmp(v
[0], ".") == 0)
89 else if (filename
[0] == '/')
97 if (strcmp(*p
, ".") == 0 || strcmp(*p
, "") == 0)
101 else if (strcmp(*p
, "..") == 0)
106 if (strcmp(*pout
, "..") != 0)
115 *pout
++ = g_strdup(*p
);
118 ret
= g_build_filenamev(out
);
126 get_full_path(const gchar
* location
, const gchar
* path
)
130 dir
= g_path_get_dirname(location
);
131 setptr(dir
, g_build_filename(dir
, path
, NULL
));
132 setptr(dir
, normpath(dir
));
137 get_relative_path(const gchar
* location
, const gchar
* path
)
143 if (!g_path_is_absolute(path
))
145 return g_strdup(path
);
148 dir
= g_path_get_dirname(location
);
149 setptr(dir
, normpath(dir
));
154 if (strstr(path
, dir
) == path
)
158 setptr(dir
, g_strdup(path
+ strlen(dir
) + 1));
161 else if (plen
== dlen
)
163 return g_strdup("./");
171 save_config(GKeyFile
* config
, const gchar
* path
)
173 gchar
*data
= g_key_file_to_data(config
, NULL
, NULL
);
174 p_utils
->write_file(path
, data
);
179 config_length(GKeyFile
* config
, const gchar
* section
, const gchar
* name
)
184 key
= g_strdup_printf("%s%d", name
, i
);
185 while (g_key_file_has_key(config
, section
, key
, NULL
))
189 key
= g_strdup_printf("%s%d", name
, i
);
196 /* Gets a list of files from the specified directory.
197 * Locale encoding is expected for path and used for the file list.
198 * The list and the data in the list should be freed after use.
199 * Returns: The list or NULL if no files found.
200 * length will point to the number of non-NULL data items in the list, unless NULL.
201 * error is the location for storing a possible error, or NULL. */
203 get_file_list(const gchar
* path
, guint
* length
, gboolean(*func
) (const gchar
*), GError
** error
)
215 g_return_val_if_fail(path
!= NULL
, NULL
);
217 if (g_path_is_absolute(path
))
219 abs_path
= g_strdup(path
);
223 abs_path
= g_get_current_dir();
224 setptr(abs_path
, g_build_filename(abs_path
, path
, NULL
));
226 dir
= g_dir_open(abs_path
, 0, error
);
235 const gchar
*name
= g_dir_read_name(dir
);
239 filename
= g_build_filename(abs_path
, name
, NULL
);
241 if (g_file_test(filename
, G_FILE_TEST_IS_DIR
))
245 GSList
*lst
= get_file_list(filename
, &l
, func
, &err
);
249 g_slist_foreach(list
, (GFunc
) g_free
, NULL
);
256 list
= g_slist_concat(list
, lst
);
261 if (!func
|| func(filename
))
263 list
= g_slist_prepend(list
, filename
);