Beispiel #1
0
        public static IniFile ReadFromFile(IniDefinition definition, string file)
        {
            var iniFileStream = IniFileStream.FromFile(file);
            var lines         = iniFileStream.GetLines().GetEnumerator();
            var result        = new IniFile()
            {
                HeaderLines = new List <IniFileStream.Line>(),
                Definition  = definition,
                Sections    = new List <IniSection>()
            };

            //
            // Read any header comments
            //
            bool hasElement = lines.MoveNext();

            while (hasElement && lines.Current.Type != IniFileStream.LineType.SectionHeader)
            {
                result.HeaderLines.Add(lines.Current);
                hasElement = lines.MoveNext();
            }

            if (hasElement)
            {
                result.Sections.AddRange(IniSection.ReadAllSections(lines, definition));
            }

            return(result);
        }
Beispiel #2
0
        internal static IEnumerable <IniSection> ReadAllSections(IEnumerator <IniFileStream.Line> iniLines, IniDefinition definition)
        {
            IniSection currentSection = null;
            int        ordinal        = 0;

            do
            {
                switch (iniLines.Current.Type)
                {
                case IniFileStream.LineType.SectionHeader:
                    if (currentSection != null)
                    {
                        yield return(currentSection);
                    }

                    currentSection = new IniSection(iniLines.Current.Value);
                    break;

                default:
                    var baseEntry = IniEntry.FromLine(iniLines.Current);
                    currentSection.Entries.Add(baseEntry);

                    if (!baseEntry.IsBlank && !baseEntry.IsComment)
                    {
                        IniSectionDefinition sectionDefinition;
                        definition.Sections.TryGetValue(currentSection.SectionName, out sectionDefinition);
                        var fieldEntry = new IniFieldEntry {
                            Order = ordinal++, Entry = GetFieldEntry(sectionDefinition, baseEntry)
                        };

                        List <IniFieldEntry> fieldEntries;
                        if (!currentSection.Fields.TryGetValue(baseEntry.Key, out fieldEntries))
                        {
                            fieldEntries = new List <IniFieldEntry>();
                            currentSection.Fields[baseEntry.Key] = fieldEntries;
                        }

                        fieldEntries.Add(fieldEntry);
                    }
                    break;
                }
            } while (iniLines.MoveNext());

            yield return(currentSection);
        }