File: open.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 (288 lines) | stat: -rw-r--r-- 7,431 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 *****************************************************************
 * open routines
 *
 * G__open (element, name, mapset, mode)
 *      char *element         database element name
 *      char *name            map file name
 *      char *mapset          mapset containing map "name"
 *      int mode              0=read, 1=write, 2=read/write
 * 
 *      this is the lowest level open routine.
 *      opens the file 'name' in 'element' ("cell", etc)
 *      in mapset 'mapset' according to the i/o 'mode'
 *
 *      mode = 0 (read) will look for 'name' in 'mapset'
 *               and open the file for read only
 *               the file must exist
 *
 *      mode = 1 (write) will create an empty file 'name' in the
 *               current mapset and open the file for write only
 *               'mapset' ignored
 *
 *      mode = 2 (read and write) will open a file in the
 *               current mapset for reading and writing
 *               creating a new file if necessary
 *               'mapset' ignored
 *
 *      returns: open file descriptor (int)
 *               or -1 could not open
 *
 *******************************************************************
 * G_open_new (element, name)
 *      char *element         database element name
 *      char *name            map file name
 *
 *      creates 'name' in the current mapset and opens it
 *      for write only.
 *
 *      returns: open file descriptor (int)
 *               or -1 could not open
 *
 *******************************************************************
 * G_open_old (element, name, mapset)
 *      char *element         database element name
 *      char *name            map file name
 *      char *mapset          mapset containing map "name"
 *
 *      opens 'name' in 'mapset' for read only.
 *
 *      returns: open file descriptor (int)
 *               or -1 could not open
 *
 *******************************************************************
 * G_fopen_new (element, name)
 *      char *element         database element name
 *      char *name            map file name
 *
 *      creates 'name' in the current mapset and opens it
 *      for write only.
 *
 *      returns: open file descriptor (FILE *)
 *               or NULL could not open
 *
 *******************************************************************
 * G_fopen_old (element, name, mapset)
 *      char *element         database element name
 *      char *name            map file name
 *
 *      opens 'name' in 'mapset' for read only.
 *
 *      returns: open file descriptor (FILE *)
 *               or NULL could not open
 *******************************************************************/

#include "config.h"
#include <string.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include "gis.h"
#include <unistd.h>
#include <fcntl.h>

int G__open (
    char *element,
    char *name,
    char *mapset,
    int mode)
{
    char path[1024];
    char xname[512], xmapset[512], *dummy;


    G__check_gisinit();

/* READ */
    if (mode == 0)
    {
	if (G__name_is_fully_qualified (name, xname, xmapset))
	{
	    if (strcmp (xmapset, mapset) != 0) {
		fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n",
			mapset,xmapset);
		    return -1;
	    }
	    name = xname;
	}
	if ((dummy = G_find_file (element, name, mapset)) == NULL)
	    return -1;
	G_free (dummy);
	G__file_name (path, element, name, mapset);

	return open (path, 0);
    }
/* WRITE */
    if (mode == 1 || mode == 2)
    {
	if (G__name_is_fully_qualified (name, xname, xmapset))
	{
	    if (strcmp (xmapset, G_mapset()) != 0) {
		fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n",
			xmapset,G_mapset());
		return -1;
	    }
	    name = xname;
	}

	if (G_legal_filename(name) == -1)
	    return -1;

	G__file_name (path, element, name, G_mapset());
	if(mode == 1 || access(path,0) != 0)
	{
	    G__make_mapset_element (element);
	    close (creat (path, 0666));
	}

	return open (path, mode);
    }
    return -1;
}


/*!
 * \brief open a new database file
 *
 * The database file <b>name</b> under the <b>element</b> in the
 * current mapset is created and opened for writing (but not reading).
 * The UNIX open( ) routine is used to open the file. If the file does not exist,
 * -1 is returned. Otherwise the file is positioned at the end of the file and
 * the file descriptor from the open( ) is returned.
 *
 *  \param element
 *  \param name
 *  \return int
 */

int G_open_new (char *element,char *name)
{
    return G__open (element, name, G_mapset(), 1);
}


/*!
 * \brief open a database file for reading
 *
 * The database file <b>name</b> under the
 * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
 * not for writing).
 * The UNIX open( ) routine is used to open the file. If the file does not exist,
 * -1 is returned. Otherwise the file descriptor from the open( ) is returned.
 *
 *  \param element
 *  \param name
 *  \param mapset
 *  \return int
 */

int G_open_old (char *element,char *name,char *mapset)
{
    return G__open (element, name, mapset, 0);
}


/*!
 * \brief open a database file for update
 *
 * The database file <b>name</b> under the <b>element</b> in the
 * current mapset is opened for reading and writing.
 * The UNIX open( ) routine is used to open the file. If the file does not exist,
 * -1 is returned. Otherwise the file is positioned at the end of the file and
 * the file descriptor from the open( ) is returned.
 *
 *  \param element
 *  \param name
 *  \return int
 */

int G_open_update (char *element,char *name)
{
    int fd;
    fd = G__open (element, name, G_mapset(), 2);
    if (fd >= 0) lseek (fd, 0L, 2);
    return fd;
}


/*!
 * \brief open a new database file
 *
 * The database file <b>name</b> under the <b>element</b> in the
 * current mapset is created and opened for writing (but not reading).
 * The UNIX fopen( ) routine, with "w" write mode, is used to open the file.  If
 * the file does not exist, the NULL pointer is returned. Otherwise the file is
 * positioned at the end of the file and the file descriptor from the fopen( ) is
 * returned.
 *
 *  \param element
 *  \param name
 *  \return FILE * 
 */

FILE *G_fopen_new (char *element,char *name)
{
    int fd;

    fd = G__open (element, name, G_mapset(), 1);
    if (fd < 0)
	return (FILE *) 0;

    return fdopen (fd, "w");
}


/*!
 * \brief open a database file for reading
 *
 * The database file <b>name</b> under the
 * <b>element</b> in the specified <b>mapset</b> is opened for reading (but
 * not for writing).
 * The UNIX fopen( ) routine, with "r" read mode, is used to open the file.  If
 * the file does not exist, the NULL pointer is returned. Otherwise the file
 * descriptor from the fopen( ) is returned.
 *
 *  \param element
 *  \param name
 *  \param mapset
 *  \return FILE * 
 */

FILE *
G_fopen_old (char *element,char *name,char *mapset)
{
    int fd;

    fd = G__open (element, name, mapset, 0);
    if (fd < 0)
	return (FILE *) 0;

    return fdopen (fd, "r");
}

FILE *
G_fopen_append (char *element,char *name)
{
    int fd;

    fd = G__open (element, name, G_mapset(), 2);
    if (fd < 0)
	return (FILE *) 0;
    lseek (fd, 0L, 2);

    return fdopen (fd, "a");
}

FILE *G_fopen_modify (char *element,char *name)
{
    int fd;

    fd = G__open (element, name, G_mapset(), 2);
    if (fd < 0)
	return (FILE *) 0;
    lseek (fd, 0L, 0);

    return fdopen (fd, "r+");
}