/// <summary>
        /// Set property value either public or private.
        /// </summary>
        public static void SetPropertyValue <TTarget, TValue>
        (
            TTarget target,
            [NotNull] string propertyName,
            TValue value
        )
        {
            Sure.NotNullNorEmpty(propertyName, nameof(propertyName));

            PropertyInfo propertyInfo = typeof(TTarget).GetProperty
                                        (
                propertyName,
                BindingFlags.Public | BindingFlags.NonPublic
                | BindingFlags.Instance | BindingFlags.Static
                                        );

            if (ReferenceEquals(propertyInfo, null))
            {
                Log.Error
                (
                    nameof(ReflectionUtility) + "::" + nameof(SetPropertyValue)
                    + ": can't find property="
                    + propertyName
                );

                throw new ArgumentException(propertyName);
            }

            propertyInfo.SetValue(target, value, null);
        }
        private void _ResolveHostAddress
        (
            string host
        )
        {
            Sure.NotNullNorEmpty(host, nameof(host));

            if (ReferenceEquals(_address, null))
            {
                _address = SocketUtility.ResolveAddressIPv4(host);
                if (_address.AddressFamily != AddressFamily.InterNetwork)
                {
                    throw new IrbisNetworkException
                          (
                              "Address must be IPv4 only!"
                          );
                }
            }

            //if (ReferenceEquals(_address, null))
            //{
            //    throw new IrbisNetworkException
            //        (
            //            "Can't resolve host " + host
            //        );
            //}
        }
Esempio n. 3
0
        public static RawRecord ParseMfnStatusVersion
        (
            [NotNull] string line1,
            [NotNull] string line2,
            [NotNull] RawRecord record
        )
        {
            Sure.NotNullNorEmpty(line1, nameof(line1));
            Sure.NotNullNorEmpty(line2, nameof(line2));
            Sure.NotNull(record, nameof(record));

            Regex regex = new Regex(@"^(-?\d+)\#(\d*)?");
            Match match = regex.Match(line1);

            record.Mfn = Math.Abs(int.Parse(match.Groups[1].Value));
            if (match.Groups[2].Length > 0)
            {
                record.Status = (RecordStatus)int.Parse
                                (
                    match.Groups[2].Value
                                );
            }
            match = regex.Match(line2);
            if (match.Groups[2].Length > 0)
            {
                record.Version = int.Parse(match.Groups[2].Value);
            }

            return(record);
        }
        ///// <summary>
        ///// Gets the custom attribute.
        ///// </summary>
        //[CanBeNull]
        //public static T GetCustomAttribute<T>
        //    (
        //        [NotNull] PropertyDescriptor propertyDescriptor
        //    )
        //    where T : Attribute
        //{
        //    return (T)propertyDescriptor.Attributes[typeof(T)];
        //}

        ///// <summary>
        ///// Get default constructor for given type.
        ///// </summary>
        ///// <param name="type"></param>
        ///// <returns></returns>
        //public static ConstructorInfo GetDefaultConstructor
        //    (
        //        [NotNull] Type type
        //    )
        //{
        //    ConstructorInfo result = type.GetConstructor
        //        (
        //            BindingFlags.Instance | BindingFlags.Public,
        //            null,
        //            Type.EmptyTypes,
        //            null
        //        );
        //    return result;
        //}

        /// <summary>
        /// Get field value either public or private.
        /// </summary>
        public static object GetFieldValue <T>
        (
            T target,
            [NotNull] string fieldName
        )
        {
            Sure.NotNullNorEmpty(fieldName, nameof(fieldName));

            FieldInfo fieldInfo = typeof(T).GetField
                                  (
                fieldName,
                BindingFlags.Public | BindingFlags.NonPublic
                | BindingFlags.Instance | BindingFlags.Static
                                  );

            if (ReferenceEquals(fieldInfo, null))
            {
                Log.Error
                (
                    nameof(ReflectionUtility) + "::" + nameof(GetFieldValue)
                    + ": can't find field="
                    + fieldName
                );

                throw new ArgumentException(fieldName);
            }

            return(fieldInfo.GetValue(target));
        }
Esempio n. 5
0
        public static RecordField ParseLine
        (
            [NotNull] string line
        )
        {
            Sure.NotNullNorEmpty(line, "line");

            StringReader reader = new StringReader(line);

            RecordField result = new RecordField
            {
                Tag   = NumericUtility.ParseInt32(_ReadTo(reader, '#')),
                Value = _ReadTo(reader, '^')
            };

            while (true)
            {
                int next = reader.Read();
                if (next < 0)
                {
                    break;
                }
                char     code     = char.ToLower((char)next);
                string   text     = _ReadTo(reader, '^');
                SubField subField = new SubField
                {
                    Code  = code,
                    Value = text
                };
                result.SubFields.Add(subField);
            }

            return(result);
        }
Esempio n. 6
0
        public static T[] ThrowIfNullOrEmpty <T>
        (
            [CanBeNull] this T[] array,
            [NotNull] string message
        )
        {
            Sure.NotNullNorEmpty(message, nameof(message));

            if (ReferenceEquals(array, null))
            {
                Log.Error
                (
                    nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty)
                    + ": "
                    + "array is null"
                );

                throw new ArgumentNullException(message);
            }

            if (array.Length == 0)
            {
                Log.Error
                (
                    nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty)
                    + ": "
                    + "array is empty"
                );

                throw new ArgumentException(message);
            }

            return(array);
        }
Esempio n. 7
0
        public static IList <T> ThrowIfNullOrEmpty <T>
        (
            [CanBeNull] this IList <T> list,
            [NotNull] string message
        )
        {
            Sure.NotNullNorEmpty(message, nameof(message));

            if (ReferenceEquals(list, null))
            {
                Log.Error
                (
                    nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty)
                    + ": "
                    + "list is null"
                );

                throw new ArgumentNullException(message);
            }

            if (list.Count == 0)
            {
                Log.Error
                (
                    nameof(ListUtility) + "::" + nameof(ThrowIfNullOrEmpty)
                    + ": "
                    + "list is empty"
                );

                throw new ArgumentException(message);
            }

            return(list);
        }
Esempio n. 8
0
        /// <summary>
        /// Saves <see cref="GblFile"/> to local JSON file.
        /// </summary>
        public static void SaveLocalJsonFile
        (
            [NotNull] this GblFile gbl,
            [NotNull] string fileName
        )
        {
            Sure.NotNull(gbl, "gbl");
            Sure.NotNullNorEmpty(fileName, "fileName");

#if WINMOBILE || PocketPC
            Log.Error
            (
                "GblUtility::SaveLocalJsonFile: "
                + "not implemented"
            );

            throw new NotImplementedException();
#else
            string contents = JArray.FromObject(gbl)
                              .ToString(Formatting.Indented);

            File.WriteAllText
            (
                fileName,
                contents,
                IrbisEncoding.Utf8
            );
#endif
        }
Esempio n. 9
0
        public static ProtocolLine Parse
        (
            [NotNull] string line
        )
        {
            Sure.NotNullNorEmpty(line, line);

            ProtocolLine result = new ProtocolLine
            {
                Text    = line,
                Success = true
            };

            string[] parts = line.Split('#');
            foreach (string part in parts)
            {
                string[] p = part.Split('=');
                if (p.Length > 0)
                {
                    string name  = p[0].ToUpper();
                    string value = string.Empty;
                    if (p.Length > 1)
                    {
                        value = p[1];
                    }
                    switch (name)
                    {
                    case "DBN":
                        result.Database = value;
                        break;

                    case "MFN":
                        result.Mfn = value.SafeToInt32();
                        break;

                    case "AUTOIN":
                        result.Autoin = value;
                        break;

                    case "UPDATE":
                        result.Update = value;
                        break;

                    case "STATUS":
                        result.Status = value;
                        break;

                    case "UPDUF":
                        result.UpdUf = value;
                        break;

                    case "GBL_ERROR":
                        result.Error   = value;
                        result.Success = false;
                        break;
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// Constructor.
        /// </summary>
        public BatchRecordFormatter
        (
            [NotNull] string connectionString,
            [NotNull] string database,
            [NotNull] string format,
            int batchSize,
            [NotNull] IEnumerable <int> range
        )
        {
            Sure.NotNullNorEmpty(connectionString, "connectionString");
            Sure.NotNullNorEmpty(database, "database");
            Sure.NotNullNorEmpty(format, "format");
            Sure.NotNull(range, "range");
            if (batchSize < 1)
            {
                Log.Error
                (
                    "BatchRecordFormatter::Constructor: "
                    + "batchSize="
                    + batchSize
                );

                throw new ArgumentOutOfRangeException("batchSize");
            }

            Connection     = ConnectionFactory.CreateConnection(connectionString);
            _ownConnection = true;
            Database       = database;
            BatchSize      = batchSize;
            Format         = format;

            _packages    = range.Slice(batchSize).ToArray();
            TotalRecords = _packages.Sum(p => p.Length);
        }
Esempio n. 11
0
        public static GblFile ParseLocalXmlFile
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, "fileName");

#if WINMOBILE || PocketPC
            Log.Error
            (
                "GblUtility::ParseLocalXmlFile: "
                + "not implemented"
            );

            throw new NotImplementedException();
#else
            string text = File.ReadAllText
                          (
                fileName,
                IrbisEncoding.Utf8
                          );
            GblFile result = FromXml(text);

            return(result);
#endif
        }
Esempio n. 12
0
        public static void ExpandTypes
        (
            [NotNull] JObject obj,
            [NotNull] string nameSpace,
            [NotNull] string assembly
        )
        {
            Sure.NotNull(obj, nameof(obj));
            Sure.NotNullNorEmpty(nameSpace, nameof(nameSpace));
            Sure.NotNullNorEmpty(assembly, nameof(assembly));

            IEnumerable <JValue> values = obj
                                          .SelectTokens("$..$type")
                                          .OfType <JValue>();

            foreach (JValue value in values)
            {
                string typeName = value.Value.ToString();
                if (!typeName.Contains('.'))
                {
                    typeName = nameSpace
                               + "."
                               + typeName
                               + ", "
                               + assembly;
                    value.Value = typeName;
                }
            }
        }
Esempio n. 13
0
        public static IrbisUpperCaseTable ParseText
        (
            [NotNull] Encoding encoding,
            [NotNull] string text
        )
        {
            Sure.NotNull(encoding, "encoding");
            Sure.NotNullNorEmpty(text, "text");

            List <byte> table = new List <byte>(256);

            MatchCollection matches = Regex.Matches(text, @"\d+");

            foreach (Match match in matches)
            {
                byte b = byte.Parse(match.Value);
                table.Add(b);
            }

            IrbisUpperCaseTable result = new IrbisUpperCaseTable
                                         (
                encoding,
                table.ToArray()
                                         );

            return(result);
        }
Esempio n. 14
0
        public static MarcRecord[] ParseAllFormat
        (
            [NotNull] string database,
            [NotNull] IIrbisConnection connection,
            [NotNull] string[] lines
        )
        {
            Sure.NotNullNorEmpty(database, "database");
            Sure.NotNull(connection, "connection");
            Sure.NotNull(lines, "lines");

            List <MarcRecord> result = new List <MarcRecord>();

            foreach (string line in lines)
            {
                MarcRecord record = new MarcRecord
                {
                    HostName = connection.Host,
                    Database = database
                };
                record = ProtocolText.ParseResponseForAllFormat
                         (
                    line,
                    record
                         );

                // coverity[NULL_RETURNS]
                result.Add(record.ThrowIfNull("record"));
            }

            return(result.ToArray());
        }
Esempio n. 15
0
        public static MarcRecord[] ParseAllFormat
        (
            [NotNull] string database,
            [NotNull] ServerResponse response
        )
        {
            Sure.NotNullNorEmpty(database, "database");
            Sure.NotNull(response, "response");

            List <MarcRecord> result = new List <MarcRecord>();

            while (true)
            {
                MarcRecord record = new MarcRecord
                {
                    HostName = response.Connection.Host,
                    Database = database
                };
                record = ProtocolText.ParseResponseForAllFormat
                         (
                    response,
                    record
                         );
                if (ReferenceEquals(record, null))
                {
                    break;
                }
                result.Add(record);
            }

            return(result.ToArray());
        }
Esempio n. 16
0
        /// <summary>
        /// Set field value either public or private.
        /// </summary>
        public static void SetFieldValue <TTarget, TValue>
        (
            TTarget target,
            [NotNull] string fieldName,
            TValue value
        )
            where TTarget : class
        {
            Sure.NotNullNorEmpty(fieldName, nameof(fieldName));

            FieldInfo fieldInfo = typeof(TTarget).GetField
                                  (
                fieldName,
                BindingFlags.Public | BindingFlags.NonPublic
                | BindingFlags.Instance | BindingFlags.Static
                                  );

            if (ReferenceEquals(fieldInfo, null))
            {
                Log.Error
                (
                    nameof(ReflectionUtility) + "::" + nameof(SetFieldValue)
                    + ": can't find field="
                    + fieldName
                );

                throw new ArgumentException(fieldName);
            }

            fieldInfo.SetValue(target, value);
        }
Esempio n. 17
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public FileLogger
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            FileName = fileName;
        }
Esempio n. 18
0
        /// <summary>
        /// Конструктор
        /// </summary>
        public IrbisDate
        (
            [NotNull] string text
        )
        {
            Sure.NotNullNorEmpty(text, "text");

            Text = text;
            Date = ConvertStringToDate(text);
        }
Esempio n. 19
0
        /// <summary>
        /// Initializes a new instance of the
        /// <see cref="UnmanagedLibrary"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public UnmanagedLibrary
        (
            [NotNull] string name
        )
        {
            Sure.NotNullNorEmpty(name, nameof(name));

            Name   = name;
            Handle = new SafeLibraryHandle(Kernel32.LoadLibrary(name));
        }
Esempio n. 20
0
        /// <summary>
        /// Restore subfield from JSON.
        /// </summary>
        public static SubField FromJson
        (
            [NotNull] string text
        )
        {
            Sure.NotNullNorEmpty(text, "text");

            SubField result = JsonConvert.DeserializeObject <SubField>(text);

            return(result);
        }
Esempio n. 21
0
        public GblSettings SetFileName
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            FileName = fileName;

            return(this);
        }
Esempio n. 22
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public UniversalCommand
        (
            [NotNull] IIrbisConnection connection,
            [NotNull] string commandCode
        )
            : base(connection)
        {
            Sure.NotNullNorEmpty(commandCode, "commandCode");

            CommandCode = commandCode;
        }
Esempio n. 23
0
        /// <summary>
        /// Save references to the archive file.
        /// </summary>
        public static void SaveToZipFile
        (
            [NotNull][ItemNotNull] RecordReference[] references,
            [NotNull] string fileName
        )
        {
            Sure.NotNull(references, "references");
            Sure.NotNullNorEmpty(fileName, "fileName");

            references.SaveToFile(fileName);
        }
Esempio n. 24
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public FileSpecification
        (
            IrbisPath path,
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            Path     = path;
            FileName = fileName;
        }
Esempio n. 25
0
        public GblSettings SetSearchExpression
        (
            [NotNull] string searchExpression
        )
        {
            Sure.NotNullNorEmpty(searchExpression, nameof(searchExpression));

            SearchExpression = searchExpression;

            return(this);
        }
Esempio n. 26
0
        public static Encoding FromConfig
        (
            [NotNull] string key
        )
        {
            Sure.NotNullNorEmpty(key, nameof(key));

            string   name   = CM.AppSettings[key];
            Encoding result = ByName(name);

            return(result);
        }
Esempio n. 27
0
        public static MenuFile ParseServerResponse
        (
            [NotNull] string response
        )
        {
            Sure.NotNullNorEmpty(response, nameof(response));

            TextReader reader = new StringReader(response);
            MenuFile   result = ParseStream(reader);

            return(result);
        }
Esempio n. 28
0
        /// <summary>
        /// Deserialize the string.
        /// </summary>
        public static T DeserializeString <T>
        (
            [NotNull] string xmlText
        )
        {
            Sure.NotNullNorEmpty(xmlText, "xmlText");

            StringReader  reader     = new StringReader(xmlText);
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            return((T)serializer.Deserialize(reader));
        }
Esempio n. 29
0
        public static ByteNavigator FromFile
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            byte[]        data   = File.ReadAllBytes(fileName);
            ByteNavigator result = new ByteNavigator(data);

            return(result);
        }
Esempio n. 30
0
        public static JObject ReadObjectFromFile
        (
            [NotNull] string fileName
        )
        {
            Sure.NotNullNorEmpty(fileName, nameof(fileName));

            string  text   = File.ReadAllText(fileName);
            JObject result = JObject.Parse(text);

            return(result);
        }