list.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*!
  2. \file lib/gis/list.c
  3. \brief List elements
  4. \author Unknown (probably CERL)
  5. (C) 2000, 2010 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. */
  9. #include <stdlib.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <sys/types.h>
  14. #include <dirent.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. static int list_element(FILE *, const char *, const char *, const char *,
  18. int (*)(const char *, const char *, const char *));
  19. /*!
  20. \brief General purpose list function
  21. Will list files from all mapsets in the mapset list for a specified
  22. database element.
  23. Note: output is to stdout piped thru the more utility
  24. \code
  25. lister (char *name char *mapset, char* buf)
  26. \endcode
  27. Given file <em>name</em>, and <em>mapset</em>, lister() should
  28. copy a string into 'buf' when called with name == "", should set
  29. buf to general title for mapset list.
  30. \param element database element (eg, "cell", "cellhd", etc.)
  31. \param desc description for element (if NULL, element is used)
  32. \param mapset mapset to be listed "" to list all mapsets in mapset search list
  33. "." will list current mapset
  34. \param lister if given will call this routine to get a list title.
  35. NULL if no titles desired.
  36. */
  37. void G_list_element(const char *element,
  38. const char *desc,
  39. const char *mapset,
  40. int (*lister) (const char *, const char *, const char *))
  41. {
  42. struct Popen pager;
  43. int n;
  44. FILE *more;
  45. int count;
  46. count = 0;
  47. if (desc == 0 || *desc == 0)
  48. desc = element;
  49. /*
  50. * G_popen() the more command to page the output
  51. */
  52. more = G_open_pager(&pager);
  53. fprintf(more, "----------------------------------------------\n");
  54. /*
  55. * if no specific mapset is requested, list the mapsets
  56. * from the mapset search list
  57. * otherwise just list the specified mapset
  58. */
  59. if (mapset == 0 || *mapset == 0)
  60. for (n = 0; (mapset = G_get_mapset_name(n)); n++)
  61. count += list_element(more, element, desc, mapset, lister);
  62. else
  63. count += list_element(more, element, desc, mapset, lister);
  64. if (count == 0) {
  65. if (mapset == 0 || *mapset == 0)
  66. fprintf(more, _("no %s files available in current mapset\n"),
  67. desc);
  68. else
  69. fprintf(more, _("no %s files available in mapset <%s>\n"),
  70. desc, mapset);
  71. fprintf(more, "----------------------------------------------\n");
  72. }
  73. /*
  74. * close the more
  75. */
  76. G_close_pager(&pager);
  77. }
  78. static int list_element(FILE *out, const char *element, const char *desc, const char *mapset,
  79. int (*lister)(const char *, const char *, const char *))
  80. {
  81. char path[GPATH_MAX];
  82. int count = 0;
  83. char **list;
  84. int i;
  85. /*
  86. * convert . to current mapset
  87. */
  88. if (strcmp(mapset, ".") == 0)
  89. mapset = G_mapset();
  90. /*
  91. * get the full name of the GIS directory within the mapset
  92. * and list its contents (if it exists)
  93. *
  94. * if lister() routine is given, the ls command must give 1 name
  95. */
  96. G_file_name(path, element, "", mapset);
  97. if (access(path, 0) != 0) {
  98. fprintf(out, "\n");
  99. return count;
  100. }
  101. /*
  102. * if a title so that we can call lister() with the names
  103. * otherwise the ls must be forced into columnar form.
  104. */
  105. list = G_ls2(path, &count);
  106. if (count > 0) {
  107. fprintf(out, _("%s files available in mapset <%s>:\n"), desc, mapset);
  108. if (lister) {
  109. char title[400];
  110. char name[GNAME_MAX];
  111. *name = *title = 0;
  112. lister(name, mapset, title);
  113. if (*title)
  114. fprintf(out, "\n%-18s %-.60s\n", name, title);
  115. }
  116. }
  117. if (lister) {
  118. for (i = 0; i < count; i++) {
  119. char title[400];
  120. lister(list[i], mapset, title);
  121. fprintf(out, "%-18s %-.60s\n", list[i], title);
  122. }
  123. }
  124. else
  125. G_ls_format(list, count, 0, out);
  126. fprintf(out, "\n");
  127. for (i = 0; i < count; i++)
  128. G_free((char *)list[i]);
  129. if (list)
  130. G_free(list);
  131. return count;
  132. }
  133. /*!
  134. \brief List specified type of elements. Application must release
  135. the allocated memory.
  136. \param element element type (G_ELEMENT_RASTER, G_ELEMENT_VECTOR, G_ELEMENT_REGION )
  137. \param gisbase path to GISBASE
  138. \param location location name
  139. \param mapset mapset name
  140. \return zero terminated array of element names
  141. */
  142. char **G_list(int element, const char *gisbase, const char *location,
  143. const char *mapset)
  144. {
  145. char *el;
  146. char *buf;
  147. DIR *dirp;
  148. struct dirent *dp;
  149. int count;
  150. char **list;
  151. switch (element) {
  152. case G_ELEMENT_RASTER:
  153. el = "cell";
  154. break;
  155. case G_ELEMENT_GROUP:
  156. el = "group";
  157. break;
  158. case G_ELEMENT_VECTOR:
  159. el = "vector";
  160. break;
  161. case G_ELEMENT_REGION:
  162. el = "windows";
  163. break;
  164. default:
  165. G_fatal_error(_("G_list: Unknown element type"));
  166. }
  167. buf = (char *)G_malloc(strlen(gisbase) + strlen(location)
  168. + strlen(mapset) + strlen(el) + 4);
  169. sprintf(buf, "%s/%s/%s/%s", gisbase, location, mapset, el);
  170. dirp = opendir(buf);
  171. G_free(buf);
  172. if (dirp == NULL) { /* this can happen if element does not exist */
  173. list = (char **)G_calloc(1, sizeof(char *));
  174. return list;
  175. }
  176. count = 0;
  177. while ((dp = readdir(dirp)) != NULL) {
  178. if (dp->d_name[0] == '.')
  179. continue;
  180. count++;
  181. }
  182. rewinddir(dirp);
  183. list = (char **)G_calloc(count + 1, sizeof(char *));
  184. count = 0;
  185. while ((dp = readdir(dirp)) != NULL) {
  186. if (dp->d_name[0] == '.')
  187. continue;
  188. list[count] = (char *)G_malloc(strlen(dp->d_name) + 1);
  189. strcpy(list[count], dp->d_name);
  190. count++;
  191. }
  192. closedir(dirp);
  193. return list;
  194. }
  195. /*!
  196. \brief Free list
  197. \param list char* array to be freed
  198. \return
  199. */
  200. void G_free_list(char **list)
  201. {
  202. int i = 0;
  203. if (!list)
  204. return;
  205. while (list[i]) {
  206. G_free(list[i]);
  207. i++;
  208. }
  209. G_free(list);
  210. }