GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Original author CERL, probably Dave Gerdes or Mike Higgins.
12 \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
13 */
14
15#include <inttypes.h>
16#include <unistd.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19
20#include <grass/vector.h>
21#include <grass/glocale.h>
22
23#include "local_proto.h"
24
25static int check_coor(struct Map_info *Map);
26
27/*!
28 \brief Open existing vector map (level 1)
29
30 Map->name and Map->mapset must be set before.
31
32 \param Map pointer to Map_info structure
33 \param update non-zero for write mode, otherwise read-only
34
35 \return 0 success
36 \return -1 error
37 */
38int V1_open_old_nat(struct Map_info *Map, int update)
39{
40 char path[GPATH_MAX];
41 struct Coor_info CInfo;
42
43 G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
44 Map->mapset);
45
47 dig_file_init(&(Map->dig_fp));
48 if (update)
49 Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
50 else
51 Map->dig_fp.file = G_fopen_old(path, GV_COOR_ELEMENT, Map->mapset);
52
53 if (Map->dig_fp.file == NULL) {
54 const char *map_name = Vect_get_full_name(Map);
55 G_warning(_("Unable to open coor file for vector map <%s>"), map_name);
56 G_free((void *)map_name);
57 return -1;
58 }
59
60 /* needed to determine file size, Map->head.size will be updated
61 by dig__read_head(Map) */
63 Map->head.size = CInfo.size;
64
65 if (!(dig__read_head(Map))) {
66 G_debug(1, "dig__read_head(): failed");
67 return -1;
68 }
69
70 /* compare coor size stored in head with real size */
71 /* check should catch if LFS is required but not available */
72 check_coor(Map);
73
74 /* set conversion matrices */
75 dig_init_portable(&(Map->head.port), Map->head.port.byte_order);
76
77 /* load to memory */
78 if (!update)
80 &(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 */
95int 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", name,
100 with_z, Map->temporary);
101
102 /* Set the 'coor' file version */
103 Map->head.coor_version.major = GV_COOR_VER_MAJOR;
104 Map->head.coor_version.minor = GV_COOR_VER_MINOR;
105 Map->head.coor_version.back_major = GV_COOR_EARLIEST_MAJOR;
106 Map->head.coor_version.back_minor = GV_COOR_EARLIEST_MINOR;
107
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));
118 Map->dig_fp.file = G_fopen_modify(path, GV_COOR_ELEMENT);
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 */
130 dig_init_portable(&(Map->head.port), dig__byte_order_out());
131
132 /* write coor header */
133 if (!(dig__write_head(Map)))
134 return -1;
135
136 return 0;
137}
138
139/* check file size */
140int check_coor(struct Map_info *Map)
141{
142 struct Coor_info CInfo;
143 off_t dif;
144
145 /* NOTE: coor file is open */
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(
153 _("Coor file of vector map <%s@%s> is larger than it should be "
154 "(%" PRId64 " bytes excess)"),
155 Map->name, Map->mapset, dif);
156 }
157 else if (dif < 0) {
158 G_warning(_("Coor file of vector <%s@%s> is shorter than it should be "
159 "(%" PRId64 " bytes missing)."),
160 Map->name, Map->mapset, -dif);
161 }
162 return 1;
163}
#define NULL
Definition ccmath.h:32
AMI_err name(char **stream_name)
Definition ami_stream.h:426
FILE * G_fopen_modify(const char *, const char *)
Open a database file for update (r+ mode)
Definition gis/open.c:310
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
FILE * G_fopen_new(const char *, const char *)
Open a new database file.
Definition gis/open.c:218
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:250
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_coor_info(struct Map_info *, struct Coor_info *)
Update Coor_info structure.
const char * Vect_get_full_name(struct Map_info *)
Get fully qualified name of vector map.
#define GV_COOR_EARLIEST_MINOR
#define GV_COOR_VER_MAJOR
The latest versions of files known by current version of the library. Used for new files.
#define GV_COOR_ELEMENT
Native format, coordinates.
Definition dig_defines.h:12
#define GV_COOR_VER_MINOR
#define GV_TOPO_ELEMENT
Native format, topology file.
Definition dig_defines.h:20
#define GV_COOR_EARLIEST_MAJOR
The oldest versions of the library, which are capable to read the files created by the current versio...
int dig_file_load(struct gvfile *file)
Load opened struct gvfile to memory.
Definition file.c:186
int dig__read_head(struct Map_info *)
Definition head.c:84
int dig__byte_order_out(void)
Get byte order.
Definition portable.c:1006
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition portable.c:898
int dig__write_head(struct Map_info *)
Definition head.c:20
void dig_file_init(struct gvfile *file)
Initialize gvfile structure.
Definition file.c:169
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
int V1_open_old_nat(struct Map_info *Map, int update)
Open existing vector map (level 1)
Definition open_nat.c:38
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
Coor file info.
Vector map info.
Definition path.h:15
#define unlink
Definition unistd.h:11
#define access
Definition unistd.h:7
#define F_OK
Definition unistd.h:22
char * Vect__get_element_path(char *file_path, struct Map_info *Map, const char *element)
Get map element full path (internal use only)
char * Vect__get_path(char *path, struct Map_info *Map)
Get map directory name (internal use only)