File: open_nat.c

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (153 lines) | stat: -rw-r--r-- 3,866 bytes parent folder | download | duplicates (3)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*!
   \file open_nat.c

   \brief Vector library - open vector map (native format)

   Higher level functions for reading/writing/manipulating vectors.

   (C) 2001-2008 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.

   \author Original author CERL, probably Dave Gerdes or Mike Higgins.
   Update to GRASS 5.7 Radim Blazek and David D. Gray.

   \date 2001
 */

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>

#include <grass/Vect.h>
#include <grass/gis.h>
#include <grass/glocale.h>

static char name_buf[GPATH_MAX];
static int check_coor(struct Map_info *Map);

/**
   \brief Open existing vector map

   Map->name and Map->mapset must be set before.

   \param Map poiter to vector map
   \param update non-zero for write mode, otherwise read-only

   \return 0 success
   \return -1 error
*/
int V1_open_old_nat(struct Map_info *Map, int update)
{
    char buf[1000];

    G_debug(1, "V1_open_old_nat(): name = %s mapset = %s", Map->name,
	    Map->mapset);

    sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, Map->name);
    dig_file_init(&(Map->dig_fp));
    if (update)
	Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
    else
	Map->dig_fp.file =
	    G_fopen_old(buf, GRASS_VECT_COOR_ELEMENT, Map->mapset);

    if (Map->dig_fp.file == NULL)
	return -1;

    if (!(dig__read_head(Map)))
	return (-1);
    check_coor(Map);

    /* set conversion matrices */
    dig_init_portable(&(Map->head.port), Map->head.port.byte_order);

    /* load to memory */
    if (!update)
	dig_file_load(&(Map->dig_fp));

    return (0);
}

/**
   \brief Open/Create new vector map.

   \param Map pointer to vector map
   \param name map name
   \param with_z 2D or 3D (unused?)


   \return 0 success
   \return -1 error 
*/
int V1_open_new_nat(struct Map_info *Map, const char *name, int with_z)
{
    char buf[1000];
    struct stat info;

    G_debug(1, "V1_open_new_nat(): name = %s", name);

    sprintf(buf, "%s/%s", GRASS_VECT_DIRECTORY, name);

    /* Set the 'coor' file version */
    Map->head.Version_Major = GV_COOR_VER_MAJOR;
    Map->head.Version_Minor = GV_COOR_VER_MINOR;
    Map->head.Back_Major = GV_COOR_EARLIEST_MAJOR;
    Map->head.Back_Minor = GV_COOR_EARLIEST_MINOR;

    /* TODO open better */
    dig_file_init(&(Map->dig_fp));
    Map->dig_fp.file = G_fopen_new(buf, GRASS_VECT_COOR_ELEMENT);
    if (Map->dig_fp.file == NULL)
	return (-1);
    fclose(Map->dig_fp.file);

    dig_file_init(&(Map->dig_fp));
    Map->dig_fp.file = G_fopen_modify(buf, GRASS_VECT_COOR_ELEMENT);
    if (Map->dig_fp.file == NULL)
	return (-1);

    /* check to see if dig_plus file exists and if so, remove it */
    G__file_name(name_buf, buf, GV_TOPO_ELEMENT, G_mapset());
    if (stat(name_buf, &info) == 0)	/* file exists? */
	unlink(name_buf);

    G__file_name(name_buf, buf, GRASS_VECT_COOR_ELEMENT, G_mapset());

    Map->head.size = 0;
    Map->head.head_size = GV_COOR_HEAD_SIZE;
    Vect__write_head(Map);

    /* set conversion matrices */
    dig_init_portable(&(Map->head.port), dig__byte_order_out());

    if (!(dig__write_head(Map)))
	return (-1);

    return 0;
}

/* check file size */
int check_coor(struct Map_info *Map)
{
    struct Coor_info CInfo;
    long dif;

    Vect_coor_info(Map, &CInfo);
    dif = CInfo.size - Map->head.size;
    G_debug(1, "coor size in head = %ld, real coor file size= %ld",
	    Map->head.size, CInfo.size);

    if (dif > 0) {
	G_warning(_("Coor files of vector map <%s@%s> is larger than it should be "
		   "(%ld bytes excess)"), Map->name, Map->mapset, dif);
    }
    else if (dif < 0) {
	G_warning(_("Coor files of vector <%s@%s> is shorter than it should be "
		   "(%ld bytes missing)."), Map->name, Map->mapset, -dif);
    }
    return 1;
}