View previous topic :: View next topic |
Author |
Message |
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Sat May 06, 2006 3:42 pm Post subject: StreamConduit? |
|
|
I'm trying to use the Mango approach to read data, but I'm thinking that maybe someone would want to provide a std.stream.Stream as the source, so I thought the best idea would be to write a StreamConduit. Am I right assuming that? Has anyone already done this? |
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Sat May 06, 2006 5:04 pm Post subject: |
|
|
I think I answered both of my questions: using Mango release 2.0 (with build/mango.d adapted from trunk), apparently this works:
Code: |
private:
import std.stream;
import mango.io.Buffer;
import mango.io.Conduit;
import mango.io.model.IConduit;
public:
class StreamConduit : Conduit, ISeekable
{
Stream stream;
alias Conduit.copy copy;
alias Conduit.read read;
alias Conduit.write write;
this(Stream stream)
{
ConduitStyle.Bits style;
if (stream.readable)
if (stream.writeable)
style = ConduitStyle.ReadWrite;
else
style = ConduitStyle.Read;
else
if (stream.writeable)
style = ConduitStyle.Write;
super(style, stream.seekable);
this.stream = stream;
}
override void close ()
{
super.close ();
stream.close();
}
override uint bufferSize ()
{
return 1024 * 16;
}
ulong getPosition ()
{
return stream.position();
}
long length ()
{
return stream.size();
}
override uint reader (void[] dst)
{
void * p = dst;
return stream.readBlock(p, dst.length);
}
override uint writer (void[] src)
{
return stream.writeBlock(src.ptr, src.length);
}
ulong seek (ulong offset, SeekAnchor anchor = SeekAnchor.Begin)
{
return stream.seek (offset, cast(SeekPos) anchor);
}
}
|
Comments?
Feel free to use it if you think it's useful.
EDIT: a bit of spelling
Last edited by Carlos on Sat May 06, 2006 5:24 pm; edited 1 time in total |
|
Back to top |
|
|
kris
Joined: 27 Mar 2004 Posts: 1494 Location: South Pacific
|
Posted: Sat May 06, 2006 5:16 pm Post subject: |
|
|
Yep, that looks right
Nice! |
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Sat May 06, 2006 5:25 pm Post subject: |
|
|
Thanks! |
|
Back to top |
|
|
Carlos
Joined: 19 Mar 2004 Posts: 396 Location: Canyon, TX
|
Posted: Sat May 06, 2006 5:41 pm Post subject: |
|
|
Almost forgot: unittest!
Code: |
version(UnitTest)
{
import mango.io.Reader;
unittest
{
int a = 4;
long b = 8;
char [] c = "hola";
float d = 3.14159;
auto ms = new MemoryStream;
ms.write(a);
ms.write(b);
ms.write(c);
ms.write(d);
ms.position = 0;
auto reader = new Reader(new StreamConduit(ms));
reader.get(a);
reader.get(b);
reader.get(c);
reader.get(d);
assert(a == a.init);
assert(b == b.init);
assert(c == c.init);
assert(d == d.init);
}
void main()
{
}
}
|
|
|
Back to top |
|
|
|