open_nat.c

Go to the documentation of this file.
00001 
00021 #include <unistd.h>
00022 #include <sys/types.h>
00023 #include <sys/stat.h>
00024 
00025 #include <grass/Vect.h>
00026 #include <grass/gis.h>
00027 #include <grass/glocale.h>
00028 
00029 static char name_buf[GPATH_MAX];
00030 static int check_coor(struct Map_info *Map);
00031 
00043 int V1_open_old_nat(struct Map_info *Map, int update)
00044 {
00045     char buf[1000];
00046 
00047     G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
00048             Map->mapset);
00049 
00050     sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
00051     dig_file_init(&(Map->dig_fp));
00052     if (update)
00053         Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
00054     else
00055         Map->dig_fp.file =
00056             G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
00057 
00058     if (Map->dig_fp.file == NULL)
00059         return -1;
00060 
00061     if (!(dig__read_head(Map)))
00062         return (-1);
00063     check_coor(Map);
00064 
00065     /* set conversion matrices */
00066     dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
00067 
00068     /* load to memory */
00069     if (!update)
00070         dig_file_load(&(Map->dig_fp));
00071 
00072     return (0);
00073 }
00074 
00086 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
00087 {
00088     char buf[1000];
00089     struct stat info;
00090 
00091     G_debug(1, "V1_open_new_nat(): name = %s", name);
00092 
00093     sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
00094 
00095     /* Set the 'coor' file version */
00096     Map->head.Version_Major = GV_COOR_VER_MAJOR;
00097     Map->head.Version_Minor = GV_COOR_VER_MINOR;
00098     Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
00099     Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
00100 
00101     /* TODO open better */
00102     dig_file_init(&(Map->dig_fp));
00103     Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
00104     if (Map->dig_fp.file == NULL)
00105         return (-1);
00106     fclose(Map->dig_fp.file);
00107 
00108     dig_file_init(&(Map->dig_fp));
00109     Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
00110     if (Map->dig_fp.file == NULL)
00111         return (-1);
00112 
00113     /* check to see if dig_plus file exists and if so, remove it */
00114     G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
00115     if (stat(name_buf, &info) == 0)     /* file exists? */
00116         unlink(name_buf);
00117 
00118     G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());
00119 
00120     Map->head.size = 0;
00121     Map->head.head_size = GV_COOR_HEAD_SIZE;
00122     Vect__write_head(Map);
00123 
00124     /* set conversion matrices */
00125     dig_init_portable(&(Map->head.port), dig__byte_order_out());
00126 
00127     if (!(dig__write_head(Map)))
00128         return (-1);
00129 
00130     return 0;
00131 }
00132 
00133 /* check file size */
00134 int check_coor(struct Map_info *Map)
00135 {
00136     struct Coor_info CInfo;
00137     long dif;
00138 
00139     Vect_coor_info(Map, &CInfo);
00140     dif = CInfo.size - Map->head.size;
00141     G_debug(1, "coor size in head = %ld, real coor file size= %ld",
00142             Map->head.size, CInfo.size);
00143 
00144     if (dif > 0) {
00145         G_warning(_("Coor files of vector map <%s@%s> is larger than it should be "
00146                    "(%ld bytes excess)"), Map->name, Map->mapset, dif);
00147     }
00148     else if (dif < 0) {
00149         G_warning(_("Coor files of vector <%s@%s> is shorter than it should be "
00150                    "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
00151     }
00152     return 1;
00153 }

Generated on Thu Jul 16 13:21:17 2009 for GRASS Programmer's Manual by  doxygen 1.5.6