GRASS Programmer's Manual
6.4.1(2011)
|
00001 00002 /**************************************************************************** 00003 * 00004 * MODULE: gis library 00005 * AUTHOR(S): Glynn Clements <glynn@gclements.plus.com> 00006 * COPYRIGHT: (C) 2007 Glynn Clements and the GRASS Development Team 00007 * 00008 * NOTE: Based upon open.c 00009 * 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 *****************************************************************************/ 00021 00022 #include <grass/config.h> 00023 #include <string.h> 00024 00025 #include <unistd.h> 00026 #include <fcntl.h> 00027 00028 #include <grass/gis.h> 00029 #include <grass/glocale.h> 00030 00031 static int G__open_misc(const char *dir, 00032 const char *element, 00033 const char *name, const char *mapset, int mode) 00034 { 00035 char path[GPATH_MAX]; 00036 char xname[GNAME_MAX], xmapset[GMAPSET_MAX]; 00037 00038 00039 G__check_gisinit(); 00040 00041 /* READ */ 00042 if (mode == 0) { 00043 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00044 if (*mapset && strcmp(xmapset, mapset) != 0) { 00045 G_warning(_("G__open_misc(read): mapset <%s> doesn't match xmapset <%s>"), 00046 mapset, xmapset); 00047 return -1; 00048 } 00049 name = xname; 00050 mapset = xmapset; 00051 } 00052 else if (!*mapset) 00053 mapset = G_find_file2_misc(dir, element, name, mapset); 00054 00055 if (!mapset) 00056 return -1; 00057 00058 G__file_name_misc(path, dir, element, name, mapset); 00059 00060 return open(path, 0); 00061 } 00062 /* WRITE */ 00063 if (mode == 1 || mode == 2) { 00064 mapset = G_mapset(); 00065 if (G__name_is_fully_qualified(name, xname, xmapset)) { 00066 if (strcmp(xmapset, mapset) != 0) { 00067 G_warning(_("G__open_misc(write): xmapset <%s> != G_mapset() <%s>"), 00068 xmapset, mapset); 00069 return -1; 00070 } 00071 name = xname; 00072 } 00073 00074 if (G_legal_filename(name) == -1) 00075 return -1; 00076 00077 G__file_name_misc(path, dir, element, name, mapset); 00078 if (mode == 1 || access(path, 0) != 0) { 00079 G__make_mapset_element_misc(dir, name); 00080 close(creat(path, 0666)); 00081 } 00082 00083 return open(path, mode); 00084 } 00085 return -1; 00086 } 00087 00088 00103 int G_open_new_misc(const char *dir, const char *element, const char *name) 00104 { 00105 return G__open_misc(dir, element, name, G_mapset(), 1); 00106 } 00107 00108 00124 int G_open_old_misc(const char *dir, const char *element, const char *name, 00125 const char *mapset) 00126 { 00127 return G__open_misc(dir, element, name, mapset, 0); 00128 } 00129 00130 00145 int G_open_update_misc(const char *dir, const char *element, const char *name) 00146 { 00147 int fd; 00148 00149 fd = G__open_misc(dir, element, name, G_mapset(), 2); 00150 if (fd >= 0) 00151 lseek(fd, 0L, SEEK_END); 00152 00153 return fd; 00154 } 00155 00156 00172 FILE *G_fopen_new_misc(const char *dir, const char *element, const char *name) 00173 { 00174 int fd; 00175 00176 fd = G__open_misc(dir, element, name, G_mapset(), 1); 00177 if (fd < 0) 00178 return (FILE *) 0; 00179 00180 return fdopen(fd, "w"); 00181 } 00182 00183 00200 FILE *G_fopen_old_misc(const char *dir, const char *element, const char *name, 00201 const char *mapset) 00202 { 00203 int fd; 00204 00205 fd = G__open_misc(dir, element, name, mapset, 0); 00206 if (fd < 0) 00207 return (FILE *) 0; 00208 00209 return fdopen(fd, "r"); 00210 } 00211 00212 FILE *G_fopen_append_misc(const char *dir, const char *element, 00213 const char *name) 00214 { 00215 int fd; 00216 00217 fd = G__open_misc(dir, element, name, G_mapset(), 2); 00218 if (fd < 0) 00219 return (FILE *) 0; 00220 lseek(fd, 0L, SEEK_END); 00221 00222 return fdopen(fd, "a"); 00223 } 00224 00225 FILE *G_fopen_modify_misc(const char *dir, const char *element, 00226 const char *name) 00227 { 00228 int fd; 00229 00230 fd = G__open_misc(dir, element, name, G_mapset(), 2); 00231 if (fd < 0) 00232 return (FILE *) 0; 00233 lseek(fd, 0L, SEEK_END); 00234 00235 return fdopen(fd, "r+"); 00236 }