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 G_convert_dirseps_to_host(dirpath);
00152 #else
00153 G_asprintf(&dirpath, "%s/driver/db/", G_gisbase());
00154 #endif
00155
00156 G_debug(2, "dbDbmscap(): opendir [%s]", dirpath);
00157 dir = opendir(dirpath);
00158 if (dir == NULL) {
00159 db_syserror("Cannot open drivers directory");
00160 return (dbDbmscap *) NULL;
00161 }
00162 G_free(dirpath);
00163
00164
00165 while ((ent = readdir(dir))) {
00166 char *name;
00167
00168 if ((strcmp(ent->d_name, ".") == 0)
00169 || (strcmp(ent->d_name, "..") == 0))
00170 continue;
00171
00172
00173 name = G_str_replace(ent->d_name, ".exe", "");
00174
00175 #ifdef __MINGW32__
00176 dirpath = G_malloc(strlen("\\driver\\db\\")
00177 + strlen(G_gisbase()) + strlen(ent->d_name) + 1);
00178 sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
00179 G_convert_dirseps_to_host(dirpath);
00180 #else
00181 G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name);
00182 #endif
00183 add_entry(&list, name, dirpath, "");
00184 G_free(name);
00185 G_free(dirpath);
00186 }
00187
00188 closedir(dir);
00189
00190 return list;
00191 }
00192
00193 static void
00194 add_entry(dbDbmscap ** list, char *name, char *startup, char *comment)
00195 {
00196 dbDbmscap *head, *cur, *tail;
00197
00198
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;
00207 cur->next = NULL;
00208
00209
00210 strcpy(cur->driverName, name);
00211 strcpy(cur->startup, startup);
00212 strcpy(cur->comment, comment);
00213
00214
00215 if (tail)
00216 tail->next = cur;
00217 else
00218 head = cur;
00219
00220 *list = head;
00221 }
00222
00229 void db_free_dbmscap(dbDbmscap * list)
00230 {
00231 dbDbmscap *next, *cur;
00232
00233 for (cur = list; cur; cur = next) {
00234 next = cur->next;
00235 free(cur);
00236 }
00237 }