/*! \brief Lowest level open routine. Opens the file <i>name</i> in <i>element</i> ("cell", etc.) in mapset <i>mapset</i> according to the i/o <i>mode</i>. - mode = 0 (read) will look for <i>name</i> in <i>mapset</i> and open the file for read only the file must exist - mode = 1 (write) will create an empty file <i>name</i> in the current mapset and open the file for write only <i>mapset</i> ignored - mode = 2 (read and write) will open a file in the current mapset for reading and writing creating a new file if necessary <i>mapset</i> ignored \param element database element name \param name map file name \param mapset mapset containing map <i>name</i> \param mode r/w mode 0=read, 1=write, 2=read/write \return open file descriptor (int) \return -1 could not open */ static int G__open(const char *element, const char *name, const char *mapset, int mode) { char path[GPATH_MAX]; char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; G__check_gisinit(); /* READ */ if (mode == 0) { if (G_name_is_fully_qualified(name, xname, xmapset)) { if (*mapset && strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"), mapset, xmapset); return -1; } name = xname; mapset = xmapset; } else if (!mapset || !*mapset) mapset = G_find_file2(element, name, mapset); if (!mapset) return -1; G_file_name(path, element, name, mapset); return open(path, 0); } /* WRITE */ if (mode == 1 || mode == 2) { mapset = G_mapset(); if (G_name_is_fully_qualified(name, xname, xmapset)) { if (strcmp(xmapset, mapset) != 0) { G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"), xmapset, mapset); return -1; } name = xname; } if (*name && G_legal_filename(name) == -1) return -1; G_file_name(path, element, name, mapset); if (mode == 1 || access(path, 0) != 0) { G__make_mapset_element(element); close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666)); } return open(path, mode); } return -1; }
int G__open ( char *element, char *name, char *mapset, int mode) { char path[1024]; char xname[512], xmapset[512], *dummy; int G_fd; G__check_gisinit(); /* READ */ if (mode == 0) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, mapset) != 0) { fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n", mapset,xmapset); return -1; } name = xname; } if ((dummy = G_find_file (element, name, mapset)) == NULL) return -1; G_free (dummy); G__file_name (path, element, name, mapset); G_fd = open (path, 0); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } /* WRITE */ if (mode == 1 || mode == 2) { if (G__name_is_fully_qualified (name, xname, xmapset)) { if (strcmp (xmapset, G_mapset()) != 0) { fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n", xmapset,G_mapset()); return -1; } name = xname; } if (G_legal_filename(name) == -1) return -1; G__file_name (path, element, name, G_mapset()); if(mode == 1 || access(path,0) != 0) { G__make_mapset_element (element); close (creat (path, 0666)); } G_fd = open (path, mode); #if defined R_GRASS_INTERFACE && defined __MINGW32_VERSION setmode(G_fd, O_BINARY); #endif /* __MINGW32_VERSION && R_GRASS_INTERFACE */ return G_fd; } return -1; }