www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is there a function that reads the entire contents of a

reply "Jay" <jaypinkman hotmail.com> writes:
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.
Sep 16 2014
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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
Sep 16 2014
parent reply "Jay" <jaypinkman hotmail.com> writes:
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.
Sep 16 2014
parent "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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.
Sep 16 2014
prev sibling next sibling parent reply Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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:
Sep 16 2014
parent reply "Jay" <jaypinkman hotmail.com> writes:
On Tuesday, 16 September 2014 at 14:49:08 UTC, Daniel Kozak via 
Digitalmars-d-learn wrote:
 You can use 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.
Sep 16 2014
parent "Jay" <jaypinkman hotmail.com> writes:
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.
Sep 16 2014
prev sibling next sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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): Ali
Sep 16 2014
parent "Jay" <jaypinkman hotmail.com> writes:
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): 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).
Sep 16 2014
prev sibling parent reply Justin Whear <justin economicmodeling.com> writes:
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.
Sep 16 2014
parent "Jay" <jaypinkman hotmail.com> writes:
On Tuesday, 16 September 2014 at 18:42:42 UTC, Justin Whear wrote:
 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.
thanks. that's exactly what i need.
Sep 16 2014