/// Get values of selected fields and properties in the list public static IEnumerable <Vars> ListToVars(IEnumerable o, IStringFilter filter) { if (o != null) { foreach (var v in o) { yield return(PropsToVars(v, filter)); } } }
public scanDirParams(ZipOutputStream zip, string sourceDirectory, ZipEntryFactory entryFactory, ProgressHandler progress, IStringFilter nf, IStringFilter df) { Zip = zip; EntryFactory = entryFactory; Progress = progress; NameFilter = nf; DirFilter = df; DirEntries = new Dictionary <string, ZipFSEntry>(StringComparer.InvariantCultureIgnoreCase); SourceDirectory = Utils.BackslashAdd(new DirectoryInfo(sourceDirectory).FullName); Buffer = new byte[16384]; }
/// Get values of all public object fields and properties public static Vars PropsToVars(object o, IStringFilter filter) { Vars v = new Vars(); if (o != null) { Type t = o.GetType(); foreach (var p in t.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (filter != null && !filter.IsMatch(p.Name)) { continue; } var ip = p.GetIndexParameters(); if (ip != null && ip.Length != 0) { continue; } if (!p.CanRead) { continue; } try { v[p.Name] = p.GetValue(o, null); } catch { } } foreach (var f in t.GetFields(BindingFlags.Instance | BindingFlags.Public)) { if (filter != null && !filter.IsMatch(f.Name)) { continue; } try { v[f.Name] = f.GetValue(o); } catch { } } } return(v); }
private IEnumerable<string> FilterNumbers(IEnumerable<string> splitedString) { var filters = new IStringFilter[] {new NegativeStringFilter(), new MoreThanThreeFilter(), }; foreach (var tmp in splitedString) { var passedAllFilters = filters.All(x => !x.Filter(tmp)); if (passedAllFilters) { yield return tmp; } } foreach (var stringFilter in filters) { stringFilter.Complete(); } }
private object createZip(Stream fileStream, string sourceDirectory, IStringFilter nf, IStringFilter df) { ZipEntryFactory entryFactory; switch (ZipTime) { default: case ZipTime.Now: entryFactory = new ZipEntryFactory(DateTime.Now); break; case ZipTime.UtcNow: entryFactory = new ZipEntryFactory(DateTime.UtcNow); break; case ZipTime.FileTime: entryFactory = new ZipEntryFactory(ZipEntryFactory.TimeSetting.LastWriteTime); break; case ZipTime.UtcFileTime: entryFactory = new ZipEntryFactory(ZipEntryFactory.TimeSetting.LastWriteTimeUtc); break; } entryFactory.NameTransform = new ZipNameTransform(sourceDirectory); entryFactory.IsUnicodeText = Unicode; ProgressHandler progress = delegate(object x, ProgressEventArgs y) { Context.OnProgress(1, y.Name); }; using (ZipOutputStream zip = new ZipOutputStream(fileStream)) { if (Comment != null) { zip.SetComment(Context.TransformStr(Password, Transform)); } if (Password != null) { zip.Password = Context.TransformStr(Password, Transform); } zip.SetLevel(Level); return(scanDir(sourceDirectory, new scanDirParams(zip, sourceDirectory, entryFactory, progress, nf, df))); } }
/// Dump variables matching the filter to a string for debug purposes public string ToDumpAll(IStringFilter filter) { var np = new List <Var>(this); np.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase)); var sb = new StringBuilder(); int maxlen = 0; foreach (var pair in np) { maxlen = Math.Max(pair.Name.Length, maxlen); } foreach (var pair in np) { if (filter != null && !filter.IsMatch(pair.Name)) { continue; } Dump od = new Dump(pair.Value, ((pair.Value == null) ? typeof(object) : pair.Value.GetType()), pair.Name.PadRight(maxlen + 1, ' '), 7, s_dumpAllSettings); sb.AppendLine(od.ToString().Trim()); } return(sb.ToString().TrimEnd()); }
/// Get values of selected fields and properties in the list public static IEnumerable<Vars> ListToVars(IEnumerable o, IStringFilter filter) { if (o != null) foreach (var v in o) yield return PropsToVars(v, filter); }
internal static void SaveToCsv <T>(IEnumerable <T> source, string fileName) { #region My Secret Logic if (File.Exists(fileName)) { File.Delete(fileName); } Type containedType = typeof(T); PropertyInfo[] properties = containedType.GetProperties(); using (StreamWriter writer = new StreamWriter(fileName, false)) { // Header foreach (PropertyInfo pi in properties) { string writeThis; if (pi.IsDefined(typeof(CsvNameAttribute), true)) { CsvNameAttribute csvName = (CsvNameAttribute)pi.GetCustomAttributes(typeof(CsvNameAttribute), true)[0]; writeThis = csvName.Name + ";"; } else { // Default: Ueberschrift = Feldname writeThis = pi.Name + ";"; } writer.Write(writeThis); Console.Write(writeThis); } writer.WriteLine(); Console.WriteLine(); // Content foreach (T elem in source) { foreach (PropertyInfo pi in properties) { string writeThis; if (pi.IsDefined(typeof(IStringFilter), true)) { IStringFilter flt = (IStringFilter)pi.GetCustomAttributes(typeof(IStringFilter), true)[0]; writeThis = flt.Filter(pi.GetValue(elem).ToString()) + ";"; } else { writeThis = pi.GetValue(elem) + ";"; } writer.Write(writeThis); Console.Write(writeThis); } writer.WriteLine(); Console.WriteLine(); } writer.Flush(); writer.Close(); } #endregion }
object extractZip(object zipFileName, string rootDirectory, IStringFilter nf, IStringFilter df) { object ret = null; WindowsNameTransform extractNameTransform = new WindowsNameTransform(rootDirectory); Dictionary <string, bool> dirs = new Dictionary <string, bool>(StringComparer.InvariantCultureIgnoreCase); Stream str; if (zipFileName is byte[]) { str = new MemoryStream((byte[])zipFileName); } else { str = new SeekableStream(Context.OpenStream(zipFileName.ToString()), true); } using (ZipFile zip = new ZipFile(str)) { if (Password != null) { zip.Password = Context.TransformStr(Password, Transform); } foreach (ZipEntry entry in zip) { string targetName = null; if (entry.IsFile) { targetName = extractNameTransform.TransformFile(entry.Name); if (!UsePath) { targetName = Path.Combine(rootDirectory, Path.GetFileName(targetName)); } } else if (entry.IsDirectory) { if (UsePath) { targetName = extractNameTransform.TransformDirectory(entry.Name); } else { targetName = rootDirectory; } } if (string.IsNullOrEmpty(targetName)) { continue; } if (!Hidden) { if (isDos(entry) && (((FileAttributes)entry.ExternalFileAttributes) & (FileAttributes.Hidden)) != 0) { continue; } } if (string.IsNullOrEmpty(entry.Name)) { continue; } var n = new ZipFSEntry(entry, ZipTime); if ((entry.IsFile && df.IsMatch(Path.GetDirectoryName(n.FullName)) && nf.IsMatch(n.Name)) || (entry.IsDirectory && df.IsMatch(n.FullName))) { object r = extract(zip, rootDirectory, targetName, entry, dirs); if (r != null) { return(r); } } } } return(ret); }
static WebAppConfig() { string appClassName = ConfigurationManager.AppSettings["WebAppClassName"]; _version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); _templateCacheDuration = Convert.ToInt32(ConfigurationManager.AppSettings["TemplateCacheDuration"]); _templateFileEncoding = string.IsNullOrEmpty(ConfigurationManager.AppSettings["TemplateFileEncoding"]) ? Encoding.Default : Encoding.GetEncoding(ConfigurationManager.AppSettings["TemplateFileEncoding"]); bool.TryParse(ConfigurationManager.AppSettings["EnabledHttpCompress"], out _enabledHttpCompress); string enabledPermission = ConfigurationManager.AppSettings["EnabledPermission"]; string defaultLanguageCode = ConfigurationManager.AppSettings["DefaultLanguageCode"]; if (defaultLanguageCode != null && defaultLanguageCode.Length >= 2) { _defaultLanguageCode = defaultLanguageCode; } _extension = ConfigurationManager.AppSettings["Extension"] ?? string.Empty; _appKey = ConfigurationManager.AppSettings["AppKey"]; _appSecret = ConfigurationManager.AppSettings["AppSecret"]; _oAuthUri = ConfigurationManager.AppSettings["OAuthUri"]; //if (string.IsNullOrEmpty(_appKey)) // throw new CException("AppKey not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_appSecret)) // throw new CException("AppSecret not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_oAuthUri)) // throw new CException("OAuthUri not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_publicKey)) // throw new CException("PublicKey not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_privateKey)) // throw new CException("PrivateKey not found, please check the Web.config's configuration/appSettings."); if (!string.IsNullOrEmpty(enabledPermission)) bool.TryParse(enabledPermission, out _enabledPermission); if (string.IsNullOrEmpty(appClassName)) throw new CException("WebAppClassName not found, please check the Web.config's configuration/appSettings."); Type appType = Type.GetType(appClassName, false); if (appType == null) throw new CException("Can not load the type of WebAppClassName '" + appClassName + "'"); MethodInfo initMethod = appType.GetMethod("Init", new Type[0]); if (initMethod == null || !initMethod.IsStatic) throw new CException("Can not found the static Init method of " + appType.FullName); RegisterAssembly(appType.Assembly); initMethod.Invoke(null, null); if (_sessionType == null) throw new CException("WebAppConfig.SessionType is null."); if (_securityType == null) throw new CException("WebAppConfig.SecurityType is null."); LoadControllerClasses(); if (_timeProvider == null) { _timeProvider = new RealTimeProvider(); } if (_deserializeProvider == null) { _deserializeProvider = new JSONDeserializer(); } if (_serializeProvider == null) { _serializeProvider = new JSONSerializer(); } if (_securityProvider == null) { _securityProvider = MiniSecurity.CreateInstance(); } if (_stringFilterProvider == null) { _stringFilterProvider = new MFilter(); } }
private object downloadSingleFile(IStringFilter nf, Uri single, FileInfo toFile) { var from = new UriFileInfo(single); var ff = from.Name; if (string.IsNullOrEmpty(ff)) { ff = toFile.Name; } if (nf != null && (!nf.IsMatch(ff))) { VerboseMessage("{0} did not pass filter", single); return(null); } var to = new FileOrDirectoryInfo(toFile); bool skip = false; object ret = ProcessPrepare(from, to, delegate { if (toFile.Directory != null && !toFile.Directory.Exists) { bool created; object r = createDir(new DirectoryInfo(Path.GetTempPath()), toFile.Directory, out created); if (!created || r != null) { return(r); } } bool overwrite = (Overwrite == OverwriteMode.Always); if (Overwrite == OverwriteMode.IfNewer) { if (toFile.Exists && toFile.LastWriteTimeUtc >= from.LastWriteTimeUtc) { VerboseMessage("Ignoring never file {0} ", toFile.FullName); return(null); } overwrite = true; } skip = (toFile.Exists && !overwrite); return(null); }); if (ret != null) { return(ret); } if (skip && Overwrite != OverwriteMode.Confirm) { VerboseMessage("Ignoring existing file {0} ", toFile.FullName); return(null); } ret = ProcessComplete(from, to, skip, delegate(bool skip1) { if (!skip1) { // Continue with copy if (toFile.Directory != null && !toFile.Directory.Exists) { toFile.Directory.Create(); } VerboseMessage("Downloading {0} => {1}", from.FullName, toFile.FullName); Download dn = new Download { From = single.OriginalString, To = toFile.FullName, Transform = TransformRules.None }; return(Context.Execute(dn)); } return(null); }); return(ret); }
private static List <Var> getMethodsHelp(Type type, IStringFilter sf) { var v = new List <Var>(); for (int stat = 0; stat < 2; ++stat) { List <MethodInfo> mi = new List <MethodInfo>(); var flags = BindingFlags.Public | BindingFlags.InvokeMethod | (stat == 0 ? BindingFlags.Instance : BindingFlags.Static); foreach (var p in type.GetMethods(flags)) { if (p.Name.StartsWith("get_", StringComparison.OrdinalIgnoreCase) || p.Name.StartsWith("set_", StringComparison.OrdinalIgnoreCase) || p.Name.StartsWith("CreateObjRef", StringComparison.OrdinalIgnoreCase)) { continue; } if (sf != null && !sf.IsMatch(p.Name)) { continue; } mi.Add(p); } mi.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); foreach (var info in mi) { StringBuilder sb1 = new StringBuilder(), sb = new StringBuilder(); sb1.Append(" "); if (info.IsStatic) { sb1.Append("static "); } if (info.IsAbstract) { sb1.Append("abstract "); } sb1.Append(Dump.GetFriendlyTypeName(info.ReturnType)); sb.Append(info.Name); var ge = info.GetGenericArguments(); if (ge != null && ge.Length > 0) { sb.Append("<"); for (int i = 0; i < ge.Length; ++i) { sb.Append(Dump.GetFriendlyTypeName(ge[i])); if (i != ge.Length - 1) { sb.Append(", "); } } sb.Append(">"); } sb.Append("("); bool first = true; foreach (var parameter in info.GetParameters()) { if (!first) { sb.Append(", "); } first = false; if (parameter.IsOut) { sb.Append("out "); } if (parameter.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0) { sb.Append("params "); } sb.Append(Dump.GetFriendlyTypeName(parameter.ParameterType)); sb.Append(" "); sb.Append(parameter.Name); } sb.Append(")"); v.Add(new Var(sb1.ToString(), sb.ToString())); } } return(v); }
private static bool writeTypeHelp(ScriptContext context, Type s, IStringFilter sf, int width) { bool hdr = false; List <Var> v = null; if (sf == null) { v = getConstructorsHelp(s); if (v.Count > 0) { if (!hdr) { writeHeader(context, s); } hdr = true; context.Bold.WriteLine("Public constructors:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } } v = getPropertiesHelp(s, sf, true); if (v.Count > 0) { if (!hdr) { writeHeader(context, s); } hdr = true; context.Bold.WriteLine("Public properties:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } v = getPropertiesHelp(s, sf, false); if (v.Count > 0) { if (!hdr) { writeHeader(context, s); } hdr = true; if (s.IsEnum) { context.Bold.WriteLine("Enum values:"); } else { context.Bold.WriteLine("Public fields:"); } context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } v = getMethodsHelp(s, sf); if (v.Count > 0) { if (!hdr) { writeHeader(context, s); } hdr = true; context.Bold.WriteLine("Public members:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } return(hdr); }
private object copySingleFile(IStringFilter nf, FileInfo f, FileInfo toFile) { if (nf != null && (!nf.IsMatch(f.FullName) || !CheckHidden(f))) { VerboseMessage("{0} did not pass filter", f.FullName); return null; } var from=new FileOrDirectoryInfo(f); var to = new FileOrDirectoryInfo(toFile); bool skip = false; object ret = ProcessPrepare(from, to, delegate { if (toFile.Directory != null && !toFile.Directory.Exists) { bool created; object r = createDir(f.Directory, toFile.Directory, out created); if (!created || r != null) return r; } bool overwrite = (Overwrite == OverwriteMode.Always); if (Overwrite == OverwriteMode.IfNewer) { if (toFile.Exists && toFile.LastWriteTimeUtc >= f.LastWriteTimeUtc) { VerboseMessage("Ignoring never file {0} ", toFile.FullName); return null; } overwrite = true; } skip = (toFile.Exists && !overwrite); return null; }); if (ret!=null) return ret; if (skip && Overwrite != OverwriteMode.Confirm) { VerboseMessage("Ignoring existing file {0} ", toFile.FullName); return null; } ret = ProcessComplete(from,to, skip, delegate(bool skip1) { if (!skip1) { // Continue with copy if (toFile.Directory != null && !toFile.Directory.Exists) toFile.Directory.Create(); if (toFile.Exists && (toFile.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System)) != 0) toFile.Attributes &= ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); if (Move) { if (toFile.FullName != f.FullName) toFile.Delete(); VerboseMessage("Move {0} => {1}", f.FullName, toFile.FullName); Context.MoveFile(f.FullName, toFile.FullName, true); } else { VerboseMessage("Copy {0} => {1}", f.FullName, toFile.FullName); Context.CopyFile(f.FullName, toFile.FullName, true); } } return null; }); return ret; }
private object copy(DirectoryInfo rootFrom, DirectoryInfo fromDir, DirectoryInfo toDir,IStringFilter nf, IStringFilter df) { bool isRoot = (rootFrom == fromDir); bool isVisible = (isRoot || CheckHidden(fromDir)); bool processFiles = isVisible; if (processFiles && (df != null && !df.IsMatch(fromDir.FullName))) { processFiles = false; VerboseMessage("{0} did not pass directory filter", fromDir.FullName); } var from = new FileOrDirectoryInfo(fromDir); var to = new FileOrDirectoryInfo(toDir); return ProcessPrepare(from, to, delegate { if (processFiles) { if (EmptyDirectories && !to.Exists) { bool created; object r = createDir(fromDir, toDir, out created); if (r != null || !created) return r; } foreach (FileInfo f in fromDir.GetFiles()) { FileInfo toFile = new FileInfo(Path.Combine(to.FullName, f.Name)); object r = copySingleFile(nf, f, toFile); if (r != null) return r; } } if (Recursive) { foreach (DirectoryInfo d in fromDir.GetDirectories()) { object r = copy(rootFrom, d, new DirectoryInfo(Path.Combine(to.FullName, d.Name)), nf, df); if (r != null) return r; } } return null; }); }
public scanDirParams(ZipOutputStream zip, string sourceDirectory, ZipEntryFactory entryFactory, ProgressHandler progress, IStringFilter nf, IStringFilter df) { Zip = zip; EntryFactory = entryFactory; Progress = progress; NameFilter = nf; DirFilter = df; DirEntries = new Dictionary<string, ZipFSEntry>(StringComparer.InvariantCultureIgnoreCase); SourceDirectory = Utils.BackslashAdd(new DirectoryInfo(sourceDirectory).FullName); Buffer = new byte[16384]; }
private object createZip(Stream fileStream, string sourceDirectory, IStringFilter nf, IStringFilter df) { ZipEntryFactory entryFactory; switch (ZipTime) { default: case ZipTime.Now: entryFactory = new ZipEntryFactory(DateTime.Now); break; case ZipTime.UtcNow: entryFactory = new ZipEntryFactory(DateTime.UtcNow); break; case ZipTime.FileTime: entryFactory = new ZipEntryFactory(ZipEntryFactory.TimeSetting.LastWriteTime); break; case ZipTime.UtcFileTime: entryFactory = new ZipEntryFactory(ZipEntryFactory.TimeSetting.LastWriteTimeUtc); break; } entryFactory.NameTransform = new ZipNameTransform(sourceDirectory); entryFactory.IsUnicodeText = Unicode; ProgressHandler progress = delegate(object x, ProgressEventArgs y) { Context.OnProgress(1, y.Name); }; using (ZipOutputStream zip = new ZipOutputStream(fileStream)) { if (Comment!=null) zip.SetComment(Context.TransformStr(Password, Transform)); if (Password != null) zip.Password = Context.TransformStr(Password, Transform); zip.SetLevel(Level); return scanDir(sourceDirectory, new scanDirParams(zip, sourceDirectory, entryFactory, progress, nf, df)); } }
static WebAppConfig() { string appClassName = ConfigurationManager.AppSettings["WebAppClassName"]; _version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); _templateCacheDuration = Convert.ToInt32(ConfigurationManager.AppSettings["TemplateCacheDuration"]); _templateFileEncoding = string.IsNullOrEmpty(ConfigurationManager.AppSettings["TemplateFileEncoding"]) ? Encoding.Default : Encoding.GetEncoding(ConfigurationManager.AppSettings["TemplateFileEncoding"]); bool.TryParse(ConfigurationManager.AppSettings["EnabledHttpCompress"], out _enabledHttpCompress); string enabledPermission = ConfigurationManager.AppSettings["EnabledPermission"]; string defaultLanguageCode = ConfigurationManager.AppSettings["DefaultLanguageCode"]; if (defaultLanguageCode != null && defaultLanguageCode.Length >= 2) { _defaultLanguageCode = defaultLanguageCode; } _extension = ConfigurationManager.AppSettings["Extension"] ?? string.Empty; _appKey = ConfigurationManager.AppSettings["AppKey"]; _appSecret = ConfigurationManager.AppSettings["AppSecret"]; _oAuthUri = ConfigurationManager.AppSettings["OAuthUri"]; //if (string.IsNullOrEmpty(_appKey)) // throw new CException("AppKey not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_appSecret)) // throw new CException("AppSecret not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_oAuthUri)) // throw new CException("OAuthUri not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_publicKey)) // throw new CException("PublicKey not found, please check the Web.config's configuration/appSettings."); //if (string.IsNullOrEmpty(_privateKey)) // throw new CException("PrivateKey not found, please check the Web.config's configuration/appSettings."); if (!string.IsNullOrEmpty(enabledPermission)) { bool.TryParse(enabledPermission, out _enabledPermission); } if (string.IsNullOrEmpty(appClassName)) { throw new CException("WebAppClassName not found, please check the Web.config's configuration/appSettings."); } Type appType = Type.GetType(appClassName, false); if (appType == null) { throw new CException("Can not load the type of WebAppClassName '" + appClassName + "'"); } MethodInfo initMethod = appType.GetMethod("Init", new Type[0]); if (initMethod == null || !initMethod.IsStatic) { throw new CException("Can not found the static Init method of " + appType.FullName); } RegisterAssembly(appType.Assembly); initMethod.Invoke(null, null); if (_sessionType == null) { throw new CException("WebAppConfig.SessionType is null."); } if (_securityType == null) { throw new CException("WebAppConfig.SecurityType is null."); } LoadControllerClasses(); if (_timeProvider == null) { _timeProvider = new RealTimeProvider(); } if (_deserializeProvider == null) { _deserializeProvider = new JSONDeserializer(); } if (_serializeProvider == null) { _serializeProvider = new JSONSerializer(); } if (_securityProvider == null) { _securityProvider = MiniSecurity.CreateInstance(); } if (_stringFilterProvider == null) { _stringFilterProvider = new MFilter(); } }
/// Get values of all public object fields and properties public static Vars PropsToVars(object o, IStringFilter filter) { Vars v = new Vars(); if (o != null) { Type t = o.GetType(); foreach (var p in t.GetProperties(BindingFlags.Instance|BindingFlags.Public)) { if (filter!=null && !filter.IsMatch(p.Name)) continue; var ip=p.GetIndexParameters(); if (ip != null && ip.Length != 0) continue; if (!p.CanRead) continue; try { v[p.Name] = p.GetValue(o, null); } catch { } } foreach (var f in t.GetFields(BindingFlags.Instance | BindingFlags.Public)) { if (filter != null && !filter.IsMatch(f.Name)) continue; try { v[f.Name] = f.GetValue(o); } catch { } } } return v; }
private object downloadSingleFile(IStringFilter nf, Uri single, FileInfo toFile) { var from = new UriFileInfo(single); var ff = from.Name; if (string.IsNullOrEmpty(ff)) ff = toFile.Name; if (nf != null && (!nf.IsMatch(ff))) { VerboseMessage("{0} did not pass filter", single); return null; } var to = new FileOrDirectoryInfo(toFile); bool skip = false; object ret = ProcessPrepare(from, to, delegate { if (toFile.Directory != null && !toFile.Directory.Exists) { bool created; object r = createDir(new DirectoryInfo(Path.GetTempPath()), toFile.Directory, out created); if (!created || r != null) return r; } bool overwrite = (Overwrite == OverwriteMode.Always); if (Overwrite == OverwriteMode.IfNewer) { if (toFile.Exists && toFile.LastWriteTimeUtc >= from.LastWriteTimeUtc) { VerboseMessage("Ignoring never file {0} ", toFile.FullName); return null; } overwrite = true; } skip = (toFile.Exists && !overwrite); return null; }); if (ret != null) return ret; if (skip && Overwrite != OverwriteMode.Confirm) { VerboseMessage("Ignoring existing file {0} ", toFile.FullName); return null; } ret = ProcessComplete(from, to, skip, delegate(bool skip1) { if (!skip1) { // Continue with copy if (toFile.Directory != null && !toFile.Directory.Exists) toFile.Directory.Create(); VerboseMessage("Downloading {0} => {1}", from.FullName, toFile.FullName); Download dn = new Download { From = single.OriginalString, To = toFile.FullName, Transform = TransformRules.None }; return Context.Execute(dn); } return null; }); return ret; }
private static List<Var> getMethodsHelp(Type type, IStringFilter sf) { var v = new List<Var>(); for (int stat = 0; stat < 2; ++stat) { List<MethodInfo> mi = new List<MethodInfo>(); var flags = BindingFlags.Public | BindingFlags.InvokeMethod | (stat == 0 ? BindingFlags.Instance : BindingFlags.Static); foreach (var p in type.GetMethods(flags)) { if (p.Name.StartsWith("get_", StringComparison.OrdinalIgnoreCase) || p.Name.StartsWith("set_", StringComparison.OrdinalIgnoreCase) || p.Name.StartsWith("CreateObjRef",StringComparison.OrdinalIgnoreCase)) continue; if (sf != null && !sf.IsMatch(p.Name)) continue; mi.Add(p); } mi.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); foreach (var info in mi) { StringBuilder sb1 = new StringBuilder(), sb = new StringBuilder(); sb1.Append(" "); if (info.IsStatic) sb1.Append("static "); if (info.IsAbstract) sb1.Append("abstract "); sb1.Append(Dump.GetFriendlyTypeName(info.ReturnType)); sb.Append(info.Name); var ge = info.GetGenericArguments(); if (ge!=null && ge.Length>0) { sb.Append("<"); for (int i = 0; i < ge.Length; ++i) { sb.Append(Dump.GetFriendlyTypeName(ge[i])); if (i != ge.Length - 1) sb.Append(", "); } sb.Append(">"); } sb.Append("("); bool first = true; foreach (var parameter in info.GetParameters()) { if (!first) sb.Append(", "); first = false; if (parameter.IsOut) sb.Append("out "); if (parameter.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0) sb.Append("params "); sb.Append(Dump.GetFriendlyTypeName(parameter.ParameterType)); sb.Append(" "); sb.Append(parameter.Name); } sb.Append(")"); v.Add(new Var(sb1.ToString(), sb.ToString())); } } return v; }
private static bool writeTypeHelp(ScriptContext context, Type s, IStringFilter sf, int width) { bool hdr = false; List<Var> v = null; if (sf == null) { v = getConstructorsHelp(s); if (v.Count > 0) { if (!hdr) writeHeader(context, s); hdr = true; context.Bold.WriteLine("Public constructors:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } } v = getPropertiesHelp(s, sf, true); if (v.Count > 0) { if (!hdr) writeHeader(context, s); hdr = true; context.Bold.WriteLine("Public properties:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } v = getPropertiesHelp(s, sf, false); if (v.Count > 0) { if (!hdr) writeHeader(context, s); hdr = true; if (s.IsEnum) context.Bold.WriteLine("Enum values:"); else context.Bold.WriteLine("Public fields:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } v = getMethodsHelp(s,sf); if (v.Count > 0) { if (!hdr) writeHeader(context, s); hdr = true; context.Bold.WriteLine("Public members:"); context.Write(Utils.WrapTwoColumns(v, 40, width)); context.WriteLine(); } return hdr; }
private static List <Var> getPropertiesHelp(Type type, IStringFilter sf, bool isProperties) { var v = new List <Var>(); for (int stat = 0; stat < 2; ++stat) { if (type.IsEnum && stat == 0) { continue; } List <MemberInfo> mi = new List <MemberInfo>(); if (isProperties) { BindingFlags bf = BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty; bf |= ((stat == 1) ? BindingFlags.Static : BindingFlags.Instance); foreach (var p in type.GetProperties(bf)) { mi.Add(p); } } else { BindingFlags bf = BindingFlags.Public | BindingFlags.GetField | BindingFlags.SetProperty; bf |= ((stat == 1) ? BindingFlags.Static : BindingFlags.Instance); foreach (var p in type.GetFields(bf)) { mi.Add(p); } } if (!type.IsEnum) { mi.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); } foreach (var info in mi) { if (sf != null && !sf.IsMatch(info.Name)) { continue; } var pinfo = info as PropertyInfo; var finfo = info as FieldInfo; StringBuilder sb1 = new StringBuilder(), sb = new StringBuilder(); sb1.Append(" "); if (!type.IsEnum) { if (stat == 1) { sb1.Append("static "); } sb1.Append(Dump.GetFriendlyTypeName((finfo != null) ? finfo.FieldType : pinfo.PropertyType)); sb.Append(info.Name); } else { sb1.Append(finfo.Name); sb.AppendFormat("0x{0:x10} ", Convert.ToUInt64(finfo.GetValue(null))); var dd = (DescriptionAttribute[])finfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if (dd != null && dd.Length > 0) { sb.Append(dd[0].Description.Trim()); } } bool first = true; if (pinfo != null) { foreach (var parameter in pinfo.GetIndexParameters()) { if (!first) { sb.Append(", "); } else { sb.Append("["); } first = false; if (parameter.IsOut) { sb.Append("out "); } if (parameter.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0) { sb.Append("params "); } sb.Append(Dump.GetFriendlyTypeName(parameter.ParameterType)); sb.Append(" "); sb.Append(parameter.Name); } if (!first) { sb.Append("]"); } bool gt = pinfo.GetGetMethod() != null; bool st = pinfo.GetSetMethod() != null; if (gt && st) { sb.Append(" { get; set;}"); } else if (!gt && st) { sb.Append(" { set; }"); } else if (gt && !st) { sb.Append(" { get; }"); } } v.Add(new Var(sb1.ToString(), sb.ToString())); } } return(v); }
private static List<Var> getPropertiesHelp(Type type, IStringFilter sf, bool isProperties) { var v = new List<Var>(); for (int stat = 0; stat < 2; ++stat) { if (type.IsEnum && stat==0) continue; List<MemberInfo> mi = new List<MemberInfo>(); if (isProperties) { BindingFlags bf = BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.SetProperty; bf |= ((stat == 1) ? BindingFlags.Static : BindingFlags.Instance); foreach (var p in type.GetProperties(bf)) mi.Add(p); } else { BindingFlags bf = BindingFlags.Public | BindingFlags.GetField | BindingFlags.SetProperty; bf |= ((stat == 1) ? BindingFlags.Static : BindingFlags.Instance); foreach (var p in type.GetFields(bf)) mi.Add(p); } if (!type.IsEnum) mi.Sort((a, b) => string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase)); foreach (var info in mi) { if (sf != null && !sf.IsMatch(info.Name)) continue; var pinfo = info as PropertyInfo; var finfo = info as FieldInfo; StringBuilder sb1 = new StringBuilder(), sb = new StringBuilder(); sb1.Append(" "); if (!type.IsEnum) { if (stat == 1) sb1.Append("static "); sb1.Append(Dump.GetFriendlyTypeName((finfo != null) ? finfo.FieldType : pinfo.PropertyType)); sb.Append(info.Name); } else { sb1.Append(finfo.Name); sb.AppendFormat("0x{0:x10} ",Convert.ToUInt64( finfo.GetValue(null))); var dd = (DescriptionAttribute[])finfo.GetCustomAttributes(typeof(DescriptionAttribute),true); if (dd != null && dd.Length>0) sb.Append(dd[0].Description.Trim()); } bool first = true; if (pinfo != null) { foreach (var parameter in pinfo.GetIndexParameters()) { if (!first) sb.Append(", "); else sb.Append("["); first = false; if (parameter.IsOut) sb.Append("out "); if (parameter.GetCustomAttributes(typeof (ParamArrayAttribute), false).Length > 0) sb.Append("params "); sb.Append(Dump.GetFriendlyTypeName(parameter.ParameterType)); sb.Append(" "); sb.Append(parameter.Name); } if (!first) sb.Append("]"); bool gt = pinfo.GetGetMethod() != null; bool st = pinfo.GetSetMethod() != null; if (gt && st) sb.Append(" { get; set;}"); else if (!gt && st) sb.Append(" { set; }"); else if (gt && !st) sb.Append(" { get; }"); } v.Add(new Var(sb1.ToString(), sb.ToString())); } } return v; }
private object copy(DirectoryInfo rootFrom, DirectoryInfo fromDir, DirectoryInfo toDir, IStringFilter nf, IStringFilter df) { bool isRoot = (rootFrom == fromDir); bool isVisible = (isRoot || CheckHidden(fromDir)); bool processFiles = isVisible; if (processFiles && (df != null && !df.IsMatch(fromDir.FullName))) { processFiles = false; VerboseMessage("{0} did not pass directory filter", fromDir.FullName); } var from = new FileOrDirectoryInfo(fromDir); var to = new FileOrDirectoryInfo(toDir); return(ProcessPrepare(from, to, delegate { if (processFiles) { if (EmptyDirectories && !to.Exists) { bool created; object r = createDir(fromDir, toDir, out created); if (r != null || !created) { return r; } } foreach (FileInfo f in fromDir.GetFiles()) { FileInfo toFile = new FileInfo(Path.Combine(to.FullName, f.Name)); object r = copySingleFile(nf, f, toFile); if (r != null) { return r; } } } if (Recursive) { foreach (DirectoryInfo d in fromDir.GetDirectories()) { object r = copy(rootFrom, d, new DirectoryInfo(Path.Combine(to.FullName, d.Name)), nf, df); if (r != null) { return r; } } } return null; })); }
public StringFilterBuilder Add(IStringFilter filter) { _filters.Add(filter); return(this); }
private object copySingleFile(IStringFilter nf, FileInfo f, FileInfo toFile) { if (nf != null && (!nf.IsMatch(f.FullName) || !CheckHidden(f))) { VerboseMessage("{0} did not pass filter", f.FullName); return(null); } var from = new FileOrDirectoryInfo(f); var to = new FileOrDirectoryInfo(toFile); bool skip = false; object ret = ProcessPrepare(from, to, delegate { if (toFile.Directory != null && !toFile.Directory.Exists) { bool created; object r = createDir(f.Directory, toFile.Directory, out created); if (!created || r != null) { return(r); } } bool overwrite = (Overwrite == OverwriteMode.Always); if (Overwrite == OverwriteMode.IfNewer) { if (toFile.Exists && toFile.LastWriteTimeUtc >= f.LastWriteTimeUtc) { VerboseMessage("Ignoring never file {0} ", toFile.FullName); return(null); } overwrite = true; } skip = (toFile.Exists && !overwrite); return(null); }); if (ret != null) { return(ret); } if (skip && Overwrite != OverwriteMode.Confirm) { VerboseMessage("Ignoring existing file {0} ", toFile.FullName); return(null); } ret = ProcessComplete(from, to, skip, delegate(bool skip1) { if (!skip1) { // Continue with copy if (toFile.Directory != null && !toFile.Directory.Exists) { toFile.Directory.Create(); } if (toFile.Exists && (toFile.Attributes & (FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System)) != 0) { toFile.Attributes &= ~(FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System); } if (Move) { if (toFile.FullName != f.FullName) { toFile.Delete(); } VerboseMessage("Move {0} => {1}", f.FullName, toFile.FullName); Context.MoveFile(f.FullName, toFile.FullName, true); } else { VerboseMessage("Copy {0} => {1}", f.FullName, toFile.FullName); Context.CopyFile(f.FullName, toFile.FullName, true); } } return(null); }); return(ret); }
object extractZip( object zipFileName, string rootDirectory,IStringFilter nf, IStringFilter df) { object ret = null; WindowsNameTransform extractNameTransform = new WindowsNameTransform(rootDirectory); Dictionary<string, bool> dirs = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase); Stream str; if (zipFileName is byte[]) str = new MemoryStream((byte[])zipFileName); else str=new SeekableStream(Context.OpenStream(zipFileName.ToString()),true); using (ZipFile zip = new ZipFile(str)) { if (Password != null) zip.Password = Context.TransformStr(Password, Transform); foreach (ZipEntry entry in zip) { string targetName = null; if (entry.IsFile) { targetName = extractNameTransform.TransformFile(entry.Name); if (!UsePath) targetName = Path.Combine(rootDirectory, Path.GetFileName(targetName)); } else if (entry.IsDirectory) { if (UsePath) targetName = extractNameTransform.TransformDirectory(entry.Name); else targetName = rootDirectory; } if (string.IsNullOrEmpty(targetName)) continue; if (!Hidden) { if (isDos(entry) && (((FileAttributes) entry.ExternalFileAttributes) & (FileAttributes.Hidden)) != 0) continue; } if (string.IsNullOrEmpty(entry.Name)) continue; var n = new ZipFSEntry(entry, ZipTime); if ((entry.IsFile && df.IsMatch(Path.GetDirectoryName(n.FullName)) && nf.IsMatch(n.Name)) || (entry.IsDirectory && df.IsMatch(n.FullName))) { object r = extract(zip, rootDirectory, targetName, entry, dirs); if (r!=null) return r; } } } return ret; }