Esempio n. 1
0
        private static bool ProcessSection(IniLineRange line, out IniSection section)
        {
            section = null;
            if (!textBuffer.IsContentStartWith(line, schemeStyle.SectionPrefix))
            {
                return(false);
            }
            int index = textBuffer.FindContent(line.ContentStart, line.ContentEnd, schemeStyle.SectionPostfix);

            if (index < 0)
            {
                throw new IniReaderLineFormatException(textBuffer.GetContent(line));
            }
            int    prefixLen   = schemeStyle.SectionPrefix.Length;
            string sectionName = textBuffer.GetContent(line.ContentStart + prefixLen, index - 1);

            if (string.IsNullOrEmpty(sectionName))
            {
                throw new IniReaderLineFormatException(textBuffer.GetContent(line));
            }
            if (string.IsNullOrEmpty(cachedSectionName) || cachedSectionName != sectionName)
            {
                section = new IniSection(sectionName);
            }
            cachedSectionName = sectionName;
            return(true);
        }
Esempio n. 2
0
        private static bool ProcessOptionalValue(IniLineRange line)
        {
            if (!textBuffer.IsContentStartWith(line, schemeStyle.OptionalValuePrefix))
            {
                return(false);
            }
            int index = textBuffer.FindContent(line.ContentStart, line.ContentEnd, schemeStyle.OptionalValuePostfix);

            if (index < 0)
            {
                throw new IniReaderLineFormatException(textBuffer.GetContent(line));
            }
            if (!readerStyle.IsParseOptionalValues)
            {
                return(true);
            }

            int    prefixLen    = schemeStyle.OptionalValuePrefix.Length;
            string valueContent = textBuffer.GetContent(line.ContentStart + prefixLen, index - line.ContentStart - prefixLen);

            string[] values = valueContent.Split(new string[] { schemeStyle.OptionalValueAssigment }, StringSplitOptions.RemoveEmptyEntries);
            if (values != null && values.Length > 0)
            {
                if (readerStyle.IsTrimOptionalValues)
                {
                    values = (from value in values let v = value.Trim() where !string.IsNullOrEmpty(v) select v).ToArray();
                }
                if (values.Length > 0)
                {
                    cachedOptionalValues.AddRange(values);
                }
            }
            return(true);
        }
Esempio n. 3
0
        private static ProcessLineState ProcessComment(IniTextBuffer buffer, IniData iniData)
        {
            IniLineRange range = buffer.Range.DeepCopy();

            buffer.TrimStart(range);
            if (!buffer.IsStartWith(range, schemeStyle.CommentString))
            {
                return(ProcessLineState.Unrecognized);
            }
            if (!readerStyle.IsParseComments)
            {
                return(ProcessLineState.Success);
            }

            int startIndex = buffer.FindString(range, schemeStyle.CommentString) + schemeStyle.CommentString.Length;
            int endIndex   = range.End;

            range.Start = startIndex;
            range.Size  = endIndex - startIndex + 1;

            if (readerStyle.IsTrimComments)
            {
                buffer.Trim(range);
            }

            string comment = buffer.GetString(range);

            tempComments.Add(comment);

            return(ProcessLineState.Success);
        }
Esempio n. 4
0
        private static ProcessLineState ProcessOptionalValue(IniTextBuffer buffer, IniData iniData)
        {
            IniLineRange range = buffer.Range.DeepCopy();

            buffer.Trim(range);
            if (!buffer.IsStartWith(range, schemeStyle.OptionalValueStartString))
            {
                return(ProcessLineState.Unrecognized);
            }
            if (!buffer.IsEndWith(range, schemeStyle.OptionalValueEndString))
            {
                if (!readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException($"Error:No closing option value({schemeStyle.OptionalValueEndString}). ", buffer.LineNumber, buffer.LineContent);
            }

            if (!readerStyle.IsParseOptionalValues)
            {
                return(ProcessLineState.Success);
            }

            int startIndex = range.Start + schemeStyle.OptionalValueStartString.Length;
            int endIndex   = range.End - schemeStyle.OptionalValueEndString.Length;

            range.Start = startIndex;
            range.Size  = endIndex - startIndex + 1;

            string optionalValueStr = buffer.GetString(range);

            if (string.IsNullOrEmpty(optionalValueStr))
            {
                if (!readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException($"Error:The value of optionalValue is empty. ", buffer.LineNumber, buffer.LineContent);
            }

            string[] optionalValues = optionalValueStr.Split(new string[] { schemeStyle.OptionalValueAssigmentString }, StringSplitOptions.RemoveEmptyEntries);
            if (readerStyle.IsTrimOptionalValues)
            {
                foreach (var v in optionalValues)
                {
                    string trimedValue = v.Trim();
                    if (!string.IsNullOrEmpty(trimedValue))
                    {
                        tempOptionalValues.Add(trimedValue);
                    }
                }
            }
            else
            {
                tempOptionalValues.AddRange(optionalValues);
            }
            return(ProcessLineState.Success);
        }
        public string GetContent(IniLineRange line)
        {
            if (line.ContentSize == 0)
            {
                return(string.Empty);
            }

            return(Text.Substring(line.ContentStart, line.ContentSize));
        }
Esempio n. 6
0
        public static IniConfig ReadFromString(string iniString, IniSchemeStyle schemeStyle = null, IniReaderStyle readerStyle = null)
        {
            if (string.IsNullOrEmpty(iniString))
            {
                return(null);
            }

            IniReader.schemeStyle = schemeStyle ?? new IniSchemeStyle();
            IniReader.readerStyle = readerStyle ?? new IniReaderStyle();
            cachedComments.Clear();
            cachedOptionalValues.Clear();
            cachedSectionName = null;

            textBuffer.Text = iniString;
            IniConfig    iniConfig = new IniConfig();
            IniLineRange iniLine   = new IniLineRange();

            int lineStartIndex = 0;

            while (textBuffer.ReadLine(lineStartIndex, ref iniLine))
            {
                lineStartIndex = iniLine.End + 1;
                if (textBuffer.IsContentWhitespace(iniLine))
                {
                    continue;
                }
                if (ProcessCommentLine(iniLine))
                {
                    continue;
                }
                if (ProcessSection(iniLine, out var section))
                {
                    iniConfig.AddSection(section);
                    section.Comments.AddRange(cachedComments);
                    cachedComments.Clear();
                    continue;
                }
                if (ProcessOptionalValue(iniLine))
                {
                    continue;
                }
                if (ProcessProperty(iniLine, out var property))
                {
                    property.Comments.AddRange(cachedComments);
                    property.OptionalValues.AddRange(cachedOptionalValues);

                    var s = iniConfig.GetSection(cachedSectionName);
                    s.AddProperty(property);
                    continue;
                }
            }

            return(iniConfig);
        }
        public bool IsContentWhitespace(IniLineRange line)
        {
            if (line.ContentSize == 0)
            {
                return(true);
            }
            int index = line.ContentStart;

            while (index <= line.ContentEnd && !char.IsWhiteSpace(Text[index]))
            {
                return(false);
            }
            return(true);
        }
Esempio n. 8
0
        private static ProcessLineState ProcessSection(IniTextBuffer buffer, IniData iniData)
        {
            IniLineRange range = buffer.Range.DeepCopy();

            buffer.Trim(range);
            if (!buffer.IsStartWith(range, schemeStyle.SectionStartString))
            {
                return(ProcessLineState.Unrecognized);
            }
            if (!buffer.IsEndWith(range, schemeStyle.SectionEndString))
            {
                if (!readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException($"Error:No closing section value({schemeStyle.SectionEndString}). ", buffer.LineNumber, buffer.LineContent);
            }

            int startIndex = range.Start + schemeStyle.SectionStartString.Length;
            int endIndex   = range.End - schemeStyle.SectionEndString.Length;

            range.Start = startIndex;
            range.Size  = endIndex - startIndex + 1;

            if (readerStyle.IsTrimSections)
            {
                buffer.Trim(range);
            }

            string sectionName = buffer.GetString(range);

            if (string.IsNullOrEmpty(sectionName))
            {
                if (!readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException($"Error:The name of section is empty. ", buffer.LineNumber, buffer.LineContent);
            }

            tempSectionName = sectionName;
            IniSection section = iniData.AddSection(tempSectionName);

            if (readerStyle.IsParseComments && tempComments.Count > 0)
            {
                section.Comments = tempComments;
                tempComments.Clear();
            }
            return(ProcessLineState.Success);
        }
        public void TrimContentEnd(IniLineRange line)
        {
            if (line.ContentSize == 0)
            {
                return;
            }

            while (line.ContentEnd >= line.ContentStart && char.IsWhiteSpace(Text[line.ContentEnd]))
            {
                line.ContentEnd--;
            }
            if (line.ContentEnd < line.ContentStart)
            {
                line.ContentStart = line.ContentEnd = -1;
            }
        }
        public void TrimContentStart(IniLineRange line)
        {
            if (line.ContentSize == 0)
            {
                return;
            }

            while (line.ContentStart <= line.ContentEnd && char.IsWhiteSpace(Text[line.ContentStart]))
            {
                line.ContentStart++;
            }
            if (line.ContentStart > line.ContentEnd)
            {
                line.ContentStart = line.ContentEnd = -1;
            }
        }
Esempio n. 11
0
        private static bool ProcessProperty(IniLineRange line, out IniProperty property)
        {
            property = null;
            string valueContent = textBuffer.GetContent(line);

            if (string.IsNullOrEmpty(valueContent))
            {
                return(false);
            }
            if (string.IsNullOrEmpty(cachedSectionName))
            {
                throw new IniReaderLineSectionNullException();
            }

            int assigmentIndex = valueContent.IndexOf(schemeStyle.PropertyAssigment);

            if (assigmentIndex < 0)
            {
                throw new IniReaderLineFormatException(valueContent);
            }
            string propertyKey   = valueContent.Substring(0, assigmentIndex - schemeStyle.PropertyAssigment.Length);
            string propertyValue = valueContent.Substring(assigmentIndex + schemeStyle.PropertyAssigment.Length);

            if (readerStyle.IsTrimPropertyKey)
            {
                propertyKey = propertyKey.Trim();
            }
            if (string.IsNullOrEmpty(propertyKey))
            {
                throw new IniReaderLinePropertyKeyEmptyException();
            }
            if (readerStyle.IsTrimPropertyValue)
            {
                propertyValue = propertyValue.Trim();
            }

            property = new IniProperty(propertyKey, propertyValue);

            return(true);
        }
        public bool IsContentEndWith(IniLineRange line, string postfix)
        {
            if (line.ContentSize == 0 || string.IsNullOrEmpty(postfix))
            {
                return(false);
            }
            if (line.ContentSize < postfix.Length)
            {
                return(false);
            }

            for (int lineEndIndex = line.ContentEnd, postfixIndex = postfix.Length - 1;
                 postfixIndex >= 0;
                 lineEndIndex--, postfixIndex--)
            {
                if (postfix[postfixIndex] != Text[lineEndIndex])
                {
                    return(false);
                }
            }
            return(true);
        }
        public bool IsContentStartWith(IniLineRange line, string prefix)
        {
            if (line.ContentSize == 0 || string.IsNullOrEmpty(prefix))
            {
                return(false);
            }
            if (line.ContentSize < prefix.Length)
            {
                return(false);
            }

            for (int lineStartIndex = line.ContentStart, prefixIndex = 0;
                 prefixIndex < prefix.Length;
                 lineStartIndex++, prefixIndex++)
            {
                if (prefix[prefixIndex] != Text[lineStartIndex])
                {
                    return(false);
                }
            }
            return(true);
        }
        public bool ReadLine(int start, ref IniLineRange line)
        {
            if (string.IsNullOrEmpty(Text) || start < 0 || start >= Text.Length)
            {
                return(false);
            }

            int end = start;

            while (end < Text.Length)
            {
                if (Text[end] == '\n')
                {
                    break;
                }
                end++;
            }
            line.Start        = start;
            line.End          = end;
            line.ContentStart = start;
            line.ContentEnd   = end;
            if (line.ContentSize > 0)
            {
                if (Text[line.ContentEnd] == '\n')
                {
                    line.ContentEnd--;
                    if (line.ContentSize > 0)
                    {
                        if (Text[line.ContentEnd] == '\r')
                        {
                            line.ContentEnd--;
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 15
0
        private static bool ProcessCommentLine(IniLineRange line)
        {
            if (!textBuffer.IsContentStartWith(line, schemeStyle.CommentPrefix))
            {
                return(false);
            }
            if (!readerStyle.IsParseComments)
            {
                return(true);
            }
            int    commentPrefixLen = schemeStyle.CommentPrefix.Length;
            string comment          = textBuffer.GetContent(line.ContentStart + commentPrefixLen, line.ContentSize - commentPrefixLen);

            if (readerStyle.IsTrimComments)
            {
                comment = comment.Trim();
            }
            if (!string.IsNullOrEmpty(comment))
            {
                cachedComments.Add(comment);
            }
            return(true);
        }
 public bool IsContentEmpty(IniLineRange line)
 {
     return(line.ContentSize == 0);
 }
Esempio n. 17
0
        private static ProcessLineState ProcessProperty(IniTextBuffer buffer, IniData iniData)
        {
            IniLineRange range = buffer.Range.DeepCopy();

            buffer.TrimStart(range);

            int assigmentStartIndex = buffer.FindString(range, schemeStyle.PropertyAssigmentString);

            if (assigmentStartIndex < 0)
            {
                return(ProcessLineState.Unrecognized);
            }

            if (string.IsNullOrEmpty(tempSectionName))
            {
                if (readerStyle.AllowKeysWithoutSection)
                {
                    tempSectionName = IniData.GLOBAL_SECTION_NAME;
                }
                else
                {
                    if (!readerStyle.ThrowExceptionsOnError)
                    {
                        return(ProcessLineState.Error);
                    }

                    throw new IniReaderException("Error:The section is not found for property", buffer.LineNumber, buffer.LineContent);
                }
            }

            IniLineRange keyRange = new IniLineRange();

            keyRange.Start = range.Start;
            keyRange.Size  = assigmentStartIndex - range.Start;
            buffer.Trim(keyRange);
            if (buffer.IsEmpty(keyRange))
            {
                if (readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException("Error:The key of property is empty.", buffer.LineNumber, buffer.LineContent);
            }

            IniLineRange valueRange = new IniLineRange();

            valueRange.Start = assigmentStartIndex + schemeStyle.PropertyAssigmentString.Length;
            valueRange.Size  = range.End - valueRange.Start + 1;
            if (readerStyle.IsTrimProperties)
            {
                buffer.Trim(valueRange);
            }

            string propertyKey = buffer.GetString(keyRange);

            IniSection section = iniData.GetSection(tempSectionName, true);

            if (section.ContainsProperty(propertyKey))
            {
                if (readerStyle.ThrowExceptionsOnError)
                {
                    return(ProcessLineState.Error);
                }
                throw new IniReaderException("Error:The key of property is repeated.", buffer.LineNumber, buffer.LineContent);
            }

            string propertyValue = buffer.GetString(valueRange);

            IniProperty property = section.AddProperty(propertyKey, propertyValue);

            if (readerStyle.IsParseComments && tempComments.Count > 0)
            {
                property.Comments = tempComments;
                tempComments.Clear();
            }
            if (readerStyle.IsParseOptionalValues && tempOptionalValues.Count > 0)
            {
                property.OptionalValues = tempOptionalValues;
                tempOptionalValues.Clear();
            }
            return(ProcessLineState.Success);
        }
 public void TrimContent(IniLineRange line)
 {
     TrimContentStart(line);
     TrimContentEnd(line);
 }