label.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. char *print_label(char *s, int len, int pflag, int spacing, int dot)
  6. {
  7. char *x;
  8. int n;
  9. int i;
  10. if (len <= 0) {
  11. G_warning(_("Page width is too small"));
  12. return NULL;
  13. }
  14. /* strip away leading spaces */
  15. while (*s == ' ')
  16. s++;
  17. /* if it all fits, then just print it, and add spaces to pad to len */
  18. n = strlen(s);
  19. if (n <= len) {
  20. if (pflag) {
  21. i = 0;
  22. while (*s) {
  23. putchar(*s++);
  24. i++;
  25. }
  26. while (n++ < len)
  27. putchar(spacing && ++i % spacing == 0 ? dot : ' ');
  28. }
  29. return NULL;
  30. }
  31. /* back up from len chars to first space */
  32. for (x = s + len; x != s; x--)
  33. if (*x == ' ')
  34. break;
  35. /* special case. If entire string has no spaces, just print len chars */
  36. if (*x != ' ')
  37. x = s + len;
  38. else {
  39. /* back up to first space */
  40. while (*x == ' ')
  41. x--;
  42. x++;
  43. }
  44. i = 0;
  45. while (s != x) {
  46. if (pflag)
  47. putchar(*s);
  48. i++;
  49. s++;
  50. len--;
  51. }
  52. if (pflag) {
  53. while (len-- > 0)
  54. putchar(spacing && ++i % spacing == 0 ? dot : ' ');
  55. }
  56. return s;
  57. }