1 module readers; 2 3 import std.array : appender, uninitializedArray; 4 import std.stdio : stdin, stderr, File; 5 import std.conv : to; 6 import std.file : exists; 7 8 ubyte[] readStdin() 9 { 10 auto sourceCode = appender!(ubyte[])(); 11 ubyte[4096] buf; 12 while (true) 13 { 14 auto b = stdin.rawRead(buf); 15 if (b.length == 0) 16 break; 17 sourceCode.put(b); 18 } 19 return sourceCode.data; 20 } 21 22 ubyte[] readFile(string fileName) 23 { 24 if (!exists(fileName)) 25 { 26 stderr.writefln("%s does not exist", fileName); 27 return []; 28 } 29 File f = File(fileName); 30 if (f.size == 0) 31 return []; 32 ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(f.size)); 33 f.rawRead(sourceCode); 34 return sourceCode; 35 } 36 37 string[] expandArgs(string[] args) 38 { 39 import std.file : isFile, FileException, dirEntries, SpanMode; 40 import std.algorithm.iteration : map; 41 import std.algorithm.searching : endsWith; 42 43 // isFile can throw if it's a broken symlink. 44 bool isFileSafe(T)(T a) 45 { 46 try 47 return isFile(a); 48 catch (FileException) 49 return false; 50 } 51 52 string[] rVal; 53 if (args.length == 1) 54 args ~= "."; 55 foreach (arg; args[1 .. $]) 56 { 57 if (isFileSafe(arg)) 58 rVal ~= arg; 59 else 60 foreach (item; dirEntries(arg, SpanMode.breadth).map!(a => a.name)) 61 { 62 if (isFileSafe(item) && (item.endsWith(`.d`) || item.endsWith(`.di`))) 63 rVal ~= item; 64 else 65 continue; 66 } 67 } 68 return rVal; 69 }