Esempio n. 1
0
        /// <summary>
        /// Создать таблицу, состоящую из одного среза, в файле или потоке
        /// </summary>
        /// <remarks>Для записи таблицы текущего среза</remarks>
        public void Create(SrezTable.Srez srez, DateTime srezDT)
        {
            if (srez == null)
            {
                throw new ArgumentNullException("srez");
            }

            Stream       stream = null;
            BinaryWriter writer = null;

            try
            {
                stream = ioStream == null ?
                         new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite) :
                         ioStream;
                writer = new BinaryWriter(stream);

                writer.Write(GetSrezDescrBuf(srez.SrezDescr));
                writer.Write(Arithmetic.EncodeDateTime(srezDT));
                writer.Write(GetCnlDataBuf(srez.CnlData));
                stream.SetLength(stream.Position);
            }
            finally
            {
                if (fileMode)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Создать буфер для записи события
        /// </summary>
        protected byte[] CreateEventBuffer(EventTableLight.Event ev)
        {
            byte[] evBuf = new byte[EventDataSize];
            Array.Copy(BitConverter.GetBytes(Arithmetic.EncodeDateTime(ev.DateTime)), 0, evBuf, 0, 8);
            evBuf[8]  = (byte)(ev.ObjNum % 256);
            evBuf[9]  = (byte)(ev.ObjNum / 256);
            evBuf[10] = (byte)(ev.KPNum % 256);
            evBuf[11] = (byte)(ev.KPNum / 256);
            evBuf[12] = (byte)(ev.ParamID % 256);
            evBuf[13] = (byte)(ev.ParamID / 256);
            evBuf[14] = (byte)(ev.CnlNum % 256);
            evBuf[15] = (byte)(ev.CnlNum / 256);
            Array.Copy(BitConverter.GetBytes(ev.OldCnlVal), 0, evBuf, 16, 8);
            evBuf[24] = (byte)ev.OldCnlStat;
            Array.Copy(BitConverter.GetBytes(ev.NewCnlVal), 0, evBuf, 25, 8);
            evBuf[33] = (byte)ev.NewCnlStat;
            evBuf[34] = ev.Checked ? (byte)1 : (byte)0;
            evBuf[35] = (byte)(ev.UserID % 256);
            evBuf[36] = (byte)(ev.UserID / 256);
            string descr = ev.Descr ?? "";

            if (descr.Length > MaxDescrLen)
            {
                descr = descr.Substring(0, MaxDescrLen);
            }
            evBuf[37] = (byte)descr.Length;
            Array.Copy(Encoding.Default.GetBytes(descr), 0, evBuf, 38, descr.Length);
            string data = ev.Data ?? "";

            if (data.Length > MaxDataLen)
            {
                data = data.Substring(0, MaxDataLen);
            }
            evBuf[138] = (byte)data.Length;
            Array.Copy(Encoding.Default.GetBytes(data), 0, evBuf, 139, data.Length);
            return(evBuf);
        }
Esempio n. 3
0
        /// <summary>
        /// Записать изменения таблицы срезов в файл или поток
        /// </summary>
        public void Update(SrezTable srezTable)
        {
            if (srezTable == null)
            {
                throw new ArgumentNullException("srezTable");
            }

            Stream       stream = null;
            BinaryWriter writer = null;

            try
            {
                stream = ioStream == null ?
                         new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite) :
                         ioStream;
                writer = new BinaryWriter(stream);

                // запись изменённых срезов
                foreach (SrezTable.Srez srez in srezTable.ModifiedSrezList)
                {
                    stream.Seek(srez.Position + 8, SeekOrigin.Begin);
                    writer.Write(GetCnlDataBuf(srez.CnlData));
                }

                // установка позиции записи добавленных срезов в поток,
                // восстановление таблицы срезов в случае необходимости
                SrezTable.Srez lastSrez = srezTable.LastStoredSrez;

                if (lastSrez == null)
                {
                    stream.Seek(0, SeekOrigin.Begin);
                }
                else
                {
                    stream.Seek(0, SeekOrigin.End);
                    long offset = lastSrez.Position + lastSrez.CnlNums.Length * 9 + 8;

                    if (stream.Position < offset)
                    {
                        byte[] buf = new byte[offset - stream.Position];
                        stream.Write(buf, 0, buf.Length);
                    }
                    else
                    {
                        stream.Seek(offset, SeekOrigin.Begin);
                    }
                }

                // запись добавленных срезов
                SrezTable.SrezDescr prevSrezDescr = lastSrez == null ? null : lastSrez.SrezDescr;

                foreach (SrezTable.Srez srez in srezTable.AddedSrezList)
                {
                    // запись номеров каналов среза
                    if (srez.SrezDescr.Equals(prevSrezDescr))
                    {
                        writer.Write(EmptyCnlNumsBuf);
                    }
                    else
                    {
                        writer.Write(GetSrezDescrBuf(srez.SrezDescr));
                    }

                    prevSrezDescr = srez.SrezDescr;

                    // запись данных среза
                    srez.Position = stream.Position;
                    writer.Write(Arithmetic.EncodeDateTime(srez.DateTime));
                    writer.Write(GetCnlDataBuf(srez.CnlData));
                    lastSrez = srez;
                }

                // подтверждение успешного сохранения изменений
                srezTable.AcceptChanges();
                srezTable.LastStoredSrez = lastSrez;
            }
            finally
            {
                if (fileMode)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Записать таблицу dataTable в файл или поток
        /// </summary>
        public void Update(DataTable dataTable)
        {
            if (dataTable == null)
            {
                throw new ArgumentNullException("dataTable");
            }

            Stream       stream = null;
            BinaryWriter writer = null;

            try
            {
                stream = ioStream == null ?
                         new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite) :
                         ioStream;
                writer = new BinaryWriter(stream, Encoding.Default);

                // запись заголовка
                byte fieldCnt = dataTable.Columns.Count > byte.MaxValue ?
                                byte.MaxValue : (byte)dataTable.Columns.Count;
                writer.Write(fieldCnt);
                writer.Write((ushort)0); // резерв

                if (fieldCnt > 0)
                {
                    // формирование и запись определений полей
                    FieldDef[] fieldDefs   = new FieldDef[fieldCnt];
                    int        recSize     = 2;                                        // размер строки в файле
                    byte[]     fieldDefBuf = new byte[FieldDefSize];
                    fieldDefBuf[FieldDefSize - 1] = fieldDefBuf[FieldDefSize - 2] = 0; // резерв

                    for (int i = 0; i < fieldCnt; i++)
                    {
                        FieldDef   fieldDef = new FieldDef();
                        DataColumn col      = dataTable.Columns[i];
                        Type       type     = col.DataType;

                        if (type == typeof(int))
                        {
                            fieldDef.DataType  = DataTypes.Integer;
                            fieldDef.DataSize  = sizeof(int);
                            fieldDef.MaxStrLen = 0;
                        }
                        else if (type == typeof(double))
                        {
                            fieldDef.DataType  = DataTypes.Double;
                            fieldDef.DataSize  = sizeof(double);
                            fieldDef.MaxStrLen = 0;
                        }
                        else if (type == typeof(bool))
                        {
                            fieldDef.DataType  = DataTypes.Boolean;
                            fieldDef.DataSize  = 1;
                            fieldDef.MaxStrLen = 0;
                        }
                        else if (type == typeof(DateTime))
                        {
                            fieldDef.DataType  = DataTypes.DateTime;
                            fieldDef.DataSize  = sizeof(double);
                            fieldDef.MaxStrLen = 0;
                        }
                        else // String
                        {
                            fieldDef.DataType = DataTypes.String;
                            int maxLen = Math.Min(col.MaxLength, MaxStringLen);
                            fieldDef.DataSize  = 2 /*запись длины*/ + Encoding.UTF8.GetMaxByteCount(maxLen);
                            fieldDef.MaxStrLen = maxLen;
                        }

                        fieldDef.Name      = col.ColumnName;
                        fieldDef.AllowNull = col.AllowDBNull;

                        recSize += fieldDef.DataSize;
                        if (fieldDef.AllowNull)
                        {
                            recSize++;
                        }
                        fieldDefs[i] = fieldDef;

                        fieldDefBuf[0] = (byte)fieldDef.DataType;
                        Array.Copy(BitConverter.GetBytes((ushort)fieldDef.DataSize), 0, fieldDefBuf, 1, 2);
                        Array.Copy(BitConverter.GetBytes((ushort)fieldDef.MaxStrLen), 0, fieldDefBuf, 3, 2);
                        fieldDefBuf[5] = fieldDef.AllowNull ? (byte)1 : (byte)0;
                        ConvertStr(fieldDef.Name, MaxFieldNameLen, MaxFieldNameDataSize,
                                   fieldDefBuf, 6, Encoding.ASCII);

                        writer.Write(fieldDefBuf);
                    }

                    // запись строк
                    byte[] rowBuf = new byte[recSize];
                    rowBuf[0] = rowBuf[1] = 0; // резерв
                    foreach (DataRowView rowView in dataTable.DefaultView)
                    {
                        DataRow row    = rowView.Row;
                        int     bufInd = 2;

                        foreach (FieldDef fieldDef in fieldDefs)
                        {
                            int    colInd = dataTable.Columns.IndexOf(fieldDef.Name);
                            object val    = colInd >= 0 ? row[colInd] : null;
                            bool   isNull = val == null || val == DBNull.Value;

                            if (fieldDef.AllowNull)
                            {
                                rowBuf[bufInd++] = isNull ? (byte)1 : (byte)0;
                            }

                            switch (fieldDef.DataType)
                            {
                            case DataTypes.Integer:
                                int intVal = isNull ? 0 : (int)val;
                                Array.Copy(BitConverter.GetBytes(intVal), 0, rowBuf, bufInd, fieldDef.DataSize);
                                break;

                            case DataTypes.Double:
                                double dblVal = isNull ? 0.0 : (double)val;
                                Array.Copy(BitConverter.GetBytes(dblVal), 0, rowBuf, bufInd, fieldDef.DataSize);
                                break;

                            case DataTypes.Boolean:
                                rowBuf[bufInd] = (byte)(isNull ? 0 : (bool)val ? 1 : 0);
                                break;

                            case DataTypes.DateTime:
                                double dtVal = isNull ? 0.0 : Arithmetic.EncodeDateTime((DateTime)val);
                                Array.Copy(BitConverter.GetBytes(dtVal), 0, rowBuf, bufInd, fieldDef.DataSize);
                                break;

                            default:
                                string strVal = isNull ? "" : val.ToString();
                                ConvertStr(strVal, fieldDef.MaxStrLen, fieldDef.DataSize,
                                           rowBuf, bufInd, Encoding.UTF8);
                                break;
                            }

                            bufInd += fieldDef.DataSize;
                        }

                        writer.Write(rowBuf);
                    }
                }
            }
            finally
            {
                if (fileMode)
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (stream != null)
                    {
                        stream.Close();
                    }
                }
            }
        }