Beispiel #1
0
        /// <summary>
        ///     Copies the <see cref="string"/> representation of the specified
        ///     <see cref="object"/> value into the specified section of an INI file. If
        ///     the file does not exist, it is created.
        ///     <para>
        ///         The Win32-API is used for writing in this case. Please note that empty
        ///         sections are not permitted and that this function writes all changes
        ///         directly on the disk, which means that the entire file is rewritten
        ///         every time. This causes many write accesses when used incorrectly.
        ///     </para>
        /// </summary>
        /// <param name="file">
        ///     The path of the INI file to write.
        /// </param>
        /// <param name="section">
        ///     The name of the section to which the value will be copied.
        /// </param>
        /// <param name="key">
        ///     The name of the key to be associated with a value.
        ///     <para>
        ///         If this parameter is <see langword="null"/>, the entire section,
        ///         including all entries within the section, is deleted.
        ///     </para>
        /// </param>
        /// <param name="value">
        ///     The value to be written to the file.
        ///     <para>
        ///         If this parameter is <see langword="null"/>, the key pointed to by the
        ///         key parameter is deleted.
        ///     </para>
        /// </param>
        /// <param name="forceOverwrite">
        ///     <see langword="true"/> to enable overwriting of a key with the same value
        ///     as specified; otherwise, <see langword="false"/>.
        /// </param>
        /// <param name="skipExistValue">
        ///     <see langword="true"/> to skip an existing value, even it is not the same
        ///     value as specified; otherwise, <see langword="false"/>.
        /// </param>
        public static bool Write(string file, string section, string key, object value, bool forceOverwrite = true, bool skipExistValue = false)
        {
            try
            {
                var path = PathEx.Combine(file);
                if (string.IsNullOrWhiteSpace(section))
                {
                    throw new ArgumentNullException(nameof(section));
                }
                if (!File.Exists(path))
                {
                    if (string.IsNullOrWhiteSpace(key) || value == null || !Path.HasExtension(path) || !PathEx.IsValidPath(path))
                    {
                        throw new PathNotFoundException(path);
                    }
                    var dir = Path.GetDirectoryName(path);
                    if (string.IsNullOrWhiteSpace(dir))
                    {
                        throw new ArgumentInvalidException(nameof(file));
                    }
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                    File.Create(path).Close();
                }
                var strValue = value?.ToString();
                if (!forceOverwrite || skipExistValue)
                {
                    var curValue = Read(section, key, path);
                    if (!forceOverwrite && curValue.Equals(strValue, StringComparison.Ordinal) || skipExistValue && !string.IsNullOrWhiteSpace(curValue))
                    {
                        return(false);
                    }
                }
                if (string.Concat(section, key, value).All(TextEx.IsAscii))
                {
                    goto Write;
                }
                var encoding = EncodingEx.GetEncoding(path);
                if (!encoding.Equals(Encoding.Unicode) && !encoding.Equals(Encoding.BigEndianUnicode))
                {
                    EncodingEx.ChangeEncoding(path, Encoding.Unicode);
                }
Write:
                return(WinApi.NativeMethods.WritePrivateProfileString(section, key, strValue, path) != 0);
            }
            catch (Exception ex) when(ex.IsCaught())
            {
                Log.Write(ex);
                return(false);
            }
        }
        private void ChangeEncoding(string encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            if (newEncoding == null || ReferenceEquals(newEncoding, encoding) || encoding.Equals(newEncoding))
            {
                return;
            }

            decoder  = newEncoding.GetDecoder();
            encoding = newEncoding;
        }
Beispiel #3
0
        private bool TryHandleParameterizedCommand(string name, string parameters)
        {
            var commandName = name.ToUpperInvariant();

            switch (commandName)
            {
            case "FLAG":     // parse in the try string
                return(TrySetFlagMode(parameters));

            case "KEY":     // parse in the keyboard string
                Builder.KeyString = Builder.Dedup(parameters);
                return(true);

            case "TRY":     // parse in the try string
                Builder.TryString = Builder.Dedup(parameters);
                return(true);

            case "SET":     // parse in the name of the character set used by the .dict and .aff
                var encoding = EncodingEx.GetEncodingByName(parameters);
                if (encoding == null)
                {
                    Builder.LogWarning("Failed to get encoding: " + parameters);
                    return(false);
                }

                Builder.Encoding = encoding;
                return(true);

            case "LANG":     // parse in the language for language specific codes
                Builder.Language = Builder.Dedup(parameters.Trim());
                Builder.Culture  = GetCultureFromLanguage(Builder.Language);
                return(true);

            case "SYLLABLENUM":     // parse in the flag used by compound_check() method
                Builder.CompoundSyllableNum = Builder.Dedup(parameters);
                return(true);

            case "WORDCHARS":     // parse in the extra word characters
                Builder.WordChars = CharacterSet.Create(parameters);
                return(true);

            case "IGNORE":     // parse in the ignored characters (for example, Arabic optional diacretics characters)
                Builder.IgnoredChars = CharacterSet.Create(parameters);
                return(true);

            case "COMPOUNDFLAG":     // parse in the flag used by the controlled compound words
                return(TryParseFlag(parameters, out Builder.CompoundFlag));

            case "COMPOUNDMIDDLE":     // parse in the flag used by compound words
                return(TryParseFlag(parameters, out Builder.CompoundMiddle));

            case "COMPOUNDBEGIN":     // parse in the flag used by compound words
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundEnd)
                        : TryParseFlag(parameters, out Builder.CompoundBegin));

            case "COMPOUNDEND":     // parse in the flag used by compound words
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundBegin)
                        : TryParseFlag(parameters, out Builder.CompoundEnd));

            case "COMPOUNDWORDMAX":     // parse in the data used by compound_check() method
                Builder.CompoundWordMax = IntEx.TryParseInvariant(parameters);
                return(Builder.CompoundWordMax.HasValue);

            case "COMPOUNDMIN":     // parse in the minimal length for words in compounds
                Builder.CompoundMin = IntEx.TryParseInvariant(parameters);
                if (!Builder.CompoundMin.HasValue)
                {
                    Builder.LogWarning("Failed to parse CompoundMin: " + parameters);
                    return(false);
                }

                if (Builder.CompoundMin.GetValueOrDefault() < 1)
                {
                    Builder.CompoundMin = 1;
                }

                return(true);

            case "COMPOUNDROOT":     // parse in the flag sign compounds in dictionary
                return(TryParseFlag(parameters, out Builder.CompoundRoot));

            case "COMPOUNDPERMITFLAG":     // parse in the flag used by compound_check() method
                return(TryParseFlag(parameters, out Builder.CompoundPermitFlag));

            case "COMPOUNDFORBIDFLAG":     // parse in the flag used by compound_check() method
                return(TryParseFlag(parameters, out Builder.CompoundForbidFlag));

            case "COMPOUNDSYLLABLE":     // parse in the max. words and syllables in compounds
                return(TryParseCompoundSyllable(parameters));

            case "NOSUGGEST":
                return(TryParseFlag(parameters, out Builder.NoSuggest));

            case "NONGRAMSUGGEST":
                return(TryParseFlag(parameters, out Builder.NoNgramSuggest));

            case "FORBIDDENWORD":     // parse in the flag used by forbidden words
                Builder.ForbiddenWord = TryParseFlag(parameters);
                return(Builder.ForbiddenWord.HasValue);

            case "LEMMA_PRESENT":     // parse in the flag used by forbidden words
                return(TryParseFlag(parameters, out Builder.LemmaPresent));

            case "CIRCUMFIX":     // parse in the flag used by circumfixes
                return(TryParseFlag(parameters, out Builder.Circumfix));

            case "ONLYINCOMPOUND":     // parse in the flag used by fogemorphemes
                return(TryParseFlag(parameters, out Builder.OnlyInCompound));

            case "PSEUDOROOT":    // parse in the flag used by `needaffixs'
            case "NEEDAFFIX":     // parse in the flag used by `needaffixs'
                return(TryParseFlag(parameters, out Builder.NeedAffix));

            case "REP":     // parse in the typical fault correcting table
                return(TryParseStandardListItem(EntryListType.Replacements, parameters, ref Builder.Replacements, TryParseReplacements));

            case "ICONV":     // parse in the input conversion table
                return(TryParseConv(parameters, EntryListType.Iconv, ref Builder.InputConversions));

            case "OCONV":     // parse in the output conversion table
                return(TryParseConv(parameters, EntryListType.Oconv, ref Builder.OutputConversions));

            case "PHONE":     // parse in the phonetic conversion table
                return(TryParseStandardListItem(EntryListType.Phone, parameters, ref Builder.Phone, TryParsePhone));

            case "CHECKCOMPOUNDPATTERN":     // parse in the checkcompoundpattern table
                return(TryParseStandardListItem(EntryListType.CompoundPatterns, parameters, ref Builder.CompoundPatterns, TryParseCheckCompoundPatternIntoCompoundPatterns));

            case "COMPOUNDRULE":     // parse in the defcompound table
                return(TryParseStandardListItem(EntryListType.CompoundRules, parameters, ref Builder.CompoundRules, TryParseCompoundRuleIntoList));

            case "MAP":     // parse in the related character map table
                return(TryParseStandardListItem(EntryListType.Map, parameters, ref Builder.RelatedCharacterMap, TryParseMapEntry));

            case "BREAK":     // parse in the word breakpoints table
                return(TryParseStandardListItem(EntryListType.Break, parameters, ref Builder.BreakPoints, TryParseBreak));

            case "VERSION":
                Builder.Version = parameters;
                return(true);

            case "MAXNGRAMSUGS":
                Builder.MaxNgramSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxNgramSuggestions.HasValue);

            case "MAXDIFF":
                Builder.MaxDifferency = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxDifferency.HasValue);

            case "MAXCPDSUGS":
                Builder.MaxCompoundSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxCompoundSuggestions.HasValue);

            case "KEEPCASE":     // parse in the flag used by forbidden words
                return(TryParseFlag(parameters, out Builder.KeepCase));

            case "FORCEUCASE":
                return(TryParseFlag(parameters, out Builder.ForceUpperCase));

            case "WARN":
                return(TryParseFlag(parameters, out Builder.Warn));

            case "SUBSTANDARD":
                return(TryParseFlag(parameters, out Builder.SubStandard));

            case "PFX":
            case "SFX":
                var parseAsPrefix = "PFX" == commandName;
                if (EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes))
                {
                    parseAsPrefix = !parseAsPrefix;
                }

                return(parseAsPrefix
                        ? TryParseAffixIntoList(parameters, ref Builder.Prefixes)
                        : TryParseAffixIntoList(parameters, ref Builder.Suffixes));

            case "AF":
                return(TryParseStandardListItem(EntryListType.AliasF, parameters, ref Builder.AliasF, TryParseAliasF));

            case "AM":
                return(TryParseStandardListItem(EntryListType.AliasM, parameters, ref Builder.AliasM, TryParseAliasM));

            default:
                Builder.LogWarning($"Unknown command {commandName} with params: {parameters}");
                return(false);
            }
        }
Beispiel #4
0
        private bool TryHandleParameterizedCommand(string commandName, string parameters)
        {
            if (commandName == null || !CommandMap.TryGetValue(commandName, out AffixReaderCommandKind command))
            {
                Builder.LogWarning($"Unknown command {commandName} with params: {parameters}");
                return(false);
            }

            switch (command)
            {
            case AffixReaderCommandKind.Flag:
                return(TrySetFlagMode(parameters));

            case AffixReaderCommandKind.KeyString:
                Builder.KeyString = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.TryString:
                Builder.TryString = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.SetEncoding:
                var encoding = EncodingEx.GetEncodingByName(parameters);
                if (encoding == null)
                {
                    Builder.LogWarning("Failed to get encoding: " + parameters);
                    return(false);
                }

                Builder.Encoding = encoding;
                return(true);

            case AffixReaderCommandKind.Language:
                Builder.Language = Builder.Dedup(parameters.Trim());
                Builder.Culture  = GetCultureFromLanguage(Builder.Language);
                return(true);

            case AffixReaderCommandKind.CompoundSyllableNum:
                Builder.CompoundSyllableNum = Builder.Dedup(parameters);
                return(true);

            case AffixReaderCommandKind.WordChars:
                Builder.WordChars = CharacterSet.Create(parameters);
                return(true);

            case AffixReaderCommandKind.Ignore:
                Builder.IgnoredChars = CharacterSet.Create(parameters);
                return(true);

            case AffixReaderCommandKind.CompoundFlag:
                return(TryParseFlag(parameters, out Builder.CompoundFlag));

            case AffixReaderCommandKind.CompoundMiddle:
                return(TryParseFlag(parameters, out Builder.CompoundMiddle));

            case AffixReaderCommandKind.CompoundBegin:
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundEnd)
                        : TryParseFlag(parameters, out Builder.CompoundBegin));

            case AffixReaderCommandKind.CompoundEnd:
                return(EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes)
                        ? TryParseFlag(parameters, out Builder.CompoundBegin)
                        : TryParseFlag(parameters, out Builder.CompoundEnd));

            case AffixReaderCommandKind.CompoundWordMax:
                Builder.CompoundWordMax = IntEx.TryParseInvariant(parameters);
                return(Builder.CompoundWordMax.HasValue);

            case AffixReaderCommandKind.CompoundMin:
                Builder.CompoundMin = IntEx.TryParseInvariant(parameters);
                if (!Builder.CompoundMin.HasValue)
                {
                    Builder.LogWarning("Failed to parse CompoundMin: " + parameters);
                    return(false);
                }

                if (Builder.CompoundMin.GetValueOrDefault() < 1)
                {
                    Builder.CompoundMin = 1;
                }

                return(true);

            case AffixReaderCommandKind.CompoundRoot:
                return(TryParseFlag(parameters, out Builder.CompoundRoot));

            case AffixReaderCommandKind.CompoundPermitFlag:
                return(TryParseFlag(parameters, out Builder.CompoundPermitFlag));

            case AffixReaderCommandKind.CompoundForbidFlag:
                return(TryParseFlag(parameters, out Builder.CompoundForbidFlag));

            case AffixReaderCommandKind.CompoundSyllable:
                return(TryParseCompoundSyllable(parameters));

            case AffixReaderCommandKind.NoSuggest:
                return(TryParseFlag(parameters, out Builder.NoSuggest));

            case AffixReaderCommandKind.NoNGramSuggest:
                return(TryParseFlag(parameters, out Builder.NoNgramSuggest));

            case AffixReaderCommandKind.ForbiddenWord:
                Builder.ForbiddenWord = TryParseFlag(parameters);
                return(Builder.ForbiddenWord.HasValue);

            case AffixReaderCommandKind.LemmaPresent:
                return(TryParseFlag(parameters, out Builder.LemmaPresent));

            case AffixReaderCommandKind.Circumfix:
                return(TryParseFlag(parameters, out Builder.Circumfix));

            case AffixReaderCommandKind.OnlyInCompound:
                return(TryParseFlag(parameters, out Builder.OnlyInCompound));

            case AffixReaderCommandKind.NeedAffix:
                return(TryParseFlag(parameters, out Builder.NeedAffix));

            case AffixReaderCommandKind.Replacement:
                return(TryParseStandardListItem(EntryListType.Replacements, parameters, ref Builder.Replacements, TryParseReplacements));

            case AffixReaderCommandKind.InputConversions:
                return(TryParseConv(parameters, EntryListType.Iconv, ref Builder.InputConversions));

            case AffixReaderCommandKind.OutputConversions:
                return(TryParseConv(parameters, EntryListType.Oconv, ref Builder.OutputConversions));

            case AffixReaderCommandKind.Phone:
                return(TryParseStandardListItem(EntryListType.Phone, parameters, ref Builder.Phone, TryParsePhone));

            case AffixReaderCommandKind.CheckCompoundPattern:
                return(TryParseStandardListItem(EntryListType.CompoundPatterns, parameters, ref Builder.CompoundPatterns, TryParseCheckCompoundPatternIntoCompoundPatterns));

            case AffixReaderCommandKind.CompoundRule:
                return(TryParseStandardListItem(EntryListType.CompoundRules, parameters, ref Builder.CompoundRules, TryParseCompoundRuleIntoList));

            case AffixReaderCommandKind.Map:
                return(TryParseStandardListItem(EntryListType.Map, parameters, ref Builder.RelatedCharacterMap, TryParseMapEntry));

            case AffixReaderCommandKind.Break:
                return(TryParseStandardListItem(EntryListType.Break, parameters, ref Builder.BreakPoints, TryParseBreak));

            case AffixReaderCommandKind.Version:
                Builder.Version = parameters;
                return(true);

            case AffixReaderCommandKind.MaxNgramSuggestions:
                Builder.MaxNgramSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxNgramSuggestions.HasValue);

            case AffixReaderCommandKind.MaxDifferency:
                Builder.MaxDifferency = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxDifferency.HasValue);

            case AffixReaderCommandKind.MaxCompoundSuggestions:
                Builder.MaxCompoundSuggestions = IntEx.TryParseInvariant(parameters);
                return(Builder.MaxCompoundSuggestions.HasValue);

            case AffixReaderCommandKind.KeepCase:
                return(TryParseFlag(parameters, out Builder.KeepCase));

            case AffixReaderCommandKind.ForceUpperCase:
                return(TryParseFlag(parameters, out Builder.ForceUpperCase));

            case AffixReaderCommandKind.Warn:
                return(TryParseFlag(parameters, out Builder.Warn));

            case AffixReaderCommandKind.SubStandard:
                return(TryParseFlag(parameters, out Builder.SubStandard));

            case AffixReaderCommandKind.Prefix:
            case AffixReaderCommandKind.Suffix:
                var parseAsPrefix = AffixReaderCommandKind.Prefix == command;
                if (EnumEx.HasFlag(Builder.Options, AffixConfigOptions.ComplexPrefixes))
                {
                    parseAsPrefix = !parseAsPrefix;
                }

                return(parseAsPrefix
                        ? TryParseAffixIntoList(parameters, ref Builder.Prefixes)
                        : TryParseAffixIntoList(parameters, ref Builder.Suffixes));

            case AffixReaderCommandKind.AliasF:
                return(TryParseStandardListItem(EntryListType.AliasF, parameters, ref Builder.AliasF, TryParseAliasF));

            case AffixReaderCommandKind.AliasM:
                return(TryParseStandardListItem(EntryListType.AliasM, parameters, ref Builder.AliasM, TryParseAliasM));

            default:
                Builder.LogWarning($"Unknown parsed command {command}");
                return(false);
            }
        }
        private void ChangeEncoding(ReadOnlySpan <char> encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            ChangeEncoding(newEncoding);
        }
        private void ChangeEncoding(StringSlice encodingName)
        {
            var newEncoding = EncodingEx.GetEncodingByName(encodingName);

            ChangeEncoding(newEncoding);
        }
Beispiel #7
0
 /// <summary>
 /// Zwraca obiekt kodowania znaków wskazujący informację jakiej użyto strony kodowej do zapisania danych w podanym pliku.
 /// </summary>
 /// <param name="filePath">Ścieżka do pliku.</param>
 /// <returns>Strona kodowa znaków.</returns>
 protected Encoding GetEncodingFile(string filePath)
 {
     return(EncodingEx.GetEncoding(filePath, GetDefaultEncoding()));
 }