[GRASS-SVN] r48504 - grass/trunk/display/d.vect

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Sep 27 15:04:27 EDT 2011


Author: martinl
Date: 2011-09-27 12:04:27 -0700 (Tue, 27 Sep 2011)
New Revision: 48504

Modified:
   grass/trunk/display/d.vect/main.c
Log:
d.vect: sort symbols


Modified: grass/trunk/display/d.vect/main.c
===================================================================
--- grass/trunk/display/d.vect/main.c	2011-09-27 18:08:42 UTC (rev 48503)
+++ grass/trunk/display/d.vect/main.c	2011-09-27 19:04:27 UTC (rev 48504)
@@ -25,53 +25,9 @@
 #include "plot.h"
 #include "local_proto.h"
 
-/* adopted from r.colors */
-static char *icon_files(void)
-{
-    char *list = NULL, path[4096], path_i[4096];
-    size_t len = 0, l;
-    DIR *dir, *dir_i;
-    struct dirent *d, *d_i;
+static int cmp(const void *, const void *);
+static char *icon_files(void);
 
-    sprintf(path, "%s/etc/symbol", G_gisbase());
-
-    dir = opendir(path);
-    if (!dir)
-	return NULL;
-
-    /*loop over etc/symbol */
-    while ((d = readdir(dir))) {
-	if (d->d_name[0] == '.')
-	    continue;
-
-	sprintf(path_i, "%s/etc/symbol/%s", G_gisbase(), d->d_name);
-	dir_i = opendir(path_i);
-
-	if (!dir_i)
-	    continue;
-
-	/*loop over each directory in etc/symbols */
-	while ((d_i = readdir(dir_i))) {
-	    if (d_i->d_name[0] == '.')
-		continue;
-
-	    l = strlen(d->d_name) + strlen(d_i->d_name) + 3;
-	    list = G_realloc(list, len + l);
-	    sprintf(list + len, "%s/%s,", d->d_name, d_i->d_name);
-	    len += l - 1;
-	}
-
-	closedir(dir_i);
-    }
-
-    closedir(dir);
-
-    if (len)
-	list[len - 1] = 0;
-
-    return list;
-}
-
 int main(int argc, char **argv)
 {
     int ret, level;
@@ -527,3 +483,78 @@
     G_done_msg(" ");
     exit(EXIT_SUCCESS);
 }
+
+int cmp(const void *a, const void *b) 
+{
+    return (strcmp(*(char **)a, *(char **)b));
+}
+
+/* adopted from r.colors */
+char *icon_files(void)
+{
+    char **list, *ret;
+    char buf[GNAME_MAX], path[GPATH_MAX], path_i[GPATH_MAX];
+    int i, count;
+    size_t len;
+    DIR *dir, *dir_i;
+    struct dirent *d, *d_i;
+
+    list = NULL;
+    len = 0;
+    sprintf(path, "%s/etc/symbol", G_gisbase());
+
+    dir = opendir(path);
+    if (!dir)
+	return NULL;
+
+    count = 0;
+    
+    /* loop over etc/symbol */
+    while ((d = readdir(dir))) {
+	if (d->d_name[0] == '.')
+	    continue;
+
+	sprintf(path_i, "%s/etc/symbol/%s", G_gisbase(), d->d_name);
+	dir_i = opendir(path_i);
+
+	if (!dir_i)
+	    continue;
+
+	/* loop over each directory in etc/symbols */
+	while ((d_i = readdir(dir_i))) {
+	    if (d_i->d_name[0] == '.')
+		continue;
+	    
+	    list = G_realloc(list, (count + 1) * sizeof(char *));
+	    
+	    sprintf(buf, "%s/%s", d->d_name, d_i->d_name);
+	    list[count++] = G_store(buf);
+	    
+	    len += strlen(d->d_name) + strlen(d_i->d_name) + 2; /* '/' + ',' */
+	}
+
+	closedir(dir_i);
+    }
+
+    closedir(dir);
+
+    qsort(list, count, sizeof(char *), cmp);
+    
+    if (len > 0) {
+	ret = G_malloc((len + 1) * sizeof(char)); /* \0 */
+	*ret = '\0';
+	for (i = 0; i < count; i++) {
+	    if (i > 0)
+		strcat(ret, ",");
+	    strcat(ret, list[i]);
+	    G_free(list[i]);
+	}
+	G_free(list);
+    }
+    else {
+	ret = G_store("");
+    }
+    
+    return ret;
+}
+



More information about the grass-commit mailing list