Revision: 4264 http://geany.svn.sourceforge.net/geany/?rev=4264&view=rev Author: ntrel Date: 2009-09-29 12:10:17 +0000 (Tue, 29 Sep 2009)
Log Message: ----------- Add utils_get_file_list_full() which can optionally sort or include a full path for each list item.
Modified Paths: -------------- trunk/ChangeLog trunk/src/symbols.c trunk/src/templates.c trunk/src/utils.c trunk/src/utils.h
Modified: trunk/ChangeLog =================================================================== --- trunk/ChangeLog 2009-09-29 11:39:35 UTC (rev 4263) +++ trunk/ChangeLog 2009-09-29 12:10:17 UTC (rev 4264) @@ -4,6 +4,9 @@ Change Perl tag parser to ctags SVN r601. This removes support for buggy local/my/our but it parses constant/format/labels and should be less buggy overall (closes #2861232). + * src/templates.c, src/utils.c, src/utils.h, src/symbols.c: + Add utils_get_file_list_full() which can optionally sort or include + a full path for each list item.
2009-09-28 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
Modified: trunk/src/symbols.c =================================================================== --- trunk/src/symbols.c 2009-09-29 11:39:35 UTC (rev 4263) +++ trunk/src/symbols.c 2009-09-29 12:10:17 UTC (rev 4264) @@ -1444,18 +1444,6 @@ }
-static void utils_slist_add_path(GSList *list, const gchar *path) -{ - GSList *node; - - for (node = list; node != NULL; node = g_slist_next(node)) - { - setptr(node->data, - g_build_path(G_DIR_SEPARATOR_S, path, node->data, NULL)); - } -} - - static GHashTable *init_user_tags(void) { GSList *file_list = NULL, *list = NULL; @@ -1468,12 +1456,10 @@ { utils_mkdir(dir, FALSE); } - file_list = utils_get_file_list(dir, NULL, NULL); - utils_slist_add_path(file_list, dir); + file_list = utils_get_file_list_full(dir, TRUE, TRUE, NULL);
dir = utils_build_path(app->datadir, "tags", NULL); - list = utils_get_file_list(dir, NULL, NULL); - utils_slist_add_path(list, dir); + list = utils_get_file_list_full(dir, TRUE, TRUE, NULL); file_list = g_slist_concat(file_list, list);
lang_hash = get_tagfile_hash(file_list);
Modified: trunk/src/templates.c =================================================================== --- trunk/src/templates.c 2009-09-29 11:39:35 UTC (rev 4263) +++ trunk/src/templates.c 2009-09-29 12:10:17 UTC (rev 4264) @@ -529,24 +529,15 @@ { gchar *path = g_build_path(G_DIR_SEPARATOR_S, app->configdir, GEANY_TEMPLATES_SUBDIR, "files", NULL); - GSList *list = NULL; - GDir *dir; - const gchar *fname; + GSList *list = utils_get_file_list_full(path, FALSE, FALSE, NULL);
- dir = g_dir_open(path, 0, NULL); - if (dir) + if (!list) { - foreach_dir(fname, dir) - list = g_slist_append(list, g_strdup(fname)); - g_dir_close(dir); - } - if (!dir || !list) - { utils_mkdir(path, FALSE); return FALSE; } list = g_slist_sort(list, compare_filenames_by_filetype); - g_slist_foreach(list, add_file_item, toolbar_new_file_menu); + g_slist_foreach(list, add_file_item, NULL); g_slist_foreach(list, (GFunc) g_free, NULL); g_slist_free(list); g_free(path); @@ -563,14 +554,14 @@ /* we hold our own ref on the menu in case it is not used in the toolbar */ g_object_ref(toolbar_new_file_menu);
- create_new_menu_items(toolbar_new_file_menu); + create_new_menu_items();
sep1 = gtk_separator_menu_item_new(); gtk_container_add(GTK_CONTAINER(new_with_template_menu), sep1); sep2 = gtk_separator_menu_item_new(); gtk_container_add(GTK_CONTAINER(toolbar_new_file_menu), sep2);
- if (add_custom_template_items(toolbar_new_file_menu)) + if (add_custom_template_items()) { gtk_widget_show(sep1); gtk_widget_show(sep2);
Modified: trunk/src/utils.c =================================================================== --- trunk/src/utils.c 2009-09-29 11:39:35 UTC (rev 4263) +++ trunk/src/utils.c 2009-09-29 12:10:17 UTC (rev 4264) @@ -1341,36 +1341,35 @@ }
-/** - * Gets a sorted list of files from the specified directory. +/* + * Gets a list of files from the specified directory. * Locale encoding is expected for @a path and used for the file list. The list and the data * in the list should be freed after use, e.g.: * @code * g_slist_foreach(list, (GFunc) g_free, NULL); * g_slist_free(list); @endcode * - * @note If you don't want sorted filenames you should use the foreach_dir() macro instead - - * it's more efficient and perhaps easier to use than freeing the list and its contents. + * @note If you don't need a list you should use the foreach_dir() macro instead - + * it's more efficient. * * @param path The path of the directory to scan, in locale encoding. - * @param length The location to store the number of non-@c NULL data items in the list, - * unless @c NULL. + * @param full_path Whether to include the full path for each filename in the list. Obviously this + * will use more memory. + * @param sort Whether to sort alphabetically (UTF-8 safe). * @param error The location for storing a possible error, or @c NULL. * - * @return A newly allocated list or @c NULL if no files found. The list and its data should be - * freed when no longer needed. + * @return A newly allocated list or @c NULL if no files were found. The list and its data should be + * freed when no longer needed. + * @see utils_get_file_list(). **/ -GSList *utils_get_file_list(const gchar *path, guint *length, GError **error) +GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error) { GSList *list = NULL; - guint len = 0; GDir *dir; const gchar *filename;
if (error) *error = NULL; - if (length) - *length = 0; g_return_val_if_fail(path != NULL, NULL);
dir = g_dir_open(path, 0, error); @@ -1379,15 +1378,40 @@
foreach_dir(filename, dir) { - list = g_slist_append(list, g_strdup(filename)); - len++; + list = g_slist_append(list, full_path ? + g_build_path(G_DIR_SEPARATOR_S, path, filename, NULL) : g_strdup(filename)); } g_dir_close(dir); /* sorting last is quicker than on insertion */ - list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp); + if (sort) + list = g_slist_sort(list, (GCompareFunc) utils_str_casecmp); + return list; +}
+ +/** + * Gets a sorted list of files from the specified directory. + * Locale encoding is expected for @a path and used for the file list. The list and the data + * in the list should be freed after use, e.g.: + * @code + * g_slist_foreach(list, (GFunc) g_free, NULL); + * g_slist_free(list); @endcode + * + * @param path The path of the directory to scan, in locale encoding. + * @param length The location to store the number of non-@c NULL data items in the list, + * unless @c NULL. + * @param error The location for storing a possible error, or @c NULL. + * + * @return A newly allocated list or @c NULL if no files were found. The list and its data should be + * freed when no longer needed. + * @see utils_get_file_list_full(). + **/ +GSList *utils_get_file_list(const gchar *path, guint *length, GError **error) +{ + GSList *list = utils_get_file_list_full(path, FALSE, TRUE, error); + if (length) - *length = len; + *length = g_slist_length(list); return list; }
Modified: trunk/src/utils.h =================================================================== --- trunk/src/utils.h 2009-09-29 11:39:35 UTC (rev 4263) +++ trunk/src/utils.h 2009-09-29 12:10:17 UTC (rev 4264) @@ -215,4 +215,6 @@
gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
+GSList *utils_get_file_list_full(const gchar *path, gboolean full_path, gboolean sort, GError **error); + #endif
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.