예제 #1
0
        protected override void OnInitialize(Stream stream)
        {
            Byte[] headerBuff = new byte[mDbfHeaderWriter.RecordWidth];
            mDbfHeader = new DbfHeader();
            int read = stream.Read(headerBuff, 0, headerBuff.Length);

            if (read > 0)
            {
                mDbfHeaderWriter.Read(this, headerBuff, mDbfHeader);
                //File size reported by the operating system must match the logical file size. Logical file size = ( Length of header + ( Number of records * Length of each record ) )
                //mHeaderSerializer.mQuickWriteMethod(this, headerBuff, header);
                var nbColumns   = ((mDbfHeader.HeaderWidth - mDbfHeaderWriter.RecordWidth) / mDbfColumnWriter.RecordWidth) + 1;
                var totalLength = stream.Length;
                mRecordWidth = mDbfHeader.RecordWidth;
                var recordsLength = totalLength - mDbfHeader.HeaderWidth;
                if (mRecordWidth > 0)
                {
                    var columns = new List <ColumnDefinition>(nbColumns);
                    mRecordCount = (UInt32)(recordsLength / mRecordWidth);

                    Byte[]           columnHeaderBuff = new byte[mDbfColumnWriter.RecordWidth];
                    DbfColumnHeader  dbfColumnHeader  = new DbfColumnHeader();
                    ColumnDefinition columnDefinition = null;
                    //new ColumnDefinition();
                    //columnDefinition.Initialize(DbfVersion.dBaseIII, 0, typeof(DbfRecord).GetField("DeletedFlag",
                    //    BindingFlags.Instance
                    //   | BindingFlags.Public
                    //   | BindingFlags.NonPublic
                    //   | BindingFlags.DeclaredOnly), 0, null);
                    //columns.Add(columnDefinition);

                    for (int i = 1; i < nbColumns; i++)
                    {
                        read = stream.Read(columnHeaderBuff, 0, columnHeaderBuff.Length);
                        mDbfColumnWriter.Read(this, columnHeaderBuff, dbfColumnHeader);
                        System.Diagnostics.Trace.WriteLine(string.Format("{0},{1},{2},{3}",
                                                                         dbfColumnHeader.ColumnName,
                                                                         dbfColumnHeader.ColumnType,
                                                                         dbfColumnHeader.ColumnWidth,
                                                                         dbfColumnHeader.Decimals));

                        columnDefinition = new ColumnDefinition();
                        columnDefinition.Initialize(dbfColumnHeader.ColumnName, dbfColumnHeader.ColumnType, dbfColumnHeader.ColumnWidth, dbfColumnHeader.Decimals);
                        columns.Add(columnDefinition);
                    }
                    int lastByte = stream.ReadByte();
                    if (lastByte == END_OF_COLUMN_DEFINITIONS)
                    {
                        mColumns     = columns;
                        mHeaderWidth = mDbfHeader.HeaderWidth;
                    }
                }
            }
            mRecordWriter = new QuickSerializer(
                typeof(TRecord),
                mVersion,
                mColumns,
                mRecordWidth,
                false,
                true);

            if (mColumns == null)
            {
                mColumns     = mRecordWriter.Columns;
                mRecordWidth = mRecordWriter.RecordWidth;

                mDbfHeader.VerNumber  = 131;
                mDbfHeader.LastUpdate = DateTime.Now;
                mDbfHeader.NbRecords  = 0;
                var nbColumns = mColumns.Count;
                mDbfHeader.HeaderWidth           = (UInt16)(mDbfHeaderWriter.RecordWidth + mDbfColumnWriter.RecordWidth * nbColumns + 1);
                mDbfHeader.RecordWidth           = (UInt16)mRecordWriter.RecordWidth;
                mDbfHeader.Zero                  = 0;
                mDbfHeader.IncompleteTransaction = 0;
                mDbfHeader.EncryptionFlag        = 0;
                //mDbfHeader.LanOnly = [];
                mDbfHeader.Indexed  = 0;
                mDbfHeader.Language = 0;
                mDbfHeader.Zero2    = 0;

                WriteHeader(stream, /*withColumns*/ true);
            }
        }
예제 #2
0
        private void AddColumn(FieldInfo fieldInfo, RecordAttribute recordAttribute)
        {
            // not sure whether it is more convenient for the user to inherit or not
            var atts = fieldInfo.GetCustomAttributes(typeof(ColumnAttribute), true);

            ColumnAttribute column = null;

            if (atts.Length == 0)
            {
                var fm = recordAttribute.FieldMapping;
                if (fieldInfo.IsPublic)
                {
                    if ((fm & FieldMapping.PublicFields) == 0)
                    {
                        return;
                    }
                }
                else
                {
                    if ((fm & FieldMapping.PrivateFields) == 0)
                    {
                        return;
                    }
                }
            }
            else
            {
                column = (ColumnAttribute)atts[0];
            }
            ColumnDefinition cd = null;

            if (mCreateColumns)
            {
                cd = new ColumnDefinition();
                cd.Initialize(mVersion, fieldInfo, column);
                mColumns.Add(cd);
            }
            else if (column != null && column.Type == ColumnType.DELETED_FLAG)
            {
                cd = new ColumnDefinition();
                cd.Initialize(mVersion, fieldInfo, column);
                mColumns.Insert(0, cd);
            }
            else
            {
                bool found = false;
                for (int i = 0; i < mColumns.Count; i++)
                {
                    cd = mColumns[i];
                    if (fieldInfo.Name == cd.mColumnName)
                    {
                        cd.mFieldInfo = fieldInfo;
                        found         = true;
                        break;
                    }
                }
                if (found == false && mIgnoreMissingFields == false)
                {
                    throw new Exception(string.Format("Column {0} was not found in existing DBF file", fieldInfo.Name));
                }
            }

            if (cd.mWidth == 0)
            {
                mIsFixedRecordWidth = false;
            }
        }