/// <summary> /// Initializes a new instance of the <see cref="Ini"/> class and loads the data from /// the specified <see cref="TextReader"/>. /// </summary> /// <param name="reader">The <see cref="TextReader"/> to load from.</param> /// <param name="settings">Optional Ini file settings.</param> /// <exception cref="ArgumentNullException">Thrown if the specified text reader is null.</exception> public Ini(TextReader reader, IniLoadSettings settings = null) : base(GetEqualityComparer(settings)) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } ParseIniFile(reader, settings); }
/// <summary> /// Transforms the INI content into an in-memory object model. /// </summary> /// <param name="reader">The INI content.</param> /// <param name="settings">Optional Ini file settings.</param> private void ParseIniFile(TextReader reader, IniLoadSettings settings) { settings = settings ?? IniLoadSettings.Default; // Go through all the lines and build a flat collection of INI objects. IList <IniItem> lineItems = ParseLines(reader).ToList(); // Go through the line objects and construct an object model. CreateObjectModel(settings, lineItems); }
/// <summary> /// Initializes a new instance of the <see cref="Ini"/> class and loads the data from /// the specified stream. /// </summary> /// <param name="stream">The stream to load from.</param> /// <param name="settings">Optional Ini file settings.</param> /// <exception cref="ArgumentNullException">Thrown if the specified stream is null.</exception> /// <exception cref="ArgumentException">Thrown if the stream cannot be read.</exception> public Ini(Stream stream, IniLoadSettings settings = null) : base(GetEqualityComparer(settings)) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException(ErrorMessages.StreamNotReadable, nameof(stream)); } using (var reader = new StreamReader(stream, settings.Encoding ?? Encoding.UTF8, settings.DetectEncoding)) ParseIniFile(reader, settings); }
/// <summary> /// Initializes a new instance of the <see cref="Ini"/> class and loads the data from /// the specified file. /// </summary> /// <param name="iniFilePath">The path to the .ini file.</param> /// <param name="settings">Optional Ini file settings.</param> /// <exception cref="ArgumentNullException">Thrown if the <c>iniFilePath</c> is <c>null</c>.</exception> /// <exception cref="FileNotFoundException">Thrown if the specified file does not exist.</exception> public Ini(string iniFilePath, IniLoadSettings settings = null) : base(GetEqualityComparer(settings)) { if (iniFilePath == null) { throw new ArgumentNullException(nameof(iniFilePath)); } if (!File.Exists(iniFilePath)) { throw new FileNotFoundException(string.Format(ErrorMessages.IniFileDoesNotExist, iniFilePath), iniFilePath); } using (var stream = new FileStream(iniFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var reader = new StreamReader(stream, settings.Encoding ?? Encoding.UTF8, settings.DetectEncoding)) ParseIniFile(reader, settings); }
/// <summary> /// Go through each line object and construct a hierarchical object model, with properties /// under sections and comments/blank lines under respective sections and properties. /// </summary> /// <param name="settings">The INI load settings that control the load behavior.</param> /// <param name="lineItems"></param> private void CreateObjectModel(IniLoadSettings settings, IList <IniItem> lineItems) { Section currentSection = null; var minorItems = new List <MinorIniItem>(); foreach (IniItem item in lineItems) { switch (item) { case BlankLine blankLine when !settings.IgnoreBlankLines: minorItems.Add(blankLine); break; case Comment comment when !settings.IgnoreComments: minorItems.Add(comment); break; case Section section: currentSection = section; AddRangeAndClear(currentSection.Items, minorItems); Add(currentSection); break; case Property property when currentSection == null: throw new FormatException(string.Format(CultureInfo.CurrentCulture, ErrorMessages.PropertyWithoutSection, property.Name)); case Property property: AddRangeAndClear(property.Items, minorItems); if (currentSection == null) { throw new InvalidOperationException(ErrorMessages.CreateObjectModelInvalidCurrentSection); } currentSection.Add(property); break; } } // If there are comments or blank lines at the end of the INI, which cannot be assigned // to any section or property, assign them to the TrailingItems collection. if (minorItems.Count > 0) { AddRangeAndClear(TrailingItems, minorItems); } }
public Ini(FileInfo iniFile, IniLoadSettings settings = null) : base(GetEqualityComparer(settings)) { if (iniFile == null) { throw new ArgumentNullException(nameof(iniFile)); } if (!iniFile.Exists) { throw new FileNotFoundException(string.Format(CultureInfo.CurrentCulture, ErrorMessages.IniFileDoesNotExist, iniFile.FullName), iniFile.FullName); } _settings = settings ?? IniLoadSettings.Default; using (var stream = new FileStream(iniFile.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var reader = new StreamReader(stream, _settings.Encoding ?? Encoding.UTF8, _settings.DetectEncoding)) ParseIniFile(reader); } }
/// <summary> /// Initializes a new empty instance of the <see cref="Ini"/> class with the specified /// settings. /// </summary> /// <param name="settings">The Ini file settings.</param> public Ini(IniLoadSettings settings) : base(GetEqualityComparer(settings)) { }
/// <summary> /// Initializes a new instance of the <see cref="Ini"/> class and loads the data from /// the specified string. /// </summary> /// <param name="content">The string representing the Ini content.</param> /// <param name="settings">Optional Ini file settings.</param> /// <returns>An instance of the <see cref="Ini"/> class.</returns> public static Ini Load(string content, IniLoadSettings settings = null) { using (var reader = new StringReader(content)) return(new Ini(reader, settings)); }