00001
00015 #include <grass/config.h>
00016 #include <string.h>
00017
00018 #include <unistd.h>
00019 #include <fcntl.h>
00020
00021 #include <grass/gis.h>
00022 #include <grass/glocale.h>
00023
00049 static int G__open(const char *element,
00050 const char *name, const char *mapset, int mode)
00051 {
00052 char path[GPATH_MAX];
00053 char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
00054
00055
00056 G__check_gisinit();
00057
00058
00059 if (mode == 0) {
00060 if (G__name_is_fully_qualified(name, xname, xmapset)) {
00061 if (*mapset && strcmp(xmapset, mapset) != 0) {
00062 G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
00063 mapset, xmapset);
00064 return -1;
00065 }
00066 name = xname;
00067 mapset = xmapset;
00068 }
00069 else if (!mapset || !*mapset)
00070 mapset = G_find_file2(element, name, mapset);
00071
00072 if (!mapset)
00073 return -1;
00074
00075 G__file_name(path, element, name, mapset);
00076
00077 return open(path, 0);
00078 }
00079
00080 if (mode == 1 || mode == 2) {
00081 mapset = G_mapset();
00082 if (G__name_is_fully_qualified(name, xname, xmapset)) {
00083 if (strcmp(xmapset, mapset) != 0) {
00084 G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
00085 xmapset, mapset);
00086 return -1;
00087 }
00088 name = xname;
00089 }
00090
00091 if (G_legal_filename(name) == -1)
00092 return -1;
00093
00094 G__file_name(path, element, name, mapset);
00095
00096 if (mode == 1 || access(path, 0) != 0) {
00097 G__make_mapset_element(element);
00098 close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
00099 }
00100
00101 return open(path, mode);
00102 }
00103 return -1;
00104 }
00105
00125 int G_open_new(const char *element, const char *name)
00126 {
00127 return G__open(element, name, G_mapset(), 1);
00128 }
00129
00130
00147 int G_open_old(const char *element, const char *name, const char *mapset)
00148 {
00149 return G__open(element, name, mapset, 0);
00150 }
00151
00168 int G_open_update(const char *element, const char *name)
00169 {
00170 int fd;
00171
00172 fd = G__open(element, name, G_mapset(), 2);
00173 if (fd >= 0)
00174 lseek(fd, 0L, SEEK_END);
00175
00176 return fd;
00177 }
00178
00179
00197 FILE *G_fopen_new(const char *element, const char *name)
00198 {
00199 int fd;
00200
00201 fd = G__open(element, name, G_mapset(), 1);
00202 if (fd < 0)
00203 return (FILE *) 0;
00204
00205 return fdopen(fd, "w");
00206 }
00207
00208
00226 FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
00227 {
00228 int fd;
00229
00230 fd = G__open(element, name, mapset, 0);
00231 if (fd < 0)
00232 return (FILE *) 0;
00233
00234 return fdopen(fd, "r");
00235 }
00236
00252 FILE *G_fopen_append(const char *element, const char *name)
00253 {
00254 int fd;
00255
00256 fd = G__open(element, name, G_mapset(), 2);
00257 if (fd < 0)
00258 return (FILE *) 0;
00259 lseek(fd, 0L, SEEK_END);
00260
00261 return fdopen(fd, "a");
00262 }
00263
00279 FILE *G_fopen_modify(const char *element, const char *name)
00280 {
00281 int fd;
00282
00283 fd = G__open(element, name, G_mapset(), 2);
00284 if (fd < 0)
00285 return (FILE *) 0;
00286 lseek(fd, 0L, SEEK_END);
00287
00288 return fdopen(fd, "r+");
00289 }