[Aldor-l] Reading from stdin
Christian Aistleitner
tmgisi at gmx.at
Tue Aug 1 03:52:17 EDT 2006
Hello,
in a private Email I have been asked, about reading from stdin. As it
probably applies to other people as well, I decided to answer to the list.
The naive approach via
#include "aldor"
import from TextReader;
import from TextWriter;
import from String;
import from Character;
local str: String := << stdin;
stdout << "Received: " << str << newline;
Gives problems, as it does not necessarily give the whole line:
____________________________________________
tmgisi at spencer
cwd: ~
$ LC_ALL=C /opt/aldor/bin/aldor -M no-abbrev -C args=-Wopts=-m32 -Ffm \
-Fx -lalgebra -laldor test2.as && ( echo 3/4 | ./test2 )
cc1: note: -fwritable-strings is deprecated; see documentation for details
cc1: note: -fwritable-strings is deprecated; see documentation for details
Received: 3
You'll have to use quotes to achive this:
____________________________________________
tmgisi at spencer
cwd: ~
$ LC_ALL=C /opt/aldor/bin/aldor -M no-abbrev -C args=-Wopts=-m32 -Ffm \
-Fx -lalgebra -laldor test2.as && ( echo \"3/4\" | ./test2 )
cc1: note: -fwritable-strings is deprecated; see documentation for details
cc1: note: -fwritable-strings is deprecated; see documentation for details
Received: 3/4
Forcing users to quote things is nasty. For the CharSet library I used the
following function:
readString():String == {
import from Character;
import from MachineInteger;
local ret : String := empty;
local ch : Character;
ch := <<$Character stdin;
while ch ~= newline repeat
{
ret := ret + ch::String;
ch := <<$Character stdin;
}
ret;
}
It reads _every_ character until a newline occurs. The returned string
does not contain a trailing newline:
____________________________________________
tmgisi at spencer
cwd: ~
$ LC_ALL=C /opt/aldor/bin/aldor -M no-abbrev -C args=-Wopts=-m32 -Ffm -Fx
-lalgebra -laldor test3.as && ( echo 3/4 | ./test3 )
cc1: note: -fwritable-strings is deprecated; see documentation for details
cc1: note: -fwritable-strings is deprecated; see documentation for details
Received: 3/4
On systems with suitable readline support (Every 32-bit GNU/Linux system
should do), you can use something like:
#include "aldor"
import from TextReader;
import from TextWriter;
import from String;
import from Character;
Readline: with {
(<<): ( prompt: String == "" ) -> String;
} == add {
import {
readline: String -> String;
} from Foreign C;
(<<)( prompt ): String == {
readline( prompt );
}
}
import from Readline;
local str: String := <<();
stdout << "Received: " << str << newline;
to take advantage readline. I could not test the readline snippet, as I do
not have 32-bit version of readline -- still waiting for 64-bit Aldor.
--
Kind regards,
Christian
More information about the Aldor-l
mailing list