فهرست منبع

Add G_set_ls_filter()

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@33265 15284696-431f-4ddb-bdfa-cd5b030d7da7
Glynn Clements 16 سال پیش
والد
کامیت
9eb223f619
2فایلهای تغییر یافته به همراه31 افزوده شده و 6 حذف شده
  1. 1 0
      include/gisdefs.h
  2. 30 6
      lib/gis/ls.c

+ 1 - 0
include/gisdefs.h

@@ -702,6 +702,7 @@ char *G_location(void);
 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 *);

+ 30 - 6
lib/gis/ls.c

@@ -28,6 +28,10 @@
 #  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)
 {
@@ -38,6 +42,25 @@ static int cmp_names(const void *aa, const void *bb)
 }
 
 /**
+ * \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 @@ char **G__ls(const char *dir, int *num_files)
 	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 */