GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72846
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
open_nat.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/open_nat.c
3 
4  \brief Vector library - open vector map (native format) - level 1
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2001-2009 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Original author CERL, probably Dave Gerdes or Mike Higgins.
14  \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
15  */
16 
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include <grass/vector.h>
22 #include <grass/glocale.h>
23 
24 #include "local_proto.h"
25 
26 static int check_coor(struct Map_info *Map);
27 
28 /*!
29  \brief Open existing vector map (level 1)
30 
31  Map->name and Map->mapset must be set before.
32 
33  \param Map pointer to Map_info structure
34  \param update non-zero for write mode, otherwise read-only
35 
36  \return 0 success
37  \return -1 error
38 */
39 int V1_open_old_nat(struct Map_info *Map, int update)
40 {
41  char path[GPATH_MAX];
42  struct Coor_info CInfo;
43 
44  G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
45  Map->mapset);
46 
47  Vect__get_path(path, Map);
48  dig_file_init(&(Map->dig_fp));
49  if (update)
51  else
52  Map->dig_fp.file =
53  G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset);
54 
55  if (Map->dig_fp.file == NULL) {
56  G_warning(_("Unable to open coor file for vector map <%s>"),
57  Vect_get_full_name(Map));
58  return -1;
59  }
60 
61  /* needed to determine file size, Map->head.size will be updated
62  by dig__read_head(Map) */
63  Vect_coor_info(Map, &CInfo);
64  Map->head.size = CInfo.size;
65 
66  if (!(dig__read_head(Map))) {
67  G_debug(1, "dig__read_head(): failed");
68  return -1;
69  }
70 
71  /* compare coor size stored in head with real size */
72  /* check should catch if LFS is required but not available */
73  check_coor(Map);
74 
75  /* set conversion matrices */
77 
78  /* load to memory */
79  if (!update)
80  dig_file_load(&(Map->dig_fp)); /* has currently no effect, file never loaded */
81 
82  return 0;
83 }
84 
85 /*!
86  \brief Create new vector map (level 1)
87 
88  \param[out] Map pointer to Map_info structure
89  \param name vector map name to be created
90  \param with_z 2D or 3D (unused?)
91 
92  \return 0 success
93  \return -1 error
94 */
95 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
96 {
97  char path[GPATH_MAX];
98 
99  G_debug(1, "V1_open_new_nat(): name = %s with_z = %d is_tmp = %d",
100  name, with_z, Map->temporary);
101 
102  /* Set the 'coor' file version */
107 
108  Vect__get_path(path, Map);
109 
110  /* TODO: open better */
111  dig_file_init(&(Map->dig_fp));
112  Map->dig_fp.file = G_fopen_new(path, GV_COOR_ELEMENT);
113  if (Map->dig_fp.file == NULL)
114  return -1;
115  fclose(Map->dig_fp.file);
116 
117  dig_file_init(&(Map->dig_fp));
119  if (Map->dig_fp.file == NULL)
120  return -1;
121 
122  /* if overwrite OK, any existing files have already been deleted by
123  * Vect_open_new(): remove this check ? */
124  /* check to see if dig_plus file exists and if so, remove it */
126  if (access(path, F_OK) == 0)
127  unlink(path); /* remove topo file if exists */
128 
129  /* set conversion matrices */
131 
132  /* write coor header */
133  if (!(dig__write_head(Map)))
134  return -1;
135 
136  return 0;
137 }
138 
139 /* check file size */
140 int check_coor(struct Map_info *Map)
141 {
142  struct Coor_info CInfo;
143  off_t dif;
144 
145  /* NOTE: coor file is open */
146  Vect_coor_info(Map, &CInfo);
147  dif = CInfo.size - Map->head.size;
148  G_debug(1, "coor size in head = %lu, real coor file size= %lu",
149  (unsigned long) Map->head.size, (unsigned long) CInfo.size);
150 
151  if (dif > 0) {
152  G_warning(_("Coor file of vector map <%s@%s> is larger than it should be "
153  "(%ld bytes excess)"), Map->name, Map->mapset, dif);
154  }
155  else if (dif < 0) {
156  G_warning(_("Coor file of vector <%s@%s> is shorter than it should be "
157  "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
158  }
159  return 1;
160 }
struct Version_info coor_version
Version info for coor file.
Definition: dig_structs.h:339
char * name
Map name (for 4.0)
Definition: dig_structs.h:1332
char * Vect__get_element_path(char *file_path, const struct Map_info *Map, const char *element)
Get map element full path (internal use only)
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition: portable.c:905
int back_major
Earliest version that can use this data format (major)
Definition: dig_structs.h:284
off_t size
Total size (in bytes)
Definition: dig_structs.h:384
#define GV_COOR_EARLIEST_MAJOR
The oldest versions of the library, which are capable to read the files created by the current versio...
Definition: dig_defines.h:160
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
Create new vector map (level 1)
Definition: open_nat.c:95
#define GV_COOR_VER_MAJOR
The latest versions of files known by current version of the library. Used for new files...
Definition: dig_defines.h:149
const char * Vect_get_full_name(const struct Map_info *Map)
Get fully qualified name of vector map.
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition: dig_defines.h:20
FILE * G_fopen_modify(const char *element, const char *name)
Open a database file for update (r+ mode)
Definition: gis/open.c:308
#define NULL
Definition: ccmath.h:32
int dig__read_head(struct Map_info *)
Definition: head.c:83
#define GV_COOR_EARLIEST_MINOR
Definition: dig_defines.h:161
int major
Current version (major)
Definition: dig_structs.h:280
struct Port_info port
Portability information.
Definition: dig_structs.h:361
int dig_file_load(struct gvfile *file)
Load opened struct gvfile to memory.
Definition: file.c:187
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
Update Coor_info structure.
fclose(fd)
void dig_file_init(struct gvfile *file)
Initialize gvfile strcuture.
Definition: file.c:170
#define GPATH_MAX
Definition: gis.h:151
char * mapset
Mapset name.
Definition: dig_structs.h:1336
struct dig_head head
Header info.
Definition: dig_structs.h:1403
Vector map info.
Definition: dig_structs.h:1259
off_t size
Coor file size.
Definition: dig_structs.h:352
int byte_order
File byte order.
Definition: dig_structs.h:191
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:220
FILE * file
File descriptor.
Definition: dig_structs.h:101
#define GV_COOR_VER_MINOR
Definition: dig_defines.h:150
int dig__byte_order_out()
Get byte order.
Definition: portable.c:1013
int temporary
Temporary map flag.
Definition: dig_structs.h:1276
Coor file info.
Definition: dig_structs.h:379
Definition: path.h:16
#define _(str)
Definition: glocale.h:13
struct gvfile dig_fp
GV file pointer (native format only)
Definition: dig_structs.h:1410
int minor
Current version (minor)
Definition: dig_structs.h:282
char * Vect__get_path(char *path, const struct Map_info *Map)
Get map directory name (internal use only)
int back_minor
Earliest version that can use this data format (minor)
Definition: dig_structs.h:286
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:253
const char * name
Definition: named_colr.c:7
#define GV_COOR_ELEMENT
Native format, coordinates.
Definition: dig_defines.h:12
int dig__write_head(struct Map_info *)
Definition: head.c:22
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
int V1_open_old_nat(struct Map_info *Map, int update)
Open existing vector map (level 1)
Definition: open_nat.c:39