dbmscap.c 5.9 KB

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