[GRASS-dev] r.colors: rules file list unsorted

Paul Kelly paul-grass at stjohnspoint.co.uk
Mon Jan 29 07:35:36 EST 2007


And for the sake of it, here's yet another proposed patch! Using the new 
G__ls function.

On Sun, 28 Jan 2007, Daniel Calvelo wrote:

> Ok, It's working now. Attached.
>
> Cheers,
>
> Daniel.
>
> On 1/28/07, Daniel Calvelo <dca.gis at gmail.com> wrote:
>> I'm debugging this patch now. Only to show you the idea; it's not
>> working currently.
>> 
>> BTW has anybody here experimented with extreme programming techniques?
>> I've always been intrigued by the approach, and this kind of on-line
>> concurrent patching weems to be rather along their principles...
>> 
>> On 1/28/07, Wolf Bergenheim <wolf+grass at bergenheim.net> wrote:
>> > Markus Neteler wrote:
>> > > Hi Wolf,
>> > >
>> > > wow, high speed programming! I cannot tell you anything,
>> > > most likely Glynn will have a comment or Brad.
>> > >
>> > > thanks anyway, please submit if Glynn doesn't object
>> > > (maybe give him 24 hours...) :-)
>> >
>> > Yeah, I'm eagerly waiting their comments. Daniel what is your approach?
>> >
>> > --Wolf
>> >
>> > --
>> >
>> > <:3 )---- Wolf Bergenheim ----( 8:>
>> >
>> > _______________________________________________
>> > grass-dev mailing list
>> > grass-dev at grass.itc.it
>> > http://grass.itc.it/mailman/listinfo/grass-dev
>> >
>> 
>> 
>> --
>> -- Daniel Calvelo Aros
>> 
>> 
>> 
>
>
> -- 
> -- Daniel Calvelo Aros
>
-------------- next part --------------
Index: raster/r.colors/main.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.colors/main.c,v
retrieving revision 2.11
diff -u -r2.11 main.c
--- raster/r.colors/main.c	17 Oct 2006 11:43:08 -0000	2.11
+++ raster/r.colors/main.c	29 Jan 2007 12:31:32 -0000
@@ -24,8 +24,8 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
-#include <dirent.h>
+#include <unistd.h>
+#include <errno.h>
 #include <grass/gis.h>
 #include "local_proto.h"
 #include <grass/glocale.h>
@@ -34,29 +34,25 @@
 static char *rules_files(void)
 {
 	char path[4096];
+	char **files;
+	int i, num_files;
+   
 	char *list = NULL;
 	int size = 0;
 	int len = 0;
-	DIR *dir;
 
 	sprintf(path, "%s/etc/colors", G_gisbase());
 
-	dir = opendir(path);
-	if (!dir)
+	if (access(path, R_OK))
 		return NULL;
+   
+	files = G__ls(path, &num_files);
 
-	for (;;)
+	for (i = 0; i < num_files; i++)
 	{
-		struct dirent *d = readdir(dir);
 		int n;
 
-		if (!d)
-			break;
-
-		if (d->d_name[0] == '.')
-			continue;
-
-		n = strlen(d->d_name);
+		n = strlen(files[i]);
 
 		if (size < len + n + 2)
 		{
@@ -64,63 +60,30 @@
 			list = G_realloc(list, size);
 		}
 
-		if (len > 0)
+		if (i > 0)
 			list[len++] = ',';
 
-		memcpy(&list[len], d->d_name, n + 1);
+		memcpy(&list[len], files[i], n + 1);
 		len += n;
 	}
 
-	closedir(dir);
-
 	return list;
 }
 
-static int cmp_names(const void *aa, const void *bb)
-{
-	char * const *a = aa;
-	char * const *b = bb;
-
-	return strcmp(*a, *b);
-}
-
 static void list_rules_files(void)
 {
-	static char **names;
-	static int names_size;
+	char **names;
 	char path[4096];
-	DIR *dir;
-	int names_len = 0;
+	int names_len;
 	int i;
 
 	sprintf(path, "%s/etc/colors", G_gisbase());
 
-	dir = opendir(path);
-	if (!dir)
-		G_fatal_error("Rules directory doesn't exist");
-
-	for (;;)
-	{
-		struct dirent *d = readdir(dir);
-
-		if (!d)
-			break;
-
-		if (d->d_name[0] == '.')
-			continue;
-
-		if (names_len >= names_size)
-		{
-			names_size = names_len + 20;
-			names = G_realloc(names, names_size * sizeof(char *));
-		}
-
-		names[names_len++] = G_store(d->d_name);
-	}
-
-	closedir(dir);
-
-	qsort(names, names_len, sizeof(char *), cmp_names);
+	if (access(path, R_OK))
+		G_fatal_error("Problem accessing rules directory: %s", 
+			      strerror(errno));
+   
+	names = G__ls(path, &names_len);
 
 	for (i = 0; i < names_len; i++)
 	{


More information about the grass-dev mailing list