[GRASS-SVN] r55401 - in grass/trunk: include/defs lib/vector/Vlib lib/vector/diglib

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Mar 16 07:40:42 PDT 2013


Author: martinl
Date: 2013-03-16 07:40:42 -0700 (Sat, 16 Mar 2013)
New Revision: 55401

Modified:
   grass/trunk/include/defs/vector.h
   grass/trunk/lib/vector/Vlib/build.c
   grass/trunk/lib/vector/Vlib/cindex.c
   grass/trunk/lib/vector/Vlib/close.c
   grass/trunk/lib/vector/Vlib/field.c
   grass/trunk/lib/vector/Vlib/header.c
   grass/trunk/lib/vector/Vlib/init_head.c
   grass/trunk/lib/vector/Vlib/local_proto.h
   grass/trunk/lib/vector/Vlib/map.c
   grass/trunk/lib/vector/Vlib/open.c
   grass/trunk/lib/vector/Vlib/open_nat.c
   grass/trunk/lib/vector/diglib/head.c
Log:
vlib: Vect_open_tmp_new() implemented
      basic support for temporary vector maps (work in progress)


Modified: grass/trunk/include/defs/vector.h
===================================================================
--- grass/trunk/include/defs/vector.h	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/include/defs/vector.h	2013-03-16 14:40:42 UTC (rev 55401)
@@ -223,6 +223,7 @@
 int Vect_open_old_head(struct Map_info *, const char *, const char *);
 int Vect_open_old_head2(struct Map_info *, const char *, const char *, const char *);
 int Vect_open_new(struct Map_info *, const char *, int);
+int Vect_open_tmp_new(struct Map_info *, const char *, int);
 int Vect_open_update(struct Map_info *, const char *, const char *);
 int Vect_open_update2(struct Map_info *, const char *, const char *, const char *);
 int Vect_open_update_head(struct Map_info *, const char *, const char *);

Modified: grass/trunk/lib/vector/Vlib/build.c
===================================================================
--- grass/trunk/lib/vector/Vlib/build.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/build.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -23,6 +23,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 #define SEP "-----------------------------------\n"
 
 #if !defined HAVE_OGR || !defined HAVE_POSTGRES
@@ -987,17 +989,18 @@
 int Vect_save_topo(struct Map_info *Map)
 {
     struct Plus_head *plus;
-    char buf[GPATH_MAX];
+    char *path;
     struct gvfile fp;
 
     G_debug(1, "Vect_save_topo()");
 
+    /*  write out all the accumulated info to the plus file  */
     plus = &(Map->plus);
+    dig_file_init(&fp);
 
-    /*  write out all the accumulated info to the plus file  */
-    sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-    dig_file_init(&fp);
-    fp.file = G_fopen_new(buf, GV_TOPO_ELEMENT);
+    path = Vect__get_path(Map);
+    fp.file = G_fopen_new(path, GV_TOPO_ELEMENT);
+    G_free(path);
     if (fp.file == NULL) {
 	G_warning(_("Unable to create topo file for vector map <%s>"), Map->name);
 	return 0;
@@ -1224,7 +1227,7 @@
 int Vect_save_sidx(struct Map_info *Map)
 {
     struct Plus_head *plus;
-    char fname[GPATH_MAX], buf[GPATH_MAX];
+    char *file_path;
 
     G_debug(1, "Vect_save_spatial_index()");
 
@@ -1238,14 +1241,14 @@
     /* new or update mode ? */
     if (plus->Spidx_new == TRUE) {
 	/*  write out rtrees to sidx file  */
-	sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-	G_file_name(fname, buf, GV_SIDX_ELEMENT, Map->mapset);
-	G_debug(1, "Open sidx: %s", fname);
+        file_path = Vect__get_element_path(Map, GV_SIDX_ELEMENT);
+	G_debug(1, "Open sidx: %s", file_path);
 	dig_file_init(&(plus->spidx_fp));
-	plus->spidx_fp.file = fopen(fname, "w+");
+	plus->spidx_fp.file = fopen(file_path, "w+");
+        G_free(file_path);
 	if (plus->spidx_fp.file == NULL) {
-	    G_warning(_("Unable open spatial index file for write <%s>"),
-		      fname);
+	    G_warning(_("Unable to create spatial index file for vector map <%s>"),
+		      Vect_get_name(Map));
 	    return 0;
 	}
 

Modified: grass/trunk/lib/vector/Vlib/cindex.c
===================================================================
--- grass/trunk/lib/vector/Vlib/cindex.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/cindex.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -21,6 +21,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 static int cmp_cat(const void *pa, const void *pb);
 
 static void check_status(const struct Map_info *Map)
@@ -443,21 +445,22 @@
 int Vect_cidx_save(struct Map_info *Map)
 {
     struct Plus_head *plus;
-    char fname[1024], buf[1024];
+    char *path;
     struct gvfile fp;
 
     G_debug(2, "Vect_cidx_save()");
     check_status(Map);
 
     plus = &(Map->plus);
-
-    sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-    G_file_name(fname, buf, GV_CIDX_ELEMENT, Map->mapset);
-    G_debug(2, "Open cidx: %s", fname);
+    
     dig_file_init(&fp);
-    fp.file = fopen(fname, "w");
+    
+    path = Vect__get_path(Map);
+    fp.file = G_fopen_new(path, GV_CIDX_ELEMENT);
+    G_free(path);
     if (fp.file == NULL) {
-	G_warning(_("Unable to open cidx file <%s> for write"), fname);
+	G_warning(_("Unable to create category index file for vector map <%s>"),
+                  Vect_get_name(Map));
 	return 1;
     }
 
@@ -465,7 +468,7 @@
     dig_init_portable(&(plus->cidx_port), dig__byte_order_out());
 
     if (0 > dig_write_cidx(&fp, plus)) {
-	G_warning(_("Error writing out category index file <%s>"), fname);
+	G_warning(_("Error writing out category index file"));
 	return 1;
     }
 

Modified: grass/trunk/lib/vector/Vlib/close.c
===================================================================
--- grass/trunk/lib/vector/Vlib/close.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/close.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -24,6 +24,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 static int clo_dummy()
 {
     return -1;
@@ -75,8 +77,8 @@
     int create_link; /* used for external formats only */
     struct Coor_info CInfo;
     
-    G_debug(1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
-	    Map->name, Map->mapset, Map->format, Map->level);
+    G_debug(1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d, is_tmp = %d",
+	    Map->name, Map->mapset, Map->format, Map->level, Map->temporary);
     
     /* check for external formats whether to create a link */
     create_link = TRUE;
@@ -190,6 +192,10 @@
 	}
     }
 
+    if (Map->temporary) {
+        Vect__delete(Map->name, TRUE);
+    }
+
     G_free(Map->name);
     G_free(Map->mapset);
     G_free(Map->location);
@@ -257,13 +263,14 @@
 
 void unlink_file(const struct Map_info *Map, const char *name)
 {
-    char buf[GPATH_MAX];
-    char file_path[GPATH_MAX];
+    char *path;
 
     /* delete old support files if available */
-    sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
+    path = Vect__get_element_path(Map, name);
+    if (access(path, F_OK) == 0) { /* file exists? */
+        G_debug(2, "\t%s: unlink", path);
+        unlink(path);
+    }
 
-    G_file_name(file_path, buf, name, G_mapset());
-    if (access(file_path, F_OK) == 0) /* file exists? */
-        unlink(file_path);
+    G_free(path);
 }

Modified: grass/trunk/lib/vector/Vlib/field.c
===================================================================
--- grass/trunk/lib/vector/Vlib/field.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/field.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -27,6 +27,8 @@
 #include <grass/dbmi.h>
 #include <grass/vector.h>
 
+#include "local_proto.h"
+
 #ifdef HAVE_GDAL
 #include <gdal_version.h>	/* needed for FID detection */
 #endif	/* HAVE_GDAL */
@@ -558,7 +560,7 @@
     char file[1024], buf[2001];
     char tab[1024], col[1024], db[1024], drv[1024], fldstr[1024], *fldname;
     int fld;
-    char *c;
+    char *c, *path;
     int row, rule;
     struct dblinks *dbl;
     char **tokens;
@@ -567,15 +569,12 @@
     dbl = Map->dblnk;
 
     /* Read dblink for native format */
-    sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
-	    Map->mapset, GV_DIRECTORY, Map->name,
-	    GV_DBLN_ELEMENT);
-    G_debug(1, "dbln file: %s", file);
-
-    fd = fopen(file, "r");
+    path = Vect__get_path(Map);
+    fd = G_fopen_old(path, GV_DBLN_ELEMENT, Map->mapset);
+    G_free(path);
     if (fd == NULL) {		/* This may be correct, no tables defined */
 	G_debug(1, "Cannot open vector database definition file");
-	return (-1);
+	return -1;
     }
 
     row = 0;
@@ -889,7 +888,7 @@
 {
     int i;
     FILE *fd;
-    char file[GPATH_MAX], buf[GPATH_MAX];
+    char *path, buf[1024];
     struct dblinks *dbl;
 
     if (Map->format != GV_FORMAT_NATIVE)
@@ -901,16 +900,13 @@
 
     dbl = Map->dblnk;
 
-    sprintf(file, "%s/%s/%s/%s/%s/%s", Map->gisdbase, Map->location,
-	    Map->mapset, GV_DIRECTORY, Map->name,
-	    GV_DBLN_ELEMENT);
-    G_debug(1, "dbln file: %s", file);
-
-    fd = fopen(file, "w");
+    path = Vect__get_path(Map);
+    fd = G_fopen_new(path, GV_DBLN_ELEMENT);
+    G_free(path);
     if (fd == NULL) {		/* This may be correct, no tables defined */
-	G_warning(_("Unable to open vector database definition file '%s'"),
-		  file);
-	return (-1);
+	G_warning(_("Unable to create database definition file for vector map <%s>"),
+		  Vect_get_name(Map));
+	return -1;
     }
 
     for (i = 0; i < dbl->n_fields; i++) {

Modified: grass/trunk/lib/vector/Vlib/header.c
===================================================================
--- grass/trunk/lib/vector/Vlib/header.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/header.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -20,7 +20,7 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
-static int lookup(const char *file, const char *key, char *value, size_t len);
+#include "local_proto.h"
 
 /*!
    \brief Print vector map header to stdout
@@ -77,12 +77,12 @@
  */
 int Vect__write_head(const struct Map_info *Map)
 {
-    char buf[GPATH_MAX];
+    char *path;
     FILE *head_fp;
 
-    sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-
-    head_fp = G_fopen_new(buf, GV_HEAD_ELEMENT);
+    path = Vect__get_path(Map);
+    head_fp = G_fopen_new(path, GV_HEAD_ELEMENT);
+    G_free(path);
     if (head_fp == NULL) {
 	G_warning(_("Unable to create header file for vector map <%s>"),
 		  Vect_get_full_name(Map));
@@ -117,23 +117,16 @@
 int Vect__read_head(struct Map_info *Map)
 {
     FILE *head_fp;
-    char buff[GPATH_MAX];
-    char *ptr;
+    char buff[2000];
+    char *path, *ptr;
 
     /* Reset / init */
-    Vect_set_organization(Map, "");
-    Vect_set_date(Map, "");
-    Vect_set_person(Map, "");
-    Vect_set_map_name(Map, "");
-    Vect_set_map_date(Map, "");
-    Vect_set_scale(Map, 1);
-    Vect_set_comment(Map, "");
-    Vect_set_zone(Map, -1);
-    Vect_set_thresh(Map, 0.);
-
+    Vect__init_head(Map);
+    
     G_debug(1, "Vect__read_head(): vector = %s@%s", Map->name, Map->mapset);
-    sprintf(buff, "%s/%s", GV_DIRECTORY, Map->name);
-    head_fp = G_fopen_old(buff, GV_HEAD_ELEMENT, Map->mapset);
+    path = Vect__get_path(Map);
+    head_fp = G_fopen_old(path, GV_HEAD_ELEMENT, Map->mapset);
+    G_free(path);
     if (head_fp == NULL) {
 	G_warning(_("Unable to open header file of vector <%s>"),
 		  Vect_get_full_name(Map));
@@ -589,6 +582,5 @@
  */
 double Vect_get_thresh(const struct Map_info *Map)
 {
-    G_debug(1, "Vect_get_thresh(): thresh = %f", Map->head.digit_thresh);
     return Map->head.digit_thresh;
 }

Modified: grass/trunk/lib/vector/Vlib/init_head.c
===================================================================
--- grass/trunk/lib/vector/Vlib/init_head.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/init_head.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -30,30 +30,26 @@
 {
     char buf[64];
 
+    G_zero(&(Map->head), sizeof(struct dig_head *));
+
     /* organization */
-    Map->head.organization = NULL;
     Vect_set_organization(Map, "");
     
     /* date */
-    Map->head.date = NULL;
     Vect_set_date(Map, "");
 
     /* user name */
-    Map->head.user_name = NULL;
     sprintf(buf, "%s", G_whoami());
     Vect_set_person(Map, buf);
 
     /* map name */
-    Map->head.map_name = NULL;
     Vect_set_map_name(Map, "");
 
     /* source date */
-    Map->head.source_date = NULL;
     sprintf(buf, "%s", G_date());
     Vect_set_map_date(Map, buf);
 
     /* comments */
-    Map->head.comment = NULL;
     Vect_set_comment(Map, "");
 
     /* scale, threshold */

Modified: grass/trunk/lib/vector/Vlib/local_proto.h
===================================================================
--- grass/trunk/lib/vector/Vlib/local_proto.h	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/local_proto.h	2013-03-16 14:40:42 UTC (rev 55401)
@@ -7,4 +7,13 @@
                               const struct line_pnts *, const struct line_cats *,
                               int (*external_routine) (const struct Map_info *, int));
 
+/* map.c */
+int Vect__delete(const char *, int);
+
+/* open.c */
+int Vect__open_old(struct Map_info *, const char *, const char *,
+                   const char *, int, int, int);
+char *Vect__get_path(const struct Map_info *);
+char *Vect__get_element_path(const struct Map_info *, const char *);
+
 #endif /* PG_LOCAL_PROTO_H__ */

Modified: grass/trunk/lib/vector/Vlib/map.c
===================================================================
--- grass/trunk/lib/vector/Vlib/map.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/map.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -28,6 +28,8 @@
 #include <grass/dbmi.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 /*!
    \brief Copy file
 
@@ -371,19 +373,38 @@
  */
 int Vect_delete(const char *map)
 {
-    int i, n, ret;
+    return Vect__delete(map, FALSE);
+}
+
+/*!
+  \brief Delete vector map (internal use only)
+  
+  \param map name of vector map to be delete 
+  \param is_tmp TRUE for temporary maps
+
+  \return -1 error
+  \return 0 success
+*/
+int Vect__delete(const char *map, int is_tmp)
+{
+    int ret;
+    char *path, path_buf[GPATH_MAX];
+    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+    const char *tmp, *mapset;
+    
     struct Map_info Map;
-    struct field_info *Fi;
-    char buf[GPATH_MAX];
+    
     DIR *dir;
     struct dirent *ent;
-    const char *tmp;
-    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
 
-    G_debug(3, "Delete vector '%s'", map);
+    G_debug(3, "Delete vector '%s' (is_tmp = %d)", map, is_tmp);
 
+    mapset = G_mapset();
+    
     /* remove mapset from fully qualified name */
     if (G_name_is_fully_qualified(map, xname, xmapset)) {
+        if (strcmp(mapset, xmapset) != 0)
+            G_warning(_("Ignoring invalid mapset: %s"), xmapset);
         map = xname;
     }
 
@@ -392,24 +413,27 @@
         return -1;
     }
 
-    sprintf(buf, "%s/%s/%s/%s/%s/%s", G_gisdbase(), G_location(),
-            G_mapset(), GV_DIRECTORY, map, GV_DBLN_ELEMENT);
-
-    G_debug(1, "dbln file: %s", buf);
-
-    if (access(buf, F_OK) == 0) {
-        /* Open input */
-        Vect_set_open_level(1); /* Topo not needed */
-        ret = Vect_open_old_head(&Map, map, G_mapset());
-        if (ret < 1) {
+    Vect_set_open_level(1); /* Topo not needed */
+    ret = Vect__open_old(&Map, map, mapset, NULL, FALSE, TRUE, is_tmp);
+    if (ret < 1) {
+        if (is_tmp)
+            return 0; /* temporary vector map doesn't exist */
+        else {
             G_warning(_("Unable to open header file for vector map <%s>"),
                       map);
             return -1;
         }
+    }
+        
+    path = Vect__get_element_path(&Map, GV_DBLN_ELEMENT);
+    G_debug(1, "dbln file: %s", path);
 
+    if (access(path, F_OK) == 0) {
+        int i, n;
+        struct field_info *Fi;
+        
         /* Delete all tables, NOT external (OGR) */
         if (Map.format == GV_FORMAT_NATIVE) {
-
             n = Vect_get_num_dblinks(&Map);
             for (i = 0; i < n; i++) {
                 Fi = Vect_get_dblink(&Map, i);
@@ -446,15 +470,15 @@
                 }
             }
         }
-        Vect_close(&Map);
     }
-
+    G_free(path);
+    
     /* Delete all files from vector/name directory */
-    sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
-    G_debug(3, "opendir '%s'", buf);
-    dir = opendir(buf);
+    path = Vect__get_element_path(&Map, NULL);
+    G_debug(3, "opendir '%s'", path);
+    dir = opendir(path);
     if (dir == NULL) {
-        G_warning(_("Unable to open directory '%s'"), buf);
+        G_warning(_("Unable to open directory '%s'"), path);
         return -1;
     }
 
@@ -463,29 +487,28 @@
         if ((strcmp(ent->d_name, ".") == 0) ||
             (strcmp(ent->d_name, "..") == 0))
             continue;
-        sprintf(buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map,
-                ent->d_name);
-        G_debug(3, "delete file '%s'", buf);
-        ret = unlink(buf);
+        
+        sprintf(path_buf, "%s/%s", path, ent->d_name);
+        G_debug(3, "delete file '%s'", path_buf);
+        ret = unlink(path_buf);
         if (ret == -1) {
-            G_warning(_("Unable to delete file '%s'"), buf);
+            G_warning(_("Unable to delete file '%s'"), path_buf);
             closedir(dir);
             return -1;
         }
     }
     closedir(dir);
-
+    G_free(path);
+    
     /* NFS can create .nfsxxxxxxxx files for those deleted 
      *  -> we have to move the directory to ./tmp before it is deleted */
-    sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
-
+    path = Vect__get_element_path(&Map, NULL);
     tmp = G_tempfile();
 
-    G_debug(3, "rename '%s' to '%s'", buf, tmp);
-    ret = rename(buf, tmp);
-
+    G_debug(3, "rename '%s' to '%s'", path, tmp);
+    ret = rename(path, tmp);
     if (ret == -1) {
-        G_warning(_("Unable to rename directory '%s' to '%s'"), buf, tmp);
+        G_warning(_("Unable to rename directory '%s' to '%s'"), path, tmp);
         return -1;
     }
 

Modified: grass/trunk/lib/vector/Vlib/open.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/open.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -6,7 +6,7 @@
   
   Higher level functions for reading/writing/manipulating vectors.
   
-  (C) 2001-2009, 2012 by the GRASS Development Team
+  (C) 2001-2009, 2012-2013 by the GRASS Development Team
   
   This program is free software under the GNU General Public License
   (>=v2).  Read the file COPYING that comes with GRASS for details.
@@ -26,6 +26,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "local_proto.h"
+
 #ifdef HAVE_POSTGRES
 #include "pg_local_proto.h"
 #endif
@@ -107,8 +109,7 @@
 #endif
 };
 
-static int open_old(struct Map_info *, const char *, const char *,
-                    const char *, int, int);
+static int open_new(struct Map_info *, const char *, int, int);
 static int map_format(struct Map_info *);
 
 /*!
@@ -153,23 +154,25 @@
  \param update non-zero to open for update otherwise read-only mode
  \param head_only read only header info from 'head', 'dbln', 'topo',
  'cidx' is not opened. The header may be opened on level 2 only.
- 
+ \param is_tmp TRUE for temporary maps
+
  \return level of openness (1, 2)
  \return -1 in error
 */
-int open_old(struct Map_info *Map, const char *name, const char *mapset,
-             const char *layer, int update, int head_only)
+int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset,
+                   const char *layer, int update, int head_only, int is_tmp)
 {
-    char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
-        xmapset[GMAPSET_MAX];
+    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
+    char *path;
     FILE *fp;
     int level, level_request;
     int format, ret;
     int ogr_mapset;
     const char *fmapset;
 
-    G_debug(1, "Vect__open_old(): name='%s' mapset='%s' layer='%s' update=%d",
-            name, mapset, layer, update);
+    G_debug(1, "Vect__open_old(): name = %s, mapset = %s, layer = %s, update = %d, "
+            "head_only = %d, is_tmp = %d", name, mapset, layer ? layer : "", update, head_only,
+            is_tmp);
 
     /* zero Map_info structure */
     G_zero(Map, sizeof(struct Map_info));
@@ -199,37 +202,48 @@
                 Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
             }
         }
-        else {
-            sprintf(buf,  "%s/%s", GV_DIRECTORY, xname);
-            sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
-        }
         Map->name = G_store(xname);
         Map->mapset = G_store(xmapset);
     }
     else {
-        sprintf(buf, "%s/%s", GV_DIRECTORY, name);
-        sprintf(buf2, "%s",   GV_COOR_ELEMENT);
         Map->name = G_store(name);
 
+        Map->temporary = is_tmp;
+        /* temporary maps can be accessed only in the current mapset */
         if (mapset)
             Map->mapset = G_store(mapset);
         else
             Map->mapset = G_store("");
     }
 
+    path = Vect__get_path(Map);
+
     if (!ogr_mapset) {
         /* try to find vector map (not for OGR mapset) */
-        fmapset = G_find_vector2(Map->name, Map->mapset);
-        if (fmapset == NULL) {
-            if (mapset && strcmp(mapset, G_mapset()) == 0)
-                G_fatal_error(_("Vector map <%s> not found in current mapset"),
+        if (!Map->temporary) {
+            fmapset = G_find_vector2(Map->name, Map->mapset);
+            if (fmapset == NULL) {
+                if (mapset && strcmp(mapset, G_mapset()) == 0)
+                    G_warning(_("Vector map <%s> not found in current mapset"),
                               Vect_get_name(Map));
-            else
-                G_fatal_error(_("Vector map <%s> not found"),
+                else
+                    G_warning(_("Vector map <%s> not found"),
                               Vect_get_full_name(Map));
-            return -1;
+                return -1;
+            }
+            Map->mapset = G_store(fmapset);
         }
-        Map->mapset = G_store(fmapset);
+        else {
+            char file_path[GPATH_MAX];
+            
+            if (strcmp(Map->mapset, G_mapset()) != 0) {
+                G_warning(_("Temporary vector maps can be accessed only in the current mapset"));
+                return -1;
+            }
+            G_file_name(file_path, path, GV_HEAD_ELEMENT, Map->mapset);
+            if (access(file_path, F_OK) != 0)
+                return -1;
+        }
     }
     
     Map->location = G_store(G_location());
@@ -240,8 +254,8 @@
         return -1;
     }
 
-    G_debug(1, "Map name: %s", Map->name);
-    G_debug(1, "Map mapset: %s", Map->mapset);
+    G_debug(1, "Map: name = %s, mapset = %s, temporary = %d", Map->name, Map->mapset,
+            Map->temporary);
 
     /* read vector format information */
     if (ogr_mapset) {
@@ -249,10 +263,7 @@
     }
     else {
         format = 0;
-        sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-        G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
-                GV_FRMT_ELEMENT);
-        fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
+        fp = G_fopen_old(path, GV_FRMT_ELEMENT, Map->mapset);
         if (fp == NULL) {
             G_debug(1, "Vector format: %d (native)", format);
             format = GV_FORMAT_NATIVE;
@@ -451,7 +462,7 @@
         Map->head_only = FALSE;
     }
 
-    G_debug(1, "Vect_open_old(): vector opened on level %d", level);
+    G_debug(1, "Vect__open_old(): vector opened on level %d", level);
 
     if (level == 1) {           /* without topology */
         Map->plus.built = GV_BUILD_NONE;
@@ -467,9 +478,8 @@
     Vect_read_dblinks(Map);
 
     /* open history file */
-    sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
     if (update && !ogr_mapset) {                /* native only */
-        Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
+        Map->hist_fp = G_fopen_modify(path, GV_HIST_ELEMENT);
         if (Map->hist_fp == NULL) {
             G_warning(_("Unable to open history file for vector map <%s>"),
                       Vect_get_full_name(Map));
@@ -483,7 +493,7 @@
     else {
         if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR ||
             Map->format == GV_FORMAT_POSTGIS) {
-            Map->hist_fp = G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
+            Map->hist_fp = G_fopen_old(path, GV_HIST_ELEMENT, Map->mapset);
             /* If NULL (does not exist) then Vect_hist_read() handle that */
         }
         else {
@@ -499,27 +509,26 @@
     if (update && !head_only) {
         char file_path[GPATH_MAX];
 
-        sprintf(buf, "%s/%s", GV_DIRECTORY, name);
-
-        G_file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
+        G_file_name(file_path, path, GV_TOPO_ELEMENT, G_mapset());
         if (access(file_path, F_OK) == 0)       /* topo file exists? */
             unlink(file_path);
 
-        G_file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
+        G_file_name(file_path, path, GV_SIDX_ELEMENT, G_mapset());
         if (access(file_path, F_OK) == 0)       /* sidx file exists? */
             unlink(file_path);
 
-        G_file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
+        G_file_name(file_path, path, GV_CIDX_ELEMENT, G_mapset());
         if (access(file_path, F_OK) == 0)       /* cidx file exists? */
             unlink(file_path);
 
         if (format == GV_FORMAT_OGR || format == GV_FORMAT_POSTGIS) {
-            G_file_name(file_path, buf, GV_FIDX_ELEMENT, G_mapset());
+            G_file_name(file_path, path, GV_FIDX_ELEMENT, G_mapset());
             if (access(file_path, F_OK) == 0)   /* fidx file exists? */
                 unlink(file_path);
         }
     }
-
+    G_free(path);
+    
     return level;
 }
 
@@ -541,7 +550,7 @@
 */
 int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
 {
-    return open_old(Map, name, mapset, NULL, 0, 0);
+    return Vect__open_old(Map, name, mapset, NULL, FALSE, FALSE, FALSE);
 }
 
 /*!
@@ -561,7 +570,7 @@
 int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset,
                    const char *layer)
 {
-    return open_old(Map, name, mapset, layer, 0, 0);
+    return Vect__open_old(Map, name, mapset, layer, FALSE, FALSE, FALSE);
 }
 
 /*!
@@ -585,7 +594,7 @@
 */
 int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
 {
-    return open_old(Map, name, mapset, NULL, 1, 0);
+    return Vect__open_old(Map, name, mapset, NULL, TRUE, FALSE, FALSE);
 }
 
 /*!
@@ -607,7 +616,7 @@
 */
 int Vect_open_update2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
 {
-    return open_old(Map, name, mapset, layer, 1, 0);
+    return Vect__open_old(Map, name, mapset, layer, TRUE, FALSE, FALSE);
 }
 
 /*! 
@@ -630,7 +639,7 @@
 */
 int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
 {
-    return open_old(Map, name, mapset, NULL, 0, 1);
+    return Vect__open_old(Map, name, mapset, NULL, FALSE, TRUE, FALSE);
 }
 
 /*! 
@@ -657,7 +666,7 @@
 int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset,
                         const char *layer)
 {
-    return open_old(Map, name, mapset, layer, 0, 1);
+    return Vect__open_old(Map, name, mapset, layer, FALSE, TRUE, FALSE);
 }
 
 /*!  \brief Open header file of existing vector map for updating
@@ -674,34 +683,16 @@
 int Vect_open_update_head(struct Map_info *Map, const char *name,
                           const char *mapset)
 {
-    return open_old(Map, name, mapset, NULL, 1, 1);
+    return Vect__open_old(Map, name, mapset, NULL, TRUE, TRUE, FALSE);
 }
 
-/*!
-  \brief Create new vector map for reading/writing
- 
-  By default list of updated features is not maintained, see
-  Vect_set_updated() for details.
-  
-  By default map format is native (GV_FORMAT_NATIVE). If OGR file is
-  found in the current mapset then the map (ie. OGR layer) is created
-  in given OGR datasource (GV_FORMAT_OGR). Similarly if PG file exists
-  then the map (ie. PostGIS table) is created using PostGIS interface
-  (GV_FORMAT_POSTGIS). The format of map is stored in Map->format.
-
-  \param[out] Map pointer to Map_info structure
-  \param name name of vector map to be created
-  \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
-  
-  \return 1 on success
-  \return -1 on error
-*/
-int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
+int open_new(struct Map_info *Map, const char *name, int with_z, int is_tmp)
 {
     int ret;
-    char xname[GNAME_MAX], xmapset[GMAPSET_MAX], buf[GPATH_MAX];
+    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
 
-    G_debug(2, "Vect_open_new(): name = %s with_z = %d", name, with_z);
+    G_debug(1, "Vect_open_new(): name = %s with_z = %d is_tmp = %d",
+            name, with_z, is_tmp);
 
     /* zero Map_info structure */
     G_zero(Map, sizeof(struct Map_info));
@@ -712,8 +703,8 @@
     /* check for fully-qualified map name */
     if (G_name_is_fully_qualified(name, xname, xmapset)) {
         if (strcmp(xmapset, G_mapset()) != 0) {
-            G_fatal_error(_("<%s> is not the current mapset (%s)"), name,
-                          G_mapset());
+            G_warning(_("Unable to create new vector map: <%s> is not the current mapset (%s)"),
+                      name, G_mapset());
             return -1;
         }
         name = xname;
@@ -721,15 +712,17 @@
 
     /* check for [A-Za-z][A-Za-z0-9_]* in name */
     if (Vect_legal_filename(name) < 0) {
-        G_fatal_error(_("Vector map name is not SQL compliant"));
+        G_warning(_("Unable to create vector map <%s>: name is not SQL compliant"),
+            name);
         return -1;
     }
 
-    /* store basic info (map at mapset) */
-    Map->name = G_store(name);
-    Map->mapset = G_store(G_mapset());
-    Map->location = G_store(G_location());
-    Map->gisdbase = G_store(G_gisdbase());
+    /* store basic info */
+    Map->name      = G_store(name);
+    Map->mapset    = G_store(G_mapset());
+    Map->location  = G_store(G_location());
+    Map->gisdbase  = G_store(G_gisdbase());
+    Map->temporary = is_tmp;
     
     /* determine output format */
     if (strcmp(G_program_name(), "v.external") != 0)
@@ -739,19 +732,31 @@
 
     if (Map->format != GV_FORMAT_OGR_DIRECT &&
         getenv("GRASS_VECTOR_PGFILE") == NULL) { /* GRASS_VECTOR_PGFILE defined by v.out.postgis */
+        char *path;
+        
         G_debug(2, " using non-direct format");
 
-        /* check if map already exists */
-        if (G_find_vector2(name, G_mapset()) != NULL) {
-            G_warning(_("Vector map <%s> already exists and will be overwritten"),
-                      name);
-            
-            ret = Vect_delete(name);
-            if (ret == -1) {
+        /* check if map already exists
+           temporary maps are automatically overwritten
+         */
+        if (Map->temporary) {
+            if (-1 == Vect__delete(name, Map->temporary)) {
                 G_warning(_("Unable to delete vector map <%s>"), name);
                 return -1;
             }
         }
+        else {
+            if (G_find_vector2(name, G_mapset()) != NULL) {
+                G_warning(_("Vector map <%s> already exists and will be overwritten"),
+                          name);
+                
+                ret = Vect_delete(name);
+                if (ret == -1) {
+                    G_warning(_("Unable to delete vector map <%s>"), name);
+                    return -1;
+                }
+            }
+        }
 
         /* write header file
 
@@ -764,8 +769,9 @@
         Vect__write_head(Map);
 
         /* create history file */
-        sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-        Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
+        path = Vect__get_path(Map);
+        Map->hist_fp = G_fopen_new(path, GV_HIST_ELEMENT);
+        G_free(path);
         if (Map->hist_fp == NULL) {
             G_warning(_("Unable to open history file of vector map <%s>"),
                       name);
@@ -808,6 +814,63 @@
 }
 
 /*!
+  \brief Create new vector map for reading/writing
+ 
+  By default list of updated features is not maintained, see
+  Vect_set_updated() for details.
+  
+  By default map format is native (GV_FORMAT_NATIVE). If OGR file is
+  found in the current mapset then the map (ie. OGR layer) is created
+  in given OGR datasource (GV_FORMAT_OGR). Similarly if PG file exists
+  then the map (ie. PostGIS table) is created using PostGIS interface
+  (GV_FORMAT_POSTGIS). The format of map is stored in Map->format.
+
+  \param[out] Map pointer to Map_info structure
+  \param name name of vector map to be created
+  \param with_z WITH_Z for 3D vector data otherwise WITHOUT_Z
+  
+  \return 1 on success
+  \return -1 on error
+*/
+int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
+{
+    return open_new(Map, name, with_z, FALSE);
+}
+
+/*!
+  \brief Create new temporary vector map
+
+  Temporary vector maps are stored in the current mapset (directory
+  <tt>.tmp/<hostname>/vector</tt>).
+
+  Temporary vector maps are automatically deleted when closing the map
+  (see Vect_close() for details).
+
+  If <em>name</em> is not given (is NULL), then the name is determined
+  by process id (<tt>tmp_<pid></tt>).
+
+  \param[out] Map pointer to output Map_info struct
+  \param name name for new vector map (or NULL)
+  \param with_z WITH_Z for 3D vector data
+
+  \return 1 on success
+  \return -1 on error
+*/
+int Vect_open_tmp_new(struct Map_info *Map, const char *name, int with_z)
+{
+    char tmp_name[GNAME_MAX];
+
+    if (!name) {
+        sprintf(tmp_name, "tmp_%d", getpid());
+    }
+    else {
+        sprintf(tmp_name, "%s", name);
+    }
+    
+    return open_new(Map, tmp_name, with_z, TRUE); /* temporary map */
+}
+
+/*!
   \brief Update Coor_info structure
   
   \param Map pointer to Map_info structure
@@ -818,16 +881,17 @@
 */
 int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
 {
-    char buf[GPATH_MAX], path[GPATH_MAX];
+    char *path, file_path[GPATH_MAX];
     STRUCT_STAT stat_buf;
     
     switch (Map->format) {
     case GV_FORMAT_NATIVE:
-        sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-        G_file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
-        G_debug(1, "get coor info: %s", path);
-        if (0 != stat(path, &stat_buf)) {
-            G_warning(_("Unable to stat file <%s>"), path);
+        path = Vect__get_path(Map);
+        G_file_name(file_path, path, GV_COOR_ELEMENT, Map->mapset);
+        G_free(path);
+        G_debug(1, "get coor info: %s", file_path);
+        if (0 != stat(file_path, &stat_buf)) {
+            G_warning(_("Unable to stat file <%s>"), file_path);
             Info->size = -1L;
             Info->mtime = -1L;
         }
@@ -853,7 +917,7 @@
         Info->mtime = 0L;
         break;
     }
-    G_debug(1, "Info->size = %lu, Info->mtime = %ld",
+    G_debug(1, "Vect_coor_info(): Info->size = %lu, Info->mtime = %ld",
             (unsigned long)Info->size, Info->mtime);
 
     return 1;
@@ -1007,7 +1071,6 @@
 */
 int Vect_open_sidx(struct Map_info *Map, int mode)
 {
-    char buf[GPATH_MAX], file_path[GPATH_MAX];
     int err;
     struct Coor_info CInfo;
     struct Plus_head *Plus;
@@ -1025,15 +1088,17 @@
     dig_file_init(&(Plus->spidx_fp));
 
     if (mode < 2) {
-        sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
-        G_file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
+        char *path, file_path[GPATH_MAX];
+        
+        path = Vect__get_path(Map);
+        G_file_name(file_path, path, GV_SIDX_ELEMENT, Map->mapset);
 
         if (access(file_path, F_OK) != 0)       /* does not exist */
             return 1;
 
-        Plus->spidx_fp.file =
-            G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
-
+        Plus->spidx_fp.file = G_fopen_old(path, GV_SIDX_ELEMENT, Map->mapset);
+        G_free(path);
+        
         if (Plus->spidx_fp.file == NULL) {  /* sidx file is not available */
             G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
                     Map->name, Map->mapset);
@@ -1222,5 +1287,52 @@
         }
     }
     
+    G_debug(2, "map_format = %d", Map->format);
     return Map->format;
 }
+
+/*!
+  \brief Get map directory name (internal use only)
+
+  Allocate string should be freed by G_free().
+
+  \param Map pointer to Map_info struct
+
+  \return allocated buffer containing path
+*/
+char *Vect__get_path(const struct Map_info *Map)
+{
+    char path[GPATH_MAX];
+    
+    if (Map->temporary) {
+        char path_tmp[GPATH_MAX];
+        G__temp_element(path_tmp);
+        sprintf(path, "%s/%s/%s", path_tmp, GV_DIRECTORY, Map->name);
+    }
+    else {
+        sprintf(path, "%s/%s", GV_DIRECTORY, Map->name);
+    }
+    
+    return G_store(path);
+}
+
+/*!
+  \brief Get map element full path (internal use only)
+
+  Allocate string should be freed by G_free().
+
+  \param Map pointer to Map_info struct
+  \param element element name, eg. GV_TOPO_ELEMENT
+
+  \return allocated buffer containing path
+*/
+char *Vect__get_element_path(const struct Map_info *Map, const char *element)
+{
+    char file_path[GPATH_MAX], *path;
+    
+    path = Vect__get_path(Map);
+    G_file_name(file_path, path, element, Map->mapset);
+    G_free(path);
+
+    return G_store(file_path);
+}

Modified: grass/trunk/lib/vector/Vlib/open_nat.c
===================================================================
--- grass/trunk/lib/vector/Vlib/open_nat.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/Vlib/open_nat.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -21,7 +21,8 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
-static char name_buf[GPATH_MAX];
+#include "local_proto.h"
+
 static int check_coor(struct Map_info *Map);
 
 /*!
@@ -90,44 +91,45 @@
 */
 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
 {
-    char buf[GPATH_MAX];
+    char *path, file_path[GPATH_MAX];
 
-    G_debug(1, "V1_open_new_nat(): name = %s", name);
+    G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d",
+            name, with_z, Map->temporary);
 
-    sprintf(buf, "%s/%s", GV_DIRECTORY, name);
+    path = Vect__get_path(Map);
 
     /* Set the 'coor' file version */
     Map->head.Version_Major = GV_COOR_VER_MAJOR;
     Map->head.Version_Minor = GV_COOR_VER_MINOR;
-    Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
-    Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
+    Map->head.Back_Major    = GV_COOR_EARLIEST_MAJOR;
+    Map->head.Back_Minor    = GV_COOR_EARLIEST_MINOR;
 
     /* TODO: open better */
     dig_file_init(&(Map->dig_fp));
-    Map->dig_fp.file = G_fopen_new(buf, GV_COOR_ELEMENT);
+    Map->dig_fp.file = G_fopen_new(path, GV_COOR_ELEMENT);
     if (Map->dig_fp.file == NULL)
-	return (-1);
+	return -1;
     fclose(Map->dig_fp.file);
 
     dig_file_init(&(Map->dig_fp));
-    Map->dig_fp.file = G_fopen_modify(buf, GV_COOR_ELEMENT);
+    Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
     if (Map->dig_fp.file == NULL)
-	return (-1);
+	return -1;
 
     /* if overwrite OK, any existing files have already been deleted by
      * Vect_open_new(): remove this check ? */
     /* check to see if dig_plus file exists and if so, remove it */
-    G_file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
-    if (access(name_buf, F_OK) == 0)	/* file exists? */
-	unlink(name_buf);
-
-    G_file_name(name_buf, buf, GV_COOR_ELEMENT, G_mapset());
-
+    G_file_name(file_path, path, GV_TOPO_ELEMENT, G_mapset());
+    G_free(path);
+    if (access(file_path, F_OK) == 0)
+        unlink(file_path); /* remove topo file if exists */
+    
     /* set conversion matrices */
     dig_init_portable(&(Map->head.port), dig__byte_order_out());
 
+    /* write coor header */
     if (!(dig__write_head(Map)))
-	return (-1);
+	return -1;
 
     return 0;
 }

Modified: grass/trunk/lib/vector/diglib/head.c
===================================================================
--- grass/trunk/lib/vector/diglib/head.c	2013-03-16 14:20:57 UTC (rev 55400)
+++ grass/trunk/lib/vector/diglib/head.c	2013-03-16 14:40:42 UTC (rev 55401)
@@ -75,7 +75,7 @@
     }
 
     G_debug(2, "coor body offset %"PRI_OFF_T, dig_ftell(&(Map->dig_fp)));
-    return (1);
+    return 1;
 }
 
 



More information about the grass-commit mailing list