[GRASS-SVN] r33265 - in grass/trunk: include lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Sep 4 18:23:24 EDT 2008


Author: glynn
Date: 2008-09-04 18:23:23 -0400 (Thu, 04 Sep 2008)
New Revision: 33265

Modified:
   grass/trunk/include/gisdefs.h
   grass/trunk/lib/gis/ls.c
Log:
Add G_set_ls_filter()


Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h	2008-09-04 21:17:42 UTC (rev 33264)
+++ grass/trunk/include/gisdefs.h	2008-09-04 22:23:23 UTC (rev 33265)
@@ -702,6 +702,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/trunk/lib/gis/ls.c
===================================================================
--- grass/trunk/lib/gis/ls.c	2008-09-04 21:17:42 UTC (rev 33264)
+++ grass/trunk/lib/gis/ls.c	2008-09-04 22:23:23 UTC (rev 33265)
@@ -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