~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/gis/open.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*!
2
 
 * \file gis/open.c
 
2
 * \file lib/gis/open.c
3
3
 * 
4
 
 * \brief GIS Library - open file functions
 
4
 * \brief GIS Library - Open file functions
5
5
 *
6
 
 * (C) 1999-2008 by the GRASS Development Team
 
6
 * (C) 1999-2009 by the GRASS Development Team
7
7
 *
8
8
 * This program is free software under the GNU General Public
9
9
 * License (>=v2). Read the file COPYING that comes with GRASS
13
13
 */
14
14
 
15
15
#include <grass/config.h>
 
16
#include <errno.h>
16
17
#include <string.h>
17
18
 
18
19
#include <unistd.h>
21
22
#include <grass/gis.h>
22
23
#include <grass/glocale.h>
23
24
 
 
25
#include "local_proto.h"
 
26
 
24
27
/*!
25
28
  \brief Lowest level open routine.
26
29
 
49
52
static int G__open(const char *element,
50
53
                   const char *name, const char *mapset, int mode)
51
54
{
 
55
    int fd;
52
56
    char path[GPATH_MAX];
53
57
    char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
54
58
 
57
61
 
58
62
    /* READ */
59
63
    if (mode == 0) {
60
 
        if (G__name_is_fully_qualified(name, xname, xmapset)) {
 
64
        if (G_name_is_fully_qualified(name, xname, xmapset)) {
61
65
            if (*mapset && strcmp(xmapset, mapset) != 0) {
62
66
                G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
63
67
                          mapset, xmapset);
66
70
            name = xname;
67
71
            mapset = xmapset;
68
72
        }
69
 
        else if (!mapset || !*mapset)
70
 
            mapset = G_find_file2(element, name, mapset);
 
73
 
 
74
        mapset = G_find_file2(element, name, mapset);
71
75
 
72
76
        if (!mapset)
73
77
            return -1;
74
78
 
75
 
        G__file_name(path, element, name, mapset);
 
79
        G_file_name(path, element, name, mapset);
76
80
 
77
 
        return open(path, 0);
 
81
        if ((fd = open(path, 0)) < 0)
 
82
            G_warning(_("G__open(read): Unable to open '%s': %s"),
 
83
                      path, strerror(errno));
 
84
        return fd;
78
85
    }
79
86
    /* WRITE */
80
87
    if (mode == 1 || mode == 2) {
81
88
        mapset = G_mapset();
82
 
        if (G__name_is_fully_qualified(name, xname, xmapset)) {
 
89
        if (G_name_is_fully_qualified(name, xname, xmapset)) {
83
90
            if (strcmp(xmapset, mapset) != 0) {
84
91
                G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
85
92
                          xmapset, mapset);
88
95
            name = xname;
89
96
        }
90
97
 
91
 
        if (G_legal_filename(name) == -1)
 
98
        if (*name && G_legal_filename(name) == -1)
92
99
            return -1;
93
100
 
94
 
        G__file_name(path, element, name, mapset);
 
101
        G_file_name(path, element, name, mapset);
95
102
 
96
103
        if (mode == 1 || access(path, 0) != 0) {
97
 
            G__make_mapset_element(element);
 
104
            G_make_mapset_element(element);
98
105
            close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
99
106
        }
100
107
 
101
 
        return open(path, mode);
 
108
        if ((fd = open(path, mode)) < 0)
 
109
            G_warning(_("G__open(write): Unable to open '%s': %s"),
 
110
                      path, strerror(errno));
 
111
        return fd;
102
112
    }
103
113
    return -1;
104
114
}
191
201
  \param name map file name
192
202
 
193
203
  \return open file descriptor (FILE *)
194
 
  \return NULL could not open
 
204
  \return NULL on error
195
205
 */
196
206
 
197
207
FILE *G_fopen_new(const char *element, const char *name)
199
209
    int fd;
200
210
 
201
211
    fd = G__open(element, name, G_mapset(), 1);
202
 
    if (fd < 0)
 
212
    if (fd < 0) {
 
213
        G_debug(1, "G_fopen_new(): element = %s, name = %s : NULL",
 
214
                element, name);
203
215
        return (FILE *) 0;
 
216
    }
204
217
 
 
218
    G_debug(2, "\tfile open: new (mode = w)");
205
219
    return fdopen(fd, "w");
206
220
}
207
221
 
221
235
  \param mapset mapset name containing map <i>name</i>
222
236
 
223
237
  \return open file descriptor (FILE *)
224
 
  \return NULL could not open
 
238
  \return NULL on error
225
239
*/
226
240
FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
227
241
{
229
243
 
230
244
    fd = G__open(element, name, mapset, 0);
231
245
    if (fd < 0)
232
 
        return (FILE *) 0;
 
246
        return (FILE *) NULL;
233
247
 
 
248
    G_debug(2, "\tfile open: read (mode = r)");
234
249
    return fdopen(fd, "r");
235
250
}
236
251
 
247
262
  \param name map file name
248
263
 
249
264
  \return open file descriptor (FILE *)
250
 
  \return NULL could not open
 
265
  \return NULL on error
251
266
*/
252
267
FILE *G_fopen_append(const char *element, const char *name)
253
268
{
258
273
        return (FILE *) 0;
259
274
    lseek(fd, 0L, SEEK_END);
260
275
 
 
276
    G_debug(2, "\tfile open: append (mode = a)");
261
277
    return fdopen(fd, "a");
262
278
}
263
279
 
274
290
  \param name map file name
275
291
 
276
292
  \return open file descriptor (FILE *)
277
 
  \return NULL
 
293
  \return NULL on error
278
294
*/
279
295
FILE *G_fopen_modify(const char *element, const char *name)
280
296
{
285
301
        return (FILE *) 0;
286
302
    lseek(fd, 0L, SEEK_END);
287
303
 
 
304
    G_debug(2, "\tfile open: modify (mode = r+)");
288
305
    return fdopen(fd, "r+");
289
306
}