Canonical/Idiomatic in memory files

Diggory diggsey at googlemail.com
Tue May 28 22:04:51 PDT 2013


On Wednesday, 29 May 2013 at 04:48:07 UTC, Russel Winder wrote:
> On Tue, 2013-05-28 at 13:46 -0400, Jonathan M Davis wrote:
> […]
>> Do you mean something like std.mmfile.MmFile which operates on 
>> a file as an array in memory using mmap, or do you mean 
>> operating on memory that has no connection to a file at all 
>> (in which case, I'm not sure why you'd want to use File)? If 
>> the former, well use std.file.MmFile. If the latter, I expect 
>> that you're out of luck. Unfortunately, std.stdio.File is 
>> currently just a wrapper around FILE and is thus limited by 
>> what you can do with FILE - that and I don't think that the 
>> File API took into consideration the possibility of operating 
>> on anything other than an actual file (and I honestly don't 
>> know what you'd be trying to do with it where it would make 
>> any sense for to operate on anything other than an actual 
>> file).
>
> Looks like I am out of luck then. :-(
>
> The context is writing a small program that can be a filter or 
> operate
> on files: actually it is the wc program. So it needs to work 
> with opened
> files and stdin. That is fine (sort of). The issue comes when 
> writing
> unit tests for the code: unit tests should not touch the file 
> system, so
> I need a memory buffer backed std.stdio.File for the tests. A 
> mock file
> in a sense.As noted earlier Go, Python, all JVM languages have 
> such
> things, and it really needs to be part of D. If there really is 
> nothing
> like this, I should add a JIRA issue and see if I can create a 
> pull
> request later in the summer.

It could be implemented in phobos fairly easily - on posix there 
is "fmemopen" or "open_memstream" to get a FILE* from a memory 
buffer. On windows there is "CreateFileMapping" in conjunction 
with "MapViewOfFile". The semantics are slightly different but 
they both allow the functionality you are after.


More information about the Digitalmars-d mailing list