Ejemplo n.º 1
0
        public void WriteHeader()
        {
            StreamSeek(_Stream, 0);

            DBF_FILEHEADER_3 header = new DBF_FILEHEADER_3();

            header.headerlength = (ushort)HeaderLength;
            header.crypt        = 0;
            header.incomplete   = 0;

            DateTimeOffset now = DateTimeOffset.UtcNow;

            header.lastupdate.yy = (byte)(now.Year - 1900);
            header.lastupdate.mm = (byte)now.Month;
            header.lastupdate.dd = (byte)now.Day;
            header.recordcount   = (ushort)RecordCount;
            header.recordlength  = (ushort)RecordLength;
            header.version       = (byte)(HasMemo ? Const.MAGIC_DBASE_DEFAULT_MEMO : Const.MAGIC_DBASE_DEFAULT);
            header.unused_0      = 0;
            for (int i = 0; i < 16; i++)
            {
                //header.unused[i] = 0;
            }
            byte[] bytes = Utility.StructureToPtr <DBF_FILEHEADER_3>(header);
            StreamWrite(_Stream, bytes);
            StreamSeek(_Stream, HeaderLength + RecordCount * RecordLength);

            bytes = new byte[] { (byte)Const.CPM_TEXT_TERMINATOR };
            StreamWrite(_Stream, bytes);

            if (HasMemo)
            {
                _MemoFile.WriteHeader();
            }
        }
Ejemplo n.º 2
0
        private static bool sanity_check(DBF_FILEHEADER_3 header)
        {
            bool ok = ((header.lastupdate.mm >= 1) && (header.lastupdate.mm <= 12)) &&
                      ((header.lastupdate.dd >= 1) && (header.lastupdate.dd <= 31))
            ;

            return(ok);
        }
Ejemplo n.º 3
0
        public bool Attach(Stream stream, string filename, bool read_header)
        {
            bool ok = (stream != null);

            if (ok && read_header)
            {
                byte[] bytes = new byte[Marshal.SizeOf(typeof(Byte))];
                ok = (bytes.Length == StreamRead(stream, bytes));
                if (ok)
                {
                    Byte version = bytes[0];
                    int  len_header;
                    int  len_field;

                    switch (version)
                    {
                    case Const.MAGIC_DBASE4:
                    case Const.MAGIC_DBASE4_MEMO:
                        len_header = Marshal.SizeOf(typeof(DBF_FILEHEADER_4));
                        len_field  = Marshal.SizeOf(typeof(DBF_FILEFIELD_4));
                        break;

                    case Const.MAGIC_DBASE3:
                    case Const.MAGIC_DBASE3_MEMO:
                    case Const.MAGIC_DBASE3_MEMO_2:
                    case Const.MAGIC_FOXPRO:
                    default:
                        len_header = Marshal.SizeOf(typeof(DBF_FILEHEADER_3));
                        len_field  = Marshal.SizeOf(typeof(DBF_FILEFIELD_3));
                        break;
                    }

                    bytes = new byte[len_header];
                    StreamSeek(stream, 0);

                    ok = (len_header == StreamRead(stream, bytes));
                    if (ok)
                    {
                        switch (version)
                        {
                        case Const.MAGIC_DBASE4:
                        case Const.MAGIC_DBASE4_MEMO:
                        {
                            DBF_FILEHEADER_4 header = Utility.PtrToStructure <DBF_FILEHEADER_4>(bytes);
                            _RecordCount = header.recordcount;
                            RecordLength = header.recordlength;
                            HeaderLength = header.headerlength;
                            ok           = sanity_check(header);
                            break;
                        }

                        case Const.MAGIC_DBASE3:
                        case Const.MAGIC_DBASE3_MEMO:
                        case Const.MAGIC_DBASE3_MEMO_2:
                        case Const.MAGIC_FOXPRO:
                        default:
                        {
                            DBF_FILEHEADER_3 header = Utility.PtrToStructure <DBF_FILEHEADER_3>(bytes);
                            _RecordCount = header.recordcount;
                            RecordLength = header.recordlength;
                            HeaderLength = header.headerlength;
                            ok           = sanity_check(header);
                            break;
                        }
                        }
                        if (ok)
                        {
                            if ((RecordCount == 0) && (RecordLength != 0))
                            {
                                _RecordCount = (stream.Length - HeaderLength) / RecordLength;
                            }

                            int fieldcount = (HeaderLength - (len_header + 1)) / len_field;

                            bytes = new byte[len_field];

                            for (int i = 0; i < fieldcount; i++)
                            {
                                ColumnInfo field;

                                StreamRead(stream, bytes);
                                if ((bytes[0] >= 0) && (bytes[0] <= (byte)' '))
                                {
                                    break;
                                }
                                switch (version)
                                {
                                case Const.MAGIC_DBASE4:
                                case Const.MAGIC_DBASE4_MEMO:
                                {
                                    DBF_FILEFIELD_4 item = Utility.PtrToStructure <DBF_FILEFIELD_4>(bytes);
                                    DataType        type = (DataType)Const.DataTypes.IndexOf(item.type);
                                    field = new ColumnInfo()
                                    {
                                        Name = item.title, DataType = Convert.Type(type), Length = item.length, DecCount = item.deccount
                                    };
                                    break;
                                }

                                case Const.MAGIC_DBASE3:
                                case Const.MAGIC_DBASE3_MEMO:
                                case Const.MAGIC_DBASE3_MEMO_2:
                                case Const.MAGIC_FOXPRO:
                                default:
                                {
                                    DBF_FILEFIELD_3 item = Utility.PtrToStructure <DBF_FILEFIELD_3>(bytes);
                                    DataType        type = (DataType)Const.DataTypes.IndexOf(item.type);
                                    field = new ColumnInfo()
                                    {
                                        Name = item.title, DataType = Convert.Type(type), Length = item.length, DecCount = item.deccount
                                    };
                                    break;
                                }
                                }
                                Columns.Add(field);
                            }
                        }
                    }
                }
            }
            if (ok)
            {
                _Stream  = stream;
                Filename = filename;
            }
            return(ok);
        }