Exemple #1
0
        public static string WriteToString(IniData iniData, IniSchemeStyle schemeStyle = null, IniWriterStyle writerStyle = null)
        {
            BeginWrite();

            TextWriter writer = new StringWriter(new StringBuilder());

            IniSection globalSection = iniData.GetSection(IniData.GLOBAL_SECTION_NAME, false);

            if (globalSection != null)
            {
                WriteSection(globalSection, writer);
            }
            foreach (var section in iniData)
            {
                if (section.Name != IniData.GLOBAL_SECTION_NAME)
                {
                    WriteSection(section, writer);
                }
            }

            EndWrite();

            writer.Flush();
            string iniString = writer.ToString();

            writer.Close();
            return(iniString);
        }
Exemple #2
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);
        }