Esempio n. 1
0
 private static void Parse3Tokens(Stream stream, out string token1, out string token2, out string token3)
 {
     var reader = new StringReader(stream.ReadLine());
     token1 = reader.ReadUntilWhitespace();
     reader.SkipWhitespace();
     token2 = reader.ReadUntilWhitespace();
     reader.SkipWhitespace();
     token3 = reader.ReadLine();
 }
Esempio n. 2
0
        private static void ParseHeaders(Stream stream, Action<string, string> onHeader)
        {
            string headerLine = null;

            while (true)
            {
                headerLine = stream.ReadLine();

                if (headerLine == String.Empty)
                {
                    break;
                }

                var headerReader = new StringReader(headerLine);
                string key = headerReader.ReadUntil(c => c == ':');
                headerReader.Read();
                headerReader.SkipWhitespace();
                string value = headerReader.ReadToEnd();

                onHeader(key, value);
            }
        }
        /// <summary>
        /// Parses a single configuration line.
        /// </summary>
        /// <param name="lineIndex">The index of the line.</param>
        /// <param name="line">The contents of the line.</param>
        /// <param name="currentSectionName">The name of the current section.</param>
        private void Parse(int lineIndex, string line, ref string currentSectionName)
        {
            char c;
            var reader = new StringReader(line);
            reader.SkipWhitespace();

            // First character
            if (!reader.TryRead(out c))
                return;

            if (c == '[') {
                // Read section name
                var sectionNameBuilder = new StringBuilder();
                while (reader.TryRead(out c) && c != ']') {
                    sectionNameBuilder.Append(c);
                }
                if (c != ']')
                    throw new Exception("Invalid section name");

                // Set and add section
                currentSectionName = sectionNameBuilder.ToString().Trim();
                Section section;
                if (!_sections.TryGetValue(currentSectionName, out section))
                    _sections.Add(currentSectionName, section = new Section(currentSectionName));
                section.Lines.Add(lineIndex);
            } else if (c != ';' && c != '#') {
                // Read property name and value
                var propertyNameBuilder = new StringBuilder();
                propertyNameBuilder.Append(c);
                while (reader.TryRead(out c) && c != '=' && c != ':') {
                    propertyNameBuilder.Append(c);
                }
                if (c != '=' && c != ':')
                    throw new Exception("Expected = or : after property name");

                string propertyName = propertyNameBuilder.ToString().Trim();
                string value = ReadPropertyValue(reader);

                // Set or add property
                Property property = GetOrAddProperty(currentSectionName, propertyName);
                property.Value = value;
                property.Lines.Add(lineIndex);
            }
        }