Forum Navigation
Stream, MemoryStream and File
Posted: 08/30/08 20:46:30Hi. I'm new on Tango and I don't know how to do somethings that I used to do with Phobos.
Concretely I need a seekable generic Stream.
For example. How can I do this, using tango?
import std.stdio, std.stream; void readTest(Stream s) { uint v; s.position = 4; s.read(v); assert(v == 2); } void main() { Stream s; s = new File("myfile", FileMode.In | FileMode.Out); { s.write(cast(uint)1); s.write(cast(uint)2); s.position = 0; readTest(s); } s.close(); s = new MemoryStream(); { s.write(cast(uint)1); s.write(cast(uint)2); s.position = 0; readTest(s); } s.close(); }I tried to figure out how to do it in Tango. But I have a mess with Conduits, and buffers and I don't understand how to do it.
To solve it I made (temporally) a Stream subset similar to Phobos one:
abstract class Stream { alias IConduit.Seek.Anchor Anchor; ubyte read1() { ubyte v; read((&v)[0..1]); return v; } ushort read2() { ushort v; read((&v)[0..1]); return v; } uint read4() { uint v; read((&v)[0..1]); return v; } ulong read8() { ulong v; read((&v)[0..1]); return v; } void write(ubyte v) { write((&v)[0..1]); return v; } void write(ushort v) { write((&v)[0..1]); return v; } void write(uint v) { write((&v)[0..1]); return v; } void write(ulong v) { write((&v)[0..1]); return v; } abstract uint read (void[] v); abstract uint write(void[] v); abstract long seek(long offset, Anchor anchor = Anchor.Begin); ulong length() { ulong back = position; ulong ret = seek(0, Anchor.End); position = back; return ret; } ulong position() { return seek(0, Anchor.Current); } ulong position(ulong set) { return seek(set, Anchor.Begin); } } class FileStream : Stream { FileConduit file; this(char[] name) { file = new FileConduit(name); } override uint read (void[] v) { return file.read(v); } override uint write(void[] v) { return file.write(v); } override long seek(long offset, Anchor anchor = Anchor.Begin) { return file.seek(offset, anchor); } } class MemoryStream : Stream { long _position; long _length; ubyte[] data; this(ubyte[] data) { this.data = data; _position = 0; _length = data.length; } this(int _length) { this.data = new ubyte[_length]; this._position = 0; this._length = _length; } override uint read(void[] v) { ubyte[] vu = cast(ubyte[])v; int read = min(vu.length, _length - _position); vu[0..read] = data[_position.._position + read]; _position += read; return read; } override uint write(void[] v) { ubyte[] vu = cast(ubyte[])v; int read = min(vu.length, _position + _length); data[_position.._position + read] = vu[0..read]; _position += read; return read; } override long seek(long offset, Anchor anchor = Anchor.Begin) { switch (anchor) { case Anchor.Begin: return (_position = offset); case Anchor.Current: return (_position += offset); case Anchor.End: return (_position = _length + offset); } return -1; } override ulong length() { return _length; } override ulong position() { return _position; } }Thanks in advance, and sorry about my bad english.












