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