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