GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
gis/open.c
Go to the documentation of this file.
1 
15 #include <grass/config.h>
16 #include <errno.h>
17 #include <string.h>
18 
19 #include <unistd.h>
20 #include <fcntl.h>
21 
22 #include <grass/gis.h>
23 #include <grass/glocale.h>
24 
25 #include "local_proto.h"
26 
52 static int G__open(const char *element,
53  const char *name, const char *mapset, int mode)
54 {
55  int fd;
56  char path[GPATH_MAX];
57  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
58 
59 
61 
62  /* READ */
63  if (mode == 0) {
64  if (G_name_is_fully_qualified(name, xname, xmapset)) {
65  if (*mapset && strcmp(xmapset, mapset) != 0) {
66  G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
67  mapset, xmapset);
68  return -1;
69  }
70  name = xname;
71  mapset = xmapset;
72  }
73 
74  mapset = G_find_file2(element, name, mapset);
75 
76  if (!mapset)
77  return -1;
78 
79  G_file_name(path, element, name, mapset);
80 
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;
85  }
86  /* WRITE */
87  if (mode == 1 || mode == 2) {
88  mapset = G_mapset();
89  if (G_name_is_fully_qualified(name, xname, xmapset)) {
90  if (strcmp(xmapset, mapset) != 0) {
91  G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
92  xmapset, mapset);
93  return -1;
94  }
95  name = xname;
96  }
97 
98  if (*name && G_legal_filename(name) == -1)
99  return -1;
100 
101  G_file_name(path, element, name, mapset);
102 
103  if (mode == 1 || access(path, 0) != 0) {
104  G_make_mapset_element(element);
105  close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
106  }
107 
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;
112  }
113  return -1;
114 }
115 
135 int G_open_new(const char *element, const char *name)
136 {
137  return G__open(element, name, G_mapset(), 1);
138 }
139 
140 
157 int G_open_old(const char *element, const char *name, const char *mapset)
158 {
159  return G__open(element, name, mapset, 0);
160 }
161 
178 int G_open_update(const char *element, const char *name)
179 {
180  int fd;
181 
182  fd = G__open(element, name, G_mapset(), 2);
183  if (fd >= 0)
184  lseek(fd, 0L, SEEK_END);
185 
186  return fd;
187 }
188 
189 
207 FILE *G_fopen_new(const char *element, const char *name)
208 {
209  int fd;
210 
211  fd = G__open(element, name, G_mapset(), 1);
212  if (fd < 0) {
213  G_debug(1, "G_fopen_new(): element = %s, name = %s : NULL",
214  element, name);
215  return (FILE *) 0;
216  }
217 
218  G_debug(2, "\tfile open: new (mode = w)");
219  return fdopen(fd, "w");
220 }
221 
222 
240 FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
241 {
242  int fd;
243 
244  fd = G__open(element, name, mapset, 0);
245  if (fd < 0)
246  return (FILE *) NULL;
247 
248  G_debug(2, "\tfile open: read (mode = r)");
249  return fdopen(fd, "r");
250 }
251 
267 FILE *G_fopen_append(const char *element, const char *name)
268 {
269  int fd;
270 
271  fd = G__open(element, name, G_mapset(), 2);
272  if (fd < 0)
273  return (FILE *) 0;
274  lseek(fd, 0L, SEEK_END);
275 
276  G_debug(2, "\tfile open: append (mode = a)");
277  return fdopen(fd, "a");
278 }
279 
295 FILE *G_fopen_modify(const char *element, const char *name)
296 {
297  int fd;
298 
299  fd = G__open(element, name, G_mapset(), 2);
300  if (fd < 0)
301  return (FILE *) 0;
302  lseek(fd, 0L, SEEK_END);
303 
304  G_debug(2, "\tfile open: modify (mode = r+)");
305  return fdopen(fd, "r+");
306 }
int G_make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:35
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
FILE * G_fopen_modify(const char *element, const char *name)
Open a database file for update (r+ mode)
Definition: gis/open.c:295
#define NULL
Definition: ccmath.h:32
Definition: lidar.h:89
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
int G_open_update(const char *element, const char *name)
Open a database file for update.
Definition: gis/open.c:178
int G_open_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:157
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:207
int G_open_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:135
Definition: path.h:16
void G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:98
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_append(const char *element, const char *name)
Open a database file for update (append mode)
Definition: gis/open.c:267
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:240
const char * name
Definition: named_colr.c:7
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203
const char * G_find_file2(const char *element, const char *name, const char *mapset)
Searches for a file from the mapset search list or in a specified mapset. (look but don&#39;t touch) ...
Definition: find_file.c:247