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 "config.h"
00076 #include <string.h>
00077
00078 #ifdef HAVE_UNISTD_H
00079 #include <unistd.h>
00080 #endif
00081
00082 #include "gis.h"
00083 #include <unistd.h>
00084 #include <fcntl.h>
00085
00086 int G__open (
00087 char *element,
00088 char *name,
00089 char *mapset,
00090 int mode)
00091 {
00092 char path[1024];
00093 char xname[512], xmapset[512], *dummy;
00094
00095
00096 G__check_gisinit();
00097
00098
00099 if (mode == 0)
00100 {
00101 if (G__name_is_fully_qualified (name, xname, xmapset))
00102 {
00103 if (strcmp (xmapset, mapset) != 0) {
00104 fprintf(stderr, "G__open(r): mapset (%s) doesn't match xmapset (%s)\n",
00105 mapset,xmapset);
00106 return -1;
00107 }
00108 name = xname;
00109 }
00110 if ((dummy = G_find_file (element, name, mapset)) == NULL)
00111 return -1;
00112 G_free (dummy);
00113 G__file_name (path, element, name, mapset);
00114
00115 return open (path, 0);
00116 }
00117
00118 if (mode == 1 || mode == 2)
00119 {
00120 if (G__name_is_fully_qualified (name, xname, xmapset))
00121 {
00122 if (strcmp (xmapset, G_mapset()) != 0) {
00123 fprintf(stderr, "G__open(w): xmapset (%s) != G_mapset() (%s)\n",
00124 xmapset,G_mapset());
00125 return -1;
00126 }
00127 name = xname;
00128 }
00129
00130 if (G_legal_filename(name) == -1)
00131 return -1;
00132
00133 G__file_name (path, element, name, G_mapset());
00134 if(mode == 1 || access(path,0) != 0)
00135 {
00136 G__make_mapset_element (element);
00137 close (creat (path, 0666));
00138 }
00139
00140 return open (path, mode);
00141 }
00142 return -1;
00143 }
00144
00145
00160 int G_open_new (char *element,char *name)
00161 {
00162 return G__open (element, name, G_mapset(), 1);
00163 }
00164
00165
00181 int G_open_old (char *element,char *name,char *mapset)
00182 {
00183 return G__open (element, name, mapset, 0);
00184 }
00185
00186
00201 int G_open_update (char *element,char *name)
00202 {
00203 int fd;
00204 fd = G__open (element, name, G_mapset(), 2);
00205 if (fd >= 0) lseek (fd, 0L, 2);
00206 return fd;
00207 }
00208
00209
00225 FILE *G_fopen_new (char *element,char *name)
00226 {
00227 int fd;
00228
00229 fd = G__open (element, name, G_mapset(), 1);
00230 if (fd < 0)
00231 return (FILE *) 0;
00232
00233 return fdopen (fd, "w");
00234 }
00235
00236
00253 FILE *
00254 G_fopen_old (char *element,char *name,char *mapset)
00255 {
00256 int fd;
00257
00258 fd = G__open (element, name, mapset, 0);
00259 if (fd < 0)
00260 return (FILE *) 0;
00261
00262 return fdopen (fd, "r");
00263 }
00264
00265 FILE *
00266 G_fopen_append (char *element,char *name)
00267 {
00268 int fd;
00269
00270 fd = G__open (element, name, G_mapset(), 2);
00271 if (fd < 0)
00272 return (FILE *) 0;
00273 lseek (fd, 0L, 2);
00274
00275 return fdopen (fd, "a");
00276 }
00277
00278 FILE *G_fopen_modify (char *element,char *name)
00279 {
00280 int fd;
00281
00282 fd = G__open (element, name, G_mapset(), 2);
00283 if (fd < 0)
00284 return (FILE *) 0;
00285 lseek (fd, 0L, 0);
00286
00287 return fdopen (fd, "r+");
00288 }