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 <sys/types.h>
00021 #include <sys/stat.h>
00022 #include <unistd.h>
00023 #include "Vect.h"
00024 
00025 static int
00026 clo_dummy () {
00027     return -1;
00028 }
00029 
00030 static int format () { G_fatal_error ("Requested format is not compiled in this version"); return 0; }
00031 
00032 static int (*Close_array[][2]) () =
00033 {
00034      { clo_dummy, V1_close_nat }
00035 #ifdef HAVE_OGR
00036    , { clo_dummy, V1_close_ogr }
00037 #else
00038    ,{ clo_dummy, format }
00039 #endif
00040 };
00041 
00042 
00049 int 
00050 Vect_close (struct Map_info *Map)
00051 {
00052     struct Coor_info CInfo;
00053     
00054     G_debug (1, "Vect_close(): name = %s, mapset = %s, format = %d, level = %d",
00055                  Map->name, Map->mapset, Map->format, Map->level);
00056     
00057     /* Store support files if in write mode on level 2 */
00058     if ( strcmp(Map->mapset,G_mapset()) == 0 && Map->support_updated && Map->plus.built == GV_BUILD_ALL) {
00059         char buf[1000];
00060         char file_path[2000];
00061         struct stat info;
00062 
00063         /* Delete old support files if available */
00064         sprintf (buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00065 
00066         G__file_name ( file_path, buf, GV_TOPO_ELEMENT, G_mapset ());
00067         if (stat (file_path, &info) == 0)      /* file exists? */
00068             unlink (file_path);
00069 
00070         G__file_name ( file_path, buf, GV_SIDX_ELEMENT, G_mapset ());
00071         if (stat (file_path, &info) == 0)      /* file exists? */
00072             unlink (file_path);
00073 
00074         G__file_name ( file_path, buf, GV_CIDX_ELEMENT, G_mapset ());
00075         if (stat (file_path, &info) == 0)      /* file exists? */
00076             unlink (file_path);
00077 
00078         Vect_coor_info ( Map, &CInfo);
00079         Map->plus.coor_size = CInfo.size;
00080         Map->plus.coor_mtime = CInfo.mtime;
00081 
00082         Vect_save_topo ( Map );
00083 
00084         /* Spatial index is not saved */
00085         /* Vect_save_spatial_index ( Map ); */
00086 
00087         Vect_cidx_save ( Map );
00088 
00089 #ifdef HAVE_OGR
00090         if ( Map->format == GV_FORMAT_OGR ) 
00091             V2_close_ogr ( Map );
00092 #endif
00093     }
00094     
00095     if ( Map->level == 2 && Map->plus.release_support ) {
00096         G_debug (1, "free topology" );
00097         dig_free_plus ( &(Map->plus) );
00098 
00099         if ( !Map->head_only ) {
00100             G_debug (1, "free spatial index" );
00101             dig_spidx_free ( &(Map->plus) );
00102         }
00103 
00104         G_debug (1, "free category index" );
00105         dig_cidx_free ( &(Map->plus) );
00106 
00107     }
00108 
00109     if ( Map->format == GV_FORMAT_NATIVE ) {
00110         G_debug (1, "close history file" );
00111         if ( Map->hist_fp != NULL ) fclose ( Map->hist_fp );
00112     }
00113 
00114     /* Close level 1 files / data sources if not head_only */
00115     if ( !Map->head_only ) {
00116         if (  ((*Close_array[Map->format][1]) (Map)) != 0 ) { 
00117             G_warning ("Cannot close vector '%s'", Vect_get_full_name(Map) );
00118             return 1;
00119         }
00120     }
00121 
00122     free (Map->name); Map->name = NULL;
00123     free (Map->mapset); Map->mapset = NULL;
00124 
00125     Map->open = VECT_CLOSED_CODE;
00126 
00127     return 0;
00128 }
00129 

Generated on Wed Aug 23 17:49:00 2006 for GRASS by  doxygen 1.4.7