Exemple #1
0
        public static bool TryProcess(MarkerFileContent content, string keyName, string keyValue, [NotNullWhen(false)] out string?exceptionMessage)
        {
            List <MarkerInfo> markers = content.GetMarkers() !;

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

            if (markerNumber != markers.Count + 1)
            {
                exceptionMessage = $"{Resources.NonConsecutiveMarkerNumber} {keyName}";
                return(false);
            }

            if (!TryParseMarkerInfo(keyValue, out MarkerInfo markerInfo, out string?errorText))
            {
                exceptionMessage = $"{errorText} {Resources.Marker} {keyName}";
                return(false);
            }

            markers.Add(markerInfo);

            exceptionMessage = null;
            return(true);
        }
Exemple #2
0
 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
     });
Exemple #3
0
 private static void InitializeSectionContent(MarkerFileContent content, Definitions.Section section)
 {
     switch (section)
     {
     case Definitions.Section.MarkerInfos:
         content.SetMarkers(new List <MarkerInfo>());
         break;
     }
 }
Exemple #4
0
 public IMarkerFileContentVer1 LoadVer1()
 {
     if (_newFileBeingCreated)
     {
         MarkerFileContent content = new MarkerFileContent(Definitions.IdentificationText, new Version(1, 0));
         return(content);
     }
     else
     {
         FileLoader fileLoader = new FileLoader(_file);
         return(fileLoader.LoadVer1());
     }
 }
        public static void ProcessSectionComment(MarkerFileContent 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.MarkerInfos:
                content.InlinedComments.BelowMarkerInfosSection = FileLoaderCommon.ConcatenateWithNewLine(content.InlinedComments.BelowMarkerInfosSection, comment);
                break;

            default:
                throw new NotImplementedException();     // should never happen
            }
        }
Exemple #6
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);
        }
Exemple #7
0
        public static bool TryProcess(MarkerFileContent content, string keyName, string keyValue, [NotNullWhen(false)] out string?exceptionMessage)
        {
            if (!Enum.TryParse(keyName, true, out Definitions.CommonInfosKeys key))
            {
                exceptionMessage = $"{Resources.UnrecognizedKey} {keyName}";
                return(false);
            }

            switch (key)
            {
            case Definitions.CommonInfosKeys.Codepage:
            {
                // resolving Utf-8 string as Utf8 enum
                if (0 == string.Compare(keyValue, Definitions.Utf8Enum, true, CultureInfo.InvariantCulture))
                {
                    content.CodePage = Codepage.Utf8;
                }
                else
                {
                    if (!Enum.TryParse(keyValue, true, out Codepage value))
                    {
                        exceptionMessage = $"{Resources.UnrecognizedKeyValue} {keyValue}";
                        return(false);
                    }

                    // Utf8 is not valid, only Utf-8 is valid
                    if (value == Codepage.Utf8)
                    {
                        exceptionMessage = $"{Resources.UnrecognizedKeyValue} {keyValue}";
                        return(false);
                    }

                    content.CodePage = value;
                }
            }
            break;

            case Definitions.CommonInfosKeys.DataFile:
                if (string.IsNullOrWhiteSpace(keyValue))
                {
                    exceptionMessage = $"{Resources.FileNameCannotBeEmpty} {keyName}";
                    return(false);
                }

                string eegFileExtension        = Path.GetExtension(keyValue);
                bool   isEegFileExtensionValid = s_validEegFileExtensions.Select(p => $".{p}").Contains(eegFileExtension);
                if (!isEegFileExtensionValid)
                {
                    exceptionMessage = $"{Resources.UnrecognizedFileExtension} {ToCommaSeparatedText(s_validEegFileExtensions)}: {keyName}";
                    return(false);
                }
                content.DataFile = keyValue;
                break;

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

            exceptionMessage = null;
            return(true);
        }