private static void ReadValues(string currentLine, StringReader reader, ConfigurationSectionList section) { var split = currentLine.Split(Equal, 2); if (split.Length != 2) { return; } var config = new ConfigurationSectionList(); while (TryReadLine(reader, out currentLine)) { if (CanSkip(currentLine)) { continue; } if (currentLine[0] == GroupClose) { break; } ReadValue(currentLine, reader, config); } section.Add(new KeyValuePair <string, object>(split[0].Trim(), config)); }
/// <summary> /// Deserialize a configuration value into a <see cref="ConfigurationSectionList" /> for querying values by key. /// </summary> /// <param name="configuration">The configuration to parse.</param> /// <returns>Returns a configuration list of key-value pairs.</returns> public static ConfigurationSectionList Deserialize(string configuration) { if (configuration is null) { throw new ArgumentNullException(nameof(configuration)); } var reader = new StringReader(configuration); var config = new ConfigurationSectionList(); while (TryReadLine(reader, out string currentLine)) { if (CanSkip(currentLine)) { continue; } while (IsSectionLine(currentLine)) { var section = new ConfigurationSectionList(); currentLine = ReadSection(currentLine, reader, section); config.Add(new KeyValuePair <string, object>(section.Name, section)); } } return(config); }
private static void SerializeSection(StringBuilder sb, ConfigurationSectionList section, Krb5ConfigurationSerializationConfig serializerConfig) { sb.AppendFormat(CultureInfo.InvariantCulture, "[{0}]", section.Name); sb.AppendLine(); foreach (var config in section) { var val = config.Value; SerializeValue(sb, config.Key, val, serializerConfig); } sb.AppendLine(); }
public void Parse(string value) { if (string.IsNullOrWhiteSpace(value)) { return; } var split = value.Split(SplitOn, StringSplitOptions.RemoveEmptyEntries); if (split.Length != 2) { return; } this.EType = (EncryptionType)ConfigurationSectionList.ParseEnum(split[0], typeof(EncryptionType)); this.SaltType = (KeySaltType)ConfigurationSectionList.ParseEnum(split[1], typeof(KeySaltType)); }
/// <summary> /// Converts a <see cref="Krb5Config"/> instance into a <see cref="ConfigurationSectionList" /> for possible future serialization. /// </summary> /// <param name="config"></param> /// <returns></returns> public static ConfigurationSectionList FromConfigObject(Krb5Config config) { if (config is null) { throw new ArgumentNullException(nameof(config)); } var list = new ConfigurationSectionList(); foreach (var property in config.GetType().GetProperties()) { var section = property.GetValue(config); list.Add(AddSection(section, property)); } return(list); }
private static void SerializeValues(StringBuilder sb, string key, ConfigurationSectionList multiVal, Krb5ConfigurationSerializationConfig serializerConfig) { Indent(sb, serializerConfig); sb.AppendFormat(CultureInfo.InvariantCulture, "{0} = {{", key); sb.AppendLine(); serializerConfig.CurrentIndent++; foreach (var multi in multiVal) { var val = multi.Value; SerializeValue(sb, multi.Key, val, serializerConfig); } serializerConfig.CurrentIndent--; Indent(sb, serializerConfig); sb.AppendLine("}"); }
private static void ReadValue(string currentLine, StringReader reader, ConfigurationSectionList section) { if (currentLine.IndexOf(GroupOpen) >= 0) { ReadValues(currentLine, reader, section); } else { var split = currentLine.Split(Equal, 2); if (split.Length == 2) { section.Add(new KeyValuePair <string, object>(split[0].Trim(), split[1].Trim())); } if (currentLine[currentLine.Length - 1] == EndOfValue) { // skip future } } }
private static string ReadSection(string currentLine, StringReader reader, ConfigurationSectionList section) { section.Name = currentLine.Substring(1, currentLine.Length - 2); while (TryReadLine(reader, out currentLine)) { if (CanSkip(currentLine)) { continue; } if (currentLine[0] == SectionOpen) { break; } ReadValue(currentLine, reader, section); } return(currentLine); }
/// <summary> /// Serialize a configuration list into a configuration file. /// </summary> /// <param name="configuration">The configuration to serialize.</param> /// <param name="serializerConfig">Optional configuration that describes the format requested.</param> /// <returns>Returns the configuration in string form.</returns> public static string Serialize(ConfigurationSectionList configuration, Krb5ConfigurationSerializationConfig serializerConfig = null) { if (configuration is null) { throw new ArgumentNullException(nameof(configuration)); } if (serializerConfig is null) { serializerConfig = new Krb5ConfigurationSerializationConfig(); } var sb = new StringBuilder(); foreach (var config in configuration) { var section = config.Value as ConfigurationSectionList; SerializeSection(sb, section, serializerConfig); } return(sb.ToString()); }
/// <summary> /// Converts a <see cref="Krb5Config"/> instance into a <see cref="ConfigurationSectionList" /> for possible future serialization. /// </summary> /// <param name="config">The configuration instance to load</param> /// <param name="serializationConfig">Serializer configuration options</param> /// <returns>Returns a sectioned version of the configuration</returns> public static ConfigurationSectionList FromConfigObject(Krb5Config config, Krb5ConfigurationSerializationConfig serializationConfig) { if (config is null) { throw new ArgumentNullException(nameof(config)); } if (serializationConfig == null) { serializationConfig = new Krb5ConfigurationSerializationConfig(); } var list = new ConfigurationSectionList(); foreach (var property in config.GetType().GetProperties(PublicInstancePropertyFlags)) { var section = property.GetValue(config); list.Add(AddSection(section, property, serializationConfig)); } return(list); }
/// <summary> /// Serialize a configuration object instance into a configuration file. /// </summary> /// <param name="configuration">The configuration to serialize.</param> /// <param name="serializationConfig">Optional configuration that describes the format requested.</param> /// <returns>Returns the configuration in string form.</returns> public static string Serialize(Krb5Config configuration, Krb5ConfigurationSerializationConfig serializationConfig = null) { return(Serialize(ConfigurationSectionList.FromConfigObject(configuration), serializationConfig)); }