Re: Files and Buffers

2018-02-02 Thread Jerry Ferris via Digitalmars-d-learn

Good day,

I reexamined my objective and needs, and I've determined that the 
entirety of the input does not need to be read in all cases. Thus 
I've returned to using File instead std.file.read.
I should've put forth more thought; because I did not, I created 
a fairly useless OP. I do apologize, but from what I've seen, it 
at least initiated some discussion; I learned some useful 
functions too.


---
void main(string[] args)
{
	// file points to either stdin or a file specified by a user via 
args

// fileSize is 0 for stdin
auto uncompData = file.decompress(fileSize);
}

OutBuffer decompress(File file, ulong fileSize = 0)
{
// Process file's contents
}
---

Best regards,
Jerry Ferris



Re: Files and Buffers

2018-02-02 Thread Jonathan M Davis via Digitalmars-d-learn
On Friday, February 02, 2018 09:40:52 Steven Schveighoffer via Digitalmars-
d-learn wrote:
> std.file.read returns a void[].
>
> I didn't see one that returns a ubyte[], and using the readText version
> is going to validate the text I think (which may not be desired). It
> really depends on the use case of the OP, but his original code was
> working with ubyte[] without validation, so I suggested the void[]
> return with cast.

readText calls std.utf.validate, which makes some sense if char, wchar, and
dchar are supposed to be UTF-8, UTF-16, and UTF-32 respectively, but then we
turn around and end up validating Unicode all over the place thanks to how
the range API works with "narrow" strings, making the validation kind of
pointless.

Regardless, if what you want is ubyte[], then there's no reason to be
calling readText rather than read.

> It's a shame, actually, that ubyte[] isn't returned from read. I
> remember discussions at some point to change it, but that was shot down
> for some reason.

I can never remember what the reasons are for using void[] instead of
ubyte[]. I think that maybe it was argued at one point that having a
parameter be void[] made more sense, because it then can accept any array
type, but that argument doesn't hold for something returning void[]. It's
just a bunch of bytes, so I would have thought that ubyte[] would make more
sense. But I can't remember the arguments now, and without thinking through
it a bit, I'm not sure whether changing read to return ubyte[] would break
code or not. I don't _think_ so, since you have to cast from void[] to do
anything, and ubyte[] will implicitly convert to void[], but there may be
something that I'm missing that would make such a change a breaking change.
But the fact that it's void[] instead of ubyte[], means that it can't be
used in @safe code without using @trusted even if all you want is ubyte[].

- Jonathan M Davis



Re: Files and Buffers

2018-02-02 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/1/18 8:27 PM, Seb wrote:

On Thursday, 1 February 2018 at 21:42:33 UTC, Steven Schveighoffer wrote:

On 2/1/18 4:36 PM, Jerry Ferris wrote:

Hello,

I'm developing a program that will either receive data from stdin or 
a file, and pass it along to a function for processing. I want to 
place this data into a buffer so there only has to be one version of 
the function. However, since I'm new to D, I'm unsure how to go about 
this in the most idiomatic way.


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

Then cast to ubyte[].



There's also always std.string.representation which imho looks nicer 
then the cast:


https://dlang.org/library/std/string/representation.html


std.file.read returns a void[].

I didn't see one that returns a ubyte[], and using the readText version 
is going to validate the text I think (which may not be desired). It 
really depends on the use case of the OP, but his original code was 
working with ubyte[] without validation, so I suggested the void[] 
return with cast.


It's a shame, actually, that ubyte[] isn't returned from read. I 
remember discussions at some point to change it, but that was shot down 
for some reason.


-Steve


Re: Files and Buffers

2018-02-01 Thread Seb via Digitalmars-d-learn
On Thursday, 1 February 2018 at 21:42:33 UTC, Steven 
Schveighoffer wrote:

On 2/1/18 4:36 PM, Jerry Ferris wrote:

Hello,

I'm developing a program that will either receive data from 
stdin or a file, and pass it along to a function for 
processing. I want to place this data into a buffer so there 
only has to be one version of the function. However, since I'm 
new to D, I'm unsure how to go about this in the most 
idiomatic way.


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

Then cast to ubyte[].



There's also always std.string.representation which imho looks 
nicer then the cast:


https://dlang.org/library/std/string/representation.html


Re: Files and Buffers

2018-02-01 Thread Jerry Ferris via Digitalmars-d-learn
On Thursday, 1 February 2018 at 21:42:33 UTC, Steven 
Schveighoffer wrote:

On 2/1/18 4:36 PM, Jerry Ferris wrote:
https://dlang.org/phobos/std_file.html#read

Then cast to ubyte[].

Thank you in advance, and I apologize if this is a very stupid 
question.


Not a stupid question!

-Steve


On Thursday, February 01, 2018 21:36:52 Jerry Ferris via 
Digitalmars-d-learn wrote:
If you want to copy an entire file into an array, then use 
std.file.read or std.file.readText. std.stdio.File only really 
makes sense if you're trying not to read the entire file into 
memory.


- Jonathan M Davis


In retrospect, I should've looked more closely to that module. 
Regardless, thank you very much for your (prompt!) answers.


Regards,
Jerry Ferris


Re: Files and Buffers

2018-02-01 Thread Steven Schveighoffer via Digitalmars-d-learn

On 2/1/18 4:36 PM, Jerry Ferris wrote:

Hello,

I'm developing a program that will either receive data from stdin or a 
file, and pass it along to a function for processing. I want to place 
this data into a buffer so there only has to be one version of the 
function. However, since I'm new to D, I'm unsure how to go about this 
in the most idiomatic way.


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

Then cast to ubyte[].


Thank you in advance, and I apologize if this is a very stupid question.


Not a stupid question!

-Steve


Re: Files and Buffers

2018-02-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, February 01, 2018 21:36:52 Jerry Ferris via Digitalmars-d-learn 
wrote:
> Hello,
>
> I'm developing a program that will either receive data from stdin
> or a file, and pass it along to a function for processing. I want
> to place this data into a buffer so there only has to be one
> version of the function. However, since I'm new to D, I'm unsure
> how to go about this in the most idiomatic way.
>
> Thus my questions are:
> * Is there a simpler way to copy a file's contents into a buffer?

If you want to copy an entire file into an array, then use std.file.read or
std.file.readText. std.stdio.File only really makes sense if you're trying
not to read the entire file into memory.

- Jonathan M Davis