Ejemplo n.º 1
0
        public int AddSection(string section)
        {
            int idx = GetSectionIdx(section);

            if (idx < 0)
            {
                rows.Add(IniRow.Section(section));
                idx = GetSectionIdx(section);
            }
            return(idx);
        }
Ejemplo n.º 2
0
        public void SetParam(string section, string key, string value)
        {
            int idx = GetKeyValueIdx(section, key);

            if (idx < 0)
            {
                int cpos        = AddSection(section);
                int insertAfter = cpos;
                while (rows[cpos].AsSection() == null)
                {
                    if (rows[cpos].AsKeyValue() != null)
                    {
                        insertAfter = cpos;
                    }
                    cpos++;
                }
                rows.Insert(insertAfter + 1, IniRow.KeyValue(key, value, StringUtil.EscapeHint.Escaped));
            }
            if (idx >= 0)
            {
                rows[idx] = IniRow.KeyValue(key, value, StringUtil.EscapeHint.Escaped);
            }
        }
Ejemplo n.º 3
0
        public bool ParseLine(TextReader reader)
        {
            var line = reader.ReadLine();

            if (line == null)
            {
                return(false);
            }
            Match m;

            m = reSection.Match(line);
            if (m.Success)
            {
                var sectionName = m.Groups[1].Value;
                rows.Add(IniRow.Section(sectionName));
                return(true);
            }

            m = reComment.Match(line);
            if (m.Success)
            {
                var commentText = m.Groups[1].Value;
                rows.Add(IniRow.Comment(commentText));
                return(true);
            }

            StringUtil.EscapeHint hint = StringUtil.EscapeHint.Escaped;
            if (line.Contains("="))
            {
                var    args  = line.Split('=');
                string key   = args[0].Trim();
                string value = args[1].Trim();
                if (value.StartsWith("\"\"\""))
                {
                    if (!(value.Length > 3 && value.EndsWith("\"\"\"")))
                    {
                        hint   = StringUtil.EscapeHint.TripleDoubleQuoted;
                        value += ReadToTerminator("\"\"\"", reader);
                    }
                }
                else if (value.StartsWith("'''"))
                {
                    if (!(value.Length > 3 && value.EndsWith("'''")))
                    {
                        hint   = StringUtil.EscapeHint.TripleQuoted;
                        value += ReadToTerminator("'''", reader);
                    }
                }
                else if (value.StartsWith("\""))
                {
                    hint = StringUtil.EscapeHint.DoubleQuoted;
                }
                else if (value.StartsWith("'"))
                {
                    hint = StringUtil.EscapeHint.Quoted;
                }
                else if (value.EndsWith("\\"))
                {
                    value = value.TrimEnd("\\") + "\n" + ReadForContinuation("\\", reader);
                }
                rows.Add(IniRow.KeyValue(key, value.Dequote(), hint));
                return(true);
            }
            rows.Add(IniRow.Invalid(line));
            return(true);
        }