GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 <string.h>
24 
25 #include <unistd.h>
26 #include <fcntl.h>
27 
28 #include <grass/gis.h>
29 #include <grass/glocale.h>
30 
31 static int G__open_misc(const char *dir,
32  const char *element,
33  const char *name, const char *mapset, int mode)
34 {
35  char path[GPATH_MAX];
36  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
37 
38 
40 
41  /* READ */
42  if (mode == 0) {
43  if (G__name_is_fully_qualified(name, xname, xmapset)) {
44  if (*mapset && strcmp(xmapset, mapset) != 0) {
45  G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"),
46  mapset, xmapset);
47  return -1;
48  }
49  name = xname;
50  mapset = xmapset;
51  }
52  else if (!*mapset)
53  mapset = G_find_file2_misc(dir, element, name, mapset);
54 
55  if (!mapset)
56  return -1;
57 
58  G__file_name_misc(path, dir, element, name, mapset);
59 
60  return open(path, 0);
61  }
62  /* WRITE */
63  if (mode == 1 || mode == 2) {
64  mapset = G_mapset();
65  if (G__name_is_fully_qualified(name, xname, xmapset)) {
66  if (strcmp(xmapset, mapset) != 0) {
67  G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"),
68  xmapset, mapset);
69  return -1;
70  }
71  name = xname;
72  }
73 
74  if (G_legal_filename(name) == -1)
75  return -1;
76 
77  G__file_name_misc(path, dir, element, name, mapset);
78  if (mode == 1 || access(path, 0) != 0) {
79  G__make_mapset_element_misc(dir, name);
80  close(creat(path, 0666));
81  }
82 
83  return open(path, mode);
84  }
85  return -1;
86 }
87 
88 
103 int G_open_new_misc(const char *dir, const char *element, const char *name)
104 {
105  return G__open_misc(dir, element, name, G_mapset(), 1);
106 }
107 
108 
124 int G_open_old_misc(const char *dir, const char *element, const char *name,
125  const char *mapset)
126 {
127  return G__open_misc(dir, element, name, mapset, 0);
128 }
129 
130 
145 int G_open_update_misc(const char *dir, const char *element, const char *name)
146 {
147  int fd;
148 
149  fd = G__open_misc(dir, element, name, G_mapset(), 2);
150  if (fd >= 0)
151  lseek(fd, 0L, SEEK_END);
152 
153  return fd;
154 }
155 
156 
172 FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name)
173 {
174  int fd;
175 
176  fd = G__open_misc(dir, element, name, G_mapset(), 1);
177  if (fd < 0)
178  return (FILE *) 0;
179 
180  return fdopen(fd, "w");
181 }
182 
183 
200 FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name,
201  const char *mapset)
202 {
203  int fd;
204 
205  fd = G__open_misc(dir, element, name, mapset, 0);
206  if (fd < 0)
207  return (FILE *) 0;
208 
209  return fdopen(fd, "r");
210 }
211 
212 FILE *G_fopen_append_misc(const char *dir, const char *element,
213  const char *name)
214 {
215  int fd;
216 
217  fd = G__open_misc(dir, element, name, G_mapset(), 2);
218  if (fd < 0)
219  return (FILE *) 0;
220  lseek(fd, 0L, SEEK_END);
221 
222  return fdopen(fd, "a");
223 }
224 
225 FILE *G_fopen_modify_misc(const char *dir, const char *element,
226  const char *name)
227 {
228  int fd;
229 
230  fd = G__open_misc(dir, element, name, G_mapset(), 2);
231  if (fd < 0)
232  return (FILE *) 0;
233  lseek(fd, 0L, SEEK_END);
234 
235  return fdopen(fd, "r+");
236 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
FILE * G_fopen_modify_misc(const char *dir, const char *element, const char *name)
Definition: open_misc.c:225
char xmapset[512]
Definition: g3dcats.c:89
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Definition: file_name.c:70
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
char * G_find_file2_misc(const char *dir, const char *element, const char *name, const char *mapset)
Definition: find_file.c:196
FILE * G_fopen_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
Definition: open_misc.c:200
FILE * G_fopen_append_misc(const char *dir, const char *element, const char *name)
Definition: open_misc.c:212
int G_open_old_misc(const char *dir, const char *element, const char *name, const char *mapset)
open a database file for reading
Definition: open_misc.c:124
int G_open_new_misc(const char *dir, const char *element, const char *name)
open a new database file
Definition: open_misc.c:103
int G__make_mapset_element_misc(const char *dir, const char *name)
Create misc element in the current mapset.
Definition: mapset_msc.c:82
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
FILE * G_fopen_new_misc(const char *dir, const char *element, const char *name)
open a new database file
Definition: open_misc.c:172
int G__check_gisinit(void)
Checks to see if GIS engine is initialized.
Definition: gisinit.c:109
int G_open_update_misc(const char *dir, const char *element, const char *name)
open a database file for update
Definition: open_misc.c:145
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