close.c

Go to the documentation of this file.
00001 /*
00002 ****************************************************************************
00003 *
00004 * MODULE:       Vector library 
00005 *               
00006 * AUTHOR(S):    Original author CERL, probably Dave Gerdes or Mike Higgins.
00007 *               Update to GRASS 5.7 Radim Blazek and David D. Gray.
00008 *
00009 * PURPOSE:      Higher level functions for reading/writing/manipulating vectors.
00010 *
00011 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
00012 *
00013 *               This program is free software under the GNU General Public
00014 *               License (>=v2). Read the file COPYING that comes with GRASS
00015 *               for details.
00016 *
00017 *****************************************************************************/
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 #include <sys/types.h>
00022 #include <sys/stat.h>
00023 #include <unistd.h>
00024 #include <grass/Vect.h>
00025 
00026 static int
00027 clo_dummy () {
00028     return -1;
00029 }
00030 
00031 #ifndef HAVE_OGR
00032 static int format () { G_fatal_error ("Requested format is not compiled in this version"); return 0; }
00033 #endif
00034 
00035 static int (*Close_array[][2]) () =
00036 {
00037      { clo_dummy, V1_close_nat }
00038 #ifdef HAVE_OGR
00039    , { clo_dummy, V1_close_ogr }
00040 #else
00041    ,{ clo_dummy, format }
00042 #endif
00043 };
00044 
00045 
00052 int 
00053 Vect_close (struct Map_info *Map)
00054 {
00055     struct Coor_info CInfo;
00056     
00057     G_debug (1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
00058                  Map->name, Map->mapset, Map->format, Map->level);
00059     
00060     /* Store support files if in write mode on level 2 */
00061     if ( strcmp(Map->mapset,G_mapset()) == 0 && Map->support_updated && Map->plus.built == GV_BUILD_ALL) {
00062         char buf[1000];
00063         char file_path[2000];
00064         struct stat info;
00065 
00066         /* Delete old support files if available */
00067         sprintf (buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00068 
00069         G__file_name ( file_path, buf, GV_TOPO_ELEMENT, G_mapset ());
00070         if (stat (file_path, &info) == 0)      /* file exists? */
00071             unlink (file_path);
00072 
00073         G__file_name ( file_path, buf, GV_SIDX_ELEMENT, G_mapset ());
00074         if (stat (file_path, &info) == 0)      /* file exists? */
00075             unlink (file_path);
00076 
00077         G__file_name ( file_path, buf, GV_CIDX_ELEMENT, G_mapset ());
00078         if (stat (file_path, &info) == 0)      /* file exists? */
00079             unlink (file_path);
00080 
00081         Vect_coor_info ( Map, &CInfo);
00082         Map->plus.coor_size = CInfo.size;
00083         Map->plus.coor_mtime = CInfo.mtime;
00084 
00085         Vect_save_topo ( Map );
00086 
00087         /* Spatial index is not saved */
00088         /* Vect_save_spatial_index ( Map ); */
00089 
00090         Vect_cidx_save ( Map );
00091 
00092 #ifdef HAVE_OGR
00093         if ( Map->format == GV_FORMAT_OGR ) 
00094             V2_close_ogr ( Map );
00095 #endif
00096     }
00097     
00098     if ( Map->level == 2 && Map->plus.release_support ) {
00099         G_debug (1, "free topology" );
00100         dig_free_plus ( &(Map->plus) );
00101 
00102         if ( !Map->head_only ) {
00103             G_debug (1, "free spatial index" );
00104             dig_spidx_free ( &(Map->plus) );
00105         }
00106 
00107         G_debug (1, "free category index" );
00108         dig_cidx_free ( &(Map->plus) );
00109 
00110     }
00111 
00112     if ( Map->format == GV_FORMAT_NATIVE ) {
00113         G_debug (1, "close history file" );
00114         if ( Map->hist_fp != NULL ) fclose ( Map->hist_fp );
00115     }
00116 
00117     /* Close level 1 files / data sources if not head_only */
00118     if ( !Map->head_only ) {
00119         if (  ((*Close_array[Map->format][1]) (Map)) != 0 ) { 
00120             G_warning ("Cannot close vector '%s'", Vect_get_full_name(Map) );
00121             return 1;
00122         }
00123     }
00124 
00125     free (Map->name); Map->name = NULL;
00126     free (Map->mapset); Map->mapset = NULL;
00127 
00128     Map->open = VECT_CLOSED_CODE;
00129 
00130     return 0;
00131 }
00132 

Generated on Sun Apr 6 17:32:44 2008 for GRASS by  doxygen 1.5.5