[phobos] std.stdio : ByChunk for File
Masahiro Nakagawa
repeatedly at gmail.com
Sun May 30 22:07:36 PDT 2010
On Sun, 30 May 2010 19:05:25 +0900, Lars Tandle Kyllingstad
<lars at kyllingen.net> wrote:
> On Sun, 2010-05-30 at 18:44 +0900, Masahiro Nakagawa 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?
>
>
> I agree. That's what I did for UnbufferedFile, which I hope to add to
> std.stdio:
>
> http://github.com/kyllingstad/ltk/blob/master/ltk/stdio.d
>
> -Lars
>
Your templated ByChunk is good!
Masahiro
More information about the phobos
mailing list