Exemplo n.º 1
0
        /// <summary>
        /// Gets the raw value of the key.
        /// </summary>
        /// <returns>The value raw.</returns>
        /// <param name="sectionName">Section name.</param>
        /// <param name="keyName">Key name.</param>
        /// <param name="defaultValue">Default value returned if the key with the given name does not exist.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        internal virtual T GetRawValue <T>(string sectionName, string keyName, T defaultValue)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                throw new ArgumentNullException(nameof(sectionName));
            }
            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentNullException(nameof(keyName));
            }

            var iniKey = new ConfigKeyValue <T>(keyName, Settings.KeyValueSeparator, defaultValue, -1);

            if (!sections.TryGetValue(sectionName, out var section))
            {
                section = new ConfigSection(sectionName, Lines.Any() ? Lines.Max(l => l.LineNumber) : 0);
                if (Sections.Any())
                {
                    Sections.Last().AddLine(new ConfigLine());
                }
                sections.Add(sectionName, section);
            }

            var key = section.Keys.FirstOrDefault(k => Equals(keyName, k.Name));

            if (key != null)
            {
                return((T)key.ValueRaw);
            }

            section.AddLine(iniKey);
            return(defaultValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the valueless key.
        /// </summary>
        /// <param name="currentSection">The current section.</param>
        /// <param name="currentLine">The current line.</param>
        /// <param name="lineRaw">The line raw.</param>
        /// <param name="lineNumber">The line number.</param>
        private void ReadValuelessKey(ref ConfigSection currentSection, ref ConfigLine currentLine, string lineRaw,
                                      int lineNumber)
        {
            if (null != currentLine)
            {
                BackupCurrentLine(ref currentSection, ref currentLine, lineNumber);
            }

            currentLine = new ConfigKeyValue <object>(lineRaw, Settings.KeyValueSeparator, null, lineNumber);
        }
Exemplo n.º 3
0
            public T GetValue <T>(string keyName, T defaultValue = default(T))
            {
                if (string.IsNullOrWhiteSpace(keyName))
                {
                    throw new ArgumentException("Key name must be a non-empty string.", nameof(keyName));
                }

                var iniKey = new ConfigKeyValue <T>(keyName, parent.Settings.KeyValueSeparator, defaultValue, -1);
                var key    = parent.fileHeader.Section.Keys.FirstOrDefault(k => Equals(keyName, k.Name));

                if (key != null)
                {
                    return((T)key.ValueRaw);
                }

                parent.fileHeader.Section.AddLine(iniKey);
                return(defaultValue);
            }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the raw value of the key.
        /// </summary>
        /// <returns>The value raw.</returns>
        /// <param name="sectionName">Section name.</param>
        /// <param name="keyName">Key name.</param>
        /// <param name="defaultValue">Default value returned if the key with the given name does not exist.</param>
        /// <typeparam name="T">The 1st type parameter.</typeparam>
        internal virtual T GetRawValue <T>(string sectionName, string keyName, T defaultValue)
        {
            if (sectionName is null)
            {
                throw new ArgumentNullException(nameof(sectionName));
            }

            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentException("Key name must be a non-empty string.", nameof(keyName));
            }

            var iniKey = new ConfigKeyValue <T>(keyName, Settings.KeyValueSeparator, defaultValue, -1);

            if (!sections.TryGetValue(sectionName, out var section))
            {
                section = new ConfigSection(sectionName, Lines.Any() ? Lines.Max(l => l.LineNumber) : 0);
                if (Sections.Any())
                {
                    Sections.Last().AddLine(new ConfigLine());
                }
                sections.Add(sectionName, section);
            }

            var key = (section ?? fileHeader?.Section).Keys.FirstOrDefault(k => Equals(keyName, k.Name));

            if (key != null)
            {
                return((T)key.ValueRaw);
            }

            if (section is null && Settings.MultiLineValues.HasFlag(MultiLineValues.AllowEmptyTopSection))
            {
                section = fileHeader.Section;
            }

            section?.AddLine(iniKey);
            return(defaultValue);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Sets the value.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <param name="keyName">Name of the key.</param>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException"></exception>
        public bool SetValue(string sectionName, string keyName, string value)
        {
            if (string.IsNullOrWhiteSpace(sectionName))
            {
                throw new ArgumentNullException(nameof(sectionName));
            }
            if (string.IsNullOrWhiteSpace(keyName))
            {
                throw new ArgumentNullException(nameof(keyName));
            }

            if (!sections.TryGetValue(sectionName, out var section))
            {
                var lineNumber = (null != Lines && Lines.Any()) ? Lines.Max(l => l.LineNumber) : -1;
                section = new ConfigSection(sectionName, lineNumber);
                sections.Add(sectionName, section);
            }

            if (section == null)
            {
                Logger?.Warn($"Failed to create {sectionName} and store {keyName}={value} key");
                return(false);
            }

            var iniKey = section.Keys
                         .FirstOrDefault(k => Equals(keyName, k.Name));

            if (iniKey != null)
            {
                iniKey.ValueRaw = value;
            }
            else
            {
                iniKey = new ConfigKeyValue <string>(keyName, Settings.KeyValueSeparator, value, -1);
                section.AddLine((ConfigLine)iniKey);
            }
            return(true);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Reads the key and value.
        /// </summary>
        /// <param name="currentSection">The current section.</param>
        /// <param name="currentLine">The current line.</param>
        /// <param name="lineRaw">The line raw.</param>
        /// <param name="lineNumber">The line number.</param>
        /// <param name="append">if set to <c>true</c> [append].</param>
        /// <exception cref="ConfigParserException">Arrays must start from a new line and not after the key!
        /// or</exception>
        /// <exception cref="NotImplementedException"></exception>
        private void ReadKeyAndValue(ref ConfigSection currentSection, ref ConfigLine currentLine, string lineRaw,
                                     int lineNumber, bool append = false)
        {
            if (null != currentLine && !append)
            {
                BackupCurrentLine(ref currentSection, ref currentLine, lineNumber);
            }

            if (append && null == currentLine)
            {
                throw new ConfigParserException("You are trying to append value to a null line!", lineNumber);
            }

            var keyMatch  = Settings.KeyMatcher.Match(lineRaw);
            var keyName   = keyMatch.Groups["key"]?.Value;
            var separator = (string.IsNullOrWhiteSpace(keyMatch.Groups["separator"]?.Value))
                ? Settings.KeyValueSeparator
                : keyMatch.Groups["separator"]?.Value;

            if (keyMatch.Success && keyMatch.Captures.Count > 0)
            {
                lineRaw = lineRaw.Substring(keyMatch.Captures[0].Value.Length);
            }

            var valueMatch = Settings.ValueMatcher.Match(lineRaw);
            var value      = valueMatch.Groups["value"]?.Value;

            switch (Settings.MultiLineValues)
            {
            case var _ when Settings.MultiLineValues.HasFlag(MultiLineValues.QuoteDelimitedValues):
                if (!string.IsNullOrEmpty(value))
                {
                    if (Equals('"', value.First()))
                    {
                        value = value.Substring(1);
                    }
                    if (Equals('"', value.Last()))
                    {
                        value = value.Remove(value.Length - 1);
                    }
                }

                break;

            case var _ when Settings.MultiLineValues.HasFlag(MultiLineValues.NotAllowed) ||
                Settings.MultiLineValues.HasFlag(MultiLineValues.Simple):
                // Do nothing add with quotes if any
                break;

            default:
                throw new ConfigParserException("Unknown key=value situation detected!", lineNumber);
            }

            if (append)
            {
                currentLine.Content = $"{currentLine.Content}{Settings.NewLine}{value}";
            }
            else
            {
                currentLine = new ConfigKeyValue <object>(keyName, separator, value, lineNumber);
            }
        }