Exemplo n.º 1
0
        public void WriteRecord(DBFRecord record)
        {
            if (!record.IsDirty())
            {
                return;
            }
            //if (writeStream == null)
            //{
            //    throw new DBFException(
            //        "Not initialized with file for WriteRecord use, use AddRecord instead");
            //}
            //AddRecord(values, true);
            //AddRecord(record.ValueArray);

            Validate(record.ValueArray);

            //if (!writeImediately)
            //{
            //    v_records.Add(values);
            //}
            //else
            //{
            try
            {
                //WriteRecord(new BinaryWriter(writeStream), values);
                WriteRecord(_BinaryWriter, record);
                //recordCount++;
            }
            catch (IOException e)
            {
                throw new DBFException(
                          "Error occured while writing record. ", e);
            }
            //}
        }
Exemplo n.º 2
0
        public IEnumerable <DBFRecord> AsEnumerable(Func <DBFRecord, bool> filter)
        {
            var lookupFieldName = GetSelectFields()
                                  .Select((f, i) => new { f.Name, Index = i })
                                  .ToDictionary(f => f.Name, f => f.Index);

            var(objects, position) = NextRecord();

            while (objects != null)
            {
                var record = new DBFRecord(lookupFieldName, objects, position);
                if (record != null || filter(record) == true)
                {
                    yield return(record);
                }

                (objects, position) = NextRecord();
            }
        }
Exemplo n.º 3
0
        private void WriteRecord(BinaryWriter dataOutput, DBFRecord record)
        {
            if (!record.IsDirty())
            {
                return;
            }

            //var originalPosition = _BaseStream.Position;
            _BaseStream.Seek(record.Position, SeekOrigin.Begin);
            dataOutput.Write((byte)' ');

            for (var j = 0; j < _Header.FieldArray.Length; j++)
            {
                /* iterate throught fields */
                if (!record.IsDirty(j))
                {
                    _BaseStream.Seek(_Header.FieldArray[j].FieldLength, SeekOrigin.Current);
                    continue;
                }

                var objectArray = record.ValueArray;

                switch (_Header.FieldArray[j].DataType)
                {
                case NativeDbType.UnicodeChar:
                    var str_valueU = (objectArray[j] != null && objectArray[j] != DBNull.Value) ? objectArray[j].ToString() : "";
                    var bufferU    = Utils.textPadding(str_valueU,
                                                       _UCharEncoding,
                                                       _Header.FieldArray[j].FieldLength,
                                                       Utils.ALIGN_LEFT,
                                                       (byte)0x00
                                                       );
                    dataOutput.Write(bufferU);
                    break;

                case NativeDbType.Char:
                    var str_value = (objectArray[j] != null && objectArray[j] != DBNull.Value) ? objectArray[j].ToString() : "";
                    var buffer    = Utils.textPadding(str_value,
                                                      CharEncoding,
                                                      _Header.FieldArray[j].FieldLength
                                                      );
                    dataOutput.Write(buffer);
                    break;

                case NativeDbType.Date:
                    if (objectArray[j] != null && objectArray[j] != DBNull.Value)
                    {
                        var tDate = (DateTime)objectArray[j];

                        dataOutput.Write(
                            CharEncoding.GetBytes(tDate.ToString("yyyyMMdd")));
                    }
                    else
                    {
                        dataOutput.Write(
                            Utils.FillArray(new byte[8], DBFFieldType.Space));
                    }

                    break;

                case NativeDbType.Float:

                    if (objectArray[j] != null && objectArray[j] != DBNull.Value)
                    {
                        var tDouble = Convert.ToDouble(objectArray[j]);
                        dataOutput.Write(
                            Utils.NumericFormating(
                                tDouble,
                                CharEncoding,
                                _Header.FieldArray[j].FieldLength,
                                _Header.FieldArray[j].DecimalCount
                                )
                            );
                    }
                    else
                    {
                        dataOutput.Write(
                            Utils.textPadding(
                                NullSymbol,
                                CharEncoding,
                                _Header.FieldArray[j].FieldLength,
                                Utils.ALIGN_RIGHT
                                )
                            );
                    }

                    break;

                case NativeDbType.Numeric:

                    if (objectArray[j] != null && objectArray[j] != DBNull.Value)
                    {
                        var tDecimal = Convert.ToDecimal(objectArray[j]);
                        dataOutput.Write(
                            Utils.NumericFormating(
                                tDecimal,
                                CharEncoding,
                                _Header.FieldArray[j].FieldLength,
                                _Header.FieldArray[j].DecimalCount
                                )
                            );
                    }
                    else
                    {
                        dataOutput.Write(
                            Utils.textPadding(
                                NullSymbol,
                                CharEncoding,
                                _Header.FieldArray[j].FieldLength,
                                Utils.ALIGN_RIGHT
                                )
                            );
                    }

                    break;

                case NativeDbType.Logical:

                    if (objectArray[j] != null && objectArray[j] != DBNull.Value)
                    {
                        if ((bool)objectArray[j])
                        {
                            dataOutput.Write(DBFFieldType.True);
                        }
                        else
                        {
                            dataOutput.Write(DBFFieldType.False);
                        }
                    }
                    else
                    {
                        dataOutput.Write(DBFFieldType.UnknownByte);
                    }

                    break;

                case NativeDbType.Memo:
                    if (objectArray[j] != null && objectArray[j] != DBNull.Value)
                    {
                        var tMemoValue = ((MemoValue)objectArray[j]);

                        tMemoValue.Write(this);

                        dataOutput.Write(Utils.NumericFormating(tMemoValue.Block, CharEncoding, 10, 0));
                    }
                    else
                    {
                        dataOutput.Write(
                            Utils.textPadding("",
                                              CharEncoding,
                                              10
                                              )
                            );
                    }


                    break;

                default:
                    throw new DBFException("Unknown field type "
                                           + _Header.FieldArray[j].DataType);
                }
            } /* iterating through the fields */
            _BinaryWriter.Flush();
            //_BaseStream.Seek(originalPosition, SeekOrigin.Begin);
        }