/// <summary> /// To pack string argument in object. /// </summary> /// <param name="arg">Argument for packing.</param> /// <returns></returns> public static object packArgument(object arg) { if (arg == null) { return(null); } if (!(arg is string) || String.IsNullOrWhiteSpace((string)arg)) { return(arg); } SNode.IPM pm = new SNode.PM(String.Format("_({0})", arg)); SNode.Argument first = pm.FirstLevel.Args[0]; if (first.type != SNode.ArgumentType.Object) { return(arg); } return(extract((SNode.Argument[])first.data)); }
protected string doCatch(string cmd, Exception ex, Argument[] args) { Log.Info($"Catched error `{ex.Message}`"); if(args == null) { return evaluate(cmd); } if(args.Length == 2 && args[0].type == ArgumentType.EnumOrConst && args[1].type == ArgumentType.EnumOrConst) { // try{ }catch(err, msg){ } return doCatch(cmd, ex, args[0].data.ToString(), args[1].data.ToString()); } throw new NotSupportedOperationException("the format of the catch block is incorrect or not supported yet."); }
/// <summary> /// Extracts all arguments from line. /// </summary> /// <param name="data">Raw line with user arguments.</param> /// <param name="splitter">A character that delimits arguments.</param> /// <returns>List of parsed arguments or null value if data is empty.</returns> /// <exception cref="SyntaxIncorrectException">If incorrect arguments line.</exception> protected Argument[] extractArgs(string data, char splitter = ',') { if(String.IsNullOrWhiteSpace(data)) { return new Argument[0]; } StringHandler h = new StringHandler(); string[] raw = h.protectArguments(data).Split(splitter); Argument[] ret = new Argument[raw.Length]; for(int i = 0; i < raw.Length; ++i) { string arg = h.recovery(raw[i]).Trim(); if(arg.Length < 1 && splitter == ',') { // std: p1, p2, p3 throw new SyntaxIncorrectException("PM - extractArgs: incorrect arguments line '{0}'", data); } ret[i] = detectArgument(arg); } return ret; }
/// <summary> /// Packing files for signature: /// `pack.files(object files, string output, enum format, enum method, integer level)` /// </summary> /// <param name="files">Input files.</param> /// <param name="name">Output archive.</param> /// <param name="type"></param> /// <param name="method"></param> /// <param name="rate"></param> /// <param name="pm"></param> /// <returns></returns> protected string stPackFiles(Argument[] files, string name, OutArchiveFormat type, CompressionMethod method, CompressionLevel rate, IPM pm) { return stPackFiles(files, name, null, type, method, rate, pm); }
protected string stPackFiles(Argument[] files, string name, Argument[] except, OutArchiveFormat type, CompressionMethod method, CompressionLevel rate, IPM pm) { Log.Trace("stPackFiles: `{0}` : type({1}), method({2}), level({3})", name, type, method, rate); if(String.IsNullOrWhiteSpace(name)) { throw new InvalidArgumentException("The output name of archive is empty."); } if(files.Length < 1) { throw new InvalidArgumentException("List of files is empty."); } if(files.Any(p => p.type != ArgumentType.StringDouble)) { throw new InvalidArgumentException("Incorrect data from input files. Define as {\"f1\", \"f2\", ...}"); } if(except != null && except.Any(p => p.type != ArgumentType.StringDouble)) { throw new InvalidArgumentException("Incorrect data from the 'except' argument. Define as {\"f1\", \"f2\", ...}"); } // additional checking of input files. // The SevenZipSharp creates directories if input file is not exist o_O string[] input = files.Select((f, i) => pathToFile((string)f.data, i)).ToArray().ExtractFiles(); #if DEBUG Log.Trace("stPackFiles: Found files `{0}`", String.Join(", ", input)); #endif if(except != null) { input = input.Except(except .Where(f => !String.IsNullOrWhiteSpace((string)f.data)) .Select(f => location((string)f.data)) .ToArray() .ExtractFiles() ).ToArray(); } if(input.Length < 1) { throw new InvalidArgumentException("The input files was not found. Check your mask and the exception list if used."); } SevenZipCompressor zip = new SevenZipCompressor() { ArchiveFormat = type, CompressionMethod = method, CompressionLevel = rate, CompressionMode = CompressionMode.Create, FastCompression = true, // to disable some events inside SevenZip DirectoryStructure = true, }; compressFiles(zip, location(name), input); return Value.Empty; }
/// <summary> /// Packing files for signature: /// `pack.files(object files, string output, object except)` /// </summary> /// <param name="files">Input files.</param> /// <param name="name">Output archive.</param> /// <param name="except">List of files to exclude from input list.</param> /// <param name="pm"></param> /// <returns></returns> protected string stPackFiles(Argument[] files, string name, Argument[] except, IPM pm) { return stPackFiles(files, name, except, OutArchiveFormat.Zip, CompressionMethod.Deflate, CompressionLevel.Normal, pm); }
/// <summary> /// Packing files for signature: /// `pack.files(object files, string output)` /// </summary> /// <param name="files">Input files.</param> /// <param name="name">Output archive.</param> /// <param name="pm"></param> /// <returns></returns> protected string stPackFiles(Argument[] files, string name, IPM pm) { return stPackFiles(files, name, null, pm); }
protected string deleteFiles(IPM pm, Argument[] files, Argument[] except = null) { if(files.Any(p => p.type != ArgumentType.StringDouble)) { throw new InvalidArgumentException("Incorrect data from input files. Define as {\"f1\", \"f2\", ...}"); } if(except != null && except.Any(p => p.type != ArgumentType.StringDouble)) { throw new InvalidArgumentException("Incorrect data from the 'except' argument. Define as {\"f1\", \"f2\", ...}"); } Func<string, int, string> exs = delegate(string file, int idx) { if(!String.IsNullOrWhiteSpace(file)) { return location(file); } throw new InvalidArgumentException("File name is empty. Fail in '{0}' position.", idx); }; string[] input = files.Select((f, i) => exs((string)f.data, i)).ToArray().ExtractFiles(); #if DEBUG Log.Trace("deleteFiles: Found files `{0}`", String.Join(", ", input)); #endif if(except != null) { input = input.Except(except .Where(f => !String.IsNullOrWhiteSpace((string)f.data)) .Select(f => location((string)f.data)) .ToArray() .ExtractFiles() ).ToArray(); } if(input.Length < 1) { throw new InvalidArgumentException("The input files was not found. Check your mask and the exception list if used."); } deleteFiles(input); return Value.Empty; }
protected string copyFile(IPM pm, string src, string dest, bool overwrite, Argument[] except = null) { if(String.IsNullOrWhiteSpace(src) || String.IsNullOrWhiteSpace(dest)) { throw new InvalidArgumentException("The source file or the destination path argument is empty."); } if(except != null && except.Any(p => p.type != ArgumentType.StringDouble)) { throw new InvalidArgumentException("Incorrect data from the 'except' argument. Define as {\"f1\", \"f2\", ...}"); } dest = location(dest.TrimEnd()); string destDir = Path.GetDirectoryName(dest); string destFile = Path.GetFileName(dest); src = location(src); string[] input = new[] { src }.ExtractFiles(); #if DEBUG Log.Trace("Found files to copy `{0}`", String.Join(", ", input)); #endif if(except != null) { string path = Path.GetDirectoryName(src); input = input.Except(except .Where(f => !String.IsNullOrWhiteSpace((string)f.data)) .Select(f => location((string)f.data, path)) .ToArray() .ExtractFiles() ).ToArray(); } if(input.Length < 1) { throw new InvalidArgumentException("The input files was not found. Check your mask and the exception list if used."); } copyFile(destDir, destFile, overwrite, input); return Value.Empty; }
protected string copyFile(IPM pm, Argument[] files, string dest, bool overwrite, Argument[] except = null) { dest = dest.PathFormat(); foreach(Argument src in files) { if(src.type != ArgumentType.StringDouble) { throw new InvalidArgumentException("Incorrect data from input files. Define as {\"f1\", \"f2\", ...}"); } copyFile(pm, src.data.ToString(), dest, overwrite, except); } return Value.Empty; }