[phobos] std.stdio : ByChunk for File

Shin Fujishiro rsinfu at gmail.com
Mon May 31 10:00:05 PDT 2010


> I want chunks of Range version like ByLine(and I hope byChunk returns  
> ByChunk).

I want it too.

But ByChunk should be more generic IMO.  It should be an adaptor which
converts some "bulk input" to range.

This is a bulk input model (similar to the Source concept*1):
----------
struct BulkInput
{
    // Returns true if there is no more data
    bool eof();

    // Reads data in buf and returns the number of elements read
    size_t read(T[] buf);
    size_t read(U[] buf);
    ...
}
----------

File is a bulk input (currently File.read doesn't exist though).  Other
possible examples are sockets and FIFO buffers.  All of them can be
converted to ranges with generic ByChunk.

Here's a (messy) code which I played around with:
http://code.google.com/p/kabe/source/browse/trunk/etc/test_bulkinput_z.d


[*1] Boost: the Source concept
http://www.boost.org/doc/libs/1_43_0/libs/iostreams/doc/concepts/source.html


Shin


"Masahiro Nakagawa" <repeatedly at gmail.com> wrote:
> File has byChunk method that returns chunks but chunks isn't Range.
> So, Range can't treat chunks.
> I want chunks of Range version like ByLine(and I hope byChunk returns  
> ByChunk).
> 
> Following code is a simple implementation.
> -----
> /**
>   * Range that reads a chunk at a time.
>   */
> struct ByChunk
> {
>    private:
>      File    file_;
>      ubyte[] chunk_;
> 
> 
>    public:
>      this(File file, size_t size)
>      in
>      {
>          assert(size, "size must be larger than 0");
>      }
>      body
>      {
>          file_  = file;
>          chunk_ = new ubyte[](size);
> 
>          popFront();
>      }
> 
>      /// Range primitive operations.
>      @property bool empty() const
>      {
>          return !file_.isOpen;
>      }
> 
>      /// ditto
>      @property ubyte[] front()
>      {
>          return chunk_;
>      }
> 
>      /// ditto
>      void popFront()
>      {
>          enforce(file_.isOpen);
> 
>          chunk_ = file_.rawRead(chunk_);
>          if (!chunk_.length)
>              file_.detach();
>      }
> }
> -----
> 
> What do you think?
> 
> 
> Masahiro
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos


More information about the phobos mailing list