header.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <string.h>
  2. #include <grass/raster.h>
  3. #include "global.h"
  4. static int page = 0;
  5. static const char *date = NULL;
  6. static int max(int, int);
  7. static int pbuf(char *buf)
  8. {
  9. fprintf(stdout, "|%-*.*s|", page_width - 2, page_width - 2, buf);
  10. newline();
  11. return 0;
  12. }
  13. int header(int unit1, int unit2)
  14. {
  15. int i, k;
  16. char buf[1024];
  17. char tbuf1[256], tbuf2[256];
  18. char north[50], south[50];
  19. char east[50], west[50];
  20. char ns_res[50], ew_res[50];
  21. int len1, len2;
  22. char *label;
  23. char *mask;
  24. nlines = page_length;
  25. if (date == NULL)
  26. date = G_date();
  27. divider("+");
  28. page++;
  29. if (page == 1 && with_headers) {
  30. lcr("", "RASTER MAP CATEGORY REPORT", "", buf, page_width - 2);
  31. pbuf(buf);
  32. sprintf(tbuf1, "LOCATION: %s", G_location());
  33. if (with_headers && (page_length > 0))
  34. sprintf(tbuf2, "Page %d", page);
  35. else
  36. *tbuf2 = 0;
  37. lcr(tbuf1, tbuf2, date, buf, page_width - 2);
  38. pbuf(buf);
  39. divider("|");
  40. G_format_northing(window.north, north, window.proj);
  41. G_format_easting(window.east, east, window.proj);
  42. G_format_northing(window.south, south, window.proj);
  43. G_format_easting(window.west, west, window.proj);
  44. G_format_resolution(window.ns_res, ns_res, window.proj);
  45. G_format_resolution(window.ew_res, ew_res, window.proj);
  46. len1 = max(strlen(north), strlen(south));
  47. len1 = max(len1, strlen(ns_res));
  48. len2 = max(strlen(east), strlen(west));
  49. len2 = max(len2, strlen(ew_res));
  50. sprintf(buf, "%-9s north: %*s east: %*s",
  51. "", len1, north, len2, east);
  52. pbuf(buf);
  53. sprintf(buf, "%-9s south: %*s west: %*s",
  54. "REGION", len1, south, len2, west);
  55. pbuf(buf);
  56. sprintf(buf, "%-9s res: %*s res: %*s",
  57. "", len1, ns_res, len2, ew_res);
  58. pbuf(buf);
  59. divider("|");
  60. mask = maskinfo();
  61. label = "MASK:";
  62. len1 = strlen(label) + 1;
  63. while (mask) {
  64. fprintf(stdout, "|%-*s", len1, label);
  65. label = "";
  66. mask = print_label(mask, page_width - len1 - 2, 1, 0, ' ');
  67. fprintf(stdout, "|");
  68. newline();
  69. }
  70. divider("|");
  71. /*if title is available, print it, otherwise, print (untitled). in
  72. either case, print later name immediately following
  73. */
  74. label = nlayers > 1 ? "MAPS:" : "MAP:";
  75. len1 = strlen(label) + 1;
  76. for (i = 0; i < nlayers; i++) {
  77. char *title;
  78. title = Rast_get_cats_title(&(layers[i].labels));
  79. if (title)
  80. G_strip(title);
  81. if (title == NULL || *title == 0)
  82. title = "(untitled)";
  83. sprintf(buf, "%-*s%*s%s (%s in %s)", len1, label,
  84. i * 2, "", title, layers[i].name, layers[i].mapset);
  85. pbuf(buf);
  86. label = "";
  87. }
  88. divider("|");
  89. }
  90. len1 = layers[0].clen + layers[0].nlen;
  91. for (k = 0; k < 2; k++) {
  92. if (k == 0)
  93. lcr("", "Category Information", "", buf, len1);
  94. else {
  95. sprintf(tbuf1, "%*s|description", layers[0].nlen, "#");
  96. lcr(tbuf1, "", "", buf, len1);
  97. }
  98. fprintf(stdout, "|%s ", buf);
  99. for (i = unit1; i <= unit2; i++)
  100. fprintf(stdout, "|%*s", unit[i].len, unit[i].label[k]);
  101. fprintf(stdout, "|");
  102. newline();
  103. }
  104. divider("|");
  105. return 0;
  106. }
  107. int divider(char *edge)
  108. {
  109. int n;
  110. fprintf(stdout, "%s", edge);
  111. n = page_width - 2;
  112. while (--n >= 0)
  113. fprintf(stdout, "-");
  114. fprintf(stdout, "%s", edge);
  115. newline();
  116. return 0;
  117. }
  118. static int max(int a, int b)
  119. {
  120. return a > b ? a : b;
  121. }
  122. int trailer(void)
  123. {
  124. divider("+");
  125. while (nlines > 0)
  126. newline();
  127. if (use_formfeed)
  128. fprintf(stdout, "\f");
  129. return 0;
  130. }
  131. int newline(void)
  132. {
  133. fprintf(stdout, "\n");
  134. nlines--;
  135. return 0;
  136. }
  137. int lcr(const char *left, const char *center, const char *right, char *buf, int n)
  138. {
  139. int ll, lc, lr;
  140. int sc, sr;
  141. ll = strlen(left);
  142. lc = strlen(center);
  143. lr = strlen(right);
  144. sc = (n - lc) / 2 - ll;
  145. sr = n - lr - lc - (n - lc) / 2;
  146. sprintf(buf, "%s%*s%s%*s%s",
  147. left,
  148. sc > 0 ? sc : 0, "", center,
  149. sc > 0 ? sr : sr + sc , "", right);
  150. return 0;
  151. }