public string ReadString ( int length ) { Sure.Positive(length, nameof(length)); if (IsEOF) { return(null); } StringBuilder result = new StringBuilder(length); for (int i = 0; i < length; i++) { char c = ReadChar(); if (c == EOF) { break; } result.Append(c); } return(result.ToString()); }
public static MarcRecord ParseMfnStatusVersion ( [NotNull] string line1, [NotNull] string line2, [NotNull] MarcRecord record ) { Sure.NotNullNorEmpty(line1, "line1"); Sure.NotNullNorEmpty(line2, "line2"); Sure.NotNull(record, "record"); Regex regex = new Regex(@"^(-?\d+)\#(\d*)?"); Match match = regex.Match(line1); record.Mfn = Math.Abs(int.Parse(match.Groups[1].Value)); if (match.Groups[2].Length > 0) { record.Status = (RecordStatus)int.Parse ( match.Groups[2].Value ); } match = regex.Match(line2); if (match.Groups[2].Length > 0) { record.Version = int.Parse(match.Groups[2].Value); } return(record); }
/// <summary> /// Записываемся в поток. /// </summary> public void WriteTable ( [NotNull] TextWriter writer ) { Sure.NotNull(writer, "writer"); int count = 0; foreach (byte b in _table) { if (count != 0) { writer.Write(" "); } writer.Write ( "{0:000}", b ); count++; if (count == 32) { writer.WriteLine(); count = 0; } } }
/// <summary> /// Convert array of bytes to C# source code. /// </summary> public static string ToSourceCode ( [NotNull] byte[] array ) { Sure.NotNull(array, nameof(array)); StringBuilder result = new StringBuilder("{"); for (int i = 0; i < array.Length; i++) { if (i != 0) { result.Append(", "); if (i % 10 == 0) { result.AppendLine(); result.Append(" "); } } result.AppendFormat ( CultureInfo.InvariantCulture, "0x{0:X2}", array[i] ); } result.Append("}"); return(result.ToString()); }
public static T[] ThrowIfNullOrEmpty <T> ( [CanBeNull] this T[] array, [NotNull] string message ) { Sure.NotNullNorEmpty(message, nameof(message)); if (ReferenceEquals(array, null)) { Log.Error ( nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty) + ": " + "array is null" ); throw new ArgumentNullException(message); } if (array.Length == 0) { Log.Error ( nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty) + ": " + "array is empty" ); throw new ArgumentException(message); } return(array); }
public ResponseBuilder StandardHeader ( [NotNull] string command, int clientId, int commandNumber ) { Sure.NotNull(command, "command"); byte[] bytes = Memory.ToArray(); Memory = new MemoryStream(Memory.Capacity + 100); AppendAnsi(command); NewLine(); Append(clientId); NewLine(); Append(commandNumber); NewLine(); int responseSize = bytes.Length; Append(responseSize); NewLine(); // 5 пустых переводов строки NewLine(); NewLine(); NewLine(); NewLine(); NewLine(); // Предыдущее содержимое Memory.Write(bytes, 0, bytes.Length); return(this); }
/// <summary> /// Saves <see cref="GblFile"/> to local JSON file. /// </summary> public static void SaveLocalJsonFile ( [NotNull] this GblFile gbl, [NotNull] string fileName ) { Sure.NotNull(gbl, "gbl"); Sure.NotNullNorEmpty(fileName, "fileName"); #if WINMOBILE || PocketPC Log.Error ( "GblUtility::SaveLocalJsonFile: " + "not implemented" ); throw new NotImplementedException(); #else string contents = JArray.FromObject(gbl) .ToString(Formatting.Indented); File.WriteAllText ( fileName, contents, IrbisEncoding.Utf8 ); #endif }
public static MarcRecord[] ParseAllFormat ( [NotNull] string database, [NotNull] IIrbisConnection connection, [NotNull] string[] lines ) { Sure.NotNullNorEmpty(database, "database"); Sure.NotNull(connection, "connection"); Sure.NotNull(lines, "lines"); List <MarcRecord> result = new List <MarcRecord>(); foreach (string line in lines) { MarcRecord record = new MarcRecord { HostName = connection.Host, Database = database }; record = ProtocolText.ParseResponseForAllFormat ( line, record ); // coverity[NULL_RETURNS] result.Add(record.ThrowIfNull("record")); } return(result.ToArray()); }
public static MarcRecord SetSubField ( [NotNull] this MarcRecord record, int tag, int fieldOccurrence, char code, int subFieldOccurrence, [CanBeNull] string newValue ) { Sure.NotNull(record, "record"); Sure.Positive(tag, "tag"); RecordField field = record.Fields.GetField(tag, fieldOccurrence); if (!ReferenceEquals(field, null)) { SubField subField = field.GetSubField(code, subFieldOccurrence); if (!ReferenceEquals(subField, null)) { subField.Value = newValue; } } return(record); }
/// <inheritdoc cref="AbstractCommand.Execute" /> public override ServerResponse Execute ( ClientQuery query ) { Sure.NotNull(query, nameof(query)); ServerResponse result = base.Execute(query); result.GetReturnCode(); // ReSharper disable AssignNullToNotNullAttribute // ReSharper disable PossibleNullReferenceException // ReSharper disable ForCanBeConvertedToForeach for (int i = 0; i < References.Count; i++) { ProtocolText.ParseResponseForWriteRecords ( result, References[i].Record ); References[i].Mfn = References[i].Record.Mfn; } // ReSharper restore ForCanBeConvertedToForeach // ReSharper restore PossibleNullReferenceException // ReSharper restore AssignNullToNotNullAttribute return(result); }
public static MarcRecord[] ParseAllFormat ( [NotNull] string database, [NotNull] ServerResponse response ) { Sure.NotNullNorEmpty(database, "database"); Sure.NotNull(response, "response"); List <MarcRecord> result = new List <MarcRecord>(); while (true) { MarcRecord record = new MarcRecord { HostName = response.Connection.Host, Database = database }; record = ProtocolText.ParseResponseForAllFormat ( response, record ); if (ReferenceEquals(record, null)) { break; } result.Add(record); } return(result.ToArray()); }
/// <summary> /// Compares two <see cref="BitArray"/>s. /// </summary> public static bool AreEqual ( [NotNull] BitArray left, [NotNull] BitArray right ) { Sure.NotNull(left, "left"); Sure.NotNull(right, "right"); if (left.Length != right.Length) { return(false); } int length = left.Length; bool[] leftA = new bool[length]; ICollection leftCollection = left; leftCollection.CopyTo(leftA, 0); bool[] rightA = new bool[length]; ICollection rightCollection = right; rightCollection.CopyTo(rightA, 0); for (int i = 0; i < length; i++) { if (leftA[i] != rightA[i]) { return(false); } } return(true); }
public static TermInfo[] Parse ( [NotNull] ServerResponse response ) { Sure.NotNull(response, "response"); List <TermInfo> result = new List <TermInfo>(); Regex regex = new Regex(@"^(\d+)\#(.+)$"); while (true) { string line = response.GetUtfString(); if (string.IsNullOrEmpty(line)) { break; } Match match = regex.Match(line); if (match.Success) { TermInfo item = new TermInfo { Count = int.Parse(match.Groups[1].Value), Text = match.Groups[2].Value }; result.Add(item); } } return(result.ToArray()); }
/// <inheritdoc cref="NonNullCollection{T}.SetItem" /> protected override void SetItem ( int index, GblNode item ) { Sure.NotNull(item, nameof(item)); if (!ReferenceEquals(item.Parent, null)) { if (!ReferenceEquals(item.Parent, Parent)) { throw new IrbisException(); } } if (index < Count) { GblNode previousItem = this[index]; if (!ReferenceEquals(previousItem, item)) { previousItem.Parent = null; } } item.Parent = Parent; base.SetItem(index, item); }
/// <inheritdoc cref="AbstractCommand.Execute" /> public override ServerResponse Execute ( ClientQuery query ) { Sure.NotNull(query, nameof(query)); string database = Record.ThrowIfNull("Record").Database ?? Connection.Database; ServerResponse result = base.Execute(query); MaxMfn = result.GetReturnCode(); MarcRecord record = Record.ThrowIfNull("Record"); record.Database = database; record.HostName = Connection.Host; if (!DontParseResponse) { ProtocolText.ParseResponseForWriteRecord ( result, record ); } return(result); }
public static MarcRecord AddField ( [NotNull] this MarcRecord record, int tag, [NotNull] object value ) { Sure.NotNull(record, "record"); Sure.NotNull(value, "value"); RecordField field = value as RecordField; if (!ReferenceEquals(field, null)) { Debug.Assert(tag == field.Tag, "tag == field.Tag"); } else { string text = value.ToString(); if (!string.IsNullOrEmpty(text)) { field = RecordField.Parse(tag, value.ToString()); } else { field = new RecordField(tag); } } record.Fields.Add(field); return(record); }
/// <inheritdoc cref="AbstractCommand.Execute"/> public override ServerResponse Execute ( ClientQuery query ) { Sure.NotNull(query, nameof(query)); ServerResponse result = base.Execute(query); byte[] buffer = result.RawAnswer; Encoding encoding = IrbisEncoding.Ansi; byte[] preamble = encoding.GetBytes(Preamble); int offset = _FindPreamble(buffer, preamble); if (offset < 0) { Log.Error ( "ReadBinaryFileCommand::Execute: " + "no binary data received" ); throw new IrbisNetworkException ( "No binary data received" ); } offset += preamble.Length; Content = result.RawAnswer.GetSpan(offset); return(result); }
public static IrbisProcessInfo[] Parse ( [NotNull] ServerResponse response ) { Sure.NotNull(response, nameof(response)); List <IrbisProcessInfo> result = new List <IrbisProcessInfo>(); while (true) { string[] lines = response.GetAnsiStrings(10); if (ReferenceEquals(lines, null)) { break; } IrbisProcessInfo info = new IrbisProcessInfo { ProcessID = lines[0], State = lines[1], Number = lines[2], IPAddress = lines[3], Name = lines[4], ClientID = lines[5], Workstation = lines[6], Started = lines[7], LastCommand = lines[8], CommandNumber = lines[9] }; result.Add(info); } return(result.ToArray()); }
public static GblFile ParseLocalXmlFile ( [NotNull] string fileName ) { Sure.NotNullNorEmpty(fileName, "fileName"); #if WINMOBILE || PocketPC Log.Error ( "GblUtility::ParseLocalXmlFile: " + "not implemented" ); throw new NotImplementedException(); #else string text = File.ReadAllText ( fileName, IrbisEncoding.Utf8 ); GblFile result = FromXml(text); return(result); #endif }
/// <summary> /// Prints the specified text. /// </summary> /// <param name="text">The text.</param> /// <returns></returns> public virtual bool Print ( string text ) { Sure.NotNull(text, nameof(text)); using (PrintDocument document = new PrintDocument()) { document.DocumentName = DocumentName; document.OriginAtMargins = false; // ??? if (PageSettings != null) { document.DefaultPageSettings = PageSettings; } if (PrintController != null) { document.PrintController = PrintController; } if (PrinterSettings != null) { document.PrinterSettings = PrinterSettings; } document.BeginPrint += OnBeginPrint; document.EndPrint += OnEndPrint; document.PrintPage += OnPrintPage; document.QueryPageSettings += OnQueryPageSettings; _pageNumber = 1; document.Print(); return(true); } }
/// <summary> /// Convert array of characters to C# source code. /// </summary> public static string ToSourceCode ( [NotNull] char[] array ) { Sure.NotNull(array, nameof(array)); StringBuilder result = new StringBuilder("{"); for (int i = 0; i < array.Length; i++) { if (i != 0) { result.Append(", "); if (i % 10 == 0) { result.AppendLine(); result.Append(" "); } } result.Append ( "'" + EncodeCharacter(array[i]) + "'" ); } result.Append("}"); return(result.ToString()); }
/// <inheritdoc cref="AbstractClientSocket.ExecuteRequest"/> public override byte[] ExecuteRequest ( byte[] request ) { Sure.NotNull(request, nameof(request)); double probability = Probability; if (probability > 0.0 && probability < 1.0) { double value = _random.NextDouble(); if (value < probability) { throw new IrbisNetworkException ( "Broken network event" ); } } byte[] result = InnerSocket.ThrowIfNull().ExecuteRequest(request); return(result); }
public static IList <T> ThrowIfNullOrEmpty <T> ( [CanBeNull] this IList <T> list, [NotNull] string message ) { Sure.NotNullNorEmpty(message, nameof(message)); if (ReferenceEquals(list, null)) { Log.Error ( nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty) + ": " + "list is null" ); throw new ArgumentNullException(message); } if (list.Count == 0) { Log.Error ( nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty) + ": " + "list is empty" ); throw new ArgumentException(message); } return(list); }
public static void ExpandTypes ( [NotNull] JObject obj, [NotNull] string nameSpace, [NotNull] string assembly ) { Sure.NotNull(obj, nameof(obj)); Sure.NotNullNorEmpty(nameSpace, nameof(nameSpace)); Sure.NotNullNorEmpty(assembly, nameof(assembly)); IEnumerable <JValue> values = obj .SelectTokens("$..$type") .OfType <JValue>(); foreach (JValue value in values) { string typeName = value.Value.ToString(); if (!typeName.Contains('.')) { typeName = nameSpace + "." + typeName + ", " + assembly; value.Value = typeName; } } }
public static RecordField ParseLine ( [NotNull] string line ) { Sure.NotNullNorEmpty(line, "line"); StringReader reader = new StringReader(line); RecordField result = new RecordField { Tag = NumericUtility.ParseInt32(_ReadTo(reader, '#')), Value = _ReadTo(reader, '^') }; while (true) { int next = reader.Read(); if (next < 0) { break; } char code = char.ToLower((char)next); string text = _ReadTo(reader, '^'); SubField subField = new SubField { Code = code, Value = text }; result.SubFields.Add(subField); } return(result); }
public static GblStatement ParseStream ( [NotNull] TextReader reader ) { Sure.NotNull(reader, nameof(reader)); string command = reader.ReadLine(); if (string.IsNullOrEmpty(command)) { return(null); } GblStatement result = new GblStatement { Command = command.Trim(), Parameter1 = reader.RequireLine(), Parameter2 = reader.RequireLine(), Format1 = reader.RequireLine(), Format2 = reader.RequireLine() }; return(result); }
public string TrimText ( [NotNull] string text ) { Sure.NotNull(text, "text"); if ( text.Length == 0 || IsAlpha(text[0]) && IsAlpha(text[text.Length - 1]) ) { return(text); } StringBuilder builder = new StringBuilder(text); // Trim beginning of the text while (builder.Length != 0 && !IsAlpha(builder[0])) { builder.Remove(0, 1); } // Trim tail of the text while (builder.Length != 0 && !IsAlpha(builder[builder.Length - 1])) { builder.Remove(builder.Length - 1, 1); } return(builder.ToString()); }
public static string ToXml ( [NotNull] this MenuFile menu ) { Sure.NotNull(menu, nameof(menu)); XmlWriterSettings settings = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false, NewLineHandling = NewLineHandling.None }; StringBuilder output = new StringBuilder(); XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); using (XmlWriter writer = XmlWriter.Create(output, settings)) { XmlSerializer serializer = new XmlSerializer(typeof(MenuFile)); serializer.Serialize(writer, menu, namespaces); } return(output.ToString()); }
/// <inheritdoc cref="AbstractClientSocket.ExecuteRequest" /> public override byte[] ExecuteRequest ( byte[] request ) { Sure.NotNull(request, nameof(request)); ActualRequest = request; if (!ReferenceEquals(ExpectedRequest, null)) { if (!ArrayUtility.Coincide(ExpectedRequest, 0, request, 0, request.Length)) { throw new Exception(); } } byte[] answer = Response; if (ReferenceEquals(answer, null)) { throw new Exception(); } return(answer); }
public string PeekString ( int length ) { Sure.Positive(length, nameof(length)); if (IsEOF) { return(null); } int savePosition = _position, saveColumn = _column, saveLine = _line; StringBuilder result = new StringBuilder(length); for (int i = 0; i < length; i++) { char c = ReadChar(); if (c == EOF) { break; } result.Append(c); } _position = savePosition; _column = saveColumn; _line = saveLine; return(result.ToString()); }