Example #1
0
        public Task Parse()
        {
            currentGroup  = null;
            KeyValuePairs = new Dictionary <string, string>();
            Groups        = new Dictionary <string, KeyValueConfigGroup>();
            currentLine   = 0;

            using (var sr = new StreamReader(Path))
            {
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    currentLine++;

                    //primitive parsing for attributes
                    if (line.StartsWith('[') && line.EndsWith(']'))
                    {
                        ParseAsAttribute(line);
                    }
                    else
                    {
                        ParseAsKeyValue(line);
                    }
                }
            }
            return(Task.CompletedTask);
        }
Example #2
0
        private Task ParseAsAttribute(string line)
        {
            //remove begginning [ and ending ]
            line = new string(line.Skip(1).ToArray());
            line = new string(line.Take(line.Length - 1).ToArray());

            //remove whitespace
            line = line.Replace(" ", "");

            string[] splitLine = line.Split(':', 2);

            if (splitLine.Length != 2)
            {
                ThrowConfigException("Invalid attribute syntax");
            }

            string token = splitLine[0].ToLower();
            string key   = splitLine[1];

            //for now hardcheck for attribute token
            if (token == "group")
            {
                if (Groups.ContainsKey(key))
                {
                    ThrowConfigException("Group key already in use");
                }

                currentGroup = new KeyValueConfigGroup(key);
                Groups.Add(key, currentGroup);
            }
            else
            {
                ThrowConfigException("Unknown attribute token");
            }
            return(Task.CompletedTask);
        }