open.c

Go to the documentation of this file.
00001 /*
00002  *****************************************************************
00003  * open routines
00004  *
00005  * G__open (element, name, mapset, mode)
00006  *      char *element         database element name
00007  *      char *name            map file name
00008  *      char *mapset          mapset containing map "name"
00009  *      int mode              0=read, 1=write, 2=read/write
00010  * 
00011  *      this is the lowest level open routine.
00012  *      opens the file 'name' in 'element' ("cell", etc)
00013  *      in mapset 'mapset' according to the i/o 'mode'
00014  *
00015  *      mode = 0 (read) will look for 'name' in 'mapset'
00016  *               and open the file for read only
00017  *               the file must exist
00018  *
00019  *      mode = 1 (write) will create an empty file 'name' in the
00020  *               current mapset and open the file for write only
00021  *               'mapset' ignored
00022  *
00023  *      mode = 2 (read and write) will open a file in the
00024  *               current mapset for reading and writing
00025  *               creating a new file if necessary
00026  *               'mapset' ignored
00027  *
00028  *      returns: open file descriptor (int)
00029  *               or -1 could not open
00030  *
00031  *******************************************************************
00032  * G_open_new (element, name)
00033  *      char *element         database element name
00034  *      char *name            map file name
00035  *
00036  *      creates 'name' in the current mapset and opens it
00037  *      for write only.
00038  *
00039  *      returns: open file descriptor (int)
00040  *               or -1 could not open
00041  *
00042  *******************************************************************
00043  * G_open_old (element, name, mapset)
00044  *      char *element         database element name
00045  *      char *name            map file name
00046  *      char *mapset          mapset containing map "name"
00047  *
00048  *      opens 'name' in 'mapset' for read only.
00049  *
00050  *      returns: open file descriptor (int)
00051  *               or -1 could not open
00052  *
00053  *******************************************************************
00054  * G_fopen_new (element, name)
00055  *      char *element         database element name
00056  *      char *name            map file name
00057  *
00058  *      creates 'name' in the current mapset and opens it
00059  *      for write only.
00060  *
00061  *      returns: open file descriptor (FILE *)
00062  *               or NULL could not open
00063  *
00064  *******************************************************************
00065  * G_fopen_old (element, name, mapset)
00066  *      char *element         database element name
00067  *      char *name            map file name
00068  *
00069  *      opens 'name' in 'mapset' for read only.
00070  *
00071  *      returns: open file descriptor (FILE *)
00072  *               or NULL could not open
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 /* READ */
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 /* WRITE */
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 }

Generated on Sat Jul 22 22:06:15 2006 for GRASS by  doxygen 1.4.7