GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72721
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 }
dbDriver * db_start_driver_open_database(const char *drvname, const char *dbname)
Open driver/database connection.
Definition: db.c:28
#define TRUE
Definition: gis.h:49
int G_strcasecmp(const char *x, const char *y)
String compare ignoring case (upper or lower)
Definition: strings.c:46
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
int db_copy_table(const char *from_drvname, const char *from_dbname, const char *from_tblname, const char *to_drvname, const char *to_dbname, const char *to_tblname)
Copy a table.
Definition: copy_tab.c:446
struct driver * driver
Definition: driver/init.c:25
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:149
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
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
off_t size
Total size (in bytes)
Definition: dig_structs.h:384
char * table
Name of DB table.
Definition: dig_structs.h:155
int Vect_get_num_dblinks(const struct Map_info *Map)
Get number of defined dblinks.
Definition: level_two.c:163
int G_recursive_copy(const char *src, const char *dst)
Copy recursively source directory to destination directory.
Definition: copy_dir.c:70
#define NULL
Definition: ccmath.h:32
int V1_close_nat(struct Map_info *Map)
Close vector map.
Definition: close_nat.c:34
char * database
Definition: dig_structs.h:151
#define GV_MODE_RW
Read-write vector map open mode.
Definition: dig_defines.h:108
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:160
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 * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:38
char * name
Layer name (optional)
Definition: dig_structs.h:143
int Vect_add_dblink(struct dblinks *p, int number, const char *name, const char *table, const char *key, const char *db, const char *driver)
Add new DB connection to dblinks structure.
Definition: field.c:248
char * databaseName
Definition: dbmi.h:297
#define FALSE
Definition: gis.h:53
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
int Vect__write_head(const struct Map_info *Map)
Writes head information to text file (GV_HEAD_ELEMENT)
fclose(fd)
void dig_file_free(struct gvfile *file)
Free struct gvfile.
Definition: file.c:266
#define GPATH_MAX
Definition: gis.h:151
int G_recursive_remove(const char *path)
Recursively remove all files in given directory.
Definition: remove.c:113
int db_get_connection(dbConnection *connection)
Get default DB connection settings.
char * mapset
Mapset name.
Definition: dig_structs.h:1336
int Vect_write_dblinks(struct Map_info *Map)
Write dblinks to file.
Definition: field.c:906
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 field_info * Vect_get_dblink(const struct Map_info *Map, int link)
Get information about link to database.
Definition: field.c:421
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
FILE * file
File descriptor.
Definition: dig_structs.h:101
int temporary
Temporary map flag.
Definition: dig_structs.h:1276
Coor file info.
Definition: dig_structs.h:379
int number
Layer number.
Definition: dig_structs.h:139
int db_set_string(dbString *x, const char *s)
Inserts string to dbString (enlarge string)
Definition: string.c:41
#define _(str)
Definition: glocale.h:13
struct gvfile dig_fp
GV file pointer (native format only)
Definition: dig_structs.h:1410
int db_drop_table(dbDriver *driver, dbString *name)
Drop table.
Definition: c_drop_tab.c:28
struct dblinks * Vect_new_dblinks_struct(void)
Create and init new dblinks structure.
Definition: field.c:46
#define GV_MODE_WRITE
Write vector map open mode.
Definition: dig_defines.h:106
int dig__write_head(struct Map_info *)
Definition: head.c:22
char * getenv()
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
void db_init_string(dbString *x)
Initialize dbString.
Definition: string.c:25
#define DB_OK
Definition: dbmi.h:71
char * key
Name of key column (usually &#39;cat&#39;)
Definition: dig_structs.h:159