protected string StExists(IPM pm) { if (!pm.It(LevelType.Property, "exists")) { throw new IncorrectNodeException(pm); } ILevel level = pm.FirstLevel; if (!pm.FinalEmptyIs(LevelType.Method, "directory") && !pm.FinalEmptyIs(LevelType.Method, "file")) { throw new IncorrectNodeException(pm); } if (level.Is(ArgumentType.StringDouble)) { return(StExists(pm.IsData("file"), (string)level.Args[0].data, false)); } if (level.Is(ArgumentType.StringDouble, ArgumentType.Boolean)) { return(StExists(pm.IsData("file"), (string)level.Args[0].data, (bool)level.Args[1].data)); } throw new PMLevelException(level, level.Data + "(string path [, boolean environment])"); }
protected string stExists(IPM pm) { if(!pm.It(LevelType.Property, "exists")) { throw new IncorrectNodeException(pm); } ILevel level = pm.FirstLevel; if(!pm.FinalEmptyIs(LevelType.Method, "directory") && !pm.FinalEmptyIs(LevelType.Method, "file")) { throw new IncorrectNodeException(pm); } if(level.Is(ArgumentType.StringDouble)) { return stExists(pm.IsData("file"), (string)level.Args[0].data, false); } if(level.Is(ArgumentType.StringDouble, ArgumentType.Boolean)) { return stExists(pm.IsData("file"), (string)level.Args[0].data, (bool)level.Args[1].data); } throw new ArgumentPMException(level, level.Data + "(string path [, boolean environment])"); }
protected string StWrite(IPM pm, bool append, bool newline, Encoding enc) { ILevel level = pm.FirstLevel; string file = null; string std = null; // basic method signatures if (level.Is(ArgumentType.StringDouble) && pm.IsData("write", "writeLine", "append", "appendLine")) { file = (string)level.Args[0].data; } else if (level.Is(ArgumentType.StringDouble, ArgumentType.Boolean, ArgumentType.Boolean, ArgumentType.StringDouble) && pm.IsData("write")) { file = (string)level.Args[0].data; append = (bool)level.Args[1].data; newline = (bool)level.Args[2].data; enc = GetEncoding((string)level.Args[3].data); } else if (level.Is(ArgumentType.EnumOrConst) && pm.IsData("write", "writeLine")) { std = (string)level.Args[0].data; } else if (level.Is(ArgumentType.EnumOrConst, ArgumentType.Boolean, ArgumentType.Boolean, ArgumentType.StringDouble) && pm.IsData("write")) { std = (string)level.Args[0].data; append = (bool)level.Args[1].data; newline = (bool)level.Args[2].data; enc = GetEncoding((string)level.Args[3].data); } else { throw new PMLevelException(level, "write( (string name | const STD) [, boolean append, boolean newline, string encoding]); writeLine(string name | const STD); append/appendLine(string name)"); } // content string fdata; if (pm.Levels[1].Type == LevelType.RightOperandColon) { fdata = pm.Levels[1].Data; } else { throw new IncorrectNodeException(pm); } // Text file if (file != null) { file = GetLocation(file); WriteToFile(file, fdata, append, newline, enc); LSender.Send(this, $"The data:{fdata.Length} is successfully written - `{file}` : {append}, {newline}, '{enc.EncodingName}'", MsgLevel.Trace); return(Value.Empty); } // Streams switch (std) { case "STDERR": { WriteToStdErr(fdata, newline); return(Value.Empty); } case "STDOUT": { WriteToStdOut(fdata, newline); return(Value.Empty); } default: { throw new IncorrectSyntaxException($"Incorrect stream type `{std}`"); } } throw new IncorrectNodeException(pm); }
protected string stWrite(IPM pm, bool append, bool newline, Encoding enc) { ILevel level = pm.FirstLevel; string file = null; string std = null; // basic method signatures if(level.Is(ArgumentType.StringDouble) && pm.IsData("write", "writeLine", "append", "appendLine")) { file = (string)level.Args[0].data; } else if(level.Is(ArgumentType.StringDouble, ArgumentType.Boolean, ArgumentType.Boolean, ArgumentType.StringDouble) && pm.IsData("write")) { file = (string)level.Args[0].data; append = (bool)level.Args[1].data; newline = (bool)level.Args[2].data; enc = encoding((string)level.Args[3].data); } else if(level.Is(ArgumentType.EnumOrConst) && pm.IsData("write", "writeLine")) { std = (string)level.Args[0].data; } else if(level.Is(ArgumentType.EnumOrConst, ArgumentType.Boolean, ArgumentType.Boolean, ArgumentType.StringDouble) && pm.IsData("write")) { std = (string)level.Args[0].data; append = (bool)level.Args[1].data; newline = (bool)level.Args[2].data; enc = encoding((string)level.Args[3].data); } else { throw new ArgumentPMException(level, "write( (string name | const STD) [, boolean append, boolean newline, string encoding]); writeLine(string name | const STD); append/appendLine(string name)"); } // content string fdata; if(pm.Levels[1].Type == LevelType.RightOperandColon) { fdata = pm.Levels[1].Data; } else { throw new IncorrectNodeException(pm); } // Text file if(file != null) { file = location(file); writeToFile(file, fdata, append, newline, enc); Log.Trace("The data:{0} is successfully written - `{1}` : {2}, {3}, '{4}'", fdata.Length, file, append, newline, enc.EncodingName); return Value.Empty; } // Streams switch(std) { case "STDERR": { writeToStdErr(fdata, newline); return Value.Empty; } case "STDOUT": { writeToStdOut(fdata, newline); return Value.Empty; } default: { throw new InvalidArgumentException("Incorrect stream type `{0}`", std); } } throw new IncorrectNodeException(pm); }