public IniProperty AddProperty(string propertyKey, string propertyValue, string[] comments = null, string[] optionalValues = null) { if (properties.ContainsKey(propertyKey)) { return(null); } IniProperty property = new IniProperty(propertyKey, propertyValue); if (property != null) { if (comments != null && comments.Length > 0) { property.Comments.AddRange(comments); } if (optionalValues != null && optionalValues.Length > 0) { property.OptionalValues.AddRange(optionalValues); } } properties.Add(propertyKey, property); return(property); }
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); }