/src/lib/io/filter/split_output_stream.e

http://github.com/tybor/Liberty · Specman e · 115 lines · 70 code · 15 blank · 30 comment · 1 complexity · e2fd71d5ba290c72cdb3ef6e2aa3fe3f MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class SPLIT_OUTPUT_STREAM
  5. --
  6. -- A stream split into lines. The default line length is 76
  7. --
  8. inherit
  9. FILTER_OUTPUT_STREAM
  10. redefine
  11. connect_to
  12. end
  13. create {ANY}
  14. connect_to
  15. feature {ANY} -- creation
  16. Default_line_length: INTEGER 76
  17. -- This length is the default for Base64 (see http://www.faqs.org/rfcs/rfc2045)
  18. Default_line_separator: STRING "%R%N"
  19. line_length: INTEGER
  20. -- Length of a line
  21. line_separator: STRING
  22. -- What to put at the end of a line (e.g. Quoted-Printable would set "=%R%N" here)
  23. connect_to (a_stream: like stream)
  24. do
  25. Precursor(a_stream)
  26. set_line_length(Default_line_length)
  27. set_line_separator(Default_line_separator)
  28. end
  29. set_line_length (a_length: like line_length)
  30. require
  31. a_length > 0
  32. do
  33. line_length := a_length
  34. ensure
  35. line_length = a_length
  36. end
  37. set_line_separator (a_separator: like line_separator)
  38. require
  39. a_separator.count > 0
  40. do
  41. line_separator := a_separator
  42. ensure
  43. line_separator = a_separator
  44. end
  45. feature {FILTER_OUTPUT_STREAM}
  46. filtered_put_character (c: CHARACTER)
  47. local
  48. i: INTEGER
  49. do
  50. stream.filtered_put_character(c)
  51. inspect
  52. c
  53. when '%R', '%N' then
  54. current_line_length := 0
  55. else
  56. current_line_length := current_line_length + 1
  57. if current_line_length >= line_length then
  58. from
  59. i := line_separator.lower
  60. until
  61. i > line_separator.upper
  62. loop
  63. stream.filtered_put_character(line_separator.item(i))
  64. i := i + 1
  65. end
  66. current_line_length := 0
  67. end
  68. end
  69. end
  70. filtered_flush
  71. do
  72. stream.filtered_flush
  73. end
  74. feature {}
  75. current_line_length: INTEGER
  76. -- The length of the current line
  77. local_can_disconnect: BOOLEAN True
  78. invariant
  79. line_length > 0
  80. end -- SPLIT_OUTPUT_STREAM
  81. --
  82. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  83. --
  84. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  85. -- of this software and associated documentation files (the "Software"), to deal
  86. -- in the Software without restriction, including without limitation the rights
  87. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  88. -- copies of the Software, and to permit persons to whom the Software is
  89. -- furnished to do so, subject to the following conditions:
  90. --
  91. -- The above copyright notice and this permission notice shall be included in
  92. -- all copies or substantial portions of the Software.
  93. --
  94. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  95. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  96. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  97. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  98. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  99. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  100. -- THE SOFTWARE.