示例#1
0
        /// <summary>
        /// Открыть файл
        /// </summary>
        /// <param name="file">путь к файлу</param>
        /// <param name="encoding">кодировка</param>
        /// <returns>Читатель DBF</returns>
        private static DbfReader Open(string file, Encoding encoding)
        {
            var dbf = new DbfReader {
                Encoding = encoding ?? Encoding.Default, _tablename = Path.GetFileNameWithoutExtension(file) + "DBF"
            };

            using (var f = File.OpenRead(file))
            {
                var buffer = new byte[f.Length];
                f.Read(buffer, 0, (int)f.Length);
                var mem = new MemoryStream(buffer);
                dbf._reader = new BinaryReader(mem);
            }
            ReadHeader(dbf);
            return(dbf);
        }
示例#2
0
        private static void ReadHeader(DbfReader dbf)
        {
            dbf.Header = new DbfHeader
            {
                VersionNumber = (Signature)dbf._reader.ReadByte()
            };
            var year  = dbf._reader.ReadByte();
            var month = dbf._reader.ReadByte();
            var day   = dbf._reader.ReadByte();

            dbf.Header.LastUpdate         = new DateTime(1900 + year, month, day);
            dbf.Header.NumberOfRecords    = dbf._reader.ReadInt32();
            dbf.Header.LengthOfHeader     = dbf._reader.ReadInt16();
            dbf.Header.LengthOfEachRecord = dbf._reader.ReadInt16();
            //};
            // Резерв
            dbf._reader.ReadBytes(2);
            dbf.Header.Transaction = (Transaction)dbf._reader.ReadByte();
            dbf.Header.Encripted   = (Encripted)dbf._reader.ReadByte();
            // Free record thread
            dbf._reader.ReadBytes(4);
            // Reserved for multi-user dBASE
            dbf._reader.ReadBytes(8);
            dbf.Header.MDXFlag        = dbf._reader.ReadByte();
            dbf.Header.LanguageDriver = (CodePage)dbf._reader.ReadByte();

            switch (dbf.Header.LanguageDriver)
            {
            case CodePage.СodePage1251:
            case CodePage.Unknow:
                dbf.Encoding = Encoding.GetEncoding(1251);
                break;

            case CodePage.СodePage866:
                dbf.Encoding = Encoding.GetEncoding(866);
                break;
            }
            // Reserved
            dbf._reader.ReadBytes(2);

            var list = new List <ColumnInfo>();
            int flag;
            var offset = 1;
            var errors = new StringBuilder();

            var types      = new List <char>();
            var fieldsEnum = typeof(DbfColumnType).GetFields();

// ReSharper disable LoopCanBeConvertedToQuery
// ReSharper disable ForCanBeConvertedToForeach
            for (var index = 0; index < fieldsEnum.Length; index++)
// ReSharper restore ForCanBeConvertedToForeach
// ReSharper restore LoopCanBeConvertedToQuery
            {
                var type = fieldsEnum[index];
                if (type.Name.Equals("value__"))
                {
                    continue;
                }
                types.Add((char)(int)type.GetRawConstantValue());
            }
            do
            {
                var item = new ColumnInfo();
                var name = dbf.Encoding.GetString(dbf._reader.ReadBytes(11)).TrimEnd(' ', (char)0x0);

                var type = dbf._reader.ReadChar();

                item.Name = name;

                if (types.Any(x => x == type))
                {
                    item.Type = (DbfColumnType)type;
                }
                else
                {
                    errors.AppendFormat("Что за тип {0}? Как его обрабатывать? Поле {1}", type, name).AppendLine();
                }

                item.FieldDataAddress = dbf._reader.ReadInt32();
                item.FieldLength      = dbf._reader.ReadByte();
                item.DecimalCount     = dbf._reader.ReadByte();
                // Reserved for multi-user dBASE
                dbf._reader.ReadBytes(2);
                item.WorkAreaID = dbf._reader.ReadByte();
                // Reserved for multi-user dBASE
                dbf._reader.ReadBytes(2);
                item.FlagForSETFIELDS = dbf._reader.ReadByte();
                // Reserved
                dbf._reader.ReadBytes(7);
                item.IndexFieldFlag = dbf._reader.ReadByte();
                item.Offset         = offset;
                offset += item.FieldLength;
                list.Add(item);
                flag = dbf._reader.PeekChar();
            } while (flag != 0x0D);
            if (errors.Length > 0)
            {
                throw new DbfHeadException(errors.ToString());
            }
            dbf._reader.ReadByte();
            dbf.Header.Columns = list;
            dbf._reader.BaseStream.Position = dbf.Header.LengthOfHeader;
            //Console.WriteLine(dbf.Header);
        }