GRASS Programmer's Manual  6.4.1(2011)
close.c
Go to the documentation of this file.
00001 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <sys/types.h>
00025 #include <sys/stat.h>
00026 #include <unistd.h>
00027 #include <grass/Vect.h>
00028 #include <grass/glocale.h>
00029 
00030 static int clo_dummy()
00031 {
00032     return -1;
00033 }
00034 
00035 #ifndef HAVE_OGR
00036 static int format()
00037 {
00038     G_fatal_error(_("Requested format is not compiled in this version"));
00039     return 0;
00040 }
00041 #endif
00042 
00043 static int (*Close_array[][2]) () = {
00044     {
00045     clo_dummy, V1_close_nat}
00046 #ifdef HAVE_OGR
00047     , {
00048     clo_dummy, V1_close_ogr}
00049 #else
00050     , {
00051     clo_dummy, format}
00052 #endif
00053 };
00054 
00055 
00064 int Vect_close(struct Map_info *Map)
00065 {
00066     struct Coor_info CInfo;
00067 
00068     G_debug(1,
00069             "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
00070             Map->name, Map->mapset, Map->format, Map->level);
00071 
00072     /* Store support files if in write mode on level 2 */
00073     if (strcmp(Map->mapset, G_mapset()) == 0 && Map->support_updated &&
00074         Map->plus.built == GV_BUILD_ALL) {
00075         char buf[GPATH_MAX];
00076         char file_path[GPATH_MAX];
00077         struct stat info;
00078 
00079         /* Delete old support files if available */
00080         sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00081 
00082         G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
00083         if (stat(file_path, &info) == 0)        /* file exists? */
00084             unlink(file_path);
00085 
00086         G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
00087         if (stat(file_path, &info) == 0)        /* file exists? */
00088             unlink(file_path);
00089 
00090         G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
00091         if (stat(file_path, &info) == 0)        /* file exists? */
00092             unlink(file_path);
00093 
00094         Vect_coor_info(Map, &CInfo);
00095         Map->plus.coor_size = CInfo.size;
00096         Map->plus.coor_mtime = CInfo.mtime;
00097 
00098         Vect_save_topo(Map);
00099 
00100         /* Spatial index is not saved */
00101         /* Vect_save_spatial_index ( Map ); */
00102 
00103         Vect_cidx_save(Map);
00104 
00105 #ifdef HAVE_OGR
00106         if (Map->format == GV_FORMAT_OGR)
00107             V2_close_ogr(Map);
00108 #endif
00109     }
00110 
00111     if (Map->level == 2 && Map->plus.release_support) {
00112         G_debug(1, "free topology");
00113         dig_free_plus(&(Map->plus));
00114 
00115         if (!Map->head_only) {
00116             G_debug(1, "free spatial index");
00117             dig_spidx_free(&(Map->plus));
00118         }
00119 
00120         G_debug(1, "free category index");
00121         dig_cidx_free(&(Map->plus));
00122 
00123     }
00124 
00125     if (Map->format == GV_FORMAT_NATIVE) {
00126         G_debug(1, "close history file");
00127         if (Map->hist_fp != NULL)
00128             fclose(Map->hist_fp);
00129     }
00130 
00131     /* Close level 1 files / data sources if not head_only */
00132     if (!Map->head_only) {
00133         if (((*Close_array[Map->format][1]) (Map)) != 0) {
00134             G_warning(_("Unable to close vector <%s>"),
00135                       Vect_get_full_name(Map));
00136             return 1;
00137         }
00138     }
00139 
00140     G_free((void *)Map->name);
00141     Map->name = NULL;
00142     G_free((void *)Map->mapset);
00143     Map->mapset = NULL;
00144 
00145     Map->open = VECT_CLOSED_CODE;
00146 
00147     return 0;
00148 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines