list_subgp.c 3.5 KB

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