public static bool Read([NotNull] this WebResponse thisValue, [NotNull] IOSettings settings) { IIOOnRead ioOnRead = settings as IIOOnRead ?? throw new ArgumentException("Argument must provide an OnRead implementation.", nameof(settings)); Stream stream = null; StreamReader reader = null; try { stream = GetStream(thisValue); if (stream == null) { return(false); } reader = new StreamReader(stream, settings.Encoding, true); int length; char[] chars = new char[settings.BufferSize]; do { length = reader.Read(chars); }while (length > 0 && ioOnRead.OnRead(chars, length)); return(true); } catch (Exception ex) when(settings.OnError != null) { settings.OnError(ex); return(false); } finally { ObjectHelper.Dispose(ref reader); ObjectHelper.Dispose(ref stream); } }
public static string ReadToEnd([NotNull] this WebResponse thisValue, IOSettings settings = null) { settings ??= new IOSettings(); Stream stream = null; StreamReader reader = null; string result; try { stream = GetStream(thisValue); if (stream == null) { return(null); } reader = new StreamReader(stream, settings.Encoding, true, settings.BufferSize); result = reader.ReadToEnd(); } catch (Exception ex) when(settings.OnError != null) { result = null; settings.OnError(ex); } finally { ObjectHelper.Dispose(ref reader); ObjectHelper.Dispose(ref stream); } return(result); }
/// <inheritdoc /> public IOHttpDownloadFileWebRequestSettings(IOSettings settings) : base(settings) { if (settings is not IOHttpDownloadFileWebRequestSettings ioHttpDownloadFileWebRequestSettings) { return; } Overwrite = ioHttpDownloadFileWebRequestSettings.Overwrite; }
private void InitIOSettings() { useRebondTool = IOSettings.Add(new BooleanIOSetting("UseRebondTool", Importance.Low, "Should the PDBReader deduce bonding patterns?", "false")); readConnect = IOSettings.Add(new BooleanIOSetting("ReadConnectSection", Importance.Low, "Should the CONECT be read?", "true")); useHetDictionary = IOSettings.Add(new BooleanIOSetting("UseHetDictionary", Importance.Low, "Should the PDBReader use the HETATM dictionary for atom types?", "false")); }
private void InitIOSettings() { write2DCoordinates = IOSettings.Add(new BooleanIOSetting("write2DCoordinates", Importance.Low, "Should 2D coordinates be added?", "true")); write3DCoordinates = IOSettings.Add(new BooleanIOSetting("write3DCoordinates", Importance.Low, "Should 3D coordinates be added?", "true")); builder = IOSettings.Add(new StringIOSetting("builder", Importance.Low, $"Which {nameof(IChemObjectBuilder)} should be used?", "NCDK.Silent.ChemObjectBuilder")); }
private void InitIOSettings() { var basisOptions = new List <string> { "6-31g", "6-31g*", "6-31g(d)", "6-311g", "6-311+g**" }; basis = new OptionIOSetting("Basis", Importance.Medium, "Which basis set do you want to use?", basisOptions, "6-31g"); var methodOptions = new List <string> { "rb3lyp", "b3lyp", "rhf" }; method = new OptionIOSetting("Method", Importance.Medium, "Which method do you want to use?", methodOptions, "b3lyp"); var commandOptions = new List <string> { "energy calculation", "geometry optimization", "IR frequency calculation", "IR frequency calculation (with Raman)" }; command = IOSettings.Add(new OptionIOSetting("Command", Importance.High, "What kind of job do you want to perform?", commandOptions, "energy calculation")); comment = IOSettings.Add(new StringIOSetting("Comment", Importance.Low, "What comment should be put in the file?", "Created with CDK (http://cdk.sf.net/)")); memory = IOSettings.Add(new StringIOSetting("Memory", Importance.Low, "How much memory do you want to use?", "unset")); shell = IOSettings.Add(new BooleanIOSetting("OpenShell", Importance.Medium, "Should the calculation be open shell?", "false")); proccount = IOSettings.Add(new IntegerIOSetting("ProcessorCount", Importance.Low, "How many processors should be used by Gaussian?", "1")); usecheckpoint = new BooleanIOSetting("UseCheckPointFile", Importance.Low, "Should a check point file be saved?", "false"); }
/// <summary> /// Creates a PDB writer. /// </summary> /// <param name="output">the stream to write the PDB file to.</param> public PDBWriter(TextWriter output) { writer = output; writeAsHET = IOSettings.Add(new BooleanIOSetting("WriteAsHET", Importance.Low, "Should the output file use HETATM", "false")); useElementSymbolAsAtomName = IOSettings.Add(new BooleanIOSetting("UseElementSymbolAsAtomName", Importance.Low, "Should the element symbol be written as the atom name", "false")); writeCONECTRecords = IOSettings.Add(new BooleanIOSetting("WriteCONECT", Importance.Low, "Should the bonds be written as CONECT records?", "true")); writeTERRecord = IOSettings.Add(new BooleanIOSetting("WriteTER", Importance.Low, "Should a TER record be put at the end of the atoms?", "false")); writeENDRecord = IOSettings.Add(new BooleanIOSetting("WriteEND", Importance.Low, "Should an END record be put at the end of the file?", "true")); }
public static string GetString([NotNull] this WebResponse thisValue, IOSettings settings = null) { bool bufferFilled = false; int bufferSize = settings?.BufferSize ?? IOSettings.BUFFER_DEFAULT; StringBuilder sb = new StringBuilder(bufferSize); IOReadSettings readSettings = new IOReadSettings(settings, (buf, _) => { if (bufferFilled) { return(!bufferFilled); } sb.Append(buf); bufferFilled = sb.Length >= bufferSize; return(!bufferFilled); }); return(Read(thisValue, readSettings) && sb.Length > 0 ? sb.ToString() : null); }
public static Task <bool> ReadAsync([NotNull] this WebResponse thisValue, [NotNull] IOSettings settings, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); IIOOnRead ioOnRead = settings as IIOOnRead ?? throw new ArgumentException("Argument must provide an OnRead implementation.", nameof(settings)); Stream stream = null; StreamReader reader = null; try { stream = GetStream(thisValue); token.ThrowIfCancellationRequested(); if (stream == null) { return(Task.FromResult(false)); } reader = new StreamReader(stream, settings.Encoding, true); int length; char[] chars = new char[settings.BufferSize]; do { length = reader.ReadAsync(chars).Execute(); }while (!token.IsCancellationRequested && length > 0 && ioOnRead.OnRead(chars, length)); token.ThrowIfCancellationRequested(); return(Task.FromResult(true)); } catch (Exception ex) when(settings.OnError != null) { settings.OnError(ex); return(Task.FromResult(false)); } finally { ObjectHelper.Dispose(ref reader); ObjectHelper.Dispose(ref stream); } }
public static string GetTitle([NotNull] this WebResponse thisValue, IOSettings settings = null) { string result = null; StringBuilder sb = new StringBuilder(); IOReadSettings readSettings = new IOReadSettings(settings, (bf, _) => { sb.Append(bf); string contents = sb.ToString(); Match m = WebResponseHelper.TitleCheckExpression.Match(contents); if (m.Success) { // we found a <title></title> match =] result = m.Groups[1].Value; return(false); } // reached end of head-block; no title found =[ return(!contents.Contains("</head>", StringComparison.OrdinalIgnoreCase)); }); return(Read(thisValue, readSettings) ? result : null); }
public static Task <UrlSearchResult> SearchAsync([NotNull] this WebResponse thisValue, string searchFor, UrlSearchFlags flags, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); bool hasTitleFlag = flags.FastHasFlag(UrlSearchFlags.Title); bool hasBufferFlag = flags.FastHasFlag(UrlSearchFlags.Buffer); bool hasSearchFlag = !string.IsNullOrEmpty(searchFor); UrlSearchResult result = new UrlSearchResult(); try { Func <char[], int, bool> onRead; if (hasTitleFlag || hasBufferFlag || hasSearchFlag) { StringBuilder sb = new StringBuilder(); StringComparison comparison = flags.FastHasFlag(UrlSearchFlags.IgnoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture; onRead = (c, _) => { if (result.Status == UrlSearchStatus.Unknown) { result.Status = UrlSearchStatus.Success; } sb.Append(c); if (hasBufferFlag) { result.Buffer = sb.ToString(); hasBufferFlag = false; } token.ThrowIfCancellationRequested(); if (!hasTitleFlag && !hasSearchFlag) { return(false); } string contents = sb.ToString(); if (hasTitleFlag) { Match m = WebResponseHelper.TitleCheckExpression.Match(contents); if (m.Success) { result.Title = m.Groups[1].Value; hasTitleFlag = false; } if (hasTitleFlag && contents.Contains("</head>", StringComparison.OrdinalIgnoreCase)) { hasTitleFlag = false; } } if (hasSearchFlag) { // ReSharper disable once AssignNullToNotNullAttribute if (contents.Contains(searchFor, comparison)) { result.Status = UrlSearchStatus.Found; hasSearchFlag = false; } } token.ThrowIfCancellationRequested(); return(hasTitleFlag || hasSearchFlag || hasBufferFlag); }; } else { onRead = (_, _) => { token.ThrowIfCancellationRequested(); if (result.Status == UrlSearchStatus.Unknown) { result.Status = UrlSearchStatus.Success; } return(false); }; } IOReadSettings readSettings = new IOReadSettings(settings, onRead); if (!ReadAsync(thisValue, readSettings, token).Execute()) { result.Status = UrlSearchStatus.Failed; } } catch (WebException wex) { result.Status = UrlSearchStatus.Unauthorized; result.Exception = wex; } catch (Exception ex) { result.Status = UrlSearchStatus.Error; result.Exception = ex; } return(Task.FromResult(result)); }
public static Task <string> ReadToEndAsync([NotNull] this WebResponse thisValue, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); settings ??= new IOSettings(); Stream stream = null; StreamReader reader = null; string result; try { stream = GetStream(thisValue); token.ThrowIfCancellationRequested(); if (stream == null) { return(null); } reader = new StreamReader(stream, settings.Encoding, true, settings.BufferSize); result = reader.ReadToEndAsync().Execute(); token.ThrowIfCancellationRequested(); } catch (Exception ex) when(settings.OnError != null) { result = null; settings.OnError(ex); } finally { ObjectHelper.Dispose(ref reader); ObjectHelper.Dispose(ref stream); } return(Task.FromResult(result)); }
//********************************************************************************************** public override void Edit() { IOSettings form = new IOSettings(this); form.ShowDialog(); }
public static Task <string> GetTitleAsync([NotNull] this WebResponse thisValue, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); string result = null; StringBuilder sb = new StringBuilder(); IOReadSettings readSettings = new IOReadSettings(settings, (bf, _) => { token.ThrowIfCancellationRequested(); sb.Append(bf); string contents = sb.ToString(); Match m = WebResponseHelper.TitleCheckExpression.Match(contents); if (m.Success) { // we found a <title></title> match =] result = m.Groups[1].Value; return(false); } // reached end of head-block; no title found =[ return(!contents.Contains("</head>", StringComparison.OrdinalIgnoreCase)); }); return(ReadAsync(thisValue, readSettings, token) .ContinueWith(_ => result, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
public static (string Title, string Buffer) Peek([NotNull] this WebResponse thisValue, IOSettings settings = null) { bool titleFound = false; bool bufferFilled = false; string title = null; int bufferSize = settings?.BufferSize ?? IOSettings.BUFFER_DEFAULT; StringBuilder bufferFetch = new StringBuilder(bufferSize); StringBuilder sb = new StringBuilder(bufferSize); IOReadSettings readSettings = new IOReadSettings(settings, (buf, _) => { sb.Append(buf); string contents = sb.ToString(); if (!bufferFilled) { bufferFetch.Append(buf); bufferFilled = bufferFetch.Length >= bufferSize; } if (!titleFound) { Match m = WebResponseHelper.TitleCheckExpression.Match(contents); if (m.Success) { // we found a <title></title> match =] title = m.Groups[1].Value; titleFound = true; } else { // reached end of head-block; no title found =[ titleFound = contents.Contains("</head>", StringComparison.OrdinalIgnoreCase); } } return(!titleFound || !bufferFilled); }); return(Read(thisValue, readSettings) ? (title, bufferFetch.ToString()) : (null, null)); }
public static Task <string> GetStringAsync([NotNull] this WebResponse thisValue, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); bool bufferFilled = false; int bufferSize = settings?.BufferSize ?? IOSettings.BUFFER_DEFAULT; StringBuilder sb = new StringBuilder(bufferSize); IOReadSettings readSettings = new IOReadSettings(settings, (buf, _) => { if (!bufferFilled) { sb.Append(buf); bufferFilled = sb.Length >= bufferSize; } token.ThrowIfCancellationRequested(); return(!bufferFilled); }); return(ReadAsync(thisValue, readSettings, token) .ContinueWith(_ => sb.ToString(), token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
public static Task <(string Title, string Buffer)> PeekAsync([NotNull] this WebResponse thisValue, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); bool titleFound = false; bool bufferFilled = false; string title = null; int bufferSize = settings?.BufferSize ?? IOSettings.BUFFER_DEFAULT; StringBuilder bufferFetch = new StringBuilder(bufferSize); StringBuilder sb = new StringBuilder(bufferSize); IOReadSettings readSettings = new IOReadSettings(settings, (buf, _) => { token.ThrowIfCancellationRequested(); sb.Append(buf); string contents = sb.ToString(); if (!bufferFilled) { bufferFetch.Append(buf); bufferFilled = bufferFetch.Length >= bufferSize; } if (!titleFound) { Match m = WebResponseHelper.TitleCheckExpression.Match(contents); if (m.Success) { // we found a <title></title> match =] title = m.Groups[1].Value; titleFound = true; } else { // reached end of head-block; no title found =[ titleFound = contents.Contains("</head>", StringComparison.OrdinalIgnoreCase); } } token.ThrowIfCancellationRequested(); return(!titleFound || !bufferFilled); }); return(ReadAsync(thisValue, readSettings, token) .ContinueWith(_ => (title, bufferFetch.ToString()), token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Default)); }
private void InitIOSettings() { forceReadAs3DCoords = IOSettings.Add(new BooleanIOSetting("ForceReadAs3DCoordinates", Importance.Low, "Should coordinates always be read as 3D?", "false")); }
public static Task <UrlSearchResult> SearchAsync([NotNull] this WebResponse thisValue, UrlSearchFlags flags, IOSettings settings = null, CancellationToken token = default(CancellationToken)) { return(SearchAsync(thisValue, null, flags, settings, token)); }
public static UrlSearchResult Search([NotNull] this WebResponse thisValue, UrlSearchFlags flags, IOSettings settings = null) { return(Search(thisValue, null, flags, settings)); }
/// <summary> /// Initializes IO settings. /// </summary> /// <remarks> /// Please note with regards to "writeAromaticBondTypes": bond type values 4 through 8 are for SSS queries only, /// so a 'query file' is created if the container has aromatic bonds and this settings is true. /// </remarks> private void InitIOSettings() { programNameOpt = IOSettings.Add( new StringIOSetting(OptProgramName, Importance.Low, "Program name to write at the top of the molfile header, should be exactly 8 characters long", "CDK")); }
public Settings(IOSettings ioSettings, string path = "") { Path = path; IO = ioSettings; }