GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 <string.h>
19 #include <grass/gis.h>
20 #include <grass/Vect.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.Version_Major;
34  buf[1] = Map->head.Version_Minor;
35  buf[2] = Map->head.Back_Major;
36  buf[3] = Map->head.Back_Minor;
37 
38  buf[4] = Map->head.port.byte_order;
39  if (0 >= dig__fwrite_port_C(buf, 5, &(Map->dig_fp)))
40  return (0);
41 
42  /* bytes 6 - 9 : header size */
43  if (0 >= dig__fwrite_port_L(&length, 1, &(Map->dig_fp)))
44  return (0);
45 
46  /* byte 10 : dimension 2D or 3D */
47  buf[0] = Map->head.with_z;
48  if (0 >= dig__fwrite_port_C(buf, 1, &(Map->dig_fp)))
49  return (0);
50 
51  /* bytes 11 - 14 : size of coordinate file */
52  G_debug(1, "write coor size (%ld) to head", Map->head.size);
53  if (0 >= dig__fwrite_port_L(&(Map->head.size), 1, &(Map->dig_fp)))
54  return (0);
55 
56  G_debug(2, "coor body offset %ld", dig_ftell(&(Map->dig_fp)));
57  return (1);
58 }
59 
60 
61 int dig__read_head(struct Map_info *Map)
62 {
63  unsigned char buf[10];
64  struct Port_info port;
65 
66  dig_fseek(&(Map->dig_fp), 0L, 0);
67 
68  /* bytes 1 - 5 */
69  if (0 >= dig__fread_port_C(buf, 5, &(Map->dig_fp)))
70  return (0);
71  Map->head.Version_Major = buf[0];
72  Map->head.Version_Minor = buf[1];
73  Map->head.Back_Major = buf[2];
74  Map->head.Back_Minor = buf[3];
75  Map->head.port.byte_order = buf[4];
76 
77  G_debug(2,
78  "Coor header: file version %d.%d , supported from GRASS version %d.%d",
79  Map->head.Version_Major, Map->head.Version_Minor,
80  Map->head.Back_Major, Map->head.Back_Minor);
81 
82  G_debug(2, " byte order %d", Map->head.port.byte_order);
83 
84  /* check version numbers */
85  if (Map->head.Version_Major > GV_COOR_VER_MAJOR ||
86  Map->head.Version_Minor > GV_COOR_VER_MINOR) {
87  /* The file was created by GRASS library with higher version than this one */
88 
89  if (Map->head.Back_Major > GV_COOR_VER_MAJOR ||
90  Map->head.Back_Minor > GV_COOR_VER_MINOR) {
91  /* This version of GRASS lib is lower than the oldest which can read this format */
93  ("Vector 'coor' format version %d.%d is not supported by this version of GRASS. "
94  "Update your GRASS.", Map->head.Version_Major,
95  Map->head.Version_Minor);
96  return (-1);
97  }
98 
99  G_warning
100  ("Your GRASS version does not fully support vector format %d.%d."
101  " Consider to upgrade GRASS.", Map->head.Version_Major,
102  Map->head.Version_Minor);
103  }
104 
105  dig_init_portable(&port, Map->head.port.byte_order);
106  dig_set_cur_port(&port);
107 
108  /* bytes 6 - 9 : header size */
109  if (0 >= dig__fread_port_L(&(Map->head.head_size), 1, &(Map->dig_fp)))
110  return (0);
111  G_debug(2, " header size %ld", Map->head.head_size);
112 
113  /* byte 10 : dimension 2D or 3D */
114  if (0 >= dig__fread_port_C(buf, 1, &(Map->dig_fp)))
115  return (0);
116  Map->head.with_z = buf[0];
117  G_debug(2, " with_z %d", Map->head.with_z);
118 
119  /* bytes 11 - 14 : size of coordinate file */
120  if (0 >= dig__fread_port_L(&(Map->head.size), 1, &(Map->dig_fp)))
121  return (0);
122  G_debug(2, " coor size %ld", Map->head.size);
123 
124  /* Go to end of header, file may be written by new version of GRASS with longer header */
125 
126  dig_fseek(&(Map->dig_fp), Map->head.head_size, SEEK_SET);
127 
128  return (1);
129 }
int dig_set_cur_port(struct Port_info *port)
Definition: portable.c:640
int dig__read_head(struct Map_info *Map)
Definition: head.c:61
void dig_init_portable(struct Port_info *port, int byte_order)
Definition: portable.c:568
int dig__fread_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:137
int dig_fseek(GVFILE *file, long offset, int whence)
Set GVFILE position.
Definition: file.c:60
int dig__fwrite_port_L(long *buf, int cnt, GVFILE *fp)
Definition: portable.c:422
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
int dig__fread_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:347
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple Map
Definition: render.py:1310
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int dig__fwrite_port_C(char *buf, int cnt, GVFILE *fp)
Definition: portable.c:558
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
long dig_ftell(GVFILE *file)
Get GVFILE position.
Definition: file.c:36
int dig__write_head(struct Map_info *Map)
Definition: head.c:22