-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
dbmscap.c
267 lines (216 loc) · 5.92 KB
/
dbmscap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*!
\file lib/db/dbmi_base/dbmscap.c
\brief DBMI Library (base) - DBmscap management
(C) 1999-2009 by the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
\author Joel Jones (CERL/UIUC), Radim Blazek
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <grass/dbmi.h>
#include <grass/gis.h>
static char *dbmscap_files[] = {
"/etc/dbmscap",
"/lib/dbmscap",
"/usr/lib/dbmscap",
"/usr/local/lib/dbmscap",
"/usr/local/dbmi/lib/dbmscap",
NULL
};
static void
add_entry(dbDbmscap ** list, char *name, char *startup, char *comment);
static char *dbmscap_filename(int err_flag)
{
char *file;
int i;
file = getenv("DBMSCAP");
if (file)
return file;
for (i = 0; (file = dbmscap_files[i]); i++) {
if (access(file, 0) == 0)
return file;
}
if (err_flag)
db_error("DBMSCAP not set");
return ((char *)NULL);
}
/*!
\brief Get dbmscap file name
\return pointer to string with file name
*/
const char *db_dbmscap_filename(void)
{
return dbmscap_filename(1);
}
/*!
\brief Check dbms
\return 1 if true
\return 0 if false
*/
int db_has_dbms(void)
{
return (dbmscap_filename(0) != NULL);
}
/*!
\brief Copy dbmscap entry
\param dst destination
\param src source
*/
void db_copy_dbmscap_entry(dbDbmscap * dst, dbDbmscap * src)
{
strcpy(dst->driverName, src->driverName);
strcpy(dst->comment, src->comment);
strcpy(dst->startup, src->startup);
}
/*!
\brief Read dbmscap
dbmscap file was used in grass5.0 but it is not used in
grass5.7 until we find it necessary. All code for dbmscap
file is commented here.
Instead of in dbmscap file db_read_dbmscap() searches
for available dbmi drivers in $(GISBASE)/driver/db/
\return pointer to dbDbmscap
*/
dbDbmscap *db_read_dbmscap(void)
{
/*
FILE *fd;
char *file;
char name[1024];
char startup[1024];
char comment[1024];
int line;
*/
char *dirpath;
DIR *dir;
struct dirent *ent;
dbDbmscap *list = NULL;
/* START OF OLD CODE FOR dbmscap FILE - NOT USED, BUT KEEP IT FOR FUTURE */
#if 0
/* get the full name of the dbmscap file */
file = db_dbmscap_filename();
if (file == NULL)
return (dbDbmscap *) NULL;
/* open the dbmscap file */
fd = fopen(file, "r");
if (fd == NULL) {
db_syserror(file);
return (dbDbmscap *) NULL;
}
/* find all valid entries
* blank lines and lines with # as first non blank char are ignored
* format is:
* driver name:startup command:comment
*/
for (line = 1; fgets(buf, sizeof buf, fd); line++) {
if (sscanf(buf, "%1s", comment) != 1 || *comment == '#')
continue;
if (sscanf(buf, "%[^:]:%[^:]:%[^:\n]", name, startup, comment) == 3)
add_entry(&list, name, startup, comment);
else if (sscanf(buf, "%[^:]:%[^:\n]", name, startup) == 2)
add_entry(&list, name, startup, "");
else {
fprintf(stderr, "%s: line %d: invalid entry\n", file, line);
fprintf(stderr, "%d:%s\n", line, buf);
}
if (list == NULL)
break;
}
fclose(fd);
#endif
/* END OF OLD CODE FOR dbmscap FILE */
/* START OF NEW CODE FOR SEARCH IN $(GISBASE)/driver/db/ */
/* opend db drivers directory */
#ifdef __MINGW32__
dirpath = G_malloc(strlen("\\driver\\db\\") + strlen(G_gisbase()) + 1);
sprintf(dirpath, "%s\\driver\\db\\", G_gisbase());
G_convert_dirseps_to_host(dirpath);
#else
G_asprintf(&dirpath, "%s/driver/db/", G_gisbase());
#endif
G_debug(2, "dbDbmscap(): opendir [%s]", dirpath);
dir = opendir(dirpath);
if (dir == NULL) {
db_syserror("Cannot open drivers directory");
return (dbDbmscap *) NULL;
}
G_free(dirpath);
/* read all drivers */
while ((ent = readdir(dir))) {
char *name;
if ((strcmp(ent->d_name, ".") == 0)
|| (strcmp(ent->d_name, "..") == 0))
continue;
#ifdef __MINGW32__
/* skip manifest files on Windows */
if (strstr(ent->d_name, ".manifest"))
continue;
#endif
/* Remove '.exe' from name (windows extension) */
name = G_str_replace(ent->d_name, ".exe", "");
#ifdef __MINGW32__
dirpath = G_malloc(strlen("\\driver\\db\\")
+ strlen(G_gisbase()) + strlen(ent->d_name) + 1);
sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
G_convert_dirseps_to_host(dirpath);
#else
G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name);
#endif
add_entry(&list, name, dirpath, "");
G_free(name);
G_free(dirpath);
}
closedir(dir);
return list;
}
static int cmp_entry(dbDbmscap *a, dbDbmscap *b) {
return( a->driverName && b->driverName ? strcmp(a->driverName,b->driverName) : 0 );
}
static void add_entry(dbDbmscap ** list, char *name, char *startup, char *comment)
{
/* add an entry to the list, so that the list remains ordered (by driverName) */
dbDbmscap *head, *cur, *tail;
cur = (dbDbmscap *) db_malloc(sizeof(dbDbmscap));
if (cur == NULL) {
*list = NULL;
return;
/* out of memory */
}
cur->next = NULL;
/* copy each item to the dbmscap structure */
strcpy(cur->driverName, name);
strcpy(cur->startup, startup);
strcpy(cur->comment, comment);
/* find the last entry that is less than cur */
tail = head = *list;
while (tail && tail->next && cmp_entry(tail->next,cur)<0)
tail = tail->next;
/* handle the first call (head == NULL) */
if (tail && cmp_entry(tail,cur)<0) {
/* insert right after tail */
cur->next = tail->next;
tail->next = cur;
} else {
/* insert at first position */
cur->next = head;
head = cur;
}
*list = head;
}
/*!
\brief Free dbmscap
\param list pointer to dbDbmscap
*/
void db_free_dbmscap(dbDbmscap * list)
{
dbDbmscap *next, *cur;
for (cur = list; cur; cur = next) {
next = cur->next;
db_free(cur);
}
}