GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
close.c
Go to the documentation of this file.
1 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <grass/Vect.h>
28 #include <grass/glocale.h>
29 
30 static int clo_dummy()
31 {
32  return -1;
33 }
34 
35 #ifndef HAVE_OGR
36 static int format()
37 {
38  G_fatal_error(_("Requested format is not compiled in this version"));
39  return 0;
40 }
41 #endif
42 
43 static int (*Close_array[][2]) () = {
44  {
45  clo_dummy, V1_close_nat}
46 #ifdef HAVE_OGR
47  , {
48  clo_dummy, V1_close_ogr}
49 #else
50  , {
51  clo_dummy, format}
52 #endif
53 };
54 
55 
64 int Vect_close(struct Map_info *Map)
65 {
66  struct Coor_info CInfo;
67 
68  G_debug(1,
69  "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
70  Map->name, Map->mapset, Map->format, Map->level);
71 
72  /* Store support files if in write mode on level 2 */
73  if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
74  Map->plus.built == GV_BUILD_ALL) {
75  char buf[GPATH_MAX];
76  char file_path[GPATH_MAX];
77  struct stat info;
78 
79  /* Delete old support files if available */
80  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
81 
82  G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
83  if (stat(file_path, &info) == 0) /* file exists? */
84  unlink(file_path);
85 
86  G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
87  if (stat(file_path, &info) == 0) /* file exists? */
88  unlink(file_path);
89 
90  G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
91  if (stat(file_path, &info) == 0) /* file exists? */
92  unlink(file_path);
93 
94  Vect_coor_info(Map, &CInfo);
95  Map->plus.coor_size = CInfo.size;
96  Map->plus.coor_mtime = CInfo.mtime;
97 
98  Vect_save_topo(Map);
99 
100  /* Spatial index is not saved */
101  /* Vect_save_spatial_index ( Map ); */
102 
103  Vect_cidx_save(Map);
104 
105 #ifdef HAVE_OGR
106  if (Map->format == GV_FORMAT_OGR)
107  V2_close_ogr(Map);
108 #endif
109  }
110 
111  if (Map->level == 2 && Map->plus.release_support) {
112  G_debug(1, "free topology");
113  dig_free_plus(&(Map->plus));
114 
115  if (!Map->head_only) {
116  G_debug(1, "free spatial index");
117  dig_spidx_free(&(Map->plus));
118  }
119 
120  G_debug(1, "free category index");
121  dig_cidx_free(&(Map->plus));
122 
123  }
124 
125  if (Map->format == GV_FORMAT_NATIVE) {
126  G_debug(1, "close history file");
127  if (Map->hist_fp != NULL)
128  fclose(Map->hist_fp);
129  }
130 
131  /* Close level 1 files / data sources if not head_only */
132  if (!Map->head_only) {
133  if (((*Close_array[Map->format][1]) (Map)) != 0) {
134  G_warning(_("Unable to close vector <%s>"),
135  Vect_get_full_name(Map));
136  return 1;
137  }
138  }
139 
140  G_free((void *)Map->name);
141  Map->name = NULL;
142  G_free((void *)Map->mapset);
143  Map->mapset = NULL;
144 
145  Map->open = VECT_CLOSED_CODE;
146 
147  return 0;
148 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
int V1_close_ogr(struct Map_info *Map)
Definition: close_ogr.c:27
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
void dig_spidx_free(struct Plus_head *Plus)
Free spatial index (nodes, lines, areas, isles)
Definition: spindex.c:101
int Vect_save_topo(struct Map_info *Map)
Save topology file for vector map.
Definition: build.c:239
int V1_close_nat(struct Map_info *Map)
Definition: close_nat.c:25
const char * Vect_get_full_name(struct Map_info *Map)
Get full map name.
int stat
Definition: g3dcolor.c:369
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:33
int V2_close_ogr(struct Map_info *Map)
Definition: close_ogr.c:61
int
Definition: g3dcolor.c:48
void dig_cidx_free(struct Plus_head *Plus)
Definition: diglib/cindex.c:44
int Vect_coor_info(struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
void dig_free_plus(struct Plus_head *Plus)
Free Plus structure.
Definition: plus.c:241
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int Vect_close(struct Map_info *Map)
Close vector data file.
Definition: close.c:64
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
int Vect_cidx_save(struct Map_info *Map)
Save category index.
Definition: Vlib/cindex.c:444
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.