FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

StreamConduit?

 
Post new topic   Reply to topic     Forum Index -> Mango
View previous topic :: View next topic  
Author Message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Sat May 06, 2006 3:42 pm    Post subject: StreamConduit? Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Sat May 06, 2006 5:04 pm    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
kris



Joined: 27 Mar 2004
Posts: 1494
Location: South Pacific

PostPosted: Sat May 06, 2006 5:16 pm    Post subject: Reply with quote

Yep, that looks right Smile

Nice!
Back to top
View user's profile Send private message
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Sat May 06, 2006 5:25 pm    Post subject: Reply with quote

Thanks!
Back to top
View user's profile Send private message Yahoo Messenger MSN Messenger
Carlos



Joined: 19 Mar 2004
Posts: 396
Location: Canyon, TX

PostPosted: Sat May 06, 2006 5:41 pm    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Mango All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group