/[eiffelstudio]/FreeELKS/trunk/library/kernel/console.e
ViewVC logotype

Contents of /FreeELKS/trunk/library/kernel/console.e

Parent Directory Parent Directory | Revision Log Revision Log


Revision 91424 - (show annotations)
Tue Oct 26 18:39:32 2004 UTC (15 years, 2 months ago) by manus_eiffel
File size: 11666 byte(s)
Initial revision

1 indexing
2
3 description: "[
4 Commonly used console input and output mechanisms.
5 This class may be used as ancestor by classes needing its facilities.
6 ]"
7
8 status: "See notice at end of class"
9 date: "$Date$"
10 revision: "$Revision$"
11
12 class CONSOLE inherit
13
14 PLAIN_TEXT_FILE
15 rename
16 make_open_read as make_open_stdin,
17 make_open_write as make_open_stdout
18 export
19 {NONE}
20 all
21 {CONSOLE} open_read, close
22 {ANY}
23 separator, append, file_pointer, last_character, last_integer,
24 last_real, last_string, last_double, file_readable,
25 lastchar, lastint, lastreal, laststring, lastdouble,
26 readable, is_closed, extendible, is_open_write
27 redefine
28 make_open_stdin, make_open_stdout, count, is_empty, exists,
29 read_integer, read_real, read_double, read_character,
30 read_line, read_stream, read_word, next_line, put_integer,
31 put_boolean, put_real, put_double, put_string, put_character,
32 put_new_line, new_line, end_of_file, file_close,
33 readint, readreal, readdouble, readchar, readline, readstream,
34 readword, putint, putbool, putreal, putdouble, putstring, putchar,
35 dispose, read_to_string
36 end
37
38 ANY
39
40 create {STD_FILES}
41 make_open_stdin, make_open_stdout, make_open_stderr
42
43 feature -- Initialization
44
45 make_open_stdin (fn: STRING) is
46 -- Create an unix standard input file.
47 do
48 make (fn)
49 file_pointer := console_def (0)
50 set_read_mode
51 end
52
53 make_open_stdout (fn: STRING) is
54 -- Create an unix standard output file.
55 do
56 make (fn)
57 file_pointer := console_def (1)
58 set_write_mode
59 end
60
61 make_open_stderr (fn: STRING) is
62 -- Create an unix standard error file.
63 do
64 make (fn)
65 file_pointer := console_def (2)
66 set_write_mode
67 end
68
69 feature -- Status report
70
71 end_of_file: BOOLEAN is
72 -- Have we reached the end of file?
73 do
74 Result := console_eof (file_pointer)
75 end
76
77 exists: BOOLEAN is
78 -- Does file exist?
79 do
80 Result := True
81 end
82
83 count: INTEGER is 1
84 -- Useless for CONSOLE class.
85 --| `count' is non null not to invalidate invariant clauses.
86
87 feature -- Removal
88
89 dispose is
90 -- This is closed by the operating system at completion.
91 do
92 -- file_close (file_pointer)
93 end
94
95 feature -- Input
96
97 read_integer, readint is
98 -- Read a new integer from standard input.
99 -- Make result available in `last_integer'.
100 do
101 last_integer := console_readint (file_pointer)
102 end
103
104 read_real, readreal is
105 -- Read a new real from standard input.
106 -- Make result available in `last_real'.
107 do
108 last_real := console_readreal (file_pointer)
109 end
110
111 read_double, readdouble is
112 -- Read a new double from standard input.
113 -- Make result available in `last_double'.
114 do
115 last_double := console_readdouble (file_pointer)
116 end
117
118 read_line, readline is
119 -- Read a string until new line or end of file.
120 -- Make result available in `last_string'.
121 -- New line will be consumed but not part of `last_string'.
122 require else
123 is_readable: file_readable
124 local
125 str_cap: INTEGER
126 read: INTEGER -- Amount of bytes already read
127 str_area: ANY
128 done: BOOLEAN
129 do
130 from
131 if last_string = Void then
132 create_last_string (0)
133 end
134 str_area := last_string.area
135 str_cap := last_string.capacity
136 until
137 done
138 loop
139 read := read +
140 console_readline (file_pointer, $str_area, str_cap, read)
141 if read > str_cap then
142 -- End of line not reached yet
143 --|The string must be consistently set before
144 --|resizing.
145 last_string.set_count (str_cap)
146 if str_cap < 2048 then
147 last_string.grow (str_cap + 1024)
148 else
149 -- Increase capacity by `Growth_percentage' as
150 -- defined in RESIZABLE.
151 last_string.automatic_grow
152 end
153 str_area := last_string.area
154 str_cap := last_string.capacity
155 read := read - 1 -- True amount of byte read
156 else
157 last_string.set_count (read)
158 done := True
159 end
160 end
161 -- Ensure fair amount of garbage.
162 if read < 1024 then
163 last_string.resize (read)
164 end
165 end
166
167 read_stream, readstream (nb_char: INTEGER) is
168 -- Read a string of at most `nb_char' bound characters
169 -- from standard input.
170 -- Make result available in `last_string'.
171 local
172 new_count: INTEGER
173 str_area: ANY
174 do
175 if last_string = Void then
176 create_last_string (nb_char)
177 else
178 last_string.grow (nb_char)
179 end
180 str_area := last_string.area
181 new_count := console_readstream (file_pointer, $str_area, nb_char)
182 last_string.set_count (new_count)
183 end
184
185 read_word, readword is
186 -- Read a new word from standard input.
187 -- Make result available in `last_string'.
188 local
189 str_area: ANY
190 str_cap: INTEGER
191 done: BOOLEAN
192 read: INTEGER
193 do
194 from
195 if last_string = Void then
196 create_last_string (0)
197 end
198 str_area := last_string.area
199 str_cap := last_string.capacity
200 until
201 done
202 loop
203 read := read +
204 console_readword (file_pointer, $str_area, str_cap, read)
205 if read > str_cap then
206 -- End of word not reached yet
207 if str_cap < 2048 then
208 last_string.grow (str_cap + 1024)
209 else
210 -- Increase capacity by `Growth_percentage' as
211 -- defined in RESIZABLE.
212 last_string.automatic_grow
213 end
214 str_area := last_string.area
215 str_cap := last_string.capacity
216 read := read - 1 -- True amount of byte read
217 else
218 last_string.set_count (read)
219 done := True
220 end
221 end
222 -- Ensure fair amount of garbage.
223 if read < 1024 then
224 last_string.resize (read)
225 end
226 separator := console_separator (file_pointer) -- Look ahead
227 end
228
229 read_character, readchar is
230 -- Read a new character from standard input.
231 -- Make result available in `last_character'.
232 do
233 last_character := console_readchar (file_pointer)
234 end
235
236
237 next_line is
238 -- Move to next input line on standard input.
239 do
240 console_next_line (file_pointer)
241 end
242
243 feature -- Output
244
245 put_character, putchar (c: CHARACTER) is
246 -- Write `c' at end of default output.
247 do
248 console_pc (file_pointer, c)
249 end
250
251 put_string, putstring (s: STRING) is
252 -- Write `s' at end of default output.
253 local
254 external_s: ANY
255 do
256 if s.count /= 0 then
257 external_s := s.area
258 console_ps (file_pointer, $external_s, s.count)
259 end
260 end
261
262 put_real, putreal (r: REAL) is
263 -- Write `r' at end of default output.
264 do
265 console_pr (file_pointer, r)
266 end
267
268 put_double, putdouble (d: DOUBLE) is
269 -- Write `d' at end of default output.
270 do
271 console_pd (file_pointer, d)
272 end
273
274 put_integer, putint (i: INTEGER) is
275 -- Write `i' at end of default output.
276 do
277 console_pi (file_pointer, i)
278 end
279
280 put_boolean, putbool (b: BOOLEAN) is
281 -- Write `b' at end of default output.
282 do
283 if b then
284 put_string ("True")
285 else
286 put_string ("False")
287 end
288 end
289
290 put_new_line, new_line is
291 -- Write line feed at end of default output.
292 do
293 console_tnwl (file_pointer)
294 end
295
296 feature {NONE} -- Inapplicable
297
298
299 is_empty: BOOLEAN is False
300 -- Useless for CONSOLE class.
301 --| `empty' is false not to invalidate invariant clauses.
302
303 feature {NONE} -- Implementation
304
305 read_to_string (a_string: STRING; pos, nb: INTEGER): INTEGER is
306 -- Fill `a_string', starting at position `pos' with at
307 -- most `nb' characters read from current file.
308 -- Return the number of characters actually read.
309 do
310 Result := file_gss (file_pointer, a_string.area.item_address (pos - 1), nb)
311 end
312
313 console_def (number: INTEGER): POINTER is
314 -- Convert `number' to the corresponding
315 -- file descriptor.
316 external
317 "C | %"eif_console.h%""
318 end
319
320 console_eof (file: POINTER): BOOLEAN is
321 external
322 "C (FILE *): EIF_BOOLEAN | %"eif_console.h%""
323 end
324
325 console_separator (file: POINTER): CHARACTER is
326 -- ASCII code of character following last word read
327 external
328 "C (FILE *): EIF_CHARACTER | %"eif_console.h%""
329 end
330
331 console_ps (file: POINTER; s_name: POINTER; length: INTEGER) is
332 -- Write string `s' at end of `file'
333 external
334 "C (FILE *, char *, EIF_INTEGER) | %"eif_console.h%""
335 end
336
337 console_pr (file: POINTER; r: REAL) is
338 -- Write real `r' at end of `file'
339 external
340 "C (FILE *, EIF_REAL) | %"eif_console.h%""
341 end
342
343 console_pc (file: POINTER; c: CHARACTER) is
344 -- Write character `c' at end of `file'
345 external
346 "C (FILE *, EIF_CHARACTER) | %"eif_console.h%""
347 end
348
349 console_pd (file: POINTER; d: DOUBLE) is
350 -- Write double `d' at end of `file'
351 external
352 "C (FILE *, EIF_DOUBLE) | %"eif_console.h%""
353 end
354
355 console_pi (file: POINTER; i: INTEGER) is
356 -- Write integer `i' at end of `file'
357 external
358 "C (FILE *, EIF_INTEGER) | %"eif_console.h%""
359 end
360
361 console_tnwl (file: POINTER) is
362 -- Write a new_line to `file'
363 external
364 "C (FILE *) | %"eif_console.h%""
365 end
366
367 console_readreal (file: POINTER): REAL is
368 -- Read a real number from the console
369 external
370 "C blocking signature (FILE *): EIF_REAL use %"eif_console.h%""
371 end
372
373 console_readchar (file: POINTER): CHARACTER is
374 -- Read a character from the console
375 external
376 "C blocking signature (FILE *): EIF_CHARACTER use %"eif_console.h%""
377 end
378
379 console_readint (file: POINTER): INTEGER is
380 -- Read an integer from the console
381 external
382 "C blocking signature (FILE *): EIF_INTEGER use %"eif_console.h%""
383 end
384
385 console_readdouble (file: POINTER): DOUBLE is
386 -- Read a double from the console
387 external
388 "C blocking signature (FILE *): EIF_DOUBLE use %"eif_console.h%""
389 end
390
391 console_readword (file: POINTER; a_string: POINTER; length, begin: INTEGER): INTEGER is
392 -- Read a string excluding white space and stripping
393 -- leading white space from `file' into `a_string'.
394 -- White space characters are: blank, new_line,
395 -- tab, vertical tab, formfeed or end of file.
396 -- If it does not fit, result is `length' - `begin' + 1,
397 -- otherwise result is number of characters read.
398 external
399 "C blocking signature (FILE *, char *, EIF_INTEGER, EIF_INTEGER): EIF_INTEGER use %"eif_console.h%""
400 end
401
402 console_readline (file: POINTER; a_string: POINTER; length, begin: INTEGER): INTEGER is
403 -- Read a stream from the console
404 external
405 "C blocking signature (FILE *, char *, EIF_INTEGER, EIF_INTEGER): EIF_INTEGER use %"eif_console.h%""
406 end
407
408 console_next_line (file: POINTER) is
409 -- Move to next input line on standard input.
410 external
411 "C blocking signature (FILE *) use %"eif_console.h%""
412 end
413
414 console_readstream (file: POINTER; a_string: POINTER; length: INTEGER): INTEGER is
415 -- Read a stream from the console
416 external
417 "C blocking signature (FILE *, char *, EIF_INTEGER): EIF_INTEGER use %"eif_console.h%""
418 end
419
420 file_close (file: POINTER) is
421 -- Close `file'
422 external
423 "C (FILE *) | %"eif_console.h%""
424 alias
425 "console_file_close"
426 end
427
428 indexing
429
430 library: "[
431 EiffelBase: Library of reusable components for Eiffel.
432 ]"
433
434 status: "[
435 Copyright 1986-2001 Interactive Software Engineering (ISE).
436 For ISE customers the original versions are an ISE product
437 covered by the ISE Eiffel license and support agreements.
438 ]"
439
440 license: "[
441 EiffelBase may now be used by anyone as FREE SOFTWARE to
442 develop any product, public-domain or commercial, without
443 payment to ISE, under the terms of the ISE Free Eiffel Library
444 License (IFELL) at http://eiffel.com/products/base/license.html.
445 ]"
446
447 source: "[
448 Interactive Software Engineering Inc.
449 ISE Building
450 360 Storke Road, Goleta, CA 93117 USA
451 Telephone 805-685-1006, Fax 805-685-6869
452 Electronic mail <info@eiffel.com>
453 Customer support http://support.eiffel.com
454 ]"
455
456 info: "[
457 For latest info see award-winning pages: http://eiffel.com
458 ]"
459
460 end -- class CONSOLE
461
462

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.23