[Fwd: Re: [GRASS-dev] r.colors: rules file list unsorted]
Wolf Bergenheim
wolf+grass at bergenheim.net
Sun Jan 28 15:37:48 EST 2007
ok to commit the attached diff?
It uses scandir() to sort the directory entries alphabetically. I'm not
100% sure scandir can be found on all platforms in which case we need to
do something, like qsort() the names list instead.
What do you think?
--Wolf
--
<:3 )---- Wolf Bergenheim ----( 8:>
--
<:3 )---- Wolf Bergenheim ----( 8:>
-------------- next part --------------
Index: main.c
===================================================================
RCS file: /grassrepository/grass6/raster/r.colors/main.c,v
retrieving revision 2.11
diff -u -r2.11 main.c
--- main.c 17 Oct 2006 11:43:08 -0000 2.11
+++ main.c 28 Jan 2007 20:29:49 -0000
@@ -38,25 +38,26 @@
int size = 0;
int len = 0;
DIR *dir;
+ struct dirent **d;
+ int i,l;
sprintf(path, "%s/etc/colors", G_gisbase());
- dir = opendir(path);
- if (!dir)
+ l = scandir(path, &d, 0, alphasort);
+
+ if (l < 0)
return NULL;
- for (;;)
+ for(i=0;i < l; i++)
{
- struct dirent *d = readdir(dir);
int n;
-
- if (!d)
- break;
-
- if (d->d_name[0] == '.')
+ if (d[i]->d_name[0] == '.')
+ {
+ G_free(d[i]);
continue;
+ }
- n = strlen(d->d_name);
+ n = strlen(d[i]->d_name);
if (size < len + n + 2)
{
@@ -67,11 +68,11 @@
if (len > 0)
list[len++] = ',';
- memcpy(&list[len], d->d_name, n + 1);
+ memcpy(&list[len], d[i]->d_name, n + 1);
len += n;
+ G_free(d[i]);
}
-
- closedir(dir);
+ G_free(d);
return list;
}
@@ -91,23 +92,22 @@
char path[4096];
DIR *dir;
int names_len = 0;
- int i;
+ int i,n;
+ struct dirent **d;
sprintf(path, "%s/etc/colors", G_gisbase());
- dir = opendir(path);
- if (!dir)
+ n = scandir(path, &d, 0, alphasort);
+ if (n < 0)
G_fatal_error("Rules directory doesn't exist");
- for (;;)
+ for (i=0;i<n;i++)
{
- struct dirent *d = readdir(dir);
-
- if (!d)
- break;
-
- if (d->d_name[0] == '.')
+ if (d[i]->d_name[0] == '.')
+ {
+ G_free(d[i]);
continue;
+ }
if (names_len >= names_size)
{
@@ -115,12 +115,10 @@
names = G_realloc(names, names_size * sizeof(char *));
}
- names[names_len++] = G_store(d->d_name);
+ names[names_len++] = G_store(d[i]->d_name);
+ G_free(d[i]);
}
-
- closedir(dir);
-
- qsort(names, names_len, sizeof(char *), cmp_names);
+ G_free(d);
for (i = 0; i < names_len; i++)
{
More information about the grass-dev
mailing list