Beispiel #1
0
        public void AppendKVP <T>(string key, T value, TypeMarshaller <T> marshaller)
        {
            checkArgs();

            _stringBuilder.AppendFormatLine(KVP_PATTERN, key, pack());

            return;

            void checkArgs()
            {
                ThrowUtils.ThrowIf_NullArgument(key, marshaller);
                throwIfKeyInvalid();

                return;

                void throwIfKeyInvalid()
                {
                    bool haveInvalidChars = key.Any(c => !char.IsLetterOrDigit(c) && c != '_');

                    if (haveInvalidChars)
                    {
                        throw new INIBuilderException();
                    }
                }
            }

            string pack()
            {
                var success = marshaller.TryPack(value, out string packed);

                if (!success)
                {
                    throw new INIBuilderException();
                }
                else if (_invalidCharsReplacer.GetType() != marshaller.GetType())
                {
                    success = _invalidCharsReplacer.TryPack(packed, out packed);
                    if (!success)
                    {
                        throw new INIBuilderException();
                    }
                }

                return(packed);
            }
        }
Beispiel #2
0
        public bool TryGetParam <T>(string key, TypeMarshaller <T> marshaller, out T result)
        {
            if (marshaller == null || key == null)
            {
                string errArgs =
                    marshaller == null?nameof(marshaller) + "," : "" +
                    key == null?nameof(key) : "";

                throw new ArgumentNullException(errArgs);
            }

            result = default(T);
            var paramExistsAndValid = IsParamExist(key);

            if (paramExistsAndValid)
            {
                paramExistsAndValid &= CommonUtils.TryOrDefault(() => GetParam(key, marshaller), out result);
            }
            return(paramExistsAndValid);
        }
Beispiel #3
0
        public T GetParam <T>(string key, TypeMarshaller <T> marshaller)
        {
            if (marshaller == null || key == null)
            {
                string errArgs =
                    marshaller == null?nameof(marshaller) + "," : "" +
                    key == null?nameof(key) : "";

                throw new ArgumentNullException(errArgs);
            }

            var    success = true;
            string raw     = _parameters[key];

            if (_invalidCharsReplacer.GetType() != marshaller.GetType())
            {
                success = _invalidCharsReplacer.TryUnpack(_parameters[key], out raw);
            }
            success &= marshaller.TryUnpack(raw, out T result);
            return(success ? result : throw new INIParserException(ParserErrors.UNSUCCESSFUL_UNPACKING, key));
        }