GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gis/open.c
Go to the documentation of this file.
1 
15 #include <grass/config.h>
16 #include <string.h>
17 
18 #include <unistd.h>
19 #include <fcntl.h>
20 
21 #include <grass/gis.h>
22 #include <grass/glocale.h>
23 
49 static int G__open(const char *element,
50  const char *name, const char *mapset, int mode)
51 {
52  char path[GPATH_MAX];
53  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
54 
55 
57 
58  /* READ */
59  if (mode == 0) {
60  if (G__name_is_fully_qualified(name, xname, xmapset)) {
61  if (*mapset && strcmp(xmapset, mapset) != 0) {
62  G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
63  mapset, xmapset);
64  return -1;
65  }
66  name = xname;
67  mapset = xmapset;
68  }
69  else if (!mapset || !*mapset)
70  mapset = G_find_file2(element, name, mapset);
71 
72  if (!mapset)
73  return -1;
74 
75  G__file_name(path, element, name, mapset);
76 
77  return open(path, 0);
78  }
79  /* WRITE */
80  if (mode == 1 || mode == 2) {
81  mapset = G_mapset();
82  if (G__name_is_fully_qualified(name, xname, xmapset)) {
83  if (strcmp(xmapset, mapset) != 0) {
84  G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
85  xmapset, mapset);
86  return -1;
87  }
88  name = xname;
89  }
90 
91  if (G_legal_filename(name) == -1)
92  return -1;
93 
94  G__file_name(path, element, name, mapset);
95 
96  if (mode == 1 || access(path, 0) != 0) {
97  G__make_mapset_element(element);
98  close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
99  }
100 
101  return open(path, mode);
102  }
103  return -1;
104 }
105 
125 int G_open_new(const char *element, const char *name)
126 {
127  return G__open(element, name, G_mapset(), 1);
128 }
129 
130 
147 int G_open_old(const char *element, const char *name, const char *mapset)
148 {
149  return G__open(element, name, mapset, 0);
150 }
151 
168 int G_open_update(const char *element, const char *name)
169 {
170  int fd;
171 
172  fd = G__open(element, name, G_mapset(), 2);
173  if (fd >= 0)
174  lseek(fd, 0L, SEEK_END);
175 
176  return fd;
177 }
178 
179 
197 FILE *G_fopen_new(const char *element, const char *name)
198 {
199  int fd;
200 
201  fd = G__open(element, name, G_mapset(), 1);
202  if (fd < 0)
203  return (FILE *) 0;
204 
205  return fdopen(fd, "w");
206 }
207 
208 
226 FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
227 {
228  int fd;
229 
230  fd = G__open(element, name, mapset, 0);
231  if (fd < 0)
232  return (FILE *) 0;
233 
234  return fdopen(fd, "r");
235 }
236 
252 FILE *G_fopen_append(const char *element, const char *name)
253 {
254  int fd;
255 
256  fd = G__open(element, name, G_mapset(), 2);
257  if (fd < 0)
258  return (FILE *) 0;
259  lseek(fd, 0L, SEEK_END);
260 
261  return fdopen(fd, "a");
262 }
263 
279 FILE *G_fopen_modify(const char *element, const char *name)
280 {
281  int fd;
282 
283  fd = G__open(element, name, G_mapset(), 2);
284  if (fd < 0)
285  return (FILE *) 0;
286  lseek(fd, 0L, SEEK_END);
287 
288  return fdopen(fd, "r+");
289 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
char xmapset[512]
Definition: g3dcats.c:89
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
FILE * G_fopen_modify(const char *element, const char *name)
Open a database file for update (r+ mode)
Definition: gis/open.c:279
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_open_update(const char *element, const char *name)
Open a database file for update.
Definition: gis/open.c:168
int G_open_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:147
FILE * G_fopen_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:197
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) retu...
Definition: find_file.c:191
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_open_new(const char *element, const char *name)
Open a new database file.
Definition: gis/open.c:125
int G__make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:34
FILE * G_fopen_append(const char *element, const char *name)
Open a database file for update (append mode)
Definition: gis/open.c:252
int G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:109
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition: gis/open.c:226
char xname[512]
Definition: g3dcats.c:89
tuple mode
Definition: tools.py:1481
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:57