mapset_nme.c 4.0 KB

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