00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 #include <grass/config.h>
00076 #include <string.h>
00077
00078 #ifdef HAVE_UNISTD_H
00079 #include <unistd.h>
00080 #endif
00081
00082 #include <grass/gis.h>
00083 #include <unistd.h>
00084 #include <fcntl.h>
00085
00086 #ifdef __MINGW32__
00087 #include <stdlib.h>
00088 #include <fcntl.h>
00089 #undef _fmode
00090 int _fmode = _O_BINARY;
00091 #endif
00092
00093 int G__open (
00094 char *element,
00095 char *name,
00096 char *mapset,
00097 int mode)
00098 {
00099 char path[1024];
00100 char xname[512], xmapset[512], *dummy;
00101
00102
00103 G__check_gisinit();
00104
00105
00106 if (mode == 0)
00107 {
00108 if (G__name_is_fully_qualified (name, xname, xmapset))
00109 {
00110 if (strcmp (xmapset, mapset) != 0) {
00111 fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n",
00112 mapset,xmapset);
00113 return -1;
00114 }
00115 name = xname;
00116 }
00117 if ((dummy = G_find_file (element, name, mapset)) == NULL)
00118 return -1;
00119 G_free (dummy);
00120 G__file_name (path, element, name, mapset);
00121
00122 return open (path, 0);
00123 }
00124
00125 if (mode == 1 || mode == 2)
00126 {
00127 if (G__name_is_fully_qualified (name, xname, xmapset))
00128 {
00129 if (strcmp (xmapset, G_mapset()) != 0) {
00130 fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n",
00131 xmapset,G_mapset());
00132 return -1;
00133 }
00134 name = xname;
00135 }
00136
00137 if (G_legal_filename(name) == -1)
00138 return -1;
00139
00140 G__file_name (path, element, name, G_mapset());
00141 if(mode == 1 || access(path,0) != 0)
00142 {
00143 G__make_mapset_element (element);
00144 close (creat (path, 0666));
00145 }
00146
00147 return open (path, mode);
00148 }
00149 return -1;
00150 }
00151
00152
00167 int G_open_new (char *element,char *name)
00168 {
00169 return G__open (element, name, G_mapset(), 1);
00170 }
00171
00172
00188 int G_open_old (char *element,char *name,char *mapset)
00189 {
00190 return G__open (element, name, mapset, 0);
00191 }
00192
00193
00208 int G_open_update (char *element,char *name)
00209 {
00210 int fd;
00211 fd = G__open (element, name, G_mapset(), 2);
00212 if (fd >= 0) lseek (fd, 0L, SEEK_END);
00213
00214 return fd;
00215 }
00216
00217
00233 FILE *G_fopen_new (char *element,char *name)
00234 {
00235 int fd;
00236
00237 fd = G__open (element, name, G_mapset(), 1);
00238 if (fd < 0)
00239 return (FILE *) 0;
00240
00241 return fdopen (fd, "w");
00242 }
00243
00244
00261 FILE *
00262 G_fopen_old (char *element,char *name,char *mapset)
00263 {
00264 int fd;
00265
00266 fd = G__open (element, name, mapset, 0);
00267 if (fd < 0)
00268 return (FILE *) 0;
00269
00270 return fdopen (fd, "r");
00271 }
00272
00273 FILE *
00274 G_fopen_append (char *element,char *name)
00275 {
00276 int fd;
00277
00278 fd = G__open (element, name, G_mapset(), 2);
00279 if (fd < 0)
00280 return (FILE *) 0;
00281 lseek (fd, 0L, SEEK_END);
00282
00283 return fdopen (fd, "a");
00284 }
00285
00286 FILE *G_fopen_modify (char *element,char *name)
00287 {
00288 int fd;
00289
00290 fd = G__open (element, name, G_mapset(), 2);
00291 if (fd < 0)
00292 return (FILE *) 0;
00293 lseek (fd, 0L, SEEK_END);
00294
00295 return fdopen (fd, "r+");
00296 }