1 module main; 2 3 import std.stdio; 4 import std.getopt; 5 import std.file : read; 6 7 import app; 8 9 extern(C) int isatty(int); 10 alias isTTY = isatty; 11 12 int bytesPerLine = 20; 13 FileType fileType = FileType.binary; 14 string variableName = "data"; 15 string moduleName = "mod"; 16 bool helpWanted = false; 17 bool trailingComma = false; 18 bool force = false; 19 20 /// 21 void main(string[] args) 22 { 23 // only using getopt functions from phobos 2.066 24 getopt( 25 args, 26 config.passThrough, 27 "help|h", &helpWanted, 28 "bytesPerLine|l", &bytesPerLine, 29 "type|t", &fileType, 30 "variable|v", &variableName, 31 "module|m", &moduleName, 32 "trailingComma", &trailingComma, 33 "force|f", &force 34 ); 35 36 if(helpWanted) 37 { 38 writeln("file2d"); 39 writeln("Simple tool to create embedable files in #dlang"); 40 writeln(); 41 writeln(" --help"); 42 writeln(" -h Shows this help text."); 43 writeln("--bytesPerLine=<arg>"); 44 writeln(" -n How many bytes are appended to the array before starting a new line. (Defaults to 20)"); 45 writeln(" --type={binary|text}"); 46 writeln(" -t Sets the file type to interpret. (Defaults to binary)"); 47 writeln(" --variable=<arg>"); 48 writeln(" -v Sets the variable name. (Defaults to data)"); 49 writeln(" --module=<arg>"); 50 writeln(" -m Sets the module name. (Defaults to mod)"); 51 writeln("--trailingComma Array will end with a comma if this is set"); 52 writeln(" --force"); 53 writeln(" -f Forces output to stdout even if it is a terminal."); 54 return; 55 } 56 57 ubyte[] content; 58 if(args.length == 1) // No file given; read from stdin instead 59 { 60 // This allows neat piping like 61 // date | file2d 62 // file2d < README.md > readme.d 63 64 if(isTTY(0)) // Checks if stdin is terminal or piped 65 { 66 writeln("Can't read from terminal. Use pipe instead or give a file argument!"); 67 return; 68 } 69 70 string s; 71 while(!stdin.eof) 72 { 73 stdin.readf("%s", &s); 74 content ~= cast(ubyte[]) s; 75 } 76 } 77 else 78 content = cast(ubyte[])read(args[1]); 79 80 if(!force && content.length > 10_000 && isTTY(1)) // Give a warning for files with more than 10000 bytes of size 81 { 82 writeln("stdout is a terminal. Pipe to a file or use --force instead!"); 83 return; 84 } 85 86 write(createModule(fileType, content, moduleName, variableName, bytesPerLine, trailingComma)); 87 }