home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROCS.LZH / WRAP.ICN < prev    next >
Text File  |  1991-07-13  |  3KB  |  102 lines

  1. ############################################################################
  2. #
  3. #    Name:    wrap.icn
  4. #
  5. #    Title:    Wrap lines of output for use with write()
  6. #
  7. #    Author:    Robert J. Alexander
  8. #
  9. #    Date:    December 5, 1989
  10. #
  11. ############################################################################
  12. #
  13. #  wrap(s,i) -- Facilitates accumulation of small strings into longer
  14. #       output strings, outputting when the accumulated string would
  15. #       exceed a specified length (e.g. outputting items in multiple
  16. #       columns).
  17. #
  18. #       s -- string to accumulate
  19. #       i -- width of desired output string
  20. #
  21. #  Wrap fails if the string s did not necessitate output of the buffered
  22. #  output string; otherwise the output string is returned (which never
  23. #  includes s).
  24. #
  25. #  s defaults to the empty string (""), causing nothing to be
  26. #  accumulated; i defaults to 0, forcing output of any buffered string.
  27. #  Note that calling wrap() with no arguments produces the buffer (if it
  28. #  is not empty) and clears it.
  29. #
  30. #  Wrap does no output to files.
  31. #
  32. #
  33. #  Here's how wrap is normally used:
  34. #
  35. #       wrap()                  # Initialize (not really necessary unless
  36. #                               # a previous use might have left stuff in
  37. #                               # the buffer).
  38. #
  39. #       every i := 1 to 100 do  # Loop to process strings to output --
  40. #         write(wrap(x[i],80))  # only writes when 80-char line filled.
  41. #
  42. #       write(wrap())           # Output what's in buffer -- only outputs
  43. #                               # if something to write.
  44. #
  45. #
  46. #  wraps(s,i) -- Facilitates managing output of numerous small strings
  47. #       so that they do not exceed a reasonable line length (e.g.
  48. #       outputting items in multiple columns).
  49. #
  50. #       s -- string to accumulate
  51. #       i -- maximum width of desired output string
  52. #
  53. #  If the string "s" did not necessitate a line-wrap, the string "s" is
  54. #  returned.  If a line-wrap is needed, "s", preceded by a new-line
  55. #  character ("\n"), is returned.
  56. #
  57. #  "s" defaults to the empty string (""), causing nothing to be
  58. #  accumulated; i defaults to 0, forcing a new line if anything had been
  59. #  output on the current line.  Thus calling wraps() with no arguments
  60. #  reinitializes it.
  61. #
  62. #  Wraps does no output to files.
  63. #
  64. #
  65. #  Here's how wraps is normally used:
  66. #
  67. #       wraps()                 # Initialize (not really necessary unless
  68. #                               # a previous use might have left it in an
  69. #                               # unknown condition).
  70. #
  71. #       every i := 1 to 100 do  # Loop to process strings to output --
  72. #         writes(wraps(x[i],80))# only wraps when 80-char line filled.
  73. #
  74. #       writes(wraps())         # Only outputs "\n" if something written
  75. #                               # on last line.
  76. #
  77. ############################################################################
  78.  
  79. procedure wrap(s,i)
  80.    local t
  81.    static line
  82.    initial line := ""
  83.    /s := "" ; /i := 0
  84.    if *(t := line || s) > i then
  85.      return "" ~== (s :=: line)
  86.    line := t
  87. end
  88.  
  89. procedure wraps(s,i)
  90.    local t
  91.    static size
  92.    initial size := 0
  93.    /s := "" ; /i := 0
  94.    t := size + *s
  95.    if t > i & size > 0 then {
  96.       size := *s
  97.       return "\n" || s
  98.       }
  99.    size := t
  100.    return s
  101. end
  102.