list_subgp.c 3.3 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(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) == 0 || !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, int *subgs_num)
  51. {
  52. return list_subgroups(group, mapset, subgs_num);
  53. }
  54. /*!
  55. * \brief Prints maps in a subgroup (fancy version)
  56. *
  57. * \param group group name
  58. * \param subgroup subgroup name
  59. * \param ref group reference (set with I_get_subgroup_ref())
  60. * \param fd where to print (typically stdout)
  61. * \return 0
  62. */
  63. int I_list_subgroup(const char *group,
  64. const char *subgroup, const struct Ref *ref, FILE * fd)
  65. {
  66. char buf[80];
  67. int i;
  68. int len, tot_len;
  69. int max;
  70. if (ref->nfiles <= 0) {
  71. fprintf(fd, _("subgroup <%s> of group <%s> is empty\n"),
  72. subgroup, group);
  73. return 0;
  74. }
  75. max = 0;
  76. for (i = 0; i < ref->nfiles; i++) {
  77. sprintf(buf, "<%s@%s>", ref->file[i].name, ref->file[i].mapset);
  78. len = strlen(buf) + 4;
  79. if (len > max)
  80. max = len;
  81. }
  82. fprintf(fd,
  83. _
  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. sprintf(buf, "<%s@%s>", 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. }