December 10, 2018 Re: I/O extensions for common tasks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Basile B. | On Monday, 10 December 2018 at 02:06:26 UTC, Basile B. wrote:
> On Monday, 10 December 2018 at 01:51:56 UTC, Andrew Pennebaker wrote:
>> The stdlib has all the low-level components we need to do lots of different workflows. However, there are a few gaps in the API in terms of high level, common tasks. For example, the API can read an entire Unicode text file to a string for a given filename, but not for a given File object!
>>
>> For now, I am accomplishing this with:
>>
>> // Convenience function for reading an entire UTF-8 file.
>> string readUTF8(File f) {
>> string s;
>>
>> foreach(ubyte[] buf; f.byChunk(1024)) {
>> s ~= buf;
>> }
>>
>> return s;
>> }
>>
>
> There's more simple:
>
>
> auto wholeFile = f.rawRead(new char[](f.size));
>
>
> Note that this kind of question should rather go there: https://forum.dlang.org/group/learn
For pipedProcess streams, the size is basically infinite until the process ends, so that leads to an out of memory error. Again, I think a convenience function would be good to include, to avoid these kinds of mistakes!
|
December 11, 2018 Re: I/O extensions for common tasks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 10 December 2018 at 14:44:56 UTC, Steven Schveighoffer wrote: > On 12/9/18 9:37 PM, Seb wrote: >> On Monday, 10 December 2018 at 01:51:56 UTC, Andrew Pennebaker wrote: >>> The stdlib has all the low-level components we need to do lots of different workflows. However, there are a few gaps in the API in terms of high level, common tasks. For example, the API can read an entire Unicode text file to a string for a given filename, but not for a given File object! >>> >>> [...] >> >> What's wrong with readText: >> >> https://dlang.org/phobos/std_file.html#readText > > Problem statement is that given a File object, how do you read all the data out? :) std.file doesn't help there. As far as I know, the best answer is `file.byLineCopy(Yes.keepTerminator).joiner` if you want characters, and `file.byChunk(...).joiner` if you want ubytes. |
December 11, 2018 Re: I/O extensions for common tasks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On 12/11/18 12:24 AM, Paul Backus wrote: > On Monday, 10 December 2018 at 14:44:56 UTC, Steven Schveighoffer wrote: >> On 12/9/18 9:37 PM, Seb wrote: >>> On Monday, 10 December 2018 at 01:51:56 UTC, Andrew Pennebaker wrote: >>>> The stdlib has all the low-level components we need to do lots of different workflows. However, there are a few gaps in the API in terms of high level, common tasks. For example, the API can read an entire Unicode text file to a string for a given filename, but not for a given File object! >>>> >>>> [...] >>> >>> What's wrong with readText: >>> >>> https://dlang.org/phobos/std_file.html#readText >> >> Problem statement is that given a File object, how do you read all the data out? :) std.file doesn't help there. > > As far as I know, the best answer is `file.byLineCopy(Yes.keepTerminator).joiner` if you want characters, and `file.byChunk(...).joiner` if you want ubytes. Well, I would say the original request wants it in a string, so we are focusing on characters. However, this does NOT work: file.byLine(Yes.keepTerminator).joiner.array; Because it will make an array of dchars. What you really want is this: cast(string)file.byChunk(4096).joiner.array; -Steve |
Copyright © 1999-2021 by the D Language Foundation