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