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