Beispiel #1
0
 public void AddProperty(IniProperty property)
 {
     if (propertyDic.ContainsKey(property.Key))
     {
         throw new IniPropertyRepeatException(property.Key);
     }
     propertyDic.Add(property.Key, property);
 }
Beispiel #2
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);
        }
Beispiel #3
0
        public IniProperty AddProperty(string propertyKey, string propertyValue, string[] comments = null, string[] optionalValues = null)
        {
            if (propertyDic.ContainsKey(propertyKey))
            {
                throw new IniPropertyRepeatException(propertyKey);
            }

            IniProperty property = new IniProperty(propertyKey, propertyValue);

            if (comments != null && comments.Length > 0)
            {
                property.Comments.AddRange(comments);
            }
            if (optionalValues != null && optionalValues.Length > 0)
            {
                property.OptionalValues.AddRange(optionalValues);
            }

            propertyDic.Add(propertyKey, property);

            return(property);
        }
Beispiel #4
0
        private static void WriteProperty(IniProperty property, TextWriter writer)
        {
            if (writerStyle.IsNewLineBeforeProperty)
            {
                writer.Write($"{writerStyle.NewLineString}");
            }

            List <string> comments = property.Comments;

            if (comments != null && comments.Count > 0)
            {
                foreach (var comment in comments)
                {
                    writer.Write($"{schemeStyle.CommentString}{comment}{writerStyle.NewLineString}");
                }
            }

            List <string> optionalValues = property.OptionalValues;

            if (optionalValues != null && optionalValues.Count > 0)
            {
                writer.Write($"{schemeStyle.OptionalValueStartString}");
                for (int i = 0; i < optionalValues.Count; ++i)
                {
                    writer.Write($"{optionalValues[i]}");
                    if (i < optionalValues.Count - 1)
                    {
                        writer.Write($"{schemeStyle.OptionalValueAssigmentString}");
                    }
                }
                writer.Write($"{schemeStyle.OptionalValueEndString}{writerStyle.NewLineString}");
            }

            writer.Write($"{property.Key}{writerStyle.SpacesBetweenKeyAndAssigment}{schemeStyle.PropertyAssigmentString}{writerStyle.SpacesBetweenAssigmentAndValue}{property.StringValue}{writerStyle.NewLineString}");
            if (writerStyle.IsNewLineAfterProperty)
            {
                writer.Write($"{writerStyle.NewLineString}");
            }
        }
        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);
        }