Esempio n. 1
0
        public static Sentence Tokenize(string ini, DelimiterDictionary delimiters)
        {
            var lines = ini.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

            var globalSection = SectionFactory.CreateSection(Grammar.GlobalSectionName, delimiters);

            var appendSentence = new Action <Sentence>(next =>
            {
                globalSection.After.Last().Next = next;
            });

            for (var i = 0; i < lines.Length; i++)
            {
                var line = lines[i].TrimStart();

                var tokens = TokenizeLine(line, delimiters);
                appendSentence(new Sentence
                {
                    Line   = i,
                    Tokens = tokens
                });
            }

            return(globalSection);
        }
Esempio n. 2
0
        public IniFile(IniSettings settings = null)
        {
            settings = settings ?? new IniSettings();

            DuplicateSectionHandling  = settings.DuplicateSectionHandling;
            DuplicatePropertyHandling = settings.DuplicatePropertyHandling;
            Delimiters    = settings.Delimiters;
            GlobalSection = SectionFactory.CreateSection(Grammar.GlobalSectionName, Delimiters);
        }
Esempio n. 3
0
        public IniSection AddSection(string name)
        {
            var sections      = Sections.Where(x => x == name).ToList();
            var sectionExists = sections.Any();

            var addSection = new Func <IniSection>(() =>
            {
                var section = SectionFactory.CreateSection(name, Delimiters);
                GlobalSection.After.Last().Next = section;
                return(new IniSection(section, this));
            });

            if (DuplicateSectionHandling == DuplicateSectionHandling.Disallow)
            {
                if (sectionExists)
                {
                    throw new DuplicateSectionsException();
                }
                return(addSection());
            }

            if (DuplicateSectionHandling == DuplicateSectionHandling.Allow)
            {
                return(addSection());
            }

            if (DuplicateSectionHandling == DuplicateSectionHandling.Merge)
            {
                if (sectionExists)
                {
                    return(sections.Single());
                }
                return(addSection());
            }

            return(null);
        }