GRASS GIS 8 Programmer's Manual 8.2.1(2023)-exported
open_misc.c
Go to the documentation of this file.
1
2/****************************************************************************
3 *
4 * MODULE: gis library
5 * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com>
6 * COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team
7 *
8 * NOTE: Based upon open.c
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 *****************************************************************************/
21
22#include <grass/config.h>
23#include <errno.h>
24#include <string.h>
25
26#include <unistd.h>
27#include <fcntl.h>
28
29#include <grass/gis.h>
30#include <grass/glocale.h>
31
32#include "gis_local_proto.h"
33
34static int G__open_misc(const char *dir,
35 const char *element,
36 const char *name, const char *mapset, int mode)
37{
38 int fd;
39 char path[GPATH_MAX];
40 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
41
42
44
45 /* READ */
46 if (mode == 0) {
47 if (G_name_is_fully_qualified(name, xname, xmapset)) {
48 if (*mapset && strcmp(xmapset, mapset) != 0) {
49 G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"),
50 mapset, xmapset);
51 return -1;
52 }
53 name = xname;
54 mapset = xmapset;
55 }
56
57 mapset = G_find_file2_misc(dir, element, name, mapset);
58
59 if (!mapset)
60 return -1;
61
62 G_file_name_misc(path, dir, element, name, mapset);
63
64 if ((fd = open(path, 0)) < 0)
65 G_warning("G__open_misc(read): Unable to open '%s': %s",
66 path, strerror(errno));
67 return fd;
68 }
69 /* WRITE */
70 if (mode == 1 || mode == 2) {
71 mapset = G_mapset();
72 if (G_name_is_fully_qualified(name, xname, xmapset)) {
73 if (strcmp(xmapset, mapset) != 0) {
74 G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
75 xmapset, mapset);
76 return -1;
77 }
78 name = xname;
79 }
80
81 if (G_legal_filename(name) == -1)
82 return -1;
83
84 G_file_name_misc(path, dir, element, name, mapset);
85 if (mode == 1 || access(path, 0) != 0) {
87 close(creat(path, 0666));
88 }
89
90 if ((fd = open(path, mode)) < 0)
91 G_warning("G__open_misc(write): Unable to open '%s': %s",
92 path, strerror(errno));
93 return fd;
94 }
95 return -1;
96}
97
98
99/*!
100 * \brief open a new database misc file
101 *
102 * The database file <b>element</b> under <b>dir/name</b> in the
103 * current mapset is created and opened for writing (but not reading).
104 * The UNIX open( ) routine is used to open the file. If the file does not exist,
105 * -1 is returned. Otherwise the file is positioned at the end of the file and
106 * the file descriptor from the open( ) is returned.
107 *
108 * \param element
109 * \param name
110 * \return int
111 */
112
113int G_open_new_misc(const char *dir, const char *element, const char *name)
114{
115 return G__open_misc(dir, element, name, G_mapset(), 1);
116}
117
118
119/*!
120 * \brief open a database misc file for reading
121 *
122 * The database file <b>element</b> under
123 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
124 * not for writing).
125 * The UNIX open( ) routine is used to open the file. If the file does not exist,
126 * -1 is returned. Otherwise the file descriptor from the open( ) is returned.
127 *
128 * \param element
129 * \param name
130 * \param mapset
131 * \return int
132 */
133
134int G_open_old_misc(const char *dir, const char *element, const char *name,
135 const char *mapset)
136{
137 return G__open_misc(dir, element, name, mapset, 0);
138}
139
140
141/*!
142 * \brief open a database misc file for update
143 *
144 * The database file <b>element</b> under <b>dir/name</b> in the
145 * current mapset is opened for reading and writing.
146 * The UNIX open( ) routine is used to open the file. If the file does not exist,
147 * -1 is returned. Otherwise the file is positioned at the end of the file and
148 * the file descriptor from the open( ) is returned.
149 *
150 * \param element
151 * \param name
152 * \return int
153 */
154
155int G_open_update_misc(const char *dir, const char *element, const char *name)
156{
157 int fd;
158
159 fd = G__open_misc(dir, element, name, G_mapset(), 2);
160 if (fd >= 0)
161 lseek(fd, 0L, SEEK_END);
162
163 return fd;
164}
165
166
167/*!
168 * \brief open a new database misc file
169 *
170 * The database file <b>element</b> under <b>dir/name</b> in the
171 * current mapset is created and opened for writing (but not reading).
172 * The UNIX fopen( ) routine, with "w" write mode, is used to open the file. If
173 * the file does not exist, the NULL pointer is returned. Otherwise the file is
174 * positioned at the end of the file and the file descriptor from the fopen( ) is
175 * returned.
176 *
177 * \param element
178 * \param name
179 * \return FILE *
180 */
181
182FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
183{
184 int fd;
185
186 fd = G__open_misc(dir, element, name, G_mapset(), 1);
187 if (fd < 0)
188 return (FILE *) 0;
189
190 return fdopen(fd, "w");
191}
192
193
194/*!
195 * \brief open a database misc file for reading
196 *
197 * The database file <b>element</b> under
198 * <b>dir/name</b> in the specified <b>mapset</b> is opened for reading (but
199 * not for writing).
200 * The UNIX fopen( ) routine, with "r" read mode, is used to open the file. If
201 * the file does not exist, the NULL pointer is returned. Otherwise the file
202 * descriptor from the fopen( ) is returned.
203 *
204 * \param element
205 * \param name
206 * \param mapset
207 * \return FILE *
208 */
209
210FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
211 const char *mapset)
212{
213 int fd;
214
215 fd = G__open_misc(dir, element, name, mapset, 0);
216 if (fd < 0)
217 return (FILE *) 0;
218
219 return fdopen(fd, "r");
220}
221
222FILE *G_fopen_append_misc(const char *dir, const char *element,
223 const char *name)
224{
225 int fd;
226
227 fd = G__open_misc(dir, element, name, G_mapset(), 2);
228 if (fd < 0)
229 return (FILE *) 0;
230 lseek(fd, 0L, SEEK_END);
231
232 return fdopen(fd, "a");
233}
234
235FILE *G_fopen_modify_misc(const char *dir, const char *element,
236 const char *name)
237{
238 int fd;
239
240 fd = G__open_misc(dir, element, name, G_mapset(), 2);
241 if (fd < 0)
242 return (FILE *) 0;
243 lseek(fd, 0L, SEEK_END);
244
245 return fdopen(fd, "r+");
246}
char * G_file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Builds full path names to GIS misc data files.
Definition: file_name.c:101
const char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Searches for a misc file from the mapset search list or in a specified mapset. (look but don't touch)
Definition: find_file.c:270
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:100
int G_legal_filename(const char *s)
Check for legal database file name.
Definition: legal_name.c:34
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
int G__make_mapset_element_misc(const char *dir, const char *name)
Create misc element in the current mapset.
Definition: mapset_msc.c:232
const char * name
Definition: named_colr.c:7
int G_name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition: open_misc.c:210
int G_open_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition: open_misc.c:113
FILE * G_fopen_modify_misc(const char *dir, const char *element, const char *name)
Definition: open_misc.c:235
int G_open_update_misc(const char *dir, const char *element, const char *name)
open a database misc file for update
Definition: open_misc.c:155
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database misc file
Definition: open_misc.c:182
FILE * G_fopen_append_misc(const char *dir, const char *element, const char *name)
Definition: open_misc.c:222
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database misc file for reading
Definition: open_misc.c:134
Definition: lidar.h:87
Definition: path.h:16