GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-0140b987a
close_nat.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/close_nat.c
3 
4  \brief Vector library - Close map (native format)
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2001-2015 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Original author CERL, probably Dave Gerdes or Mike Higgins.
14  \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
15 */
16 
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <errno.h>
20 
21 #include <grass/vector.h>
22 #include <grass/glocale.h>
23 
24 #include "local_proto.h"
25 
26 /*!
27  \brief Close vector map
28 
29  \param Map vector map to be closed
30 
31  \return 0 on success
32  \return non-zero on error
33 */
34 int V1_close_nat(struct Map_info *Map)
35 {
36  struct Coor_info CInfo;
37 
38  G_debug(1, "V1_close_nat(): name = %s mapset= %s", Map->name,
39  Map->mapset);
40  if (!VECT_OPEN(Map))
41  return 1;
42 
43  if (Map->mode == GV_MODE_WRITE || Map->mode == GV_MODE_RW) {
44  Vect_coor_info(Map, &CInfo);
45  Map->head.size = CInfo.size;
46  dig__write_head(Map);
47 
48  Vect__write_head(Map);
49  Vect_write_dblinks(Map);
50  }
51 
52  /* close coor file */
53  fclose(Map->dig_fp.file);
54  dig_file_free(&(Map->dig_fp));
55 
56  /* delete temporary map ? */
57  if (Map->temporary) {
58  int delete;
59  char *env = getenv("GRASS_VECTOR_TEMPORARY");
60 
61  delete = TRUE;
62  if (Map->temporary == TEMPORARY_MAP_ENV && env) {
63  if (G_strcasecmp(env, "move") == 0) {
64  /* copy temporary vector map to the current mapset */
65  char path_tmp[GPATH_MAX], path_map[GPATH_MAX];
66 
67  G_debug(1, "V1_close_nat(): temporary map <%s> TO BE MOVED TO"
68  " CURRENT MAPSET",
69  Map->name);
70  Vect__get_element_path(path_tmp, Map, NULL);
71 
72  G_file_name(path_map, GV_DIRECTORY, NULL, Map->mapset);
73  if (access(path_map, 0) != 0 && G_mkdir(path_map) != 0)
74  G_fatal_error(_("Unable to create '%s': %s"),
75  path_map, strerror(errno));
76 
77  G_file_name(path_map, GV_DIRECTORY, Map->name, Map->mapset);
78 
79  G_debug(1, "V1_close_nat(): %s -> %s", path_tmp, path_map);
80  if (0 != G_recursive_copy(path_tmp, path_map))
81  G_fatal_error(_("Unable to copy '%s': %s"), path_tmp, strerror(errno));
82 
83 #ifdef TEMPORARY_MAP_DB
84  int i, ndblinks;
85  int tmp;
86 
87  struct field_info *fi;
88  dbConnection connection;
89  struct dblinks *dblinks;
90 
91  G_debug(1, "V1_close_nat(): copying attributes");
92  /* copy also attributes */
93  dblinks = Vect_new_dblinks_struct();
94  db_get_connection(&connection);
95  ndblinks = Vect_get_num_dblinks(Map);
96  for (i = 0; i < ndblinks; i++) {
97  fi = Vect_get_dblink(Map, i);
98  if (DB_OK != db_copy_table(fi->driver, fi->database, fi->table,
99  connection.driverName,
100  connection.databaseName,
101  fi->table)) {
102  G_warning(_("Unable to copy table <%s>"), fi->table);
103  continue;
104  }
105 
106  Vect_add_dblink(dblinks, fi->number, fi->name,
107  fi->table, fi->key, connection.databaseName,
108  connection.driverName);
109  G_free(fi);
110  }
111  G_free(Map->dblnk);
112  Map->dblnk = dblinks;
113  tmp = Map->temporary;
114  Map->temporary = TEMPORARY_MAP_DISABLED;
115  Vect_write_dblinks(Map);
116  Map->temporary = tmp;
117 #endif
118  }
119  else if (G_strcasecmp(env, "delete") == 0) {
120  /* delete temporary vector map */
121  G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name);
122  }
123  else {
124  /* do not delete temporary vector map */
125  G_debug(1, "V1_close_nat(): temporary map <%s> IS NOT DELETED",
126  Map->name);
127  delete = FALSE;
128  }
129  }
130  else if (Map->temporary == TEMPORARY_MAP) {
131  G_debug(1, "V1_close_nat(): temporary map <%s> TO BE DELETED", Map->name);
132  delete = TRUE;
133  }
134 
135  if (delete) {
136  char path_tmp[GPATH_MAX];
137 
138  /* delete vector directory */
139  Vect__get_element_path(path_tmp, Map, NULL);
140  G_recursive_remove(path_tmp);
141 
142 #ifndef TEMPORARY_MAP_DB
143  if (G_strcasecmp(env, "move") != 0) {
144  int i, ndblinks;
145 
146  dbDriver *driver;
147  dbString table_name;
148 
149  struct field_info *fi;
150 
151  db_init_string(&table_name);
152 
153  /* drop also attribute table */
154  ndblinks = Vect_get_num_dblinks(Map);
155  for (i = 0; i < ndblinks; i++) {
156  fi = Vect_get_dblink(Map, i);
157 
158  driver = db_start_driver_open_database(fi->driver, fi->database);
159  if (driver == NULL) {
160  G_warning(_("Unable to open database <%s> by driver <%s>"),
161  fi->database, fi->driver);
162  continue;
163  }
164 
165  db_set_string(&table_name, fi->table);
166  if (DB_OK != db_drop_table(driver, &table_name)) {
167  G_warning(_("Unable to drop table <%s>"), fi->table);
168  continue;
169  }
170  }
171  }
172 #endif
173  }
174  }
175 
176  return 0;
177 }
#define TRUE
Definition: gis.h:56
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:38
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
int db_get_connection(dbConnection *)
Get default DB connection settings for the current mapset.
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
#define GV_DIRECTORY
Name of vector directory.
Definition: dig_defines.h:8
off_t size
Total size (in bytes)
Definition: dig_structs.h:384
void db_init_string(dbString *)
Initialize dbString.
Definition: string.c:25
char * table
Name of DB table.
Definition: dig_structs.h:155
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:149
dbDriver * db_start_driver_open_database(const char *, const char *)
Open driver/database connection.
Definition: db.c:28
#define NULL
Definition: ccmath.h:32
int V1_close_nat(struct Map_info *Map)
Close vector map.
Definition: close_nat.c:34
int db_set_string(dbString *, const char *)
Inserts string to dbString (enlarge string)
Definition: string.c:41
int Vect_add_dblink(struct dblinks *, int, const char *, const char *, const char *, const char *, const char *)
Add new DB connection to dblinks structure.
Definition: field.c:277
int Vect_get_num_dblinks(const struct Map_info *)
Get number of defined dblinks.
Definition: level_two.c:163
char * database
Definition: dig_structs.h:151
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
Layer (old: field) information.
Definition: dig_structs.h:134
#define VECT_OPEN(Map)
Check if vector map is open.
Definition: dig_defines.h:136
char * name
Layer name (optional)
Definition: dig_structs.h:143
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition: strings.c:47
char * databaseName
Definition: dbmi.h:297
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition: copy_dir.c:70
#define FALSE
Definition: gis.h:60
struct field_info * Vect_get_dblink(const struct Map_info *, int)
Get information about link to database.
Definition: field.c:466
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
Definition: field.c:46
const struct driver * driver
Definition: driver/init.c:25
void dig_file_free(struct gvfile *file)
Free struct gvfile.
Definition: file.c:266
#define GPATH_MAX
Definition: gis.h:167
char * mapset
Mapset name.
Definition: dig_structs.h:1336
struct dig_head head
Header info.
Definition: dig_structs.h:1403
char * driverName
Definition: dbmi.h:295
Vector map info.
Definition: dig_structs.h:1259
off_t size
Coor file size.
Definition: dig_structs.h:352
struct dblinks * dblnk
Array of DB links.
Definition: dig_structs.h:1281
char * driver
Name of DB driver (&#39;sqlite&#39;, &#39;dbf&#39;, ...)
Definition: dig_structs.h:147
int Vect_write_dblinks(struct Map_info *)
Write dblinks to file.
Definition: field.c:951
FILE * file
File descriptor.
Definition: dig_structs.h:101
int db_copy_table(const char *, const char *, const char *, const char *, const char *, const char *)
Copy a table.
Definition: copy_tab.c:446
int temporary
Temporary map flag.
Definition: dig_structs.h:1276
void G_warning(const char *,...) __attribute__((format(printf
Coor file info.
Definition: dig_structs.h:379
int number
Layer number.
Definition: dig_structs.h:139
int G_mkdir(const char *)
Creates a new directory.
Definition: paths.c:27
#define _(str)
Definition: glocale.h:10
int db_drop_table(dbDriver *, dbString *)
Drop table.
Definition: c_drop_tab.c:28
struct gvfile dig_fp
GV file pointer (native format only)
Definition: dig_structs.h:1410
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
int Vect__write_head(const struct Map_info *)
Writes head information to text file (GV_HEAD_ELEMENT)
int dig__write_head(struct Map_info *)
Definition: head.c:22
char * getenv()
int Vect_coor_info(const struct Map_info *, struct Coor_info *)
Update Coor_info structure.
int G_recursive_remove(const char *)
Recursively remove all files in given directory.
Definition: remove.c:113
int G_debug(int, const char *,...) __attribute__((format(printf
#define DB_OK
Definition: dbmi.h:71
char * key
Name of key column (usually &#39;cat&#39;)
Definition: dig_structs.h:159