Stream Processing.

9 views
Skip to first unread message

Kostas Maistrelis

unread,
Nov 24, 2008, 2:45:28 PM11/24/08
to Sleep Developers
I plan to release the first stable release of xml parsing library,
but before i want to ask some questions that will help me
to write a better code.

I have a function that builds the dom tree (xomBuild), this function
takes one argument of type sleep.bridges.io.IOObject
I think that this method permits to read from a File by the help
of the openf function

I want to make the proper changes to permit the next scenarios
(if it is possible):

1) To read from standard input. I want to make a series of pipes in a
shell
with one sleep script on a middle

2) I want to read from a buffered stream that a user changes on the
fly.
For example i want to open a File and apply a regular expression
to
each line and while in progress to pass the control to xomBuild

Are these scenarios possible?




One more point:
is it possible that the user passes to my Loadable some initial
parameters?
(maybe from ScriptEnvironment?)


Thanks.

Raphael Mudge

unread,
Nov 24, 2008, 3:12:03 PM11/24/08
to sleep-de...@googlegroups.com
Hi Kostas,
>
>

> I have a function that builds the dom tree (xomBuild), this function
> takes one argument of type sleep.bridges.io.IOObject
> I think that this method permits to read from a File by the help
> of the openf function

Yes, a sleep.bridges.io.IOObject is a wrapper around an InputStream
and an OutputStream. It can have either or both. Use
SleepUtils.getIOHandle(... input stream ..., ... output stream ...)
to convert a stream or two into an IOObject.

> I want to make the proper changes to permit the next scenarios
> (if it is possible):
>
> 1) To read from standard input. I want to make a series of pipes in a
> shell
> with one sleep script on a middle

If you want to write a script that interacts with STDIN and STDOUT no
problem. Most IO functions will default to STDIN/STDOUT as
appropriate if you omit the file handle argument. Optionally there
is the getConsole() method that returns an IOObject that holds the
streams for STDIN/STDOUT explicitly.

> 2) I want to read from a buffered stream that a user changes on the
> fly.
> For example i want to open a File and apply a regular expression
> to
> each line and while in progress to pass the control to xomBuild
>
> Are these scenarios possible?
>

You have two options. You can read a string in line by line, process
it, and pass that on to your script. Although for an XML parser this
doesn't make much sense :) The other option is write a Java class
that extends InputStream and OutputStream and can wrap another stream
processing any data going through it as you described. I'd recommend
coding something like this in Java.

If you *really* want to be clever and do this in Sleep... I'd
consider playing around with fork. Essentially you get a
bidirectional pipe connecting two threads. You could make your
thread read bytes from whatever handle, process them, and make them
available to the other end (available as $source within the thread).

sub myRegexHandle
{
return fork({
while $text (readln($handle))
{
if ($text ismatch $regex)
{
println($source, $text);
}
}

closef($handle);
}, $handle => $1, $regex => $2);
}


Then you could do stuff like:

$h2 = myRegexHandle(openf("whatever.txt"), '\d+');
while $data (readln($h2))
{
println($data);
}
closef($h2);

To my surprise this code actually works. You can do it on a byte by
byte level as well with readb and writeb. See the IO chapter of the
Sleep manual for more details. Pick your poison and drink it proud.

-- Raphael
Reply all
Reply to author
Forward
0 new messages