[GRASS-SVN] r65555 - grass/trunk/general/g.list

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 9 10:40:39 PDT 2015


Author: hcho
Date: 2015-07-09 10:40:39 -0700 (Thu, 09 Jul 2015)
New Revision: 65555

Added:
   grass/trunk/general/g.list/global.h
   grass/trunk/general/g.list/list.c
Modified:
   grass/trunk/general/g.list/main.c
Log:
g.list: Split out list functions

Added: grass/trunk/general/g.list/global.h
===================================================================
--- grass/trunk/general/g.list/global.h	                        (rev 0)
+++ grass/trunk/general/g.list/global.h	2015-07-09 17:40:39 UTC (rev 65555)
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <grass/gis.h>
+#include <grass/manage.h>
+
+enum {
+    TYPE_RAST,
+    TYPE_RAST3D,
+    TYPE_VECT,
+    TYPE_OTHERS
+};
+
+struct elist
+{
+    char *type;
+    char *name;
+    char *mapset;
+};
+
+void make_list(struct elist **, int *, int *, const struct list *, const char *,
+	       const struct Cell_head *);
+void print_list(FILE *, struct elist *, int, const char *, int, int);

Added: grass/trunk/general/g.list/list.c
===================================================================
--- grass/trunk/general/g.list/list.c	                        (rev 0)
+++ grass/trunk/general/g.list/list.c	2015-07-09 17:40:39 UTC (rev 65555)
@@ -0,0 +1,171 @@
+#include "global.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <grass/gis.h>
+#include <grass/raster3d.h>
+#include <grass/vector.h>
+#include <grass/manage.h>
+#include <grass/glocale.h>
+
+static int region_overlaps(const struct Cell_head *, const char *, const char *,
+			   int);
+
+void make_list(struct elist **el, int *lcount, int *lalloc,
+	       const struct list *elem, const char *mapset,
+	       const struct Cell_head *window)
+{
+    char path[GPATH_MAX];
+    const char *element, *alias;
+    char **list;
+    int count, i;
+    int type;
+
+    element = elem->element[0];
+    alias = elem->alias;
+
+    G_file_name(path, element, "", mapset);
+    if (access(path, 0) != 0)
+	return;
+
+    if ((list = G_ls2(path, &count)) == NULL)
+	return;
+
+    if (strcmp(alias, "raster") == 0)
+	type = TYPE_RAST;
+    else if (strcmp(alias, "raster_3d") == 0)
+	type = TYPE_RAST3D;
+    else if (strcmp(alias, "vector") == 0)
+	type = TYPE_VECT;
+    else
+	type = TYPE_OTHERS;
+
+    /* Suppress "... found in more mapsets" warnings from G_find_file2. */
+    G_suppress_warnings(1);
+
+    if (*lcount + count > *lalloc) {
+	*lalloc = *lcount + count + 10;
+	*el = G_realloc(*el, *lalloc * sizeof(struct elist));
+    }
+
+    for (i = 0; i < count; i++) {
+
+	/* If region= is used, read the map region. */
+	if (window) {
+	    /* If the map region doesn't overlap with the input region, don't
+	     * print the map. */
+	    if (!region_overlaps(window, list[i], mapset, type))
+		continue;
+	}
+
+	(*el)[*lcount].type = G_store(alias);
+	(*el)[*lcount].name = list[i];
+	(*el)[*lcount].mapset = G_store(mapset);
+	(*lcount)++;
+    }
+
+    G_suppress_warnings(0);
+
+    G_free(list);
+}
+
+int cmp(const void *a, const void *b)
+{
+    struct elist *al = (struct elist *)a;
+    struct elist *bl = (struct elist *)b;
+    int ret;
+
+    if (!(ret = strcmp(al->type, bl->type))) {
+	if (!(ret = strcmp(al->name, bl->name)))
+	    ret = strcmp(al->mapset, bl->mapset);
+    }
+
+    return ret;
+}
+
+void print_list(FILE *fp, struct elist *el, int count, const char *separator,
+		int add_type, int add_mapset)
+{
+    int i;
+
+    if (!count)
+	return;
+
+    qsort(el, count, sizeof(struct elist), cmp);
+
+    for (i = 0; i < count; i++) {
+	int need_mapset = 0;
+
+	if (i != 0)
+	    fprintf(fp, "%s", separator);
+
+	if (add_type)
+	    fprintf(fp, "%s/", el[i].type);
+
+	fprintf(fp, "%s", el[i].name);
+
+	if (!add_mapset) {
+	    if (i + 1 < count)
+		need_mapset = strcmp(el[i].name, el[i + 1].name) == 0;
+	    if (!need_mapset && i > 0)
+		need_mapset = strcmp(el[i].name, el[i - 1].name) == 0;
+	}
+	if (add_mapset || need_mapset)
+	    fprintf(fp, "@%s", el[i].mapset);
+    }
+
+    fflush(fp);
+}
+
+static int region_overlaps(const struct Cell_head *window, const char *name,
+			   const char *mapset, int type)
+{
+    int has_region;
+    struct Cell_head map_window;
+    RASTER3D_Region region3d;
+    struct Map_info Map;
+    struct bound_box box;
+
+    switch (type) {
+    case TYPE_RAST:
+	Rast_get_cellhd(name, mapset, &map_window);
+	has_region = 1;
+	break;
+    case TYPE_RAST3D:
+	if (Rast3d_read_region_map(name, mapset, &region3d) < 0)
+	    G_fatal_error(_("Unable to read header of 3D raster map <%s@%s>"),
+			  name, mapset);
+	Rast3d_region_to_cell_head(&region3d, &map_window);
+	has_region = 1;
+	break;
+    case TYPE_VECT:
+	Vect_set_open_level(2);
+	if (Vect_open_old_head(&Map, name, mapset) < 2)
+	    G_fatal_error(_("Unable to open vector map <%s@%s> on topological level"),
+			  name, mapset);
+	Vect_get_map_box(&Map, &box);
+	Vect_close(&Map);
+
+	map_window.north = box.N;
+	map_window.south = box.S;
+	map_window.west = box.W;
+	map_window.east = box.E;
+	has_region = 1;
+	break;
+    default:
+	has_region = 0;
+	break;
+    }
+
+    /* If an element doesn't have a concept of region at all, return 1 so we
+     * can always print it. */
+    if (!has_region)
+	return 1;
+
+    /* If the map region is outside the input region, return 0. Otherwise
+     * return 1 */
+    return !(window->north <= map_window.south ||
+	     window->south >= map_window.north ||
+	     window->west >= map_window.east ||
+	     window->east <= map_window.west);
+}

Modified: grass/trunk/general/g.list/main.c
===================================================================
--- grass/trunk/general/g.list/main.c	2015-07-09 13:28:34 UTC (rev 65554)
+++ grass/trunk/general/g.list/main.c	2015-07-09 17:40:39 UTC (rev 65555)
@@ -17,39 +17,16 @@
  *
  *****************************************************************************/
 
+#define _MAIN_C_
+#include "global.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <grass/gis.h>
-#include <grass/raster3d.h>
-#include <grass/vector.h>
 #include <grass/manage.h>
 #include <grass/glocale.h>
 #include <grass/spawn.h>
 
-enum {
-    TYPE_RAST,
-    TYPE_RAST3D,
-    TYPE_VECT,
-    TYPE_OTHERS
-};
-
-struct elist
-{
-    char *type;
-    char *name;
-    char *mapset;
-};
-
-static int any = 0;
-
-static void make_list(struct elist **, int *, int *, 
-                      const struct list *, const char *,
-		      struct Cell_head *);
-static void print_list(FILE *, struct elist *, int,
-		      const char *, int, int);
-static int region_overlaps(struct Cell_head *, const char *, const char *, int);
-
 int main(int argc, char *argv[])
 {
     struct GModule *module;
@@ -370,7 +347,7 @@
 
     if (flag.pretty->answer || flag.full->answer)
 	fclose(stdout);
-    else if (any)
+    else if (lcount)
 	fprintf(fp, "\n");
 
     if (use_pager)
@@ -386,164 +363,3 @@
 
     exit(EXIT_SUCCESS);
 }
-
-static void make_list(struct elist **el, int *lcount, int *lalloc, 
-                      const struct list *elem, const char *mapset,
-		      struct Cell_head *window)
-{
-    char path[GPATH_MAX];
-    const char *element, *alias;
-    char **list;
-    int count, i;
-    int type;
-
-    element = elem->element[0];
-    alias = elem->alias;
-
-    G_file_name(path, element, "", mapset);
-    if (access(path, 0) != 0)
-	return;
-
-    if ((list = G_ls2(path, &count)) == NULL)
-	return;
-
-    if (strcmp(alias, "raster") == 0)
-	type = TYPE_RAST;
-    else if (strcmp(alias, "raster_3d") == 0)
-	type = TYPE_RAST3D;
-    else if (strcmp(alias, "vector") == 0)
-	type = TYPE_VECT;
-    else
-	type = TYPE_OTHERS;
-
-    /* Suppress "... found in more mapsets" warnings from G_find_file2. */
-    G_suppress_warnings(1);
-
-    if (*lcount + count > *lalloc) {
-	*lalloc = *lcount + count + 10;
-	*el = G_realloc(*el, *lalloc * sizeof(struct elist));
-    }
-
-    for (i = 0; i < count; i++) {
-
-	/* If region= is used, read the map region. */
-	if (window) {
-	    /* If the map region doesn't overlap with the input region, don't
-	     * print the map. */
-	    if (!region_overlaps(window, list[i], mapset, type))
-		continue;
-	}
-
-	(*el)[*lcount].type = G_store(alias);
-	(*el)[*lcount].name = list[i];
-	(*el)[*lcount].mapset = G_store(mapset);
-	(*lcount)++;
-
-	any++;
-    }
-
-    G_suppress_warnings(0);
-
-    G_free(list);
-}
-
-int cmp(const void *a, const void *b)
-{
-    struct elist *al = (struct elist *)a;
-    struct elist *bl = (struct elist *)b;
-    int ret;
-
-    if (!(ret = strcmp(al->type, bl->type))) {
-	if (!(ret = strcmp(al->name, bl->name)))
-	    ret = strcmp(al->mapset, bl->mapset);
-    }
-
-    return ret;
-}
-
-static void print_list(FILE *fp, struct elist *el, int count,
-		      const char *separator, int add_type, int add_mapset)
-{
-    int i;
-
-    if (!count)
-	return;
-
-    qsort(el, count, sizeof(struct elist), cmp);
-
-    for (i = 0; i < count; i++) {
-	int need_mapset = 0;
-
-	if (i != 0)
-	    fprintf(fp, "%s", separator);
-
-	if (add_type)
-	    fprintf(fp, "%s/", el[i].type);
-
-	fprintf(fp, "%s", el[i].name);
-
-	if (!add_mapset) {
-	    if (i + 1 < count)
-		need_mapset = strcmp(el[i].name, el[i + 1].name) == 0;
-	    if (!need_mapset && i > 0)
-		need_mapset = strcmp(el[i].name, el[i - 1].name) == 0;
-	}
-	if (add_mapset || need_mapset)
-	    fprintf(fp, "@%s", el[i].mapset);
-    }
-
-    fflush(fp);
-}
-
-static int region_overlaps(struct Cell_head *window, const char *name,
-			   const char *mapset, int type)
-{
-    int has_region;
-    struct Cell_head map_window;
-    RASTER3D_Region region3d;
-    struct Map_info Map;
-    struct bound_box box;
-
-    switch (type) {
-    case TYPE_RAST:
-	Rast_get_cellhd(name, mapset, &map_window);
-	has_region = 1;
-	break;
-    case TYPE_RAST3D:
-	if (Rast3d_read_region_map(name, mapset, &region3d) < 0)
-	    G_fatal_error(_("Unable to read header of 3D raster map <%s@%s>"),
-			  name, mapset);
-	Rast3d_region_to_cell_head(&region3d, &map_window);
-	has_region = 1;
-	break;
-    case TYPE_VECT:
-	Vect_set_open_level(2);
-	if (Vect_open_old_head(&Map, name, mapset) < 2)
-	    G_fatal_error(_("Unable to open vector map <%s@%s> on topological level"),
-			  name, mapset);
-	Vect_get_map_box(&Map, &box);
-	Vect_close(&Map);
-
-	map_window.north = box.N;
-	map_window.south = box.S;
-	map_window.west = box.W;
-	map_window.east = box.E;
-	has_region = 1;
-	break;
-    default:
-	has_region = 0;
-	break;
-    }
-
-    /* If an element doesn't have a concept of region at all, return 1 so we
-     * can always print it. */
-    if (!has_region)
-	return 1;
-
-    /* If the map region is outside the input region, return 0. Otherwise
-     * return 1 */
-    return !(window->north <= map_window.south ||
-	     window->south >= map_window.north ||
-	     window->west >= map_window.east ||
-	     window->east <= map_window.west);
-}



More information about the grass-commit mailing list