1 module dscanner.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 (fileName == "stdin") 25 return readStdin(); 26 if (!exists(fileName)) 27 { 28 stderr.writefln("%s does not exist", fileName); 29 return []; 30 } 31 File f = File(fileName); 32 if (f.size == 0) 33 return []; 34 ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(f.size)); 35 f.rawRead(sourceCode); 36 return sourceCode; 37 } 38 39 string[] expandArgs(string[] args) 40 { 41 import std.file : isFile, FileException, dirEntries, SpanMode; 42 import std.algorithm.iteration : map; 43 import std.algorithm.searching : endsWith; 44 45 // isFile can throw if it's a broken symlink. 46 bool isFileSafe(T)(T a) 47 { 48 try 49 return isFile(a); 50 catch (FileException) 51 return false; 52 } 53 54 string[] rVal; 55 if (args.length == 1) 56 args ~= "."; 57 foreach (arg; args[1 .. $]) 58 { 59 if (arg == "stdin" || isFileSafe(arg)) 60 rVal ~= arg; 61 else 62 foreach (item; dirEntries(arg, SpanMode.breadth).map!(a => a.name)) 63 { 64 if (isFileSafe(item) && (item.endsWith(`.d`) || item.endsWith(`.di`))) 65 rVal ~= item; 66 else 67 continue; 68 } 69 } 70 return rVal; 71 }