/// <summary> /// Serializes the <see cref="IniContainer"/> as a string in the INI format. /// </summary> /// <param name="container">The container to serialize</param> /// <returns>The container serialized as a string</returns> public string SerializeAsString(IniContainer container) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (container.IsEmpty) { return(string.Empty); } if (Options.NormalizeBeforeSerialization) { container = Normalizer.Normalize(container); } var builder = new StringBuilder(); AppendCommentsAndProperties(builder, container.GlobalComments, container.GlobalProperties); foreach (var section in container.Sections) { AppendSection(builder, section); } return(builder.ToString()); }
/// <inheritdoc /> public bool TryNormalizeInto(IniContainer source, IniContainer destination) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (source.IsEmpty) { return(true); } CopyComments(source.GlobalComments, destination.GlobalComments); return(TryNormalizeInto(source.GlobalProperties, destination.GlobalProperties) && TryNormalizeInto(source.Sections, destination.Sections)); }
/// <summary> /// Serializes the <see cref="IniContainer"/> into the provided <see cref="StreamWriter"/> /// in the INI format. /// </summary> /// <param name="container">The container to serialize</param> /// <param name="writer">The writer to output the serialization result</param> /// <param name="ct">The cancellation token</param> /// <returns>A task to be awaited</returns> public async Task SerializeToTextWriterAsync(IniContainer container, TextWriter writer, CancellationToken ct = new CancellationToken()) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } // TODO Performance tests var serializationResult = SerializeAsString(container); await writer.WriteAsync(serializationResult); await writer.FlushAsync(); }
/// <summary> /// Serializes the <see cref="IniContainer"/> into the provided <see cref="StreamWriter"/> /// in the INI format. /// </summary> /// <param name="container">The container to serialize</param> /// <param name="writer">The writer to output the serialization result</param> public void SerializeToTextWriter(IniContainer container, TextWriter writer) { if (container == null) { throw new ArgumentNullException(nameof(container)); } if (writer == null) { throw new ArgumentNullException(nameof(writer)); } // TODO Performance tests var serializationResult = SerializeAsString(container); writer.Write(serializationResult); writer.Flush(); }
/// <summary> /// Reads the content of the given <see cref="TextReader"/> and deserializes /// as an <see cref="IniContainer"/>. /// </summary> /// <param name="reader">The reader to extract</param> /// <param name="ct">The cancellation token</param> /// <returns>The resulting container</returns> public async Task <IniContainer> DeserializeAsContainerAsync(TextReader reader, CancellationToken ct) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } var container = new IniContainer(); IniSection currentSection = null; var linePosition = 0; string line; while ((line = (await reader.ReadLineAsync())?.Trim()) != null) { if (CanIgnoreLine(line)) { continue; } string comment; IniSection section; IniProperty property; if (TryExtractComment(line, out comment)) { if (currentSection == null) { container.GlobalComments.Add(comment); } else { currentSection.Comments.Add(comment); } } else if (TryExtractSection(line, out section)) { currentSection = section; container.Sections.Add(currentSection); } else if (TryExtractProperty(line, out property)) { if (currentSection == null) { container.GlobalProperties.Add(property); } else { currentSection.Properties.Add(property); } } else if (Options.FailOnInvalidLines) { throw new InvalidLineFormatException(linePosition, line); } ++linePosition; } if (Options.NormalizeAfterDeserialization) { container = Normalizer.Normalize(container); } return(container); }
/// <summary> /// Normalizes the <see cref="IniContainer"/> /// using the <see cref="IniNormalizer.Default"/> instance. /// </summary> /// <param name="container">The container to normalize</param> /// <param name="result">The normalized container</param> /// <returns>True if instance normalized successfully, otherwise false</returns> public static bool TryNormalize(this IniContainer container, out IniContainer result) { return(IniNormalizer.Default.TryNormalize(container, out result)); }
/// <summary> /// Normalizes the <see cref="IniContainer"/> /// using the <see cref="IniNormalizer.Default"/> instance. /// </summary> /// <param name="container">The container to normalize</param> /// <returns>The normalized container</returns> /// <exception cref="DuplicatedSection"></exception> /// <exception cref="DuplicatedProperty"></exception> public static IniContainer Normalize(this IniContainer container) { return(IniNormalizer.Default.Normalize(container)); }