コード例 #1
0
        public static void Save(StreamWriter writer, IHeaderFileContentVer1 content)
        {
            writer.WriteLine();
            writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.BinaryInfos) !));
            FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowBinaryInfosSection);

            foreach (Definitions.BinaryInfosKeys key in (Definitions.BinaryInfosKeys[])Enum.GetValues(typeof(Definitions.BinaryInfosKeys)))
            {
                string?keyValue = key switch
                {
                    Definitions.BinaryInfosKeys.BinaryFormat => content.BinaryFormat?.ToString(),
                    _ => throw new NotImplementedException(), // should never happen
                };

                if (keyValue != null)
                {
                    string line = IniFormat.FormatKeyValueLine(key.ToString(), keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #2
0
        public static void Save(StreamWriter writer, IMarkerFileContentVer1 content)
        {
            List <MarkerInfo>?markers = content.GetMarkers();

            if (markers != null)
            {
                writer.WriteLine();
                writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.MarkerInfos) !));
                FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowMarkerInfosSection);

                for (int mk = 0; mk < markers.Count; ++mk)
                {
                    MarkerInfo marker = markers[mk];

                    string keyName  = Invariant($"{Definitions.KeyMkPlaceholder}{mk + 1}");
                    string keyValue = ConvertMarkerInfoToString(marker);

                    string line = IniFormat.FormatKeyValueLine(keyName, keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #3
0
        public static void Save(StreamWriter writer, IHeaderFileContentVer1 content)
        {
            List <Coordinates>?channelCoordinates = content.GetChannelCoordinates();

            if (channelCoordinates != null)
            {
                writer.WriteLine();
                writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.Coordinates) !));
                FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowCoordinatesInfosSection);

                for (int ch = 0; ch < channelCoordinates.Count; ++ch)
                {
                    Coordinates coordinates = channelCoordinates[ch];

                    string keyName  = Invariant($"{Definitions.KeyChPlaceholder}{ch + 1}");
                    string keyValue = Invariant($"{coordinates.Radius},{coordinates.Theta},{coordinates.Phi}");

                    string line = IniFormat.FormatKeyValueLine(keyName, keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #4
0
        public static void Save(StreamWriter writer, IHeaderFileContentVer1 content)
        {
            List <ChannelInfo>?channelInfos = content.GetChannelInfos();

            if (channelInfos != null)
            {
                writer.WriteLine();
                writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.ChannelInfos) !));
                FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowChannelInfosSection);

                for (int ch = 0; ch < channelInfos.Count; ++ch)
                {
                    ChannelInfo channelInfo = channelInfos[ch];

                    string keyName  = Invariant($"{Definitions.KeyChPlaceholder}{ch + 1}");
                    string keyValue = ConvertChannelInfoToString(channelInfo);

                    string line = IniFormat.FormatKeyValueLine(keyName, keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #5
0
        public static void Save(StreamWriter writer, IMarkerFileContentVer1 content)
        {
            writer.WriteLine();
            writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.CommonInfos) !));
            FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowCommonInfosSection);

            Definitions.CommonInfosKeys[] commonInfosKeys = (Definitions.CommonInfosKeys[])Enum.GetValues(typeof(Definitions.CommonInfosKeys));

            foreach (Definitions.CommonInfosKeys key in commonInfosKeys)
            {
                var keyValue = key switch
                {
                    Definitions.CommonInfosKeys.Codepage => (content.CodePage == Codepage.Utf8) ? Definitions.Utf8Enum : content.CodePage?.ToString(), // replacing Utf8 enum with Utf-8 string
                    Definitions.CommonInfosKeys.DataFile => content.DataFile,
                    _ => throw new NotImplementedException(),                                                                                          // should never happen
                };

                if (keyValue != null)
                {
                    string line = IniFormat.FormatKeyValueLine(key.ToString(), keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #6
0
        public static void Save(StreamWriter writer, IHeaderFileContentVer1 content)
        {
            writer.WriteLine();
            writer.WriteLine(IniFormat.FormatSectionName(Definitions.GetSectionName(Definitions.Section.CommonInfos) !));
            FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.BelowCommonInfosSection);

            Definitions.CommonInfosKeys[] commonInfosKeys = (Definitions.CommonInfosKeys[])Enum.GetValues(typeof(Definitions.CommonInfosKeys));

            foreach (Definitions.CommonInfosKeys key in commonInfosKeys)
            {
                string?keyValue;

                switch (key)
                {
                case Definitions.CommonInfosKeys.Codepage:
                    // replacing Utf8 enum with Utf-8 string
                    keyValue = (content.CodePage == Codepage.Utf8) ? Definitions.Utf8Enum : content.CodePage?.ToString();
                    break;

                case Definitions.CommonInfosKeys.DataFile:
                    keyValue = content.DataFile;
                    break;

                case Definitions.CommonInfosKeys.MarkerFile:
                    keyValue = content.MarkerFile;
                    break;

                case Definitions.CommonInfosKeys.DataFormat:
                    keyValue = content.DataFormat?.ToString();
                    break;

                case Definitions.CommonInfosKeys.DataType:
                    keyValue = content.DataType?.ToString();
                    break;

                case Definitions.CommonInfosKeys.DataOrientation:
                    if (content.DataOrientation != null)
                    {
                        FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.AboveDataOrientation);
                    }
                    keyValue = content.DataOrientation?.ToString();
                    break;

                case Definitions.CommonInfosKeys.SamplingInterval:
                    if (content.SamplingInterval != null)
                    {
                        FileSaverCommon.WriteCommentBlock(writer, content.InlinedComments.AboveSamplingInterval);
                    }
                    keyValue = content.SamplingInterval?.ToString("R", CultureInfo.InvariantCulture);
                    break;

                case Definitions.CommonInfosKeys.NumberOfChannels:
                    keyValue = content.NumberOfChannels?.ToString(CultureInfo.InvariantCulture);
                    break;

                case Definitions.CommonInfosKeys.Averaged:
                    keyValue = content.Averaged.HasValue ?
                               content.Averaged.Value ? YesNo.YES.ToString() : YesNo.NO.ToString() :
                               null;
                    break;

                case Definitions.CommonInfosKeys.AveragedSegments:
                    keyValue = content.AveragedSegments?.ToString(CultureInfo.InvariantCulture);
                    break;

                case Definitions.CommonInfosKeys.SegmentDataPoints:
                    keyValue = content.SegmentDataPoints?.ToString(CultureInfo.InvariantCulture);
                    break;

                case Definitions.CommonInfosKeys.SegmentationType:
                    keyValue = content.SegmentationType?.ToString();
                    break;

                default:
                    throw new NotImplementedException();     // should never happen
                }

                if (keyValue != null)
                {
                    string line = IniFormat.FormatKeyValueLine(key.ToString(), keyValue);
                    writer.WriteLine(line);
                }
            }
        }
コード例 #7
0
ファイル: FileLoader.cs プロジェクト: dmedine/BVTools
        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);
        }