mapset_nme.c 4.1 KB

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