00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <stdlib.h>
00004 #include <dirent.h>
00005 #include <unistd.h>
00006 #include <grass/dbmi.h>
00007 #include <grass/gis.h>
00008
00009 static char *dbmscap_files[] = {
00010 "/etc/dbmscap",
00011 "/lib/dbmscap",
00012 "/usr/lib/dbmscap",
00013 "/usr/local/lib/dbmscap",
00014 "/usr/local/dbmi/lib/dbmscap",
00015 NULL
00016 };
00017
00018 static void add_entry();
00019
00020 static char *dbmscap_filename(err_flag)
00021 {
00022 char *file;
00023 int i;
00024
00025 file = getenv("DBMSCAP");
00026 if (file)
00027 return file;
00028
00029 for (i = 0; (file = dbmscap_files[i]); i++) {
00030 if (access(file, 0) == 0)
00031 return file;
00032 }
00033 if (err_flag)
00034 db_error("DBMSCAP not set");
00035
00036 return ((char *)NULL);
00037 }
00038
00045 const char *db_dbmscap_filename(void)
00046 {
00047 return dbmscap_filename(1);
00048 }
00049
00056 int db_has_dbms(void)
00057 {
00058 return (dbmscap_filename(0) != NULL);
00059 }
00060
00067 void db_copy_dbmscap_entry(dbDbmscap * dst, dbDbmscap * src)
00068 {
00069 strcpy(dst->driverName, src->driverName);
00070 strcpy(dst->comment, src->comment);
00071 strcpy(dst->startup, src->startup);
00072 }
00073
00080
00081
00082
00083
00084
00085
00086
00087 dbDbmscap *db_read_dbmscap(void)
00088 {
00089
00090
00091
00092
00093
00094
00095
00096
00097 char *dirpath;
00098 DIR *dir;
00099 struct dirent *ent;
00100
00101 dbDbmscap *list = NULL;
00102
00103
00104 #if 0
00105
00106
00107 file = db_dbmscap_filename();
00108 if (file == NULL)
00109 return (dbDbmscap *) NULL;
00110
00111
00112
00113
00114 fd = fopen(file, "r");
00115 if (fd == NULL) {
00116 db_syserror(file);
00117 return (dbDbmscap *) NULL;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127 for (line = 1; fgets(buf, sizeof buf, fd); line++) {
00128 if (sscanf(buf, "%1s", comment) != 1 || *comment == '#')
00129 continue;
00130 if (sscanf(buf, "%[^:]:%[^:]:%[^:\n]", name, startup, comment) == 3)
00131 add_entry(&list, name, startup, comment);
00132 else if (sscanf(buf, "%[^:]:%[^:\n]", name, startup) == 2)
00133 add_entry(&list, name, startup, "");
00134 else {
00135 fprintf(stderr, "%s: line %d: invalid entry\n", file, line);
00136 fprintf(stderr, "%d:%s\n", line, buf);
00137 }
00138 if (list == NULL)
00139 break;
00140 }
00141 fclose(fd);
00142 #endif
00143
00144
00145
00146
00147
00148 #ifdef __MINGW32__
00149 dirpath = G_malloc(strlen("\\driver\\db\\") + strlen(G_gisbase()) + 1);
00150 sprintf(dirpath, "%s\\driver\\db\\", G_gisbase());
00151 #else
00152 G_asprintf(&dirpath, "%s/driver/db/", G_gisbase());
00153 #endif
00154
00155 G_debug(2, "opendir %s\n", dirpath);
00156 dir = opendir(dirpath);
00157 if (dir == NULL) {
00158 db_syserror("Cannot open drivers directory");
00159 return (dbDbmscap *) NULL;
00160 }
00161 G_free(dirpath);
00162
00163
00164 while ((ent = readdir(dir))) {
00165 char *name;
00166
00167 if ((strcmp(ent->d_name, ".") == 0)
00168 || (strcmp(ent->d_name, "..") == 0))
00169 continue;
00170
00171
00172 name = G_str_replace(ent->d_name, ".exe", "");
00173
00174 #ifdef __MINGW32__
00175 dirpath = G_malloc(strlen("\\driver\\db\\")
00176 + strlen(G_gisbase()) + strlen(ent->d_name) + 1);
00177 sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
00178 #else
00179 G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name);
00180 #endif
00181 add_entry(&list, name, dirpath, "");
00182 G_free(name);
00183 G_free(dirpath);
00184 }
00185
00186 closedir(dir);
00187
00188 return list;
00189 }
00190
00191 static void
00192 add_entry(dbDbmscap ** list, char *name, char *startup, char *comment)
00193 {
00194 dbDbmscap *head, *cur, *tail;
00195
00196
00197 tail = head = *list;
00198 while (tail && tail->next)
00199 tail = tail->next;
00200 *list = NULL;
00201
00202 cur = (dbDbmscap *) db_malloc(sizeof(dbDbmscap));
00203 if (cur == NULL)
00204 return;
00205 cur->next = NULL;
00206
00207
00208 strcpy(cur->driverName, name);
00209 strcpy(cur->startup, startup);
00210 strcpy(cur->comment, comment);
00211
00212
00213 if (tail)
00214 tail->next = cur;
00215 else
00216 head = cur;
00217
00218 *list = head;
00219 }
00220
00227 void db_free_dbmscap(dbDbmscap * list)
00228 {
00229 dbDbmscap *next, *cur;
00230
00231 for (cur = list; cur; cur = next) {
00232 next = cur->next;
00233 free(cur);
00234 }
00235 }