/src/lib/io/basic/std_output.e

http://github.com/tybor/Liberty · Specman e · 176 lines · 112 code · 23 blank · 41 comment · 6 complexity · 54ba342090cd483edb3b8acbcf7d0846 MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class STD_OUTPUT
  5. --
  6. -- To use the standard output file. As for UNIX, the default standard output is the screen.
  7. --
  8. -- Note: only one instance of this class should be necessary. Access it through ANY.std_output.
  9. --
  10. -- See also STANDARD_STREAMS, STD_INPUT_OUTPUT
  11. --
  12. inherit
  13. TERMINAL_OUTPUT_STREAM
  14. redefine put_natively_stored_string, dispose
  15. end
  16. insert
  17. REDIRECTION_TOOLS
  18. rename
  19. restore_default as restore_default_output
  20. redefine
  21. restore_default_output
  22. end
  23. create {ANY}
  24. make
  25. feature {ANY}
  26. is_connected: BOOLEAN True
  27. disconnect
  28. do
  29. filter := Void
  30. end
  31. feature {}
  32. make
  33. do
  34. buffer := buffer.calloc(4096)
  35. capacity := 4096
  36. end
  37. feature {FILTER_OUTPUT_STREAM}
  38. filtered_put_character (c: CHARACTER)
  39. do
  40. if buffer_position >= 4096 then
  41. write_buffer
  42. end
  43. buffer.put(c, buffer_position)
  44. buffer_position := buffer_position + 1
  45. if c = '%N' then
  46. write_buffer
  47. end
  48. end
  49. filtered_flush
  50. do
  51. if buffer_position > 0 then
  52. write_buffer
  53. end
  54. io_flush(stdout)
  55. end
  56. feature {ABSTRACT_STRING}
  57. put_natively_stored_string (s: NATIVELY_STORED_STRING)
  58. local
  59. unused_result: INTEGER
  60. fs: FIXED_STRING
  61. do
  62. write_buffer
  63. if fs ?:= s then
  64. fs ::= s
  65. --|*** TODO: replace by an io_fwrite_slice
  66. if fs.is_shared then
  67. fs.unshare
  68. end
  69. end
  70. unused_result := io_fwrite(s.storage, s.count, stdout)
  71. end
  72. feature {FILTER}
  73. filtered_descriptor: INTEGER
  74. do
  75. Result := sequencer_descriptor(stdout)
  76. end
  77. filtered_has_descriptor: BOOLEAN True
  78. filtered_stream_pointer: POINTER
  79. do
  80. Result := stdout
  81. end
  82. filtered_has_stream_pointer: BOOLEAN True
  83. feature {STREAM_HANDLER}
  84. redirect_to (file_name: STRING)
  85. -- Redirect standard output to `file_name' instead of the default standard output. If `file_name'
  86. -- does not exist, it is created. If it exists, its previous content is erased.
  87. --
  88. -- See also `redirection_succeeded'
  89. do
  90. redirect(open_descriptor_for_create(file_name.to_external))
  91. end
  92. redirect_append_to (file_name: STRING)
  93. -- Redirect standard output to `file_name' instead of the default standard output. If `file_name'
  94. -- does not exist, it is created. If it exists, the new output is appended to it.
  95. --
  96. -- See also `redirection_succeeded'
  97. do
  98. redirect(open_descriptor_for_append(file_name.to_external))
  99. end
  100. restore_default_output
  101. -- Restore standard output to go to the default standard output.
  102. do
  103. Precursor
  104. end
  105. feature {}
  106. buffer_position: INTEGER
  107. buffer: NATIVE_ARRAY[CHARACTER]
  108. capacity: INTEGER
  109. write_buffer
  110. local
  111. unused_result: INTEGER
  112. do
  113. if buffer_position > 0 then
  114. unused_result := io_fwrite(buffer, buffer_position, stdout)
  115. buffer_position := 0
  116. end
  117. end
  118. stdout: POINTER
  119. external "plug_in"
  120. alias "{
  121. location: "${sys}/plugins"
  122. module_name: "io"
  123. feature_name: "stdout"
  124. }"
  125. end
  126. dispose
  127. do
  128. check
  129. std_output = Current
  130. end
  131. -- Nothing to dispose for `std_output'.
  132. end
  133. end -- class STD_OUTPUT
  134. --
  135. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  136. --
  137. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  138. -- of this software and associated documentation files (the "Software"), to deal
  139. -- in the Software without restriction, including without limitation the rights
  140. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  141. -- copies of the Software, and to permit persons to whom the Software is
  142. -- furnished to do so, subject to the following conditions:
  143. --
  144. -- The above copyright notice and this permission notice shall be included in
  145. -- all copies or substantial portions of the Software.
  146. --
  147. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  148. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  149. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  150. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  151. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  152. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  153. -- THE SOFTWARE.