Jump to page: 1 2
Thread overview
Is there a function that reads the entire contents of a std.stdio.File?
Sep 16, 2014
Jay
Sep 16, 2014
Marc Schütz
Sep 16, 2014
Jay
Sep 16, 2014
Marc Schütz
Sep 16, 2014
Daniel Kozak
Sep 16, 2014
Jay
Sep 16, 2014
Jay
Sep 16, 2014
Ali Çehreli
Sep 16, 2014
Jay
Sep 16, 2014
Justin Whear
Sep 16, 2014
Jay
September 16, 2014
all the functions/methods i've come across so far deal with either streams or just file names (like std.file.read) and there doesn't seem to be a way to wrap a std.stdio.File in a stream (or is there?). i need a function that takes a std.stdio.File and returns a string or byte array.
September 16, 2014
On Tuesday, 16 September 2014 at 14:37:06 UTC, Jay wrote:
> all the functions/methods i've come across so far deal with either streams or just file names (like std.file.read) and there doesn't seem to be a way to wrap a std.stdio.File in a stream (or is there?). i need a function that takes a std.stdio.File and returns a string or byte array.

You could try `std.mmfile.MmFile`, it has a constructor that takes a `File`, but only on Linux:

http://dlang.org/phobos/std_mmfile.html
https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79
September 16, 2014
V Tue, 16 Sep 2014 14:37:05 +0000
Jay via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno:

> all the functions/methods i've come across so far deal with either streams or just file names (like std.file.read) and there doesn't seem to be a way to wrap a std.stdio.File in a stream (or is there?). i need a function that takes a std.stdio.File and returns a string or byte array.


You can use rawRead: http://dlang.org/phobos/std_stdio.html#.File.rawRead

September 16, 2014
On Tuesday, 16 September 2014 at 14:43:10 UTC, Marc Schütz wrote:
> You could try `std.mmfile.MmFile`, it has a constructor that takes a `File`, but only on Linux:
>
> http://dlang.org/phobos/std_mmfile.html
> https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79

does it work with pipes/sockets? it seems to call fstat() to get the size of the contents. but anyway this is quite useful, thanks.
September 16, 2014
On Tuesday, 16 September 2014 at 14:49:08 UTC, Daniel Kozak via Digitalmars-d-learn wrote:
> You can use rawRead:
> http://dlang.org/phobos/std_stdio.html#.File.rawRead

for that i need to know the size of the contents. is there a function that does that for me? i'm looking for something like someFile.readAll(). and it shouldn't matter whether the File instance represents a regular file or a pipe/stream.
September 16, 2014
On Tuesday, 16 September 2014 at 15:03:05 UTC, Jay wrote:
> and it shouldn't matter whether the File instance represents a regular file or a pipe/stream.

i meant pipe/socket.
September 16, 2014
On Tuesday, 16 September 2014 at 14:59:45 UTC, Jay wrote:
> On Tuesday, 16 September 2014 at 14:43:10 UTC, Marc Schütz wrote:
>> You could try `std.mmfile.MmFile`, it has a constructor that takes a `File`, but only on Linux:
>>
>> http://dlang.org/phobos/std_mmfile.html
>> https://github.com/D-Programming-Language/phobos/blob/master/std/mmfile.d#L77-L79
>
> does it work with pipes/sockets? it seems to call fstat() to get the size of the contents. but anyway this is quite useful, thanks.

mmap(2) says it fails with EACCES when the fd isn't a regular file, so unfortunately it won't work.
September 16, 2014
On 09/16/2014 07:37 AM, Jay wrote:
> all the functions/methods i've come across so far deal with either
> streams or just file names (like std.file.read) and there doesn't seem
> to be a way to wrap a std.stdio.File in a stream (or is there?). i need
> a function that takes a std.stdio.File and returns a string or byte array.

std.file.read (and readText):

  http://dlang.org/phobos/std_file.html#.read

Ali

September 16, 2014
On Tuesday, 16 September 2014 at 16:54:17 UTC, Ali Çehreli wrote:
> On 09/16/2014 07:37 AM, Jay wrote:
>> all the functions/methods i've come across so far deal with either
>> streams or just file names (like std.file.read) and there doesn't seem
>> to be a way to wrap a std.stdio.File in a stream (or is there?). i need
>> a function that takes a std.stdio.File and returns a string or byte array.
>
> std.file.read (and readText):
>
>   http://dlang.org/phobos/std_file.html#.read
>
> Ali

wait, std.file.read doesn't accept a File instance. i've got a File instance generated by another function and now i need to read the entire contents of whatever it represents (a regular file/pipe/etc).
September 16, 2014
On Tue, 16 Sep 2014 14:37:05 +0000, Jay wrote:

> all the functions/methods i've come across so far deal with either streams or just file names (like std.file.read) and there doesn't seem to be a way to wrap a std.stdio.File in a stream (or is there?). i need a function that takes a std.stdio.File and returns a string or byte array.

The short answer is no.  I usually use something like this:

    // Lazy
    auto stream = stdin.byChunk(4096).joiner();

You can make it eager by tacking a `.array` on the end.  Functions used are from std.stdio, std.algorithm, std.array.
« First   ‹ Prev
1 2