source: to-imperative/trunk/library/StdIO/refal/StdIO.rf @ 2027

Last change on this file since 2027 was 2027, checked in by orlov, 15 years ago
  • Stream.Get-Line and StdIO.Read-Line! consider a line to be terminated

by any one of a line feed ('\n'), a carriage return ('\r'),
or a carriage return followed immediately by a linefeed.

  • New function Stream.Scanc.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.1 KB
Line 
1// $Id: StdIO.rf 2027 2006-07-17 17:40:42Z orlov $
2
3$use Arithm Box Class Compare Convert Error Table;
4
5//X/$use Lexer(Stream FakeStreamIndex);
6$use "../../Lexer/refal/Lexer";
7$use "../../Stream/refal/Stream";
8$use "../../Stream/refal/StreamErr";
9
10
11$table Buffers;
12/*
13 * For the moment the format is:
14 *   s.channel  -->  stream
15 */
16
17
18Open-File s.channel e.file s.mode, \{
19  # <Channel? s.channel> =
20    <Lib-Error (StdIO Open-File) "Arg 1 is not a Channel">;
21  # <Word? s.mode> =
22    <Lib-Error (StdIO Open-File) "Arg 3 is not a Word">;
23  <Prim-Open-File s.channel e.file s.mode>,
24    <Bind &Buffers (s.channel) (<Stream &Prim-Read s.channel &Read-Chunk-Size>)>;
25};
26
27/*
28 * FIXME:
29 * Should open &StdIn, &StdOut, and &StdErr like this:
30 *   <Bind &Buffers (&StdIn) (<Stream &Prim-Read &StdIn 1>)>;
31 */
32
33
34//FIXME: probably shouldn't close &StdIn, &StdOut, and &StdErr?
35Close-Channel s.channel, {
36  # <Channel? s.channel> =
37    <Lib-Error (StdIO Close-Channel) "Arg 1 is not a Channel">;
38  <Prim-Close-Channel s.channel>,
39    <Unbind &Buffers s.channel>;
40};
41
42
43EOF? s.channel, {
44  <Lookup &Buffers s.channel> : stream =
45    <End-of-Stream? stream>;
46  <Lib-Error (StdIO EOF?) "Channel is not open">;
47};
48
49
50Read-Char! s.channel, {
51  <Lookup &Buffers s.channel> : stream =
52    <Getc stream> : s.char, s.char;
53  <Lib-Error (StdIO Read-Char!) "Channel is not open"> = $fail;
54};
55
56Read-Char = <Read-Char! &StdIn>;
57
58
59Read-Line! s.channel, {
60  <Lookup &Buffers s.channel> : stream =
61    <Get-Line stream> : \{
62      e.line '\r\n' = e.line;
63      e.line '\n'   = e.line;
64      e.line '\r'   = e.line;
65      v.line        = v.line;
66    };
67  <Lib-Error (StdIO Read-Line!) "Channel is not open"> = $fail;
68};
69
70Read-Line = <Read-Line! &StdIn>;
71
72
73$func? Read-Main s.fname s.channel = term;
74
75Read! s.channel = <Read-Main Read! s.channel>;
76
77Read = <Read-Main Read &StdIn>;
78
79Read-Main s.fname s.channel, {
80  <Lookup &Buffers s.channel> : stream =
81    {
82      <Read-Term? stream>;
83      /*empty*/;
84    } :: e.result,
85    {
86      <Errors> : (t.name t.pos (e.message)) e =
87        <Lib-Error (StdIO s.fname) e.message>, $fail;;
88    },
89    e.result : term = term;
90  <Lib-Error (StdIO s.fname) "Channel is not open"> = $fail;
91};
92
Note: See TracBrowser for help on using the repository browser.