PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

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