GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
dbmscap.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <dirent.h>
5 #include <unistd.h>
6 #include <grass/dbmi.h>
7 #include <grass/gis.h>
8 
9 static char *dbmscap_files[] = {
10  "/etc/dbmscap",
11  "/lib/dbmscap",
12  "/usr/lib/dbmscap",
13  "/usr/local/lib/dbmscap",
14  "/usr/local/dbmi/lib/dbmscap",
15  NULL
16 };
17 
18 static void add_entry();
19 
20 static char *dbmscap_filename(err_flag)
21 {
22  char *file;
23  int i;
24 
25  file = getenv("DBMSCAP");
26  if (file)
27  return file;
28 
29  for (i = 0; (file = dbmscap_files[i]); i++) {
30  if (access(file, 0) == 0)
31  return file;
32  }
33  if (err_flag)
34  db_error("DBMSCAP not set");
35 
36  return ((char *)NULL);
37 }
38 
45 const char *db_dbmscap_filename(void)
46 {
47  return dbmscap_filename(1);
48 }
49 
56 int db_has_dbms(void)
57 {
58  return (dbmscap_filename(0) != NULL);
59 }
60 
67 void db_copy_dbmscap_entry(dbDbmscap * dst, dbDbmscap * src)
68 {
69  strcpy(dst->driverName, src->driverName);
70  strcpy(dst->comment, src->comment);
71  strcpy(dst->startup, src->startup);
72 }
73 
80 /* dbmscap file was used in grass5.0 but it is not used in
81  * grass5.7 until we find it necessary. All code for dbmscap
82  * file is commented here.
83  *
84  * Instead of in dbmscap file db_read_dbmscap() searches
85  * for available dbmi drivers in $(GISBASE)/driver/db/ */
86 
87 dbDbmscap *db_read_dbmscap(void)
88 {
89  /*
90  FILE *fd;
91  char *file;
92  char name[1024];
93  char startup[1024];
94  char comment[1024];
95  int line;
96  */
97  char *dirpath;
98  DIR *dir;
99  struct dirent *ent;
100 
101  dbDbmscap *list = NULL;
102 
103  /* START OF OLD CODE FOR dbmscap FILE - NOT USED, BUT KEEP IT FOR FUTURE */
104 #if 0
105  /* get the full name of the dbmscap file */
106 
107  file = db_dbmscap_filename();
108  if (file == NULL)
109  return (dbDbmscap *) NULL;
110 
111 
112  /* open the dbmscap file */
113 
114  fd = fopen(file, "r");
115  if (fd == NULL) {
116  db_syserror(file);
117  return (dbDbmscap *) NULL;
118  }
119 
120 
121  /* find all valid entries
122  * blank lines and lines with # as first non blank char are ignored
123  * format is:
124  * driver name:startup command:comment
125  */
126 
127  for (line = 1; fgets(buf, sizeof buf, fd); line++) {
128  if (sscanf(buf, "%1s", comment) != 1 || *comment == '#')
129  continue;
130  if (sscanf(buf, "%[^:]:%[^:]:%[^:\n]", name, startup, comment) == 3)
131  add_entry(&list, name, startup, comment);
132  else if (sscanf(buf, "%[^:]:%[^:\n]", name, startup) == 2)
133  add_entry(&list, name, startup, "");
134  else {
135  fprintf(stderr, "%s: line %d: invalid entry\n", file, line);
136  fprintf(stderr, "%d:%s\n", line, buf);
137  }
138  if (list == NULL)
139  break;
140  }
141  fclose(fd);
142 #endif
143  /* END OF OLD CODE FOR dbmscap FILE */
144 
145  /* START OF NEW CODE FOR SEARCH IN $(GISBASE)/driver/db/ */
146 
147  /* opend db drivers directory */
148 #ifdef __MINGW32__
149  dirpath = G_malloc(strlen("\\driver\\db\\") + strlen(G_gisbase()) + 1);
150  sprintf(dirpath, "%s\\driver\\db\\", G_gisbase());
151  G_convert_dirseps_to_host(dirpath);
152 #else
153  G_asprintf(&dirpath, "%s/driver/db/", G_gisbase());
154 #endif
155 
156  G_debug(2, "dbDbmscap(): opendir [%s]", dirpath);
157  dir = opendir(dirpath);
158  if (dir == NULL) {
159  db_syserror("Cannot open drivers directory");
160  return (dbDbmscap *) NULL;
161  }
162  G_free(dirpath);
163 
164  /* read all drivers */
165  while ((ent = readdir(dir))) {
166  char *name;
167 
168  if ((strcmp(ent->d_name, ".") == 0)
169  || (strcmp(ent->d_name, "..") == 0))
170  continue;
171 
172 #ifdef __MINGW32__
173  /* skip manifest files on Windows */
174  if (G_strstr(ent->d_name, ".manifest"))
175  continue;
176 #endif
177 
178  /* Remove '.exe' from name (windows extension) */
179  name = G_str_replace(ent->d_name, ".exe", "");
180 
181 #ifdef __MINGW32__
182  dirpath = G_malloc(strlen("\\driver\\db\\")
183  + strlen(G_gisbase()) + strlen(ent->d_name) + 1);
184  sprintf(dirpath, "%s\\driver\\db\\%s", G_gisbase(), ent->d_name);
185  G_convert_dirseps_to_host(dirpath);
186 #else
187  G_asprintf(&dirpath, "%s/driver/db/%s", G_gisbase(), ent->d_name);
188 #endif
189  add_entry(&list, name, dirpath, "");
190  G_free(name);
191  G_free(dirpath);
192  }
193 
194  closedir(dir);
195 
196  return list;
197 }
198 
199 static void
200 add_entry(dbDbmscap ** list, char *name, char *startup, char *comment)
201 {
202  dbDbmscap *head, *cur, *tail;
203 
204  /* add this entry to the head of a linked list */
205  tail = head = *list;
206  while (tail && tail->next)
207  tail = tail->next;
208  *list = NULL;
209 
210  cur = (dbDbmscap *) db_malloc(sizeof(dbDbmscap));
211  if (cur == NULL)
212  return; /* out of memory */
213  cur->next = NULL;
214 
215  /* copy each item to the dbmscap structure */
216  strcpy(cur->driverName, name);
217  strcpy(cur->startup, startup);
218  strcpy(cur->comment, comment);
219 
220  /* handle the first call (head == NULL) */
221  if (tail)
222  tail->next = cur;
223  else
224  head = cur;
225 
226  *list = head;
227 }
228 
235 void db_free_dbmscap(dbDbmscap * list)
236 {
237  dbDbmscap *next, *cur;
238 
239  for (cur = list; cur; cur = next) {
240  next = cur->next;
241  db_free(cur);
242  }
243 }
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
DIR * opendir()
void G_free(void *buf)
Free allocated memory.
Definition: gis/alloc.c:142
dbDbmscap * db_read_dbmscap(void)
Definition: dbmscap.c:87
FILE * fd
Definition: g3dcolor.c:368
string name
Definition: render.py:1314
char * G_convert_dirseps_to_host(char *path)
Converts directory separator characters in a string to the native host separator character (/ on Unix...
Definition: paths.c:73
int G_asprintf(char **out, const char *fmt,...)
Definition: asprintf.c:116
void db_error(const char *s)
char * getenv()
dir_entry * readdir()
char * G_str_replace(char *buffer, const char *old_str, const char *new_str)
Replace all occurencies of old_str in buffer with new_str.
Definition: strings.c:316
void db_syserror(const char *s)
const char * db_dbmscap_filename(void)
Definition: dbmscap.c:45
void db_copy_dbmscap_entry(dbDbmscap *dst, dbDbmscap *src)
Definition: dbmscap.c:67
void * db_malloc(int n)
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
char * G_strstr(const char *mainString, const char *subString)
Finds the first occurrence of the character C in the null-terminated string beginning at mainString...
Definition: strings.c:230
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
fclose(fd)
#define file
char * G_gisbase(void)
top level module directory
Definition: gisbase.c:42
void db_free_dbmscap(dbDbmscap *list)
Definition: dbmscap.c:235
int db_has_dbms(void)
Definition: dbmscap.c:56
void db_free(void *s)