[GRASS-SVN] r33418 - in grass/branches/develbranch_6: include
lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Sat Sep 13 01:59:50 EDT 2008
Author: neteler
Date: 2008-09-13 01:59:50 -0400 (Sat, 13 Sep 2008)
New Revision: 33418
Modified:
grass/branches/develbranch_6/include/gisdefs.h
grass/branches/develbranch_6/lib/gis/ls.c
Log:
glynn: Add G_set_ls_filter() (merge from trunk, r33265)
Modified: grass/branches/develbranch_6/include/gisdefs.h
===================================================================
--- grass/branches/develbranch_6/include/gisdefs.h 2008-09-12 20:10:33 UTC (rev 33417)
+++ grass/branches/develbranch_6/include/gisdefs.h 2008-09-13 05:59:50 UTC (rev 33418)
@@ -754,6 +754,7 @@
char *G__location_path(void);
/* ls.c */
+void G_set_ls_filter(int (*)(const char *, void *), void *);
char **G__ls(const char *, int *);
void G_ls(const char *, FILE *);
void G_ls_format(char **, int, int, FILE *);
Modified: grass/branches/develbranch_6/lib/gis/ls.c
===================================================================
--- grass/branches/develbranch_6/lib/gis/ls.c 2008-09-12 20:10:33 UTC (rev 33417)
+++ grass/branches/develbranch_6/lib/gis/ls.c 2008-09-13 05:59:50 UTC (rev 33418)
@@ -28,7 +28,11 @@
# include <sys/ioctl.h>
#endif
+typedef int ls_filter_func(const char * /*filename */ , void * /*closure */ );
+static ls_filter_func *ls_filter;
+static void *ls_closure;
+
static int cmp_names(const void *aa, const void *bb)
{
char *const *a = (char *const *)aa;
@@ -38,6 +42,25 @@
}
/**
+ * \brief Sets a function and its complementary data for G__ls filtering.
+ *
+ * Defines a filter function and its rule data that allow G__ls to filter out
+ * unwanted file names. Call this function before G__ls.
+ *
+ * \param func Filter callback function to compare a file name and closure
+ * pattern (if NULL, no filter will be used).
+ * func(filename, closure) should return 1 on success, 0 on
+ * failure.
+ * \param closure Data used to determine if a file name matches the rule.
+ **/
+
+void G_set_ls_filter(ls_filter_func *func, void *closure)
+{
+ ls_filter = func;
+ ls_closure = closure;
+}
+
+/**
* \brief Stores a sorted directory listing in an array
*
* The filenames in the specified directory are stored in an array of
@@ -64,12 +87,13 @@
G_fatal_error(_("Unable to open directory %s"), dir);
while ((dp = readdir(dfd)) != NULL) {
- if (dp->d_name[0] != '.') { /* Don't list hidden files */
- dir_listing = (char **)G_realloc(dir_listing,
- (1 + n) * sizeof(char *));
- dir_listing[n] = G_store(dp->d_name);
- n++;
- }
+ if (dp->d_name[0] == '.') /* Don't list hidden files */
+ continue;
+ if (ls_filter && !(*ls_filter)(dp->d_name, ls_closure))
+ continue;
+ dir_listing = (char **)G_realloc(dir_listing, (1 + n) * sizeof(char *));
+ dir_listing[n] = G_store(dp->d_name);
+ n++;
}
/* Sort list of filenames alphabetically */
More information about the grass-commit
mailing list