File: head.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (122 lines) | stat: -rw-r--r-- 4,190 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* $Id: head.c,v 1.7 2004/05/21 08:48:39 radim Exp $
*
****************************************************************************
*
* MODULE:       Vector library 
*   	    	
* AUTHOR(S):    Original author CERL, probably Dave Gerdes.
*               Update to GRASS 5.7 Radim Blazek.
*
* PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
*
* COPYRIGHT:    (C) 2001 by the GRASS Development Team
*
*               This program is free software under the GNU General Public
*   	    	License (>=v2). Read the file COPYING that comes with GRASS
*   	    	for details.
*
*****************************************************************************/
#include <string.h>
#include "gis.h"
#include "Vect.h"

int
dig__write_head ( struct Map_info *Map )
{
    unsigned char buf[10];	
    long length = GV_COOR_HEAD_SIZE;

    G_debug ( 1, "dig__write_head()");
    
    dig_set_cur_port (&(Map->head.port));
    dig_fseek ( &(Map->dig_fp), 0L, 0);
    
    /* bytes 1 - 5 */
    buf[0] = Map->head.Version_Major;
    buf[1] = Map->head.Version_Minor;
    buf[2] = Map->head.Back_Major;
    buf[3] = Map->head.Back_Minor;
    
    buf[4] = Map->head.port.byte_order;
    if (0 >= dig__fwrite_port_C ( buf, 5, &(Map->dig_fp) )) return (0);
    
    /* bytes 6 - 9 : header size */
    if (0 >= dig__fwrite_port_L ( &length, 1, &(Map->dig_fp) )) return (0);
    
    /* byte 10 : dimension 2D or 3D */
    buf[0] = Map->head.with_z;           
    if (0 >= dig__fwrite_port_C ( buf, 1, &(Map->dig_fp) )) return (0);
    
    /* bytes 11 - 14 : size of coordinate file */
    G_debug ( 1, "write coor size (%ld) to head", Map->head.size);
    if (0 >= dig__fwrite_port_L ( &(Map->head.size), 1, &(Map->dig_fp) )) return (0);

    G_debug (2, "coor body offset %d", dig_ftell( &(Map->dig_fp) ));
    return (1);    
}


int
dig__read_head ( struct Map_info *Map )
{
    unsigned char buf[10];
    struct Port_info port;

    dig_fseek ( &(Map->dig_fp), 0L, 0);
   
    /* bytes 1 - 5 */
    if (0 >= dig__fread_port_C ( buf, 5, &(Map->dig_fp) )) return (0);
    Map->head.Version_Major = buf[0];
    Map->head.Version_Minor = buf[1];
    Map->head.Back_Major    = buf[2];
    Map->head.Back_Minor    = buf[3];
    Map->head.port.byte_order    = buf[4];

    G_debug (2, "Coor header: file version %d.%d , supported from GRASS version %d.%d",
	         Map->head.Version_Major, Map->head.Version_Minor, 
		 Map->head.Back_Major, Map->head.Back_Minor );

    G_debug (2, "  byte order %d", Map->head.port.byte_order );
    
    /* check version numbers */
    if ( Map->head.Version_Major > GV_COOR_VER_MAJOR || Map->head.Version_Minor > GV_COOR_VER_MINOR ) {
      /* The file was created by GRASS library with higher version than this one */
	
      if ( Map->head.Back_Major > GV_COOR_VER_MAJOR || Map->head.Back_Minor > GV_COOR_VER_MINOR ) {
	  /* This version of GRASS lib is lower than the oldest which can read this format */
	  G_fatal_error ( "Vector 'coor' format version %d.%d is not supported by this version of GRASS. "
		  	  "Update your GRASS.", Map->head.Version_Major, Map->head.Version_Minor);
	  return (-1);
      }

      G_warning ( "Your GRASS version does not fully support vector format %d.%d."
		  " Consider to upgrade GRASS.",
		      Map->head.Version_Major, Map->head.Version_Minor );
    }

    dig_init_portable ( &port, Map->head.port.byte_order);
    dig_set_cur_port (&port);

    /* bytes 6 - 9 : header size */
    if (0 >= dig__fread_port_L ( &(Map->head.head_size), 1, &(Map->dig_fp) )) return (0);
    G_debug (2, "  header size %d", Map->head.head_size );

    /* byte 10 : dimension 2D or 3D */
    if (0 >= dig__fread_port_C ( buf, 1, &(Map->dig_fp) )) return (0);
    Map->head.with_z  = buf[0];
    G_debug (2, "  with_z %d", Map->head.with_z );

    /* bytes 11 - 14 : size of coordinate file */
    if (0 >= dig__fread_port_L ( &(Map->head.size), 1, &(Map->dig_fp) )) return (0);
    G_debug (2, "  coor size %d", Map->head.size );
    
    /* Go to end of header, file may be written by new version of GRASS with longer header */

    dig_fseek ( &(Map->dig_fp), Map->head.head_size, SEEK_SET );
    
    return (1);
}