dbmscap.c 5.3 KB

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