PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/src/lib/io/low_level/output_stream_tools.e

http://github.com/tybor/Liberty
Specman e | 395 lines | 304 code | 37 blank | 54 comment | 0 complexity | 95e34d2923bdd030d7f5a15d71d45f51 MD5 | raw file
  1-- This file is part of a Liberty Eiffel library.
  2-- See the full copyright at the end.
  3--
  4deferred class OUTPUT_STREAM_TOOLS
  5   --
  6   -- OUTPUT_STREAM implementation. Only OUTPUT_STREAM can inherit from this class.
  7   --
  8
  9insert
 10   STRING_HANDLER
 11
 12feature {ANY}
 13   put_character (c: CHARACTER)
 14      require
 15         is_connected
 16         not is_filtered and then can_put_character(c)
 17      deferred
 18      end
 19
 20   flush
 21         -- Flushes the pipe. If `is_filtered', calls the filter's
 22         -- `flush' instead.
 23      require
 24         is_connected
 25      deferred
 26      end
 27
 28   can_put_character (c: CHARACTER): BOOLEAN
 29      deferred
 30      end
 31
 32   is_filtered: BOOLEAN
 33      deferred
 34      end
 35
 36   is_connected: BOOLEAN
 37      deferred
 38      end
 39
 40feature {ABSTRACT_STRING}
 41   put_natively_stored_string (s: NATIVELY_STORED_STRING)
 42      require
 43         s /= Void
 44      do
 45         put_abstract_string(s)
 46      end
 47
 48   put_abstract_string (s: ABSTRACT_STRING)
 49      require
 50         s /= Void
 51      local
 52         i, count: INTEGER
 53      do
 54         from
 55            i := 1
 56            count := s.count
 57         until
 58            i > count
 59         loop
 60            put_character(s.item(i))
 61            i := i + 1
 62         end
 63      end
 64
 65feature {ANY}
 66   put_string (s: ABSTRACT_STRING)
 67         -- Output `s' to current output device.
 68      require
 69         is_connected
 70         not is_filtered
 71         s /= Void
 72      do
 73         s.print_on(as_output_stream)
 74      end
 75
 76   put_unicode_string (unicode_string: UNICODE_STRING)
 77         -- Output the UTF-8 encoding of the `unicode_string'.
 78      require
 79         is_connected
 80         not is_filtered
 81         unicode_string /= Void
 82      do
 83         tmp_string.clear_count
 84         unicode_string.utf8_encode_in(tmp_string)
 85         put_string(tmp_string)
 86      end
 87
 88   put_line (s: ABSTRACT_STRING)
 89         -- Output the string followed by a '%N'.
 90      do
 91         put_string(s)
 92         put_new_line
 93      end
 94
 95feature {ANY} -- To write a number:
 96   frozen put_integer (i: INTEGER_64)
 97         -- Output `i' to current output device.
 98      require
 99         is_connected
100         not is_filtered
101      do
102         tmp_string.clear_count
103         i.append_in(tmp_string)
104         put_string(tmp_string)
105      end
106
107   frozen put_integer_format (i: INTEGER_64; s: INTEGER)
108         -- Output `i' to current output device using at most `s' character.
109      require
110         is_connected
111         not is_filtered
112      do
113         tmp_string.clear_count
114         i.append_in_format(tmp_string, s)
115         put_string(tmp_string)
116      end
117
118   frozen put_natural_8 (n: NATURAL_8)
119         -- Output `n' to current output device.
120      require
121         is_connected
122         not is_filtered
123      do
124         tmp_string.clear_count
125         n.append_in(tmp_string)
126         put_string(tmp_string)
127      end
128
129   frozen put_natural_8_format (n: NATURAL_8; s: INTEGER)
130         -- Output `n' to current output device using at most `s' character.
131      require
132         is_connected
133         not is_filtered
134      do
135         tmp_string.clear_count
136         n.append_in_format(tmp_string, s)
137         put_string(tmp_string)
138      end
139
140   frozen put_natural_16 (n: NATURAL_16)
141         -- Output `n' to current output device.
142      require
143         is_connected
144         not is_filtered
145      do
146         tmp_string.clear_count
147         n.append_in(tmp_string)
148         put_string(tmp_string)
149      end
150
151   frozen put_natural_16_format (n: NATURAL_16; s: INTEGER)
152         -- Output `n' to current output device using at most `s' character.
153      require
154         is_connected
155         not is_filtered
156      do
157         tmp_string.clear_count
158         n.append_in_format(tmp_string, s)
159         put_string(tmp_string)
160      end
161
162   frozen put_natural_32 (n: NATURAL_32)
163         -- Output `n' to current output device.
164      require
165         is_connected
166         not is_filtered
167      do
168         tmp_string.clear_count
169         n.append_in(tmp_string)
170         put_string(tmp_string)
171      end
172
173   frozen put_natural_32_format (n: NATURAL_32; s: INTEGER)
174         -- Output `n' to current output device using at most `s' character.
175      require
176         is_connected
177         not is_filtered
178      do
179         tmp_string.clear_count
180         n.append_in_format(tmp_string, s)
181         put_string(tmp_string)
182      end
183
184   frozen put_natural_64 (n: NATURAL_64)
185         -- Output `n' to current output device.
186      require
187         is_connected
188         not is_filtered
189      do
190         tmp_string.clear_count
191         n.append_in(tmp_string)
192         put_string(tmp_string)
193      end
194
195   frozen put_natural_64_format (n: NATURAL_64; s: INTEGER)
196         -- Output `n' to current output device using at most `s' character.
197      require
198         is_connected
199         not is_filtered
200      do
201         tmp_string.clear_count
202         n.append_in_format(tmp_string, s)
203         put_string(tmp_string)
204      end
205
206   put_real (r: REAL)
207         -- Output `r' to current output device.
208      require
209         is_connected
210         not is_filtered
211      do
212         tmp_string.clear_count
213         r.append_in(tmp_string)
214         put_string(tmp_string)
215      end
216
217   put_real_format (r: REAL; f: INTEGER)
218         -- Output `r' with only `f' digit for the fractional part.
219         -- Examples:
220         --    put_real(3.519,2) print "3.51".
221      require
222         is_connected
223         not is_filtered
224         f >= 0
225      do
226         tmp_string.clear_count
227         r.append_in_format(tmp_string, f)
228         put_string(tmp_string)
229      end
230
231   put_real_scientific (r: REAL; f: INTEGER)
232         -- Output `r' using the scientific notation with only `f' digit for the fractional part.
233         -- Examples:
234         --    put_real_scientific(3.519,2) print "3.16e+00".
235      require
236         is_connected
237         not is_filtered
238         f >= 0
239      do
240         tmp_string.clear_count
241         r.append_in_scientific(tmp_string, f)
242         put_string(tmp_string)
243      end
244
245   put_number (number: NUMBER)
246         -- Output the `number'.
247      require
248         is_connected
249         not is_filtered
250         number /= Void
251      do
252         tmp_string.clear_count
253         number.append_in(tmp_string)
254         put_string(tmp_string)
255      end
256
257feature {ANY} -- Other features:
258   put_boolean (b: BOOLEAN)
259         -- Output `b' to current output device according
260         -- to the Eiffel format.
261      require
262         is_connected
263         not is_filtered
264      do
265         tmp_string.clear_count
266         b.append_in(tmp_string)
267         put_string(tmp_string)
268      end
269
270   put_pointer (p: POINTER)
271         -- Output a viewable version of `p'.
272      require
273         is_connected
274         not is_filtered
275      do
276         tmp_string.clear_count
277         p.append_in(tmp_string)
278         put_string(tmp_string)
279      end
280
281   put_new_line
282         -- Output a newline character.
283      require
284         is_connected
285         not is_filtered
286      do
287         put_character('%N')
288      end
289
290   put_spaces (nb: INTEGER)
291         -- Output `nb' spaces character.
292      require
293         is_connected
294         not is_filtered
295         nb >= 0
296      local
297         count: INTEGER
298      do
299         from
300         until
301            count >= nb
302         loop
303            put_character(' ')
304            count := count + 1
305         end
306      end
307
308   append_file (file_name: STRING)
309      require
310         is_connected
311         not is_filtered
312         ;(create {FILE_TOOLS}).is_readable(file_name)
313      local
314         c: CHARACTER
315      do
316         tmp_file_read.connect_to(file_name)
317         from
318            tmp_file_read.read_character
319         until
320            tmp_file_read.end_of_input
321         loop
322            c := tmp_file_read.last_character
323            put_character(c)
324            tmp_file_read.read_character
325         end
326         tmp_file_read.disconnect
327      end
328
329feature {}
330   as_output_stream: OUTPUT_STREAM
331      deferred
332      ensure
333         yes_indeed_it_is_the_same_object: Result.to_pointer = to_pointer
334      end
335
336feature {}
337   tmp_file_read: TEXT_FILE_READ
338      once
339         create Result.make
340      end
341
342   tmp_string: STRING
343      once
344         create Result.make(512)
345      end
346
347feature {}
348   io_putc (byte: CHARACTER; stream: POINTER)
349      external "plug_in"
350      alias "{
351         location: "${sys}/plugins"
352         module_name: "io"
353         feature_name: "io_putc"
354         }"
355      end
356
357   io_fwrite (buf: NATIVE_ARRAY[CHARACTER]; size: INTEGER; stream: POINTER): INTEGER
358      external "plug_in"
359      alias "{
360         location: "${sys}/plugins"
361         module_name: "io"
362         feature_name: "io_fwrite"
363         }"
364      end
365
366   io_flush (stream: POINTER)
367      external "plug_in"
368      alias "{
369         location: "${sys}/plugins"
370         module_name: "io"
371         feature_name: "io_flush"
372         }"
373      end
374
375end -- class OUTPUT_STREAM_TOOLS
376--
377-- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
378--
379-- Permission is hereby granted, free of charge, to any person obtaining a copy
380-- of this software and associated documentation files (the "Software"), to deal
381-- in the Software without restriction, including without limitation the rights
382-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
383-- copies of the Software, and to permit persons to whom the Software is
384-- furnished to do so, subject to the following conditions:
385--
386-- The above copyright notice and this permission notice shall be included in
387-- all copies or substantial portions of the Software.
388--
389-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
390-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
391-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
392-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
393-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
394-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
395-- THE SOFTWARE.