GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-6da4f45b8e
head.c
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * MODULE: Vector library
4  *
5  * AUTHOR(S): Original author CERL, probably Dave Gerdes.
6  * Update to GRASS 5.7 Radim Blazek.
7  *
8  * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
9  *
10  * COPYRIGHT: (C) 2001 by the GRASS Development Team
11  *
12  * This program is free software under the GNU General Public
13  * License (>=v2). Read the file COPYING that comes with GRASS
14  * for details.
15  *
16  *****************************************************************************/
17 
18 #include <inttypes.h>
19 #include <sys/types.h>
20 #include <string.h>
21 #include <grass/vector.h>
22 
23 int dig__write_head(struct Map_info *Map)
24 {
25  unsigned char buf[10];
26  long length = GV_COOR_HEAD_SIZE;
27 
28  G_debug(1, "dig__write_head()");
29 
30  dig_set_cur_port(&(Map->head.port));
31  dig_fseek(&(Map->dig_fp), 0L, 0);
32 
33  /* bytes 1 - 5 */
34  buf[0] = Map->head.coor_version.major;
35  buf[1] = Map->head.coor_version.minor;
36  buf[2] = Map->head.coor_version.back_major;
37  buf[3] = Map->head.coor_version.back_minor;
38 
39  buf[4] = Map->head.port.byte_order;
40  if (0 >= dig__fwrite_port_C((char *)buf, 5, &(Map->dig_fp)))
41  return (0);
42 
43  /* increase header size for new vectors, already set in V1_open_new_nat() */
44  length = Map->head.head_size;
45 
46  /* bytes 6 - 9 : header size */
47  if (0 >= dig__fwrite_port_L(&length, 1, &(Map->dig_fp)))
48  return (0);
49 
50  /* byte 10 : dimension 2D or 3D */
51  buf[0] = Map->head.with_z;
52  if (0 >= dig__fwrite_port_C((char *)buf, 1, &(Map->dig_fp)))
53  return (0);
54 
55  /* bytes 11 - 18 : size of coordinate file */
56  G_debug(1, "write coor size (%" PRId64 ") to head", Map->head.size);
57  if (Map->head.head_size >= GV_COOR_HEAD_SIZE + 4) {
58  if (Map->head.size > PORT_LONG_MAX) {
59  /* can only happen when sizeof(off_t) == 8 */
60  if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp),
61  sizeof(off_t)))
62  return (0);
63  }
64  else {
65  /* write twice to fill the space and set offset (account for
66  * sizeof(off_t) == 4) */
67  if (0 >=
68  dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
69  return (0);
70  if (0 >=
71  dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
72  return (0);
73  }
74  }
75  else {
76  /* old vector with shorter coor head size got modified */
77  /* bytes 11 - 14 : size of coordinate file */
78  if (0 >= dig__fwrite_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
79  return (0);
80  }
81 
82  G_debug(2, "coor body offset %" PRId64, dig_ftell(&(Map->dig_fp)));
83 
84  return 1;
85 }
86 
87 int dig__read_head(struct Map_info *Map)
88 {
89  unsigned char buf[10];
90  struct Port_info port;
91 
92  G_debug(2, "dig__read_head(): name = '%s'", Map->name);
93  dig_fseek(&(Map->dig_fp), 0L, 0);
94 
95  /* bytes 1 - 5 */
96  if (0 >= dig__fread_port_C((char *)buf, 5, &(Map->dig_fp)))
97  return (0);
98  Map->head.coor_version.major = buf[0];
99  Map->head.coor_version.minor = buf[1];
100  Map->head.coor_version.back_major = buf[2];
101  Map->head.coor_version.back_minor = buf[3];
102  Map->head.port.byte_order = buf[4];
103 
104  G_debug(
105  2,
106  "Coor header: file version %d.%d , supported from GRASS version %d.%d",
109 
110  G_debug(2, " byte order %d", Map->head.port.byte_order);
111 
112  /* check version numbers */
115  /* The file was created by GRASS library with higher version than this
116  * one */
117 
120  /* This version of GRASS lib is lower than the oldest which can read
121  * this format */
122  G_fatal_error("Vector 'coor' format version %d.%d is not supported "
123  "by this version of GRASS. "
124  "Update your GRASS.",
125  Map->head.coor_version.major,
126  Map->head.coor_version.minor);
127  return (-1);
128  }
129 
130  G_warning(
131  "Your GRASS version does not fully support vector format %d.%d."
132  " Consider to upgrade GRASS.",
134  }
135 
136  dig_init_portable(&port, Map->head.port.byte_order);
137  dig_set_cur_port(&port);
138 
139  /* bytes 6 - 9 : header size */
140  if (0 >= dig__fread_port_L(&(Map->head.head_size), 1, &(Map->dig_fp)))
141  return (0);
142  G_debug(2, " header size %ld", Map->head.head_size);
143 
144  /* byte 10 : dimension 2D or 3D */
145  if (0 >= dig__fread_port_C((char *)buf, 1, &(Map->dig_fp)))
146  return (0);
147  Map->head.with_z = buf[0];
148  G_debug(2, " with_z %d", Map->head.with_z);
149 
150  /* Map->head.size holds stats value */
151  if (Map->head.size > PORT_LONG_MAX &&
152  Map->head.head_size >= GV_COOR_HEAD_SIZE + 4) {
153  /* bytes 11 - 18 : size of coordinate file */
154  if (0 >= dig__fread_port_O(&(Map->head.size), 1, &(Map->dig_fp),
155  sizeof(off_t)))
156  return (0);
157  }
158  else {
159  /* bytes 11 - 14 : size of coordinate file */
160  if (0 >= dig__fread_port_O(&(Map->head.size), 1, &(Map->dig_fp), 4))
161  return (0);
162  }
163  G_debug(2, " coor size %" PRId64, Map->head.size);
164 
165  /* Go to end of header, file may be written by new version of GRASS with
166  * longer header */
167 
168  dig_fseek(&(Map->dig_fp), Map->head.head_size, SEEK_SET);
169 
170  return (1);
171 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
#define PORT_LONG_MAX
Definition: dig_defines.h:70
#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:150
#define GV_COOR_HEAD_SIZE
Coordinates file head size.
Definition: dig_defines.h:145
#define GV_COOR_VER_MINOR
Definition: dig_defines.h:151
int dig__fread_port_O(off_t *, size_t, struct gvfile *, size_t)
Read off_ts from the Portable Vector Format.
Definition: portable.c:167
int dig__fwrite_port_C(const char *, size_t, struct gvfile *)
Write chars to the Portable Vector Format.
Definition: portable.c:886
void dig_init_portable(struct Port_info *, int)
Set Port_info structure to byte order of file.
Definition: portable.c:900
int dig__fread_port_L(long *, size_t, struct gvfile *)
Read longs from the Portable Vector Format.
Definition: portable.c:262
int dig__fwrite_port_L(const long *, size_t, struct gvfile *)
Write longs to the Portable Vector Format.
Definition: portable.c:703
off_t dig_ftell(struct gvfile *file)
Get struct gvfile position.
Definition: file.c:36
int dig_set_cur_port(struct Port_info *)
Set current Port_info structure.
Definition: portable.c:996
int dig__fread_port_C(char *, size_t, struct gvfile *)
Read chars from the Portable Vector Format.
Definition: portable.c:511
int dig_fseek(struct gvfile *file, off_t offset, int whence)
Set struct gvfile position.
Definition: file.c:60
int dig__fwrite_port_O(const off_t *, size_t, struct gvfile *, size_t)
Write off_ts to the Portable Vector Format.
Definition: portable.c:636
int dig__read_head(struct Map_info *Map)
Definition: head.c:87
int dig__write_head(struct Map_info *Map)
Definition: head.c:23
Vector map info.
Definition: dig_structs.h:1243
struct gvfile dig_fp
GV file pointer (native format only)
Definition: dig_structs.h:1395
struct dig_head head
Header info.
Definition: dig_structs.h:1388
char * name
Map name (for 4.0)
Definition: dig_structs.h:1316
Portability info.
Definition: dig_structs.h:181
int byte_order
File byte order.
Definition: dig_structs.h:185
int minor
Current version (minor)
Definition: dig_structs.h:275
int back_major
Earliest version that can use this data format (major)
Definition: dig_structs.h:277
int back_minor
Earliest version that can use this data format (minor)
Definition: dig_structs.h:279
int major
Current version (major)
Definition: dig_structs.h:273
struct Version_info coor_version
Version info for coor file.
Definition: dig_structs.h:331
off_t size
Coor file size.
Definition: dig_structs.h:344
int with_z
2D/3D vector data
Definition: dig_structs.h:339
long head_size
Coor header size.
Definition: dig_structs.h:348
struct Port_info port
Portability information.
Definition: dig_structs.h:353