randomIO, std.file, core.stdc.stdio

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 25 21:22:35 PDT 2016


On Tuesday, 26 July 2016 at 04:05:22 UTC, Charles Hixson wrote:
> Yes, but I really despise the syntax they came up with.  It's 
> probably good if most of your I/O is ranges, but mine hasn't 
> yet ever been.  (Combining ranges with random I/O?)

that's why i wrote iv.stream, and then iv.vfs, with convenient 
things like `readNum!T`, for example. you absolutely don't need 
to reimplement the whole std.stdio.File if all you need it better 
API. thanks to UFCS, you can write your new API as free functions 
accepting std.stdio.File as first arg. or even generic stream, 
like i did in iv.stream:


enum isReadableStream(T) = is(typeof((inout int=0) {
   auto t = T.init;
   ubyte[1] b;
   auto v = cast(void[])b;
   t.rawRead(v);
}));

enum isWriteableStream(T) = is(typeof((inout int=0) {
   auto t = T.init;
   ubyte[1] b;
   t.rawWrite(cast(void[])b);
}));

T readInt(T : ulong, ST) (auto ref ST st) if 
(isReadableStream!ST) {
   T res;
   ubyte* b = cast(ubyte*)&res;
   foreach (immutable idx; 0..T.sizeof) {
     if (st.rawRead(b[idx..idx+1]).length != 1) throw new 
Exception("read error");
   }
   return res;
}


and then:
   auto fl = File("myfile");
   auto i = fl.readInt!uint;

something like that.


More information about the Digitalmars-d-learn mailing list