dbmscap.c 4.9 KB

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