list.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. \file list.c
  3. \brief List elements
  4. \author Unknown (probably CERL)
  5. (C) 2000 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. */
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <signal.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include <dirent.h>
  16. #include <grass/gis.h>
  17. #include <grass/glocale.h>
  18. static int list_element(FILE *, const char *, const char *, const char *,
  19. int (*)(const char *, const char *, const char *));
  20. /**
  21. \brief General purpose list function.
  22. Will list files from all mapsets
  23. in the mapset list for a specified database element.
  24. Note: output is to stdout piped thru the more utility
  25. lister (char *name char *mapset, char* buf)
  26. Given file 'name', and 'mapset', lister() should copy a string into 'buf'
  27. when called with name == "", should set buf to general title for mapset list.
  28. \param element Database element (eg, "cell", "cellhd", etc)
  29. \param desc Description for element (if NULL, element is used)
  30. \param mapset Mapset to be listed "" to list all mapsets in mapset search list
  31. "." will list current mapset
  32. \param lister If given will call this routine to get a list
  33. title. NULL if no titles desired.
  34. \return
  35. */
  36. void G_list_element(const char *element,
  37. const char *desc,
  38. const char *mapset,
  39. int (*lister) (const char *, const char *, const char *))
  40. {
  41. struct Popen pager;
  42. int n;
  43. FILE *more;
  44. int count;
  45. count = 0;
  46. if (desc == 0 || *desc == 0)
  47. desc = element;
  48. /*
  49. * G_popen() the more command to page the output
  50. */
  51. more = G_open_pager(&pager);
  52. fprintf(more, "----------------------------------------------\n");
  53. /*
  54. * if no specific mapset is requested, list the mapsets
  55. * from the mapset search list
  56. * otherwise just list the specified mapset
  57. */
  58. if (mapset == 0 || *mapset == 0)
  59. for (n = 0; (mapset = G__mapset_name(n)); n++)
  60. count += list_element(more, element, desc, mapset, lister);
  61. else
  62. count += list_element(more, element, desc, mapset, lister);
  63. if (count == 0) {
  64. if (mapset == 0 || *mapset == 0)
  65. fprintf(more, _("no %s files available in current mapset\n"),
  66. desc);
  67. else
  68. fprintf(more, _("no %s files available in mapset <%s>\n"),
  69. desc, mapset);
  70. fprintf(more, "----------------------------------------------\n");
  71. }
  72. /*
  73. * close the more
  74. */
  75. G_close_pager(&pager);
  76. }
  77. static int list_element(
  78. 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__ls(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,
  137. G_ELEMENT_REGION )
  138. * \param gisbase Path to GISBASE
  139. * \param location Location name
  140. * \param mapset Mapset name
  141. * \return Zero terminated array of element names
  142. */
  143. char **G_list(int element, const char *gisbase, const char *location,
  144. const char *mapset)
  145. {
  146. char *el;
  147. char *buf;
  148. DIR *dirp;
  149. struct dirent *dp;
  150. int count;
  151. char **list;
  152. switch (element) {
  153. case G_ELEMENT_RASTER:
  154. el = "cell";
  155. break;
  156. case G_ELEMENT_GROUP:
  157. el = "group";
  158. break;
  159. case G_ELEMENT_VECTOR:
  160. el = "vector";
  161. break;
  162. case G_ELEMENT_REGION:
  163. el = "windows";
  164. break;
  165. default:
  166. G_fatal_error(_("G_list: Unknown element type"));
  167. }
  168. buf = (char *)G_malloc(strlen(gisbase) + strlen(location)
  169. + strlen(mapset) + strlen(el) + 4);
  170. sprintf(buf, "%s/%s/%s/%s", gisbase, location, mapset, el);
  171. dirp = opendir(buf);
  172. G_free(buf);
  173. if (dirp == NULL) { /* this can happen if element does not exist */
  174. list = (char **)G_calloc(1, sizeof(char *));
  175. return list;
  176. }
  177. count = 0;
  178. while ((dp = readdir(dirp)) != NULL) {
  179. if (dp->d_name[0] == '.')
  180. continue;
  181. count++;
  182. }
  183. rewinddir(dirp);
  184. list = (char **)G_calloc(count + 1, sizeof(char *));
  185. count = 0;
  186. while ((dp = readdir(dirp)) != NULL) {
  187. if (dp->d_name[0] == '.')
  188. continue;
  189. list[count] = (char *)G_malloc(strlen(dp->d_name) + 1);
  190. strcpy(list[count], dp->d_name);
  191. count++;
  192. }
  193. closedir(dirp);
  194. return list;
  195. }
  196. /**
  197. \brief Free list
  198. \param list char* array to be freed
  199. \return
  200. */
  201. void G_free_list(char **list)
  202. {
  203. int i = 0;
  204. if (!list)
  205. return;
  206. while (list[i]) {
  207. G_free(list[i]);
  208. i++;
  209. }
  210. G_free(list);
  211. }