GRASS Programmer's Manual
6.4.2(2012)
|
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 /* dbmscap file was used in grass5.0 but it is not used in 00081 * grass5.7 until we find it necessary. All code for dbmscap 00082 * file is commented here. 00083 * 00084 * Instead of in dbmscap file db_read_dbmscap() searches 00085 * for available dbmi drivers in $(GISBASE)/driver/db/ */ 00086 00087 dbDbmscap *db_read_dbmscap(void) 00088 { 00089 /* 00090 FILE *fd; 00091 char *file; 00092 char name[1024]; 00093 char startup[1024]; 00094 char comment[1024]; 00095 int line; 00096 */ 00097 char *dirpath; 00098 DIR *dir; 00099 struct dirent *ent; 00100 00101 dbDbmscap *list = NULL; 00102 00103 /* START OF OLD CODE FOR dbmscap FILE - NOT USED, BUT KEEP IT FOR FUTURE */ 00104 #if 0 00105 /* get the full name of the dbmscap file */ 00106 00107 file = db_dbmscap_filename(); 00108 if (file == NULL) 00109 return (dbDbmscap *) NULL; 00110 00111 00112 /* open the dbmscap file */ 00113 00114 fd = fopen(file, "r"); 00115 if (fd == NULL) { 00116 db_syserror(file); 00117 return (dbDbmscap *) NULL; 00118 } 00119 00120 00121 /* find all valid entries 00122 * blank lines and lines with # as first non blank char are ignored 00123 * format is: 00124 * driver name:startup command:comment 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 /* END OF OLD CODE FOR dbmscap FILE */ 00144 00145 /* START OF NEW CODE FOR SEARCH IN $(GISBASE)/driver/db/ */ 00146 00147 /* opend db drivers directory */ 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 /* read all drivers */ 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 #ifdef __MINGW32__ 00173 /* skip manifest files on Windows */ 00174 if (G_strstr(ent->d_name, ".manifest")) 00175 continue; 00176 #endif 00177 00178 /* Remove '.exe' from name (windows extension) */ 00179 name = G_str_replace(ent->d_name, ".exe", ""); 00180 00181 #ifdef __MINGW32__ 00182 dirpath = G_malloc(strlen("\\driver\\db\\") 00183 + strlen(G_gisbase()) + strlen(ent->d_name) + 1); 00184 sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name); 00185 G_convert_dirseps_to_host(dirpath); 00186 #else 00187 G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name); 00188 #endif 00189 add_entry(&list, name, dirpath, ""); 00190 G_free(name); 00191 G_free(dirpath); 00192 } 00193 00194 closedir(dir); 00195 00196 return list; 00197 } 00198 00199 static void 00200 add_entry(dbDbmscap ** list, char *name, char *startup, char *comment) 00201 { 00202 dbDbmscap *head, *cur, *tail; 00203 00204 /* add this entry to the head of a linked list */ 00205 tail = head = *list; 00206 while (tail && tail->next) 00207 tail = tail->next; 00208 *list = NULL; 00209 00210 cur = (dbDbmscap *) db_malloc(sizeof(dbDbmscap)); 00211 if (cur == NULL) 00212 return; /* out of memory */ 00213 cur->next = NULL; 00214 00215 /* copy each item to the dbmscap structure */ 00216 strcpy(cur->driverName, name); 00217 strcpy(cur->startup, startup); 00218 strcpy(cur->comment, comment); 00219 00220 /* handle the first call (head == NULL) */ 00221 if (tail) 00222 tail->next = cur; 00223 else 00224 head = cur; 00225 00226 *list = head; 00227 } 00228 00235 void db_free_dbmscap(dbDbmscap * list) 00236 { 00237 dbDbmscap *next, *cur; 00238 00239 for (cur = list; cur; cur = next) { 00240 next = cur->next; 00241 db_free(cur); 00242 } 00243 }