/// <summary> /// Executes a binding operation with internal data source. /// </summary> /// <seealso href="c49dc3a5-866f-4d2d-8f89-db303aceb5fe.htm#binding" target="_self">IniKey's Value Binding</seealso> public void Bind() { foreach (var placeholderPair in this.GetPlaceholderPairs(null)) { IniKey placeholderKey = placeholderPair.Key; string placeholder = placeholderPair.Value; string placeholderName = placeholder.Substring(2, placeholder.Length - 3); string targetedValue; int separator = placeholder.IndexOf('|'); if (separator != -1) { var targetedSection = this.iniFile.Sections[placeholder.Substring(2, separator - 2)]; if (targetedSection == null) { continue; } targetedValue = GetTargetedValue( targetedSection, placeholder.Substring(separator + 1, placeholder.Length - separator - 2)); } else { targetedValue = GetTargetedValue( placeholderKey.ParentSection, placeholderName); } this.ExecuteBinding(placeholder, placeholderName, placeholderKey, targetedValue); } }
/// <summary> /// Executes a binding operation with external data source, only on specified section. /// </summary> /// <param name="dataSource">The binding data source.</param> /// <param name="sectionName">The <see cref="IniSection"/>'s name.</param> /// <seealso href="c49dc3a5-866f-4d2d-8f89-db303aceb5fe.htm#binding" target="_self">IniKey's Value Binding</seealso> public void Bind(object dataSource, string sectionName) { if (dataSource == null) { throw new ArgumentNullException("dataSource"); } var dataSourceDictionary = CreateDataSourceDictionary(dataSource); if (dataSourceDictionary == null) { return; } foreach (var placeholderPair in this.GetPlaceholderPairs(this.iniFile.Sections[sectionName])) { IniKey placeholderKey = placeholderPair.Key; string placeholder = placeholderPair.Value; string placeholderName = placeholder.Substring(2, placeholder.Length - 3); string targetedValue; dataSourceDictionary.TryGetValue(placeholderName, out targetedValue); this.ExecuteBinding(placeholder, placeholderName, placeholderKey, targetedValue); } }
private static void SetKeyValue(PropertyInfo property, object source, IniKey key) { object propertyValue = property.GetValue(source, null); if (propertyValue == null) { return; } if (property.PropertyType.IsArray || property.PropertyType.GetInterface(typeof(IList).Name) != null) { var values = new List <string>(); /* MZ(2016-01-02): Fixed issue with null items in array and list. */ foreach (var item in (IEnumerable)propertyValue) { values.Add(item != null ? item.ToString() : null); } key.Values = values.ToArray(); } else { key.Value = propertyValue.ToString(); } }
private void ReadKey(int leftIndention, string line, IniFile file) { int keyDelimiterIndex = line.IndexOf((char)this.options.KeyDelimiter, leftIndention); if (keyDelimiterIndex != -1) { if (this.currentSection == null) { this.currentSection = file.Sections.Add(IniSection.GlobalSectionName); } /* MZ(2016-04-04): Fixed issue with trimming values. */ bool spacedDelimiter = keyDelimiterIndex > 0 && line[keyDelimiterIndex - 1] == ' '; string keyName = line.Substring(leftIndention, keyDelimiterIndex - leftIndention - (spacedDelimiter ? 1 : 0)); var currentKey = new IniKey(file, keyName, this.currentTrailingComment) { LeftIndentation = leftIndention, LeadingComment = { EmptyLinesBefore = this.currentEmptyLinesBefore } }; this.currentSection.Keys.Add(currentKey); ++keyDelimiterIndex; if (spacedDelimiter && keyDelimiterIndex < line.Length && line[keyDelimiterIndex] == ' ') { ++keyDelimiterIndex; } this.ReadValue(line.Substring(keyDelimiterIndex), currentKey); } this.currentTrailingComment = null; }
internal void Initialize(string placeholderName, IniKey placeholderKey, string value, bool isValueFound) { this.placeholderName = placeholderName; this.placeholderKey = placeholderKey; this.Value = value; this.isValueFound = isValueFound; }
private void ReadKeyValueAndLeadingComment(string lineLeftover, IniKey key) { /* REMARKS: First occurrence of comment's starting character (e.g. ';') defines key's value. * * CONSIDER: Implement a support for quoted values, thus enabling them to contain comment's starting characters. */ int valueEndIndex = lineLeftover.IndexOf((char)this.options.CommentStarter); if (valueEndIndex == -1) { key.Value = lineLeftover.TrimEnd(); } else if (valueEndIndex == 0) { key.Value = key.LeadingComment.Text = string.Empty; } else { key.LeadingComment.Text = lineLeftover.Substring(valueEndIndex + 1); // The amount of 'whitespace' characters between key's value and comment's starting character. int leftIndention = 0; while (lineLeftover[--valueEndIndex] == ' ' || lineLeftover[valueEndIndex] == '\t') { leftIndention++; } key.LeadingComment.LeftIndentation = leftIndention; key.Value = lineLeftover.Substring(0, ++valueEndIndex); } }
private void ReadKey(int leftIndention, string line, IniFile file) { int keyDelimiterIndex = line.IndexOf((char)this.options.KeyDelimiter, leftIndention); if (keyDelimiterIndex != -1) { if (this.currentSection == null) { this.currentSection = file.Sections.Add(IniSection.GlobalSectionName); } var currentKey = new IniKey(file, line.Substring(leftIndention, keyDelimiterIndex - leftIndention).TrimEnd(), this.currentTrailingComment) { LeftIndentation = leftIndention, LeadingComment = { EmptyLinesBefore = this.currentEmptyLinesBefore } }; this.currentSection.Keys.Add(currentKey); this.ReadKeyValueAndLeadingComment(line.Substring(++keyDelimiterIndex).TrimStart(), currentKey); } this.currentTrailingComment = null; }
private void ExecuteBinding(string placeholder, string placeholderName, IniKey placeholderKey, string targetedValue) { this.args.Initialize(placeholderName, placeholderKey, targetedValue, targetedValue != null); if (this.Binding != null) { this.Binding(this, this.args); } if (this.args.Value != null) { placeholderKey.Value = placeholderKey.Value.Replace(placeholder, this.args.Value); } this.args.Reset(); }
private void ReadValue(string lineLeftover, IniKey key) { int valueEndIndex = lineLeftover.IndexOf((char)this.options.CommentStarter); /* MZ(2016-04-04): Fixed issue with trimming values. */ if (valueEndIndex == -1) { key.Value = lineLeftover; } else if (valueEndIndex == 0) { key.Value = string.Empty; key.LeadingComment.Text = lineLeftover.Substring(1); } else { this.ReadValueLeadingComment(lineLeftover, valueEndIndex, key); } }
private static string GetTargetedValue(IniSection targetedSection, string targetedKeyName) { IniKey targetedKey = targetedSection.Keys[targetedKeyName]; return(targetedKey != null ? targetedKey.Value : null); }
private static void SetPropertyValue(PropertyInfo property, object destination, IniKey key) { var propertyType = property.PropertyType; if (propertyType.IsArray) { /* MZ(2016-01-02): Fixed issue with null array and list. */ if (!key.IsValueArray) { return; } var values = key.Values; var itemType = propertyType.GetElementType(); var array = Array.CreateInstance(itemType, values.Length); for (int i = 0; i < values.Length; i++) { array.SetValue( TypeDescriptor.GetConverter(itemType).ConvertFromInvariantString(values[i]), i); } property.SetValue(destination, array, null); } else if (propertyType.GetInterface(typeof(IList).Name) != null) { /* MZ(2016-01-02): Fixed issue with null array and list. */ if (!key.IsValueArray) { return; } var itemType = propertyType.GetGenericArguments()[0]; var list = (IList)Activator.CreateInstance(typeof(List <>).MakeGenericType(itemType)); var values = key.Values; if (!(values.Length == 1 && string.IsNullOrEmpty(values[0]))) { foreach (var value in values) { list.Add( TypeDescriptor.GetConverter(itemType).ConvertFromInvariantString(value)); } } property.SetValue(destination, list, null); } else { property.SetValue( destination, TypeDescriptor.GetConverter(propertyType).ConvertFromInvariantString(key.Value), null); } }
/* MZ(2016-02-23): Added support for quoted values which can contain comment's starting characters. */ private void ReadValueLeadingComment(string lineLeftover, int potentialCommentIndex, IniKey key) { int quoteEndIndex = lineLeftover.IndexOf('"', 1); if (lineLeftover[0] == '"' && quoteEndIndex != -1) { while (quoteEndIndex > potentialCommentIndex && potentialCommentIndex != -1) { potentialCommentIndex = lineLeftover.IndexOf((char)this.options.CommentStarter, ++potentialCommentIndex); } } if (potentialCommentIndex == -1) { key.Value = lineLeftover.TrimEnd(); } else { key.LeadingComment.Text = lineLeftover.Substring(potentialCommentIndex + 1); // The amount of 'whitespace' characters between key's value and comment's starting character. int leftIndention = 0; while (potentialCommentIndex > 0 && (lineLeftover[--potentialCommentIndex] == ' ' || lineLeftover[potentialCommentIndex] == '\t')) { leftIndention++; } key.LeadingComment.LeftIndentation = leftIndention; key.Value = lineLeftover.Substring(0, ++potentialCommentIndex); } }
// Deep copy constructor. internal IniKey(IniFile destinationFile, IniKey sourceKey) : base(destinationFile, sourceKey) { this.Value = sourceKey.Value; }