Пример #1
0
        public IHeaderFileContentVer1 LoadVer1()
        {
            _reader.BaseStream.Seek(0, SeekOrigin.Begin);

            Definitions.Section currentSection = Definitions.Section.NoSection;
            int currentLineNumber = -1;

            string?identificationText = _reader.ReadLine();

            ++currentLineNumber;

            if (!TryParseVersionFromIdentificationText(identificationText, out Version version))
            {
                throw new InvalidHeaderFileFormatException(0, $"{Resources.UnrecognizedIdentificationText} {Definitions.IdentificationText}"); // should never happen
            }
            if (version.Major != 1)
            {
                throw new InvalidHeaderFileFormatException(0, $"{Resources.UnsupportedVersion} {version}");
            }

            HeaderFileContent content = new HeaderFileContent(identificationText, version);

            // storing sections and keys for duplication checks
            List <string> alreadyCreatedSections             = new List <string>();
            List <string> alreadyCreatedKeysInCurrentSection = new List <string>();

            string?keyInlinedComments       = null;
            bool   isInSectionInlineComment = true; // section in-line comment is any comment between [section] and first key in section

            string line;

            while ((line = _reader.ReadLine()) != null)
            {
                ++currentLineNumber;

                if (IniFormat.IsCommentLine(line))
                {
                    string commentLineText = line.Substring(1); // removing ';' from text
                    if (isInSectionInlineComment)
                    {
                        InlinedCommentsLoader.ProcessSectionComment(content, currentSection, commentLineText);
                    }
                    else
                    {
                        keyInlinedComments = FileLoaderCommon.ConcatenateWithNewLine(keyInlinedComments, commentLineText);
                    }
                }
                else if (currentSection == Definitions.Section.Comment)
                {
                    CommentInfosSectionLoader.TryProcess(content, line);
                }
                else if (IniFormat.IsValidKeyLine(line, out string?keyName, out string?keyValue))
                {
                    if (keyInlinedComments != null)
                    {
                        InlinedCommentsLoader.ProcessKeyComment(content, currentSection, keyName, keyInlinedComments);
                        keyInlinedComments = null;
                    }

                    if (alreadyCreatedKeysInCurrentSection.Any(p => 0 == string.Compare(p, keyName, true, CultureInfo.InvariantCulture)))
                    {
                        throw new InvalidHeaderFileFormatException(currentLineNumber, $"{Resources.DuplicatedKey} {keyName}");
                    }

                    if (!TryProcessKey(content, currentSection, keyName, keyValue, out string?exceptionMessage))
                    {
                        throw new InvalidHeaderFileFormatException(currentLineNumber, exceptionMessage);
                    }

                    alreadyCreatedKeysInCurrentSection.Add(keyName);
                    isInSectionInlineComment = false;
                }
                else if (IniFormat.IsSectionLine(line, out string?sectionName))
                {
                    Definitions.Section section = Definitions.ParseSectionName(sectionName);

                    bool isValidSection = section != Definitions.Section.Unknown && section != Definitions.Section.NoSection;
                    if (!isValidSection)
                    {
                        throw new InvalidHeaderFileFormatException(currentLineNumber, $"{Resources.UnrecognizedSection} {sectionName}");
                    }

                    if (alreadyCreatedSections.Any(p => 0 == string.Compare(p, sectionName, true, CultureInfo.InvariantCulture)))
                    {
                        throw new InvalidHeaderFileFormatException(currentLineNumber, $"{Resources.DuplicatedSection} {sectionName}");
                    }

                    InitializeSectionContent(content, section);

                    currentSection           = section;
                    isInSectionInlineComment = true;

                    alreadyCreatedSections.Add(sectionName);
                    alreadyCreatedKeysInCurrentSection.Clear();
                }
                else if (string.IsNullOrWhiteSpace(line))
                {
                    // do nothing
                }
                else
                {
                    throw new InvalidHeaderFileFormatException(currentLineNumber, $"{Resources.InvalidLine} {line}");
                }
            }

            ThrowExceptionIfMandatoryFieldMissing(content);
            ThrowExceptionIfMandatoryFieldHasInvalidKeyValue(content);
            ThrowExceptionIfNumberOfChannelsDoesNotMatchChannels(content);
            ThrowExceptionIfCoordinatesDoNotMatchChannels(content);
            return(content);
        }