예제 #1
0
        public static void ProcessSectionComment(HeaderFileContent content, Definitions.Section section, string comment)
        {
            switch (section)
            {
            case Definitions.Section.NoSection:
                content.InlinedComments.BelowHeaderSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowHeaderSection, comment);
                break;

            case Definitions.Section.CommonInfos:
                content.InlinedComments.BelowCommonInfosSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowCommonInfosSection, comment);
                break;

            case Definitions.Section.BinaryInfos:
                content.InlinedComments.BelowBinaryInfosSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowBinaryInfosSection, comment);
                break;

            case Definitions.Section.ChannelInfos:
                content.InlinedComments.BelowChannelInfosSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowChannelInfosSection, comment);
                break;

            case Definitions.Section.Coordinates:
                content.InlinedComments.BelowCoordinatesInfosSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowCoordinatesInfosSection, comment);
                break;

            case Definitions.Section.Comment:
                content.InlinedComments.BelowCommentSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowCommentSection, comment);
                break;

            default:
                throw new NotImplementedException();     // should never happen
            }
        }
        public static bool TryProcess(HeaderFileContent content, string keyName, string keyValue, [NotNullWhen(false)] out string?exceptionMessage)
        {
            List <Coordinates>?channelCoordinates = content.GetChannelCoordinates() !;

            if (!FileLoaderCommon.TryParseChannelNumber(keyName, out int channelNumber))
            {
                exceptionMessage = $"{Resources.UnrecognizedKey} {keyName}";
                return(false);
            }

            if (channelNumber != channelCoordinates.Count + 1)
            {
                exceptionMessage = $"{Resources.NonConsecutiveChannelNumber} {keyName}";
                return(false);
            }

            if (channelNumber <= 0)
            {
                exceptionMessage = $"{Resources.UnrecognizedKey} {keyName}";
                return(false);
            }

            bool success = TryParseCoordinates(keyValue, out Coordinates coordinates, out string?errorText);

            if (!success)
            {
                exceptionMessage = $"{errorText} {Resources.Channel} {keyName}";
                return(false);
            }

            channelCoordinates.Add(coordinates);

            exceptionMessage = null;
            return(true);
        }
예제 #3
0
        public static void ProcessKeyComment(HeaderFileContent content, Definitions.Section section, string keyName, string comment)
        {
            switch (section)
            {
            case Definitions.Section.CommonInfos:
                if (keyName == Definitions.CommonInfosKeys.DataOrientation.ToString())
                {
                    content.InlinedComments.AboveDataOrientation = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.AboveDataOrientation, comment);
                }
                else if (keyName == Definitions.CommonInfosKeys.SamplingInterval.ToString())
                {
                    content.InlinedComments.AboveSamplingInterval = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.AboveSamplingInterval, comment);
                }
                else
                {
                    Debug.Fail("Unhandled comment");
                }
                break;

            case Definitions.Section.NoSection:
            case Definitions.Section.BinaryInfos:
            case Definitions.Section.ChannelInfos:
            case Definitions.Section.Coordinates:
            case Definitions.Section.Comment:
                Debug.Fail("Unhandled comment");
                break;

            default:
                throw new NotImplementedException();     // should never happen
            }
        }
예제 #4
0
        public IMarkerFileContentVer1 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 InvalidMarkerFileFormatException(0, $"{Resources.UnrecognizedIdentificationText} {Definitions.IdentificationText}"); // should never happen
            }
            if (version.Major != 1)
            {
                throw new InvalidMarkerFileFormatException(0, $"{Resources.UnsupportedVersion} {version}");
            }

            MarkerFileContent content = new MarkerFileContent(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
                    {
                        // currently the key comments are not present in the file (only section comments).
                        // This code is never executed but I leave it to keep exactly the same code structure as in Header file
                        keyInlinedComments = FileLoaderCommon.ConcatenateWithNewLine(keyInlinedComments, commentLineText);
                    }
                }
                else if (IniFormat.IsValidKeyLine(line, out string?keyName, out string?keyValue))
                {
                    if (keyInlinedComments != null)
                    {
                        // currently the key comments are not present in the file (only section comments).
                        // This code is never executed but I leave it to keep exactly the same code structure as in Header file
                        InlinedCommentsLoader.ProcessKeyComments(currentSection);//, keyName, keyEntryComments);
                        keyInlinedComments = null;
                    }

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

                    if (!TryProcessKey(content, currentSection, keyName, keyValue, out string?exceptionMessage))
                    {
                        throw new InvalidMarkerFileFormatException(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 InvalidMarkerFileFormatException(currentLineNumber, $"{Resources.UnrecognizedSection}{sectionName}");
                    }

                    if (alreadyCreatedSections.Any(p => 0 == string.Compare(p, sectionName, true, CultureInfo.InvariantCulture)))
                    {
                        throw new InvalidMarkerFileFormatException(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 InvalidMarkerFileFormatException(currentLineNumber, $"{Resources.InvalidLine} {line}");
                }
            }

            ThrowExceptionIfMandatoryFieldMissing(content);
            return(content);
        }