mapset_nme.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*!
  2. \file mapset_nme.c
  3. \brief GIS library - Mapset name, search path routines.
  4. (C) 1999-2008 The GRASS development team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. */
  8. #include <grass/config.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <string.h>
  12. #include <dirent.h>
  13. #include <unistd.h>
  14. #include <grass/gis.h>
  15. static struct state {
  16. struct list {
  17. char **names;
  18. int count;
  19. int size;
  20. } path, path2;
  21. } state;
  22. static struct state *st = &state;
  23. static void new_mapset(const char *);
  24. /*!
  25. \brief Get name of the n'th mapset from the mapset_name[] list.
  26. The first call will initialize the list.
  27. \param n mapset index
  28. \return mapset name
  29. \return NULL if mapset not found
  30. */
  31. const char *G__mapset_name(int n)
  32. {
  33. G_get_list_of_mapsets();
  34. if (n < 0 || n >= st->path.count)
  35. return NULL;
  36. return st->path.names[n];
  37. }
  38. void G_get_list_of_mapsets(void)
  39. {
  40. FILE *fp;
  41. const char *cur;
  42. if (st->path.count > 0)
  43. return;
  44. st->path.count = 0;
  45. st->path.size = 0;
  46. st->path.names = NULL;
  47. cur = G_mapset();
  48. new_mapset(cur);
  49. fp = G_fopen_old("", "SEARCH_PATH", G_mapset());
  50. if (fp) {
  51. char name[GNAME_MAX];
  52. while (fscanf(fp, "%s", name) == 1) {
  53. if (strcmp(name, cur) == 0)
  54. continue;
  55. if (G__mapset_permissions(name) >= 0)
  56. new_mapset(name);
  57. }
  58. fclose(fp);
  59. }
  60. else {
  61. static const char perm[] = "PERMANENT";
  62. if (strcmp(perm, cur) != 0 && G__mapset_permissions(perm) >= 0)
  63. new_mapset(perm);
  64. }
  65. }
  66. static void new_mapset(const char *name)
  67. {
  68. if (st->path.count >= st->path.size) {
  69. st->path.size += 10;
  70. st->path.names = G_realloc(st->path.names, st->path.size * sizeof(char *));
  71. }
  72. st->path.names[st->path.count++] = G_store(name);
  73. }
  74. /*!
  75. \brief Define alternative mapset search path
  76. \return 0
  77. */
  78. void G__create_alt_search_path(void)
  79. {
  80. st->path2.count = st->path.count;
  81. st->path2.names = st->path.names;
  82. st->path.count = 0;
  83. }
  84. /*!
  85. \brief Switch mapset search path
  86. \return 0
  87. */
  88. void G__switch_search_path(void)
  89. {
  90. int count;
  91. char **names;
  92. count = st->path2.count;
  93. names = st->path2.names;
  94. st->path2.count = st->path.count;
  95. st->path2.names = st->path.names;
  96. st->path.count = count;
  97. st->path.names = names;
  98. }
  99. /*!
  100. \brief Reset number of mapsets
  101. \return 0
  102. */
  103. void G_reset_mapsets(void)
  104. {
  105. st->path.count = 0;
  106. }
  107. /*!
  108. \brief Get list of available mapsets for current location
  109. List is updated by each call to this function
  110. \return pointer to zero terminated array of available mapsets.
  111. */
  112. char **G_available_mapsets(void)
  113. {
  114. char **mapsets = NULL;
  115. int alloc = 50;
  116. int n = 0;
  117. DIR *dir;
  118. struct dirent *ent;
  119. G_debug(3, "G_available_mapsets");
  120. mapsets = G_calloc(alloc, sizeof(char *));
  121. dir = opendir(G_location_path());
  122. if (!dir)
  123. return mapsets;
  124. while ((ent = readdir(dir))) {
  125. char buf[GPATH_MAX];
  126. struct stat st;
  127. sprintf(buf, "%s/%s/WIND", G_location_path(), ent->d_name);
  128. if (G_stat(buf, &st) != 0) {
  129. G_debug(4, "%s is not mapset", ent->d_name);
  130. continue;
  131. }
  132. G_debug(4, "%s is mapset", ent->d_name);
  133. if (n + 2 >= alloc) {
  134. alloc += 50;
  135. mapsets = G_realloc(mapsets, alloc * sizeof(char *));
  136. }
  137. mapsets[n++] = G_store(ent->d_name);
  138. mapsets[n] = NULL;
  139. }
  140. closedir(dir);
  141. return mapsets;
  142. }
  143. /*!
  144. \brief Add mapset to the list of mapsets in search path.
  145. Mapset is add in memory only, not to the SEARCH_PATH file!
  146. List is check first if already exists.
  147. \param mapset mapset name
  148. */
  149. void G_add_mapset_to_search_path(const char *mapset)
  150. {
  151. if (!G_is_mapset_in_search_path(mapset))
  152. new_mapset(mapset);
  153. }
  154. /*!
  155. \brief Check if given mapset is in search path
  156. \param mapset mapset name
  157. \return 1 mapset found in search path
  158. \return 0 mapset not found
  159. */
  160. int G_is_mapset_in_search_path(const char *mapset)
  161. {
  162. int i;
  163. for (i = 0; i < st->path.count; i++) {
  164. if (strcmp(st->path.names[i], mapset) == 0)
  165. return 1;
  166. }
  167. return 0;
  168. }