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 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 } }
private static bool TryProcessKey(MarkerFileContent content, Definitions.Section section, string keyName, string keyValue, [NotNullWhen(false)] out string?exceptionMessage) { return(section switch { Definitions.Section.NoSection => GlobalSectionLoader.TryProcess(keyName, out exceptionMessage),//(_content, keyName, keyValue); Definitions.Section.CommonInfos => CommonInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), Definitions.Section.MarkerInfos => MarkerInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), _ => throw new NotImplementedException(), // should never happen });
private static void InitializeSectionContent(MarkerFileContent content, Definitions.Section section) { switch (section) { case Definitions.Section.MarkerInfos: content.SetMarkers(new List <MarkerInfo>()); break; } }
private static bool TryProcessKey(HeaderFileContent content, Definitions.Section section, string keyName, string keyValue, [NotNullWhen(false)] out string?exceptionMessage) { return(section switch { Definitions.Section.NoSection => GlobalSectionLoader.TryProcess(keyName, out exceptionMessage), // (content, keyName, keyValue) Definitions.Section.CommonInfos => CommonInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), Definitions.Section.BinaryInfos => BinaryInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), Definitions.Section.ChannelInfos => ChannelInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), Definitions.Section.Coordinates => CoordinatesInfosSectionLoader.TryProcess(content, keyName, keyValue, out exceptionMessage), Definitions.Section.Comment => throw new NotImplementedException(), // should never happen Definitions.Section.Unknown => throw new NotImplementedException(), // should never happen _ => throw new NotImplementedException(), // should never happen });
private static void InitializeSectionContent(HeaderFileContent content, Definitions.Section section) { switch (section) { case Definitions.Section.ChannelInfos: content.SetChannelInfos(new List <ChannelInfo>()); break; case Definitions.Section.Coordinates: content.SetChannelCoordinates(new List <Coordinates>()); break; } }
// actually no key comments to process public static void ProcessKeyComments(Definitions.Section section)//, string keyName, string keyEntryComments) { switch (section) { case Definitions.Section.NoSection: case Definitions.Section.CommonInfos: case Definitions.Section.MarkerInfos: Debug.Fail("Unhandled comment"); break; default: throw new NotImplementedException(); // should never happen } }
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); }