GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
open_nat.c
Go to the documentation of this file.
1 
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 #include <grass/Vect.h>
26 #include <grass/gis.h>
27 #include <grass/glocale.h>
28 
29 static char name_buf[GPATH_MAX];
30 static int check_coor(struct Map_info *Map);
31 
43 int V1_open_old_nat(struct Map_info *Map, int update)
44 {
45  char buf[1000];
46 
47  G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
48  Map->mapset);
49 
50  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
51  dig_file_init(&(Map->dig_fp));
52  if (update)
53  Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
54  else
55  Map->dig_fp.file =
56  G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);
57 
58  if (Map->dig_fp.file == NULL)
59  return -1;
60 
61  if (!(dig__read_head(Map)))
62  return (-1);
63  check_coor(Map);
64 
65  /* set conversion matrices */
66  dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
67 
68  /* load to memory */
69  if (!update)
70  dig_file_load(&(Map->dig_fp));
71 
72  return (0);
73 }
74 
86 int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
87 {
88  char buf[1000];
89  struct stat info;
90 
91  G_debug(1, "V1_open_new_nat(): name = %s", name);
92 
93  sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);
94 
95  /* Set the 'coor' file version */
96  Map->head.Version_Major = GV_COOR_VER_MAJOR;
97  Map->head.Version_Minor = GV_COOR_VER_MINOR;
98  Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
99  Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;
100 
101  /* TODO open better */
102  dig_file_init(&(Map->dig_fp));
103  Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
104  if (Map->dig_fp.file == NULL)
105  return (-1);
106  fclose(Map->dig_fp.file);
107 
108  dig_file_init(&(Map->dig_fp));
109  Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
110  if (Map->dig_fp.file == NULL)
111  return (-1);
112 
113  /* check to see if dig_plus file exists and if so, remove it */
114  G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
115  if (stat(name_buf, &info) == 0) /* file exists? */
116  unlink(name_buf);
117 
118  G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());
119 
120  Map->head.size = 0;
121  Map->head.head_size = GV_COOR_HEAD_SIZE;
122  Vect__write_head(Map);
123 
124  /* set conversion matrices */
125  dig_init_portable(&(Map->head.port), dig__byte_order_out());
126 
127  if (!(dig__write_head(Map)))
128  return (-1);
129 
130  return 0;
131 }
132 
133 /* check file size */
134 int check_coor(struct Map_info *Map)
135 {
136  struct Coor_info CInfo;
137  long dif;
138 
139  Vect_coor_info(Map, &CInfo);
140  dif = CInfo.size - Map->head.size;
141  G_debug(1, "coor size in head = %ld, real coor file size= %ld",
142  Map->head.size, CInfo.size);
143 
144  if (dif > 0) {
145  G_warning(_("Coor files of vector map <%s@%s> is larger than it should be "
146  "(%ld bytes excess)"), Map->name, Map->mapset, dif);
147  }
148  else if (dif < 0) {
149  G_warning(_("Coor files of vector <%s@%s> is shorter than it should be "
150  "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
151  }
152  return 1;
153 }