Beispiel #1
0
        /// <summary>
        /// Try Get Value
        /// </summary>
        public bool TryGetValue(string name, out NameValueRecord record)
        {
            if (m_Records.TryGetValue(name, out record))
            {
                return(true);
            }

            record = null;

            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// Add
        /// </summary>
        public NameValueRecord Add(NameValueRecord item)
        {
            if (null == item)
            {
                throw new ArgumentNullException(nameof(item));
            }

            CoreAddRecord(item);

            return(item);
        }
Beispiel #3
0
        internal void CoreRemoveRecord(NameValueRecord record)
        {
            if (null == record)
            {
                return;
            }

            if (record.Owner != this)
            {
                return;
            }

            m_Records.Remove(record.Name);

            record.Owner = null;
        }
Beispiel #4
0
        /// <summary>
        /// Load
        /// </summary>
        public static NameValueDocument Load(IEnumerable <string> lines,
                                             StringComparer comparer,
                                             IniFileCommentKind commentKind)
        {
            if (null == lines)
            {
                throw new ArgumentNullException(nameof(lines));
            }

            NameValueDocument result = new NameValueDocument(comparer, commentKind);

            List <string> comments = new List <string>();

            int index = 0;

            foreach (string line in lines.Select(x => x.Trim()))
            {
                index += 1;

                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                if (line.StartsWith(";") || line.StartsWith("#"))
                {
                    comments.Add(line.Substring(1));

                    continue;
                }

                if (NameValueRecord.TryParse(line, out var record))
                {
                    record.Comments = string.Join(Environment.NewLine, comments);

                    comments.Clear();

                    result.Add(record);
                }
                else
                {
                    throw new FormatException($"Syntax error in line {index}");
                }
            }

            return(result);
        }
Beispiel #5
0
        internal void CoreAddRecord(NameValueRecord record)
        {
            if (null == record)
            {
                return;
            }

            if (record.Owner == this)
            {
                return;
            }

            if (record.Owner != null)
            {
                record.Owner.CoreRemoveRecord(record);
            }

            if (m_Records.TryGetValue(record.Name, out var old))
            {
                old.Owner.CoreRemoveRecord(old);
            }

            m_Records.Add(record.Name, record);
        }