dbmscap.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <dirent.h>
00005 #include <unistd.h>
00006 #include "dbmi.h"
00007 #include "gis.h"
00008 
00009 static
00010 char *dbmscap_files[] = {
00011     "/etc/dbmscap",
00012     "/lib/dbmscap",
00013     "/usr/lib/dbmscap",
00014     "/usr/local/lib/dbmscap",
00015     "/usr/local/dbmi/lib/dbmscap",
00016     NULL };
00017 
00018 extern char *getenv();
00019 static void add_entry();
00020 
00021 static char *
00022 dbmscap_filename(err_flag)
00023 {
00024     char *file;
00025     int i;
00026 
00027     file = getenv ("DBMSCAP");
00028     if (file)
00029         return file;
00030 
00031     for (i = 0; file = dbmscap_files[i]; i++)
00032     {
00033         if (access (file, 0) == 0)
00034             return file;
00035     }
00036     if(err_flag)
00037         db_error ("DBMSCAP not set");
00038     return ((char *)NULL);
00039 }
00040 
00047 char *
00048 db_dbmscap_filename()
00049 {
00050     return dbmscap_filename(1);
00051 }
00052 
00059 int
00060 db_has_dbms()
00061 {
00062     return (dbmscap_filename(0) != NULL);
00063 }
00064 
00071 void
00072 db_copy_dbmscap_entry(dst, src)
00073     dbDbmscap *dst, *src;
00074 {
00075     strcpy (dst->driverName, src->driverName);
00076     strcpy (dst->comment, src->comment);
00077     strcpy (dst->startup, src->startup);
00078 }
00079 
00086 /* dbmscap file was used in grass5.0 but it is not used in
00087  * grass5.7 until we find it necessary. All code for dbmscap
00088  * file is commented here. 
00089  *
00090  * Instead of in dbmscap file db_read_dbmscap() searches 
00091  * for available dbmi drivers in $(GISBASE)/driver/db/  */ 
00092 
00093 dbDbmscap *
00094 db_read_dbmscap()
00095 {
00096     /*  
00097     FILE *fd;
00098     char *file;
00099     char name[1024];
00100     char startup[1024];
00101     char comment[1024];
00102     int  line;
00103     */
00104     char   buf[1024];
00105     char   *dirpath;
00106     DIR    *dir;        
00107     struct dirent *ent; 
00108 
00109     dbDbmscap *list = NULL;
00110 
00111 /* START OF OLD CODE FOR dbmscap FILE - NOT USED, BUT KEEP IT FOR FUTURE */
00112     /* get the full name of the dbmscap file */
00113     /*
00114     file = db_dbmscap_filename();
00115     if (file == NULL)
00116         return (dbDbmscap *) NULL;
00117     */
00118     
00119     /* open the dbmscap file */
00120     /*
00121     fd = fopen (file, "r");
00122     if (fd == NULL)
00123     {
00124         db_syserror (file);
00125         return (dbDbmscap *) NULL;
00126     }
00127     */ 
00128     
00129     /* find all valid entries
00130      * blank lines and lines with # as first non blank char are ignored
00131      * format is:
00132      *   driver name:startup command:comment
00133      */
00134     /*
00135     for (line = 1; fgets (buf, sizeof buf, fd); line++)
00136     {
00137         if (sscanf (buf, "%1s", comment) != 1 || *comment == '#')
00138             continue;
00139         if (sscanf (buf, "%[^:]:%[^:]:%[^:\n]", name, startup, comment) == 3)
00140             add_entry (&list, name, startup, comment);
00141         else if (sscanf (buf, "%[^:]:%[^:\n]", name, startup) == 2)
00142             add_entry (&list, name, startup, "");
00143         else
00144         {
00145             fprintf (stderr, "%s: line %d: invalid entry\n", file, line);
00146             fprintf (stderr,"%d:%s\n", line, buf);
00147         }
00148         if (list == NULL)
00149             break;
00150     }
00151     fclose (fd);
00152     */
00153 /* END OF OLD CODE FOR dbmscap FILE */
00154 
00155 /* START OF NEW CODE FOR SEARCH IN $(GISBASE)/driver/db/ */
00156     
00157     /* opend db drivers directory */
00158     G_asprintf (&dirpath, "%s/driver/db/", G_gisbase());
00159     dir = opendir(dirpath);
00160     if (dir == NULL)
00161     {
00162         db_syserror (dirpath);
00163         return (dbDbmscap *) NULL;
00164     }
00165     G_free (dirpath);
00166     
00167     /* read all drivers */
00168     while ( ent = readdir (dir) )
00169       {
00170         char *name;
00171 
00172         if ( (strcmp (ent->d_name, ".") == 0) 
00173             || (strcmp (ent->d_name, "..") == 0) ) continue;    
00174 
00175         /* Remove '.exe' from name (windows extension) */
00176         name = G_str_replace ( ent->d_name, ".exe", "" );
00177         
00178         G_asprintf (&dirpath, "%s/driver/db/%s", G_gisbase(),ent->d_name);
00179         add_entry (&list, name, dirpath, "");
00180         G_free (name);
00181         G_free (dirpath);
00182       }
00183     
00184     closedir (dir);
00185 
00186     return list;
00187 }
00188 
00189 static void
00190 add_entry (list, name, startup, comment)
00191     dbDbmscap **list;
00192     char *name;
00193     char *startup;
00194     char *comment;
00195 {
00196     dbDbmscap *head, *cur, *tail;
00197 
00198 /* add this entry to the head of a linked list */
00199     tail = head = *list;
00200     while (tail && tail->next)
00201         tail = tail->next;
00202     *list = NULL;
00203 
00204     cur = (dbDbmscap *) db_malloc (sizeof(dbDbmscap));
00205     if (cur == NULL)
00206         return; /* out of memory */
00207     cur->next = NULL;
00208 
00209 /* copy each item to the dbmscap structure */
00210     strcpy (cur->driverName, name);
00211     strcpy (cur->startup, startup);
00212     strcpy (cur->comment, comment);
00213 
00214 /* handle the first call (head == NULL) */
00215     if (tail)
00216         tail->next = cur;
00217     else
00218         head = cur;
00219 
00220     *list = head;
00221 }
00222 
00229 void
00230 db_free_dbmscap (list)
00231     dbDbmscap *list;
00232 {
00233     dbDbmscap *next, *cur;
00234 
00235     for (cur = list; cur; cur = next)
00236     {
00237         next = cur->next;
00238         free (cur);
00239     }
00240 }

Generated on Wed Aug 23 17:48:32 2006 for GRASS by  doxygen 1.4.7