Behavior for a IniFile. This describes how the file is laid out - delimiters, comments, line continuation escapes, and so on.
Пример #1
0
        /// <summary>Initialise the file, loading it from the system.</summary>
        /// <param name="path"></param>
        /// <param name="behavior"></param>
        public IniFile(string path, IniFileBehavior behavior)
            : this(behavior)
        {
            var lines = File.ReadAllLines(path, Encoding.ASCII);
            IniFileSection section = null;

            for (var index = 0; index < lines.Length; index++) {
                var line = lines[index].Trim();

                if (line.Length == 0 || line[0] == Behavior.CommentDelimiter)
                    continue;
                if (line[0] == '[') {
                    int end = line.IndexOf(']');

                    if (end < 0)
                        throw new Exception("Expected ']' to terminate a section name.");
                    section = FindOrCreateSection(line.Substring(1, end - 1));
                } else {
                    var comment = -1;
                    var split = line.IndexOf(behavior.SettingValueDelimiter);
                    string name, value;

                    if (Behavior.CommentInValue) {
                        comment = line.IndexOf(behavior.CommentDelimiter);
                        if (behavior.CommentDelimiter2 != null)
                            comment = Extensions.GetSmallerIndex(comment, line.IndexOf(behavior.CommentDelimiter2));
                    }

                    if (split < 0 && comment >= 0) {
                        for (int chIndex = 0; chIndex < comment; chIndex++)
                            if (!char.IsWhiteSpace(line[chIndex]))
                                throw new Exception("This line has non-whitespace before a comment, but is not a setting.");
                        continue;
                    }

                    if (split < 0 || (comment >= 0 && comment < split))
                        throw new Exception("This line cannot be parsed as a setting, but it's the only thing it could be.");

                    name = line.Substring(0, split).TrimEnd();
                    if (comment >= 0)
                        value = line.Substring(split + 1, comment - split - 1).Trim();
                    else
                        value = line.Substring(split + 1).Trim();

                    new IniFileSetting(section, name, value);
                }
            }
        }
Пример #2
0
        /// <summary>Initialise the file, loading it from the system.</summary>
        /// <param name="path"></param>
        /// <param name="behavior"></param>
        public IniFile(string path, IniFileBehavior behavior)
            : this(behavior) {
            var            lines   = File.ReadAllLines(path, Encoding.ASCII);
            IniFileSection section = null;

            for (var index = 0; index < lines.Length; index++)
            {
                var line = lines[index].Trim();

                if (line.Length == 0 || line[0] == Behavior.CommentDelimiter)
                {
                    continue;
                }
                if (line[0] == '[')
                {
                    int end = line.IndexOf(']');

                    if (end < 0)
                    {
                        throw new Exception("Expected ']' to terminate a section name.");
                    }
                    section = FindOrCreateSection(line.Substring(1, end - 1));
                }
                else
                {
                    var    comment = -1;
                    var    split = line.IndexOf(behavior.SettingValueDelimiter);
                    string name, value;

                    if (Behavior.CommentInValue)
                    {
                        comment = line.IndexOf(behavior.CommentDelimiter);
                        if (behavior.CommentDelimiter2 != null)
                        {
                            comment = Extensions.GetSmallerIndex(comment, line.IndexOf(behavior.CommentDelimiter2));
                        }
                    }

                    if (split < 0 && comment >= 0)
                    {
                        for (int chIndex = 0; chIndex < comment; chIndex++)
                        {
                            if (!char.IsWhiteSpace(line[chIndex]))
                            {
                                throw new Exception("This line has non-whitespace before a comment, but is not a setting.");
                            }
                        }
                        continue;
                    }

                    if (split < 0 || (comment >= 0 && comment < split))
                    {
                        throw new Exception("This line cannot be parsed as a setting, but it's the only thing it could be.");
                    }

                    name = line.Substring(0, split).TrimEnd();
                    if (comment >= 0)
                    {
                        value = line.Substring(split + 1, comment - split - 1).Trim();
                    }
                    else
                    {
                        value = line.Substring(split + 1).Trim();
                    }

                    new IniFileSetting(section, name, value);
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Create a new in-memory initialization file with the defined behavior.
 /// </summary>
 /// <param name="behavior">The behavior to follow.</param>
 public IniFile(IniFileBehavior behavior)
 {
     Sections = new List<IniFileSection>();
     SectionsByName = new Dictionary<string, IniFileSection>();
     Behavior = behavior;
 }
Пример #4
0
 /// <summary>
 /// Create a new in-memory initialization file with the defined behavior.
 /// </summary>
 /// <param name="behavior">The behavior to follow.</param>
 public IniFile(IniFileBehavior behavior)
 {
     Sections       = new List <IniFileSection>();
     SectionsByName = new Dictionary <string, IniFileSection>();
     Behavior       = behavior;
 }