list_gp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*!
  2. \file list_gp.c
  3. \brief Imagery Library - List group
  4. (C) 2001-2008 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 Prints maps in a group (fancy version)
  14. *
  15. * \param group group name
  16. * \param ref group reference (set with I_get_group_ref())
  17. * \param fd where to print (typically stdout)
  18. * \return 0
  19. */
  20. int I_list_group(const char *group, const struct Ref *ref, FILE * fd)
  21. {
  22. char buf[80];
  23. int i;
  24. int len, tot_len;
  25. int max;
  26. if (ref->nfiles <= 0) {
  27. fprintf(fd, _("group <%s> is empty\n"), group);
  28. return 0;
  29. }
  30. max = 0;
  31. for (i = 0; i < ref->nfiles; i++) {
  32. I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
  33. len = strlen(buf) + 4;
  34. if (len > max)
  35. max = len;
  36. }
  37. fprintf(fd, _("group <%s> references the following raster maps\n"),
  38. group);
  39. fprintf(fd, "-------------\n");
  40. tot_len = 0;
  41. for (i = 0; i < ref->nfiles; i++) {
  42. I__list_group_name_fit(buf, ref->file[i].name, ref->file[i].mapset);
  43. tot_len += max;
  44. if (tot_len > 78) {
  45. fprintf(fd, "\n");
  46. tot_len = max;
  47. }
  48. fprintf(fd, "%-*s", max, buf);
  49. }
  50. if (tot_len)
  51. fprintf(fd, "\n");
  52. fprintf(fd, "-------------\n");
  53. return 0;
  54. }
  55. /*!
  56. * \brief Prints maps in a group (simple version)
  57. *
  58. * Same as I_list_group(), but without all the fancy stuff.
  59. * Prints one map per line in map@mapset form.
  60. *
  61. * \param ref group reference (set with I_get_group_ref())
  62. * \param fd where to print (typically stdout)
  63. * \return 0
  64. */
  65. int I_list_group_simple(const struct Ref *ref, FILE * fd)
  66. {
  67. int i;
  68. if (ref->nfiles <= 0)
  69. return 0;
  70. for (i = 0; i < ref->nfiles; i++)
  71. fprintf(fd, "%s@%s\n", ref->file[i].name, ref->file[i].mapset);
  72. return 0;
  73. }
  74. /*!
  75. * \brief Formats map name to fit in a 80 column layout
  76. *
  77. * Results in a map name in the "<map@mapset>" form.
  78. * If necessary truncates relevant part(s) and denotes
  79. * with ellipsis, e.g. "<verylongmapname...@mapset>".
  80. *
  81. * \param[out] buf formatted map name
  82. * \param name map name
  83. * \param mapset mapset name
  84. */
  85. void I__list_group_name_fit(char *buf, const char *name, const char *mapset)
  86. {
  87. char *frmt;
  88. char fr[32];
  89. int name_length = (int)strlen(name);
  90. int mapset_length = (int)strlen(mapset);
  91. if (name_length + mapset_length + 3 < 75) {
  92. frmt = "<%s@%s>";
  93. }
  94. else if (name_length > 35 && mapset_length > 35) {
  95. frmt = "<%.33s...@%.32s...>";
  96. }
  97. else if (name_length > 35) {
  98. sprintf(fr, "<%%.%ds...@%%s>", 68 - mapset_length);
  99. frmt = fr;
  100. }
  101. else {
  102. sprintf(fr, "<%%s@%%.%ds...>", 68 - name_length);
  103. frmt = fr;
  104. }
  105. snprintf(buf, 75, frmt, name, mapset);
  106. }