Example #1
0
        /// <summary>
        /// Открыть файл
        /// </summary>
        public void Open(Stream streamDBF, Stream streamDBT, Encoding encoding, params DbfColumn[] cols)
        {
            if (isOpened)
                return;
            if (streamDBF == null)
                throw new ArgumentNullException("streamDBF");
            if (encoding == null || (encoding.CodePage != 866 && encoding.CodePage != 1251))
                throw new ArgumentException("Encoding");
            if (cols == null && cols.Length == 0)
                throw new ArgumentNullException("Columns");

            fsDBF = streamDBF;

            if (encoding.CodePage == 1251)
            {
                header = new DbfHeader(0x57);   //1251
            }
            else
            {
                header = new DbfHeader(0x26);   //866
            }
            encoder = encoding.GetEncoder();

            columns = new List<DbfColumn>();
            bool hasMemo = false;
            foreach (DbfColumn col in cols)
            {
                col.SetEncodedName(encoder);
                if (col is DbfColumnMemo)
                {
                    hasMemo = true;
                }
                columns.Add(col);
            }
            header.HasMemo = hasMemo;

            if (hasMemo)
            {
                fsDBT = streamDBT;
                foreach (DbfColumn col in cols)
                {
                    if (col is DbfColumnMemo)
                    {
                        ((DbfColumnMemo)col).SetStream(fsDBT);
                    }
                }
            }

            // Размер заголовка (32 - структура DbfHeader, 32 - структура DbfFieldDescriptor, 1 - завершающий байт)
            int headerSize = 32 + 32 * columns.Count + 1;
            if (headerSize > Int16.MaxValue)
                throw new Exception("Too many columns");
            header.HeaderSize = (Int16)headerSize;

            // Размер записи (1 - байт флага удаления, остальное - сами данные)
            int recordSize = 1;
            foreach (DbfColumn col in columns)
            {
                recordSize += col.FieldDescriptor.fieldLen;
            }
            if (recordSize > Int16.MaxValue)
                throw new Exception("Too long record size");
            header.RecordSize = (Int16)recordSize;
            isOpened = true;

            WriteHeader();
            WriteColumns();

            if (hasMemo)
            {
                DbfColumnMemo.count = 0;
                WriteDbtHeader();
                DbfColumnMemo.count = 1;
            }
        }
Example #2
0
 /// <summary>
 /// Конструктор
 /// </summary>
 public DbfWriter()
 {
     fsDBF = null;
     fsDBT = null;
     header = null;
     columns = null;
     isOpened = false;
 }
Example #3
0
 /// <summary>
 /// Закрыть файл
 /// </summary>
 public void Close()
 {
     if (!isOpened)
         return;
     try
     {
         if (fsDBF != null)
         {
             fsDBF.WriteByte(0x1A); // EOF
             WriteHeader();
             fsDBF.Flush();
         }
         if (fsDBT != null)
         {
             WriteDbtHeader();
             fsDBT.Flush();
         }
     }
     catch
     {
     }
     finally
     {
         fsDBF = null;
         fsDBT = null;
         header = null;
         columns = null;
         encoder = null;
         isOpened = false;
     }
 }