list_subgp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*!
  2. \file list_subgp.c
  3. \brief Imagery Library - List subgroup
  4. (C) 2001-2008,2013 by 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. \author USA CERL
  8. */
  9. #include <string.h>
  10. #include <grass/imagery.h>
  11. #include <grass/glocale.h>
  12. /*!
  13. * \brief Get list of subgroups which a group contatins.
  14. *
  15. * \param group group name
  16. * \param[out] subgs_num number of subgroups which the group contains
  17. * \return array of subgroup names
  18. */
  19. char **I_list_subgroups(const char *group, int *subgs_num)
  20. {
  21. /* Unlike I_list_subgroup and I_list_subgroup_simple this function
  22. returns array of subgroup names, it does not use fprintf.
  23. This approach should make the function usable in more cases. */
  24. char **subgs;
  25. char path[GPATH_MAX];
  26. char buf[GPATH_MAX];
  27. const char *mapset;
  28. struct stat sb;
  29. *subgs_num = 0;
  30. if (I_find_group(group) == 0)
  31. return NULL;
  32. mapset = G_mapset();
  33. sprintf(buf, "group/%s/subgroup", group);
  34. G_file_name(path, buf, "", mapset);
  35. if (!G_lstat(path, &sb) == 0 || !S_ISDIR(sb.st_mode))
  36. return NULL;
  37. subgs = G__ls(path, subgs_num);
  38. return subgs;
  39. }
  40. /*!
  41. * \brief Prints maps in a subgroup (fancy version)
  42. *
  43. * \param group group name
  44. * \param subgroup subgroup name
  45. * \param ref group reference (set with I_get_subgroup_ref())
  46. * \param fd where to print (typically stdout)
  47. * \return 0
  48. */
  49. int I_list_subgroup(const char *group,
  50. const char *subgroup, const struct Ref *ref, FILE * fd)
  51. {
  52. char buf[80];
  53. int i;
  54. int len, tot_len;
  55. int max;
  56. if (ref->nfiles <= 0) {
  57. fprintf(fd, _("subgroup <%s> of group <%s> is empty\n"),
  58. subgroup, group);
  59. return 0;
  60. }
  61. max = 0;
  62. for (i = 0; i < ref->nfiles; i++) {
  63. sprintf(buf, "<%s@%s>", ref->file[i].name, ref->file[i].mapset);
  64. len = strlen(buf) + 4;
  65. if (len > max)
  66. max = len;
  67. }
  68. fprintf(fd,
  69. _
  70. ("subgroup <%s> of group <%s> references the following raster maps\n"),
  71. subgroup, group);
  72. fprintf(fd, "-------------\n");
  73. tot_len = 0;
  74. for (i = 0; i < ref->nfiles; i++) {
  75. sprintf(buf, "<%s@%s>", ref->file[i].name, ref->file[i].mapset);
  76. tot_len += max;
  77. if (tot_len > 78) {
  78. fprintf(fd, "\n");
  79. tot_len = max;
  80. }
  81. fprintf(fd, "%-*s", max, buf);
  82. }
  83. if (tot_len)
  84. fprintf(fd, "\n");
  85. fprintf(fd, "-------------\n");
  86. return 0;
  87. }
  88. /*!
  89. * \brief Prints maps in a subgroup (simple version)
  90. *
  91. * Same as I_list_subgroup(), but without all the fancy stuff.
  92. * Prints one map per line in map@mapset form.
  93. *
  94. * \param ref group reference (set with I_get_subgroup_ref())
  95. * \param fd where to print (typically stdout)
  96. * \return 0
  97. */
  98. /* same as above, but one map per line in map@mapset form */
  99. int I_list_subgroup_simple(const struct Ref *ref, FILE * fd)
  100. {
  101. return I_list_group_simple(ref, fd);
  102. }